-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initial svds support based on eigs #9425
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ea6b51
initial svds support based on eigs
jaak-s 8b061df
removed extra spaces, added ritzvec param to svds
jaak-s d654afa
manual for svds
jaak-s 6e91217
renamed to SVDOperator
jaak-s 95e2d6c
svds now uses eigs of [0 A; A' 0]
jaak-s b1517a6
support for Complex operators
jaak-s be1f1bb
removed trailing whitespace
jaak-s f8edd41
replaced getOperatorType to eltype
jaak-s b9d7a30
added check for nsv
jaak-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -690,6 +690,7 @@ export | |
svd, | ||
svdfact!, | ||
svdfact, | ||
svds, | ||
svdvals!, | ||
svdvals, | ||
sylvester, | ||
|
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 |
---|---|---|
|
@@ -102,6 +102,7 @@ export | |
svd, | ||
svdfact!, | ||
svdfact, | ||
svds, | ||
svdvals!, | ||
svdvals, | ||
sylvester, | ||
|
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 |
---|---|---|
|
@@ -106,3 +106,51 @@ function eigs(A, B; | |
resid, ncv, v, ldv, sigma, iparam, ipntr, workd, workl, lworkl, rwork) | ||
|
||
end | ||
|
||
|
||
## svds | ||
|
||
type SVDOperator{T,S} <: AbstractArray{T, 2} | ||
X::S | ||
m::Int | ||
n::Int | ||
SVDOperator(X::S) = new(X, size(X,1), size(X,2)) | ||
end | ||
|
||
## v = [ left_singular_vector; right_singular_vector ] | ||
*{T,S}(s::SVDOperator{T,S}, v::Vector{T}) = [s.X * v[s.m+1:end]; s.X' * v[1:s.m]] | ||
size(s::SVDOperator) = s.m + s.n, s.m + s.n | ||
issym(s::SVDOperator) = true | ||
|
||
function getOperatorType(X) | ||
et = eltype(X) | ||
if et <: Integer || et <: Float64 | ||
return Float64 | ||
end | ||
if et <: Complex64 | ||
return Complex64 | ||
end | ||
if et <: Complex | ||
return Complex128 | ||
end | ||
if et <: Float32 | ||
return Float32 | ||
end | ||
error("Element type $et is not supported for 'svds'.") | ||
end | ||
|
||
function svds{S}(X::S; nsv::Int = 6, ritzvec::Bool = true, tol::Float64 = 0.0, maxiter::Int = 1000) | ||
otype = getOperatorType(X) | ||
ex = eigs(SVDOperator{otype,S}(X), I; ritzvec = ritzvec, nev = 2*nsv, tol = tol, maxiter = maxiter) | ||
ind = [1:2:nsv*2] | ||
sval = abs(ex[1][ind]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. travis doesn't like the trailing whitespace here or on line 151 |
||
if ! ritzvec | ||
return sval, ex[2], ex[3], ex[4], ex[5] | ||
end | ||
|
||
## calculating singular vectors | ||
left_sv = sqrt(2) * ex[2][ 1:size(X,1), ind ] .* sign(ex[1][ind]') | ||
right_sv = sqrt(2) * ex[2][ size(X,1)+1:end, ind ] | ||
return left_sv, sval, right_sv, ex[3], ex[4], ex[5], ex[6] | ||
end |
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,58 @@ | ||
using Base.Test | ||
|
||
let # svds test | ||
A = sparse([1, 1, 2, 3, 4], [2, 1, 1, 3, 1], [2.0, -1.0, 6.1, 7.0, 1.5]) | ||
S1 = svds(A, nsv = 2) | ||
S2 = svd(full(A)) | ||
|
||
## singular values match: | ||
@test_approx_eq S1[2] S2[2][1:2] | ||
|
||
## 1st left singular vector | ||
s1_left = sign(S1[1][3,1]) * S1[1][:,1] | ||
s2_left = sign(S2[1][3,1]) * S2[1][:,1] | ||
@test_approx_eq s1_left s2_left | ||
|
||
## 1st right singular vector | ||
s1_right = sign(S1[3][3,1]) * S1[3][:,1] | ||
s2_right = sign(S2[3][3,1]) * S2[3][:,1] | ||
@test_approx_eq s1_right s2_right | ||
end | ||
|
||
let # svd test with boolean matrix | ||
B = [true true false; false true false; true false true; false true true] | ||
S1 = svds(B, nsv = 2) | ||
S2 = svd(B) | ||
|
||
## singular values match: | ||
@test_approx_eq S1[2] S2[2][1:2] | ||
|
||
## 1st left singular vector | ||
s1_left = sign(S1[1][1,1]) * S1[1][:,1] | ||
s2_left = sign(S2[1][1,1]) * S2[1][:,1] | ||
@test_approx_eq s1_left s2_left | ||
|
||
## 1st right singular vector | ||
s1_right = sign(S1[3][1,1]) * S1[3][:,1] | ||
s2_right = sign(S2[3][1,1]) * S2[3][:,1] | ||
@test_approx_eq s1_right s2_right | ||
end | ||
|
||
let # complex svds test | ||
A = sparse([1, 1, 2, 3, 4], [2, 1, 1, 3, 1], exp(im*[2.0:2:10])) | ||
S1 = svds(A, nsv = 2) | ||
S2 = svd(full(A)) | ||
|
||
## singular values match: | ||
@test_approx_eq S1[2] S2[2][1:2] | ||
|
||
## left singular vectors | ||
s1_left = abs(S1[1][:,1:2]) | ||
s2_left = abs(S2[1][:,1:2]) | ||
@test_approx_eq s1_left s2_left | ||
|
||
## right singular vectors | ||
s1_right = abs(S1[3][:,1:2]) | ||
s2_right = abs(S2[3][:,1:2]) | ||
@test_approx_eq s1_right s2_right | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just simplify this to use just
eltype(X)
and avoidgetOperatorType()
altogether. The checks for whether eltype() is BlasFloat should ideally be ineigs
. It should be possible to makeSVDOperator
free of types then.