|||
Intel@ Math Kernel LibraryMKL英特尔®数学核心函数库提升了解决大型数值计算问题的软件和应用程序的性能。英特尔MKL提供了BLAS与LAPACK线性代数程序,快速傅立叶变换,矢量数学,随机数生成功能和一些其他功能。简单点讲是一个非常强大的数值计算库,支持Fortran和C语言。(注:MKL是商业函数库,版权属于Intel公司)
对于奇异矩阵的广义逆求解,SVD(Singular Value Decomposition)奇异值分解是一种常用的算法,MKL的LAPACK库里面包含简单快捷的SVD分解程序,其中f95版本参数更少,实现起来更简单。以下是实现过程:
操作系统:Win10 Professional
IDE:VS2015
Fortran编译环境:Intel Parallel Studio XE 2015
MKL版本:Intel@ Math Kernel Library 11.2 Update 3
1.VS里面设置:项目-属性-Linker-Input里面加入mkl_lapack95.lib;项目-属性-Fortran-Libraries-Use Intel Math Kernel Library选择非No的其他选项;
2.代码里面添加
USE f95_precision, ONLY: WP => SP
USE lapack95, ONLY: GESVD
具体代码如下:
!Intel@ Math Kernel Library Singular Value Decomposition - LAPACK Computational Routines
!MKL奇异值分解
Program SVD
USE f95_precision, ONLY: WP => SP
USE lapack95, ONLY: GESVD
Implicit None
Integer(4)::i,j,info
Character::job(1)
Integer::m,n
Real(8),Allocatable::a(:,:),aa(:,:),aaa(:,:),s(:),u(:,:),v(:,:),vt(:,:),ww(:),ss(:,:)
m=3
n=2
Allocate(a(m,n),aa(m,n),aaa(m,n),s(min(m,n)),ss(min(m,n),min(m,n)),u(m,m),v(n,n),vt(n,n),ww(min(m,n)-1))
Call RANDOM_SEED()
Call RANDOM_NUMBER(a)
aa=a
Write(*,*)"待分解矩阵"
Do i=1,m
Write(*,*)a(i,:)
End Do
WRITE(*,*) "CALL GESVD( A, S,U=U, VT=VT, WW=WW, JOB='N', INFO=INFO )"
Call gesvd(aa, s ,u=u ,vt=vt ,ww=ww ,job="N" ,info=info)
If(info==0)Then
Write(*,*)"Successful"
Write(*,*)"分解后A矩阵"
Do i=1,m
Write(*,*)aa(i,:)
End Do
Write(*,*)"U矩阵"
Do i=1,m
Write(*,*)u(i,:)
End Do
Write(*,*)"S矩阵的对角线元素"
Write(*,*)s
Write(*,*)"V矩阵的转置"
Do i=1,n
Write(*,*)vt(i,:)
End Do
!Write(*,*)"WW向量"
!Write(*,*)ww
Else
Write(*,*)"Error"
End If
ss=0
Do i=1,min(m,n)
ss(i,i)=s(i)
End Do
!a矩阵SVD分解成a=u*s*v'
Write(*,*)"检查SVD分解结果A=S*U*V'"
aaa=matmul(matmul(u,ss),vt)
Do i=1,m
Write(*,*)aaa(i,:)
End Do
Deallocate(a,aa,s,u,v,vt,ww)
Read(*,*)
End Program SVD
参考文献:《Reference Manual for Intel® Math Kernel Library 11.2 Update 3 》
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-23 14:15
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社