Intel fortran编译器自带的MKL函数库中,只有计算矩阵1范数和无穷范数的程序,所以本文给出fortran使用MKL函数库计算矩阵2范数的示例。
而矩阵的2范数的值近似于max(svd(A))
运行环境:win10+vs2019+ivf2020
代码如下:
program norm2
use lapack95
implicit none
integer :: i, m, n
real, allocatable :: A(:,:), U(:,:), S(:,:), V(:,:), s1(:)
write(*,'(g0)') '请输入矩阵的大小m,n: '
read(*,*) m, n
allocate( A(m,n), U(m,m), S(m,n), s1(min(m,n)), V(n,n) )
call random_seed()
call random_number(A)
write(*,'(g0)') 'A is...'
do i = 1, size(A,1)
write(*,'(*(f11.4))') A(i,:)
end do
call gesvd( A, s1, U, V ) !// 这里返回的V是V的转置Vt
write(*,'(g0)') 'The 2-norm of A is...'
write(*,'(f11.4)') maxval( s1 )
deallocate( A, U, S, V, s1 )
end program norm2
结果如下:
A is...
0.4356 0.6273 0.3247
0.8919 0.8670 0.0205
0.5135 0.0971 0.0906
The 2-norm of A is...
1.5303
上述结果与matlab的计算结果一致。