-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linalg: Singular Value Decomposition (#808)
- Loading branch information
Showing
10 changed files
with
878 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
! Singular Value Decomposition | ||
program example_svd | ||
use stdlib_linalg_constants, only: dp | ||
use stdlib_linalg, only: svd | ||
implicit none | ||
|
||
real(dp), allocatable :: A(:,:),s(:),u(:,:),vt(:,:) | ||
character(*), parameter :: fmt = "(a,*(1x,f12.8))" | ||
|
||
! We want to find the singular value decomposition of matrix: | ||
! | ||
! A = [ 3 2 2] | ||
! [ 2 3 -2] | ||
! | ||
A = transpose(reshape([ 3, 2, 2, & | ||
2, 3,-2], [3,2])) | ||
|
||
! Prepare arrays | ||
allocate(s(2),u(2,2),vt(3,3)) | ||
|
||
! Get singular value decomposition | ||
call svd(A,s,u,vt) | ||
|
||
! Singular values: [5, 3] | ||
print fmt, ' ' | ||
print fmt, 'S = ',s | ||
print fmt, ' ' | ||
|
||
! Left vectors (may be flipped): | ||
! [Ã2/2 Ã2/2] | ||
! U = [Ã2/2 -Ã2/2] | ||
! | ||
print fmt, ' ' | ||
print fmt, 'U = ',u(1,:) | ||
print fmt, ' ',u(2,:) | ||
|
||
|
||
! Right vectors (may be flipped): | ||
! [Ã2/2 Ã2/2 0] | ||
! V = [1/Ã18 -1/Ã18 4/Ã18] | ||
! [ 2/3 -2/3 -1/3] | ||
! | ||
print fmt, ' ' | ||
print fmt, ' ',vt(1,:) | ||
print fmt, 'VT= ',vt(2,:) | ||
print fmt, ' ',vt(3,:) | ||
print fmt, ' ' | ||
|
||
|
||
end program example_svd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
! Singular Values | ||
program example_svdvals | ||
use stdlib_linalg_constants, only: dp | ||
use stdlib_linalg, only: svdvals | ||
implicit none | ||
|
||
real(dp), allocatable :: A(:,:),s(:) | ||
character(*), parameter :: fmt="(a,*(1x,f12.8))" | ||
|
||
! We want to find the singular values of matrix: | ||
! | ||
! A = [ 3 2 2] | ||
! [ 2 3 -2] | ||
! | ||
A = transpose(reshape([ 3, 2, 2, & | ||
2, 3,-2], [3,2])) | ||
|
||
! Get singular values | ||
s = svdvals(A) | ||
|
||
! Singular values: [5, 3] | ||
print fmt, ' ' | ||
print fmt, 'S = ',s | ||
print fmt, ' ' | ||
|
||
end program example_svdvals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.