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
support for Complex operators
  • Loading branch information
jaak-s committed Jan 3, 2015
commit b1517a6b12553a82a9d867f0e65fe5e489638f8d
34 changes: 26 additions & 8 deletions base/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,40 @@ end

## svds

type SVDOperator <: AbstractArray{Float64, 2}
X
type SVDOperator{T,S} <: AbstractArray{T, 2}
X::S
m::Int
n::Int
SVDOperator(X) = new(X, size(X,1), size(X,2))
SVDOperator(X::S) = new(X, size(X,1), size(X,2))
end

## 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]]
*{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 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
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)
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 can just simplify this to use just eltype(X) and avoid getOperatorType() altogether. The checks for whether eltype() is BlasFloat should ideally be in eigs. It should be possible to make SVDOperator free of types then.

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])

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]
Expand Down
38 changes: 38 additions & 0 deletions test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,41 @@ let # svds test
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