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
Prev Previous commit
Next Next commit
svds now uses eigs of [0 A; A' 0]
  • Loading branch information
jaak-s committed Jan 2, 2015
commit 95e2d6c5a34355324a2326df922a15d27f3acabc
30 changes: 19 additions & 11 deletions base/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,27 @@ end

type SVDOperator <: 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.

m::Int
n::Int
SVDOperator(X) = new(X, size(X,1), size(X,2))
end

*(s::SVDOperator, v::Vector{Float64}) = s.X' * (s.X * v)
size(s::SVDOperator) = size(s.X, 2), size(s.X, 2)
## v = [ left_singular_vector; right_singular_vector ]
*(s::SVDOperator, v::Vector{Float64}) = [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 svds(X; ritzvec::Bool = true, args...)
ex = eigs(SVDOperator(X), I; ritzvec = ritzvec, args...)
if ! ritzvec
return sqrt(ex[1]), ex[2], ex[3], ex[4], ex[5]
end

## 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]
function svds(X; ritzvec::Bool = true, nsv::Int = 6, tol::Float64 = 0.0, maxiter::Int = 1000)
ex = eigs(SVDOperator(X), I; ritzvec = ritzvec, nev = 2*nsv, tol = tol, maxiter = maxiter)
ind = [1:2:nsv*2]
sval = (abs(ex[1][ind]) + abs(ex[1][ind + 1])) / 2

Copy link
Contributor

Choose a reason for hiding this comment

The 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
2 changes: 1 addition & 1 deletion test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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)
S1 = svds(A, nsv = 2)
S2 = svd(full(A))

## singular values match:
Expand Down