$ octave
GNU Octave, version 3.0.5
octave:1> A=[2 2 2 2; 1.7 0.1 -1.7 -0.1; 0.6 1.8 -0.6 -1.8]
A =
2.00000 2.00000 2.00000 2.00000
1.70000 0.10000 -1.70000 -0.10000
0.60000 1.80000 -0.60000 -1.80000
octave:2> [U, S, V]=svd(A)
U =
-1.00000 0.00000 -0.00000
0.00000 -0.60000 -0.80000
-0.00000 -0.80000 0.60000
S =
4 0 0 0
0 3 0 0
0 0 2 0
V =
-0.50000 -0.50000 -0.50000 -0.50000
-0.50000 -0.50000 0.50000 0.50000
-0.50000 0.50000 0.50000 -0.50000
-0.50000 0.50000 -0.50000 0.50000
Fortran with Intel Math Kernel
[~/TEACHING/TOOLS/Linear.Algebra/SVD]
[am@aofd165]
$ svd.sh
Compiling svd.f90 ...
Done Compile.
svd.exe is running ...
DGESVD Example Program Results
Input matrix, A:
2.0000 2.0000 2.0000 2.0000
1.7000 0.1000 -1.7000 -0.1000
0.6000 1.8000 -0.6000 -1.8000
Singular values
4.0000 3.0000 2.0000 0.0000
Left singular vectors (first n columns of U)
-1.0000 0.0000 -0.0000
0.0000 -0.6000 -0.8000
-0.0000 -0.8000 0.6000
Right singular vectors by row (V**T)
-0.5000 -0.5000 -0.5000 -0.5000
-0.5000 -0.5000 0.5000 0.5000
-0.5000 0.5000 0.5000 -0.5000
-0.5000 0.5000 -0.5000 0.5000
Error estimate for the singular values
4.4E-16
Error estimates for the left singular vectors
4.4E-16 4.4E-16 4.4E-16
Error estimates for the right singular vectors
4.4E-16 4.4E-16 4.4E-16
Done svd.exe
$ srcdump.sh svd.sh
------------------------------
List of the following files:
------------------------------
svd.sh
------------------------------
Machine info
------------------------------
aofd165.bio.mie-u.ac.jp
/work1/am/TEACHING/TOOLS/Linear.Algebra/SVD
Mon Dec 19 16:43:37 JST 2016
======================
svd.sh
======================
#!/bin/bash
# Description:
src=$(basename $0 .sh).f90
exe=$(basename $0 .sh).exe
data_file=$(basename $0 .sh)_test.txt
f90=ifort
opt="-CB -traceback -fpe0 -mkl"
temp=$(basename $src .f90)
prog_name=$(echo $temp| sed -e 's/\./_/g')
cat <<EOF > ${data_file}
DGESVD Example Program Data
3 4 :Values of M and N
2.00000 2.00000 2.00000 2.00000
1.70000 0.10000 -1.70000 -0.10000
0.60000 1.80000 -0.60000 -1.80000
EOF
cat <<EOF>$src
program ${prog_name}
integer,parameter::nin=5, nout=6
integer,parameter::nb=64
double precision::eerrbd, eps
integer::lda, ldvt, lwork
integer::i, ifail, info, j, lwkopt, m, n
double precision,allocatable::a(:,:), a_in(:,:), rcondu(:), rcondv(:), &
s(:), uerrbd(:), verrbd(:), vt(:,:), work(:)
double precision::dummy(1,1)
double precision:: dlamch
external::ddisna, dgesvd
! For checking
double precision,parameter::al=1.0d0, beta=0.0d0 ! For dgemv & dgemm
double precision,allocatable:: c(:,:),x(:),y(:)
write (nout, *) 'DGESVD Example Program Results'
write (nout, *)
!
! Read in input data
!
read (nin, *)
read (nin, *) m, n
lda=m
ldvt=n
lwork=m+4*n+nb*(m+n)
allocate(a(lda,n), a_in(lda,n), rcondu(n), rcondv(n), &
s(n), uerrbd(n), verrbd(n), vt(ldvt,n), work(lwork))
allocate(c(lda,n),x(lda),y(lda))
read (nin, *)((a(i,j),j=1,n), i=1, m)
a_in(:,:)=a(:,:)
print '(A)','Input matrix, A:'
do i=1,m
print '(100F8.4)',(a_in(i,j),j=1,n)
end do !i
print *
!
! Compute eigenvalues and eigenvectors
!
call dgesvd('Overwrite A by U', 'All n rows of VT', m, n, a, lda, &
s, dummy, 1, vt, ldvt, work, lwork, info)
lwkopt = work(1)
if (info/=0)then
print *,'Error in DGESVD. INFO =', info
stop
endif
write(nout, *) 'Singular values'
write(nout, '(3X, (8F8.4))')(s(j), j=1, n)
ifail = 0
write(nout,*)
write(nout,*) 'Left singular vectors (first n columns of U)'
do i=1,m
write(nout,'(100f8.4)')(a(i,j),j=1,m)
end do !i
write(nout,*)
write(nout,*) 'Right singular vectors by row (V**T)'
do i=1,n
write(nout,'(100f8.4)')(vt(i,j),j=1,n)
end do !i
!
! Error estimate
!
eps = dlamch('Eps')
serrbd=eps*s(1)
call ddisna('Left', m, n, s, rcondu, info)
call ddisna('Right', m, n, s, rcondv, info)
! Compute the error estimates for the singular vectors
do i = 1, m
uerrbd(i) = serrbd/rcondu(i)
verrbd(i) = serrbd/rcondv(i)
end do
write (nout, *)
write (nout, *) 'Error estimate for the singular values'
write (nout, '(4X, 1P, 6E11.1)') serrbd
write (nout, *)
write (nout, *) 'Error estimates for the left singular vectors'
write (nout, '(4X, 1P, 6E11.1)')(uerrbd(i), i=1, m)
write (nout, *)
write (nout, *) 'Error estimates for the right singular vectors'
write (nout, '(4X, 1P, 6E11.1)')(verrbd(i), i=1, m)
if (lwork<lwkopt) Then
write (nout, *)
write (nout, '(1X, A, I5, /1X, A, I5)') &
'Optimum workspace required = ', lwkopt, &
'Workspace provided = ', lwork
end if
stop
end program ${prog_name}
EOF
echo
echo Compiling ${src} ...
${f90} ${opt} ${src} -o ${exe}
if [ $? -ne 0 ]; then
echo
echo Compile error!
echo
echo Terminated.
echo
exit 1
fi
echo "Done Compile."
echo
echo ${exe} is running ...
echo
${exe} < ${data_file}
if [ $? -ne 0 ]; then
echo
echo ERROR in $exe: Runtime error!
echo
echo Terminated.
echo
exit 1
fi
echo
echo "Done ${exe}"
echo
----------------------
End of svd.sh
----------------------