fortran - How to control lapack solver presion -
i'm trying use lapack zhesv
subroutine in fortran solve linear system, precision seems not good.
here code:
program main implicit none integer,parameter::n=4 integer::lda=n,ipiv(n),ldb=n,lwork=n*n,info,i complex*16::a(n,n),b(n),work(n,n),x(n) a=reshape( (/(1.,0.),(0.,0.),(0.,-6.94908e-13),(0.,-6.94908e-13),& &(0.,0.),(1.,0.0352595),(0.,-4.51893e-11),(0.,-4.51893e-11),& &(0.,-6.94908e-13),(0.,-4.51893e-11),(1.,0.0376938),(0.,0.),& &(0.,-6.94908e-13),(0.,-4.51893e-11),(0.,0.),(1.,0.0378932)/),shape(a)) a=transpose(a) b=(/(1.,0.),(0.,0.),(0.,6.94908e-13),(0.,6.94908e-13)/) x=b write(*,*) "--------------b----------------" write(*,99999) b call zhesv('upper',n,1,a,lda,ipiv,x,n,work,lwork,info) write(*,*) "--------------x----------------" write(*,99999) x write(*,*) "-------------info--------------" write(*,*) info write(*,*) "-------------error-------------" write(*,99999) matmul(a,x)-b 99999 format ((3x,4(' (',e15.8,',',e15.8,')',:))) end program main
the outputs are
--------------b---------------- ( 0.10000000e+01, 0.00000000e+00) ( 0.00000000e+00, 0.00000000e+00) ( 0.00000000e+00, 0.69490800e-12) ( 0.00000000e+00, 0.69490800e-12) --------------x---------------- ( 0.10000000e+01, 0.00000000e+00) ( 0.00000000e+00, 0.00000000e+00) ( 0.00000000e+00, 0.00000000e+00) ( 0.00000000e+00, 0.00000000e+00) -------------info-------------- 0 -------------error------------- ( 0.00000000e+00, 0.00000000e+00) ( 0.00000000e+00, 0.00000000e+00) ( 0.00000000e+00,-0.13898160e-11) ( 0.00000000e+00,-0.13898160e-11)
the error quite large compare element of b.
in contrary, tried solve in mathematica, , error quite small.
a.linearsolve[a, b] - b {0. + 0. i, 0. + 3.67342*10^-40 i, 4.13609*10^-34 + 0. i, 4.13609*10^-34 + 0. i}
so how can control precision of lapack solver achieve same precision of mathematica's linearsolver
?
zhesv computes solution complex system of linear equations a * x = b
, a
n-by-n hermitian matrix , x
, b
n-by-nrhs matrices.
but, matrix a
not hermitian matrix.
Comments
Post a Comment