Skip to content
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 9 commits into from
Jan 11, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ export
svd,
svdfact!,
svdfact,
svds,
svdvals!,
svdvals,
sylvester,
Expand Down
1 change: 1 addition & 0 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export
svd,
svdfact!,
svdfact,
svds,
svdvals!,
svdvals,
sylvester,
Expand Down
20 changes: 20 additions & 0 deletions base/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,23 @@ function eigs(A, B;
resid, ncv, v, ldv, sigma, iparam, ipntr, workd, workl, lworkl, rwork)

end


## svds

type SvdX <: AbstractArray{Float64, 2}
X
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd like to parametrize this type on the input element and matrix type (and use capital letters), i.e. something like

type SVDOperator{T,S}
data::S
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I addition, we'll have to support more than Float64 and the type should reflect that. The type can probably not be subtype of AbstractMatrix either, as the input might not be a subtype of AbstractMatrix.

end

*(s::SvdX, v::Vector{Float64}) = s.X' * (s.X * v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach ruins the precision of the small values. I think that the documentation suggests op = [0 A;A' 0] for the SVD. I'd also like to see the allocation free version, A_mul_B! for later use.

size(s::SvdX) = size(s.X, 2), size(s.X, 2)
issym(s::SvdX) = true

function svds(X; args...)
ex = eigs(SvdX(X), I; args...)
## calculating left-side singular vectors
left_sv = X * ex[2]
return left_sv ./ sqrt(sum(left_sv.^2, 1)), sqrt(ex[1]), ex[2], ex[3], ex[4], ex[5], ex[6]
end


20 changes: 20 additions & 0 deletions test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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, nev = 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