LAPACK 程序 SSYEVD 的计算特征值的应用实例 C/Fortran

server/2024/12/19 5:43:06/

 A*v(j) = lambda(j)*v(j)

0,预备环境

编译一份 Lapack源代码,会生成两个 静态链接库:

liblapack.a  librefbals.a

1,C版本

源码:

hello.c

/*SSYEVD Example.==============Program computes all eigenvalues and eigenvectors of a real symmetricmatrix A using divide and conquer algorithm, where A is:6.39   0.13  -8.23   5.71  -3.180.13   8.37  -4.46  -6.10   7.21-8.23  -4.46  -9.58  -9.25  -7.425.71  -6.10  -9.25   3.72   8.54-3.18   7.21  -7.42   8.54   2.51Description.============The routine computes all eigenvalues and, optionally, eigenvectors of ann-by-n real symmetric matrix A. The eigenvector v(j) of A satisfiesA*v(j) = lambda(j)*v(j)where lambda(j) is its eigenvalue. The computed eigenvectors areorthonormal.If the eigenvectors are requested, then this routine uses a divide andconquer algorithm to compute eigenvalues and eigenvectors.Example Program Results.========================SSYEVD Example Program ResultsEigenvalues-17.44 -11.96   6.72  14.25  19.84Eigenvectors (stored columnwise)-0.26   0.31  -0.74   0.33   0.42-0.17  -0.39  -0.38  -0.80   0.16-0.89   0.04   0.09   0.03  -0.45-0.29  -0.59   0.34   0.31   0.60-0.19   0.63   0.44  -0.38   0.48
*/
#include <stdlib.h>
#include <stdio.h>/* SSYEVD prototype */
extern void ssyevd_( char* jobz, char* uplo, int* n, float* a, int* lda,float* w, float* work, int* lwork, int* iwork, int* liwork, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, float* a, int lda );/* Parameters */
#define N 5
#define LDA N/* Main program */
int main() {/* Locals */int n = N, lda = LDA, info, lwork, liwork;int iwkopt;int* iwork;float wkopt;float* work;/* Local arrays */float w[N];float a[LDA*N] = {6.39f,  0.00f,  0.00f,  0.00f,  0.00f,0.13f,  8.37f,  0.00f,  0.00f,  0.00f,-8.23f, -4.46f, -9.58f,  0.00f,  0.00f,5.71f, -6.10f, -9.25f,  3.72f,  0.00f,-3.18f,  7.21f, -7.42f,  8.54f,  2.51f};/* Executable statements */printf( " SSYEVD Example Program Results\n" );/* Query and allocate the optimal workspace */lwork = -1;liwork = -1;ssyevd_( "Vectors", "Upper", &n, a, &lda, w, &wkopt, &lwork, &iwkopt,&liwork, &info );lwork = (int)wkopt;work = (float*)malloc( lwork*sizeof(float) );liwork = iwkopt;iwork = (int*)malloc( liwork*sizeof(int) );/* Solve eigenproblem */ssyevd_( "Vectors", "Upper", &n, a, &lda, w, work, &lwork, iwork,&liwork, &info );/* Check for convergence */if( info > 0 ) {printf( "The algorithm failed to compute eigenvalues.\n" );exit( 1 );}/* Print eigenvalues */print_matrix( "Eigenvalues", 1, n, w, 1 );/* Print eigenvectors */print_matrix( "Eigenvectors (stored columnwise)", n, n, a, lda );/* Free workspace */free( (void*)iwork );free( (void*)work );exit( 0 );
} /* End of SSYEVD Example *//* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, float* a, int lda ) {int i, j;printf( "\n %s\n", desc );for( i = 0; i < m; i++ ) {for( j = 0; j < n; j++ ) printf( " %6.2f", a[i+j*lda] );printf( "\n" );}
}

2,fortran77 版本

源码:

hello.f

*  SSYEVD Example.
*  ==============
*
*  Program computes all eigenvalues and eigenvectors of a real symmetric
*  matrix A using divide and conquer algorithm, where A is:
*
*    6.39   0.13  -8.23   5.71  -3.18
*    0.13   8.37  -4.46  -6.10   7.21
*   -8.23  -4.46  -9.58  -9.25  -7.42
*    5.71  -6.10  -9.25   3.72   8.54
*   -3.18   7.21  -7.42   8.54   2.51
*
*  Description.
*  ============
*
*  The routine computes all eigenvalues and, optionally, eigenvectors of an
*  n-by-n real symmetric matrix A. The eigenvector v(j) of A satisfies
*
*  A*v(j) = lambda(j)*v(j)
*
*  where lambda(j) is its eigenvalue. The computed eigenvectors are
*  orthonormal.
*  If the eigenvectors are requested, then this routine uses a divide and
*  conquer algorithm to compute eigenvalues and eigenvectors.
*
*  Example Program Results.
*  ========================
*
* SSYEVD Example Program Results
*
* Eigenvalues
* -17.44 -11.96   6.72  14.25  19.84
*
* Eigenvectors (stored columnwise)
*  -0.26   0.31  -0.74   0.33   0.42
*  -0.17  -0.39  -0.38  -0.80   0.16
*  -0.89   0.04   0.09   0.03  -0.45
*  -0.29  -0.59   0.34   0.31   0.60
*  -0.19   0.63   0.44  -0.38   0.48
*  =============================================================================
*
*     .. Parameters ..INTEGER          NPARAMETER        ( N = 5 )INTEGER          LDAPARAMETER        ( LDA = N )INTEGER          LWMAXPARAMETER        ( LWMAX = 1000 )
*
*     .. Local Scalars ..INTEGER          INFO, LWORK, LIWORK
*
*     .. Local Arrays ..INTEGER          IWORK( LWMAX )REAL             A( LDA, N ), W( N ), WORK( LWMAX )DATA             A/$  6.39, 0.00, 0.00, 0.00, 0.00,$  0.13, 8.37, 0.00, 0.00, 0.00,$ -8.23,-4.46,-9.58, 0.00, 0.00,$  5.71,-6.10,-9.25, 3.72, 0.00,$ -3.18, 7.21,-7.42, 8.54, 2.51$                  /
*
*     .. External Subroutines ..EXTERNAL         SSYEVDEXTERNAL         PRINT_MATRIX
*
*     .. Intrinsic Functions ..INTRINSIC        INT, MIN
*
*     .. Executable Statements ..WRITE(*,*)'SSYEVD Example Program Results'
*
*     Query the optimal workspace.
*LWORK = -1LIWORK = -1CALL SSYEVD( 'Vectors', 'Upper', N, A, LDA, W, WORK, LWORK,$             IWORK, LIWORK, INFO )LWORK = MIN( LWMAX, INT( WORK( 1 ) ) )LIWORK = MIN( LWMAX, IWORK( 1 ) )
*
*     Solve eigenproblem.
*CALL SSYEVD( 'Vectors', 'Upper', N, A, LDA, W, WORK, LWORK,$             IWORK, LIWORK, INFO )
*
*     Check for convergence.
*IF( INFO.GT.0 ) THENWRITE(*,*)'The algorithm failed to compute eigenvalues.'STOPEND IF
*
*     Print eigenvalues.
*CALL PRINT_MATRIX( 'Eigenvalues', 1, N, W, 1 )
*
*     Print eigenvectors.
*CALL PRINT_MATRIX( 'Eigenvectors (stored columnwise)', N, N, A,$                   LDA )STOPEND
*
*     End of SSYEVD Example.
*
*  =============================================================================
*
*     Auxiliary routine: printing a matrix.
*SUBROUTINE PRINT_MATRIX( DESC, M, N, A, LDA )CHARACTER*(*)    DESCINTEGER          M, N, LDAREAL             A( LDA, * )
*INTEGER          I, J
*WRITE(*,*)WRITE(*,*) DESCDO I = 1, MWRITE(*,9998) ( A( I, J ), J = 1, N )END DO
*9998 FORMAT( 11(:,1X,F6.2) )RETURNEND

3, Makefile


EXE := hello.c.out hello.f.out
all: $(EXE)%.c.out: %.cgcc $< -o $@ $(LD_FLAGS_C)LD_FLAGS_C := -L /home/hipper/ex_lapack/lapack-3.11 -llapack -lrefblas -lgfortran -lm%.f.out: %.fgfortran -g $< -o $@ $(LD_FLAGS_FORT)LD_FLAGS_FORT :=  -L /home/hipper/ex_lapack/lapack-3.11/ -llapack -lrefblas.PHONY: clean
clean:-rm -rf $(EXE)

4,编译运行

5,参考

mkl


http://www.ppmy.cn/server/151372.html

相关文章

Go-FastDFS文件服务器一镜到底使用Docker安装

本文章介绍一镜到底安装go-fastdfs并配置数据文件到linux 由于国内镜像无法安装go-fastdfs&#xff1a;国内环境已经把docker官方的网站给封闭了 我们需要从国外的一台服务器&#xff0c;下载images镜像&#xff0c;然后进行转发加载到国内服务器 一、在国外服务器拉取镜像 …

使用CNN模型训练图片识别(键盘,椅子,眼镜,水杯,鼠标)

首先是环境&#xff1a; 我是在Anaconda3中的Jupyter Notebook (tensorflow)中进行训练&#xff0c;环境各位自行安装 数据集&#xff1a; 本次数据集五个类型&#xff08;键盘&#xff0c;椅子&#xff0c;眼镜&#xff0c;水杯&#xff0c;鼠标&#xff09;我收集了每个接近两…

【347. 前 K 个高频元素 中等】

题目&#xff1a; 给你一个整数数组 nums 和一个整数 k &#xff0c;请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。 示例 1: 输入: nums [1,1,1,2,2,3], k 2 输出: [1,2] 示例 2: 输入: nums [1], k 1 输出: [1] 提示&#xff1a; 1 < nums.leng…

Linux内核 -- 普通定时器mod_timer的使用示例

Linux Kernel 普通定时器使用示例 本文介绍如何在 Linux 内核模块中使用普通定时器&#xff08;timer_list 结构&#xff09;。 示例代码 #include <linux/module.h> #include <linux/init.h> #include <linux/timer.h> #include <linux/jiffies.h>…

分布式全文检索引擎ElasticSearch-数据的写入存储底层原理

一、数据写入的核心流程 当向 ES 索引写入数据时&#xff0c;整体流程如下&#xff1a; 1、客户端发送写入请求 客户端向 ES 集群的任意节点&#xff08;称为协调节点&#xff0c;Coordinating Node&#xff09;发送一个写入请求&#xff0c;比如 index&#xff08;插入或更…

安卓课设版算法计算器

安卓课设版算法计算器&#xff08;HNUST&#xff09; 前言&#xff1a; 如果只想看函数使用说明请跳转到“四、使用函数介绍” 该版本为课设版&#xff0c;富含多个界面&#xff0c;是前版的plus版本&#xff0c;进行了更多的复杂化操作&#xff0c;故因此会觉得对于计算器有点…

springboot java ffmpeg 视频压缩、提取视频帧图片、获取视频分辨率

用到的maven依赖&#xff1a; lombok依赖就不贴出来了 <dependency><groupId>org.bytedeco</groupId><artifactId>ffmpeg-platform</artifactId><version>4.3.2-1.5.5</version></dependency><dependency><groupId&…

list使用

目录 list介绍 list使用 list创建 list迭代器 容量操作 元素访问 修改元素 其他操作 list介绍 ● list是可以在常数范围内在任意位置进行插入和删除的序列式容器&#xff0c;并且该容器可以前后双向迭代 ● list的底层是双向链表结构&#xff0c;双向链表中每个元素存…