Skip to content

Commit

Permalink
Make names more consistent with LinearAlgebra.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie committed Aug 13, 2018
1 parent f21fda7 commit e411c14
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
6 changes: 3 additions & 3 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
RandomizedLinAlg.jl is a Julia package that provides some randomized algorithms for numerical linear algebra as advocated in [^Halko2011].

```@docs
reig
rsvdfact
reigen
rsvd
rsvd_fnkz
```

Expand All @@ -31,7 +31,7 @@ rnorms
## Interpolative Decomposition

```@docs
idfact
id
```

[^Halko2011]: Halko, Nathan, Per-Gunnar Martinsson, and Joel A. Tropp. "Finding structure with randomness: Probabilistic algorithms for constructing approximate matrix decompositions." SIAM review 53.2 (2011): 217-288.
Expand Down
8 changes: 4 additions & 4 deletions src/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# Specialized factorizations
############################

export idfact
export id

"""
An interpolative decomposition.
For a matrix `A`, the interpolative decomposition `F` contains the matrices `B`
and `P` computed by `idfact()`. See the documentation of `idfact()` for details.
and `P` computed by `id()`. See the documentation of `id()` for details.
# References
Expand All @@ -20,7 +20,7 @@ struct Interpolative{T} <: Factorization{T}
end

"""
idfact(A, k, l)
id(A, k, l)
Compute and return the interpolative decomposition of `A`: A ≈ B * P
Expand Down Expand Up @@ -76,7 +76,7 @@ pivoted QR process.
}
```
"""
function idfact(A, k::Int, l::Int)
function id(A, k::Int, l::Int)
m, n = size(A)
R = randn(l, m)
Y = R * A #size l x n
Expand Down
70 changes: 35 additions & 35 deletions src/rsvd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import LinearAlgebra: Eigen, SVD

export rsvdfact, reig
export rsvd, reig

"""
rsvdfact(A, n, p=0)
rsvd(A, n, p=0)
Compute partial singular value decomposition of `A` using a randomized
algorithm.
Expand Down Expand Up @@ -39,7 +39,7 @@ algorithm.
This function calls `rrange`, which uses naive randomized rangefinding to
compute a basis for a subspace of dimension `n` (Algorithm 4.1 of
[^Halko2011]), followed by `svdfact_restricted()`, which computes the
[^Halko2011]), followed by `svd_restricted()`, which computes the
exact SVD factorization on the restriction of `A` to this randomly selected
subspace (Algorithm 5.1 of [^Halko2011]).
Expand All @@ -48,9 +48,9 @@ any of the randomized range finding algorithms to find a suitable subspace
and feeding the result to one of the routines that computes the `SVD`
restricted to that subspace.
"""
function rsvdfact(A, n::Int, p::Int=0)
function rsvd(A, n::Int, p::Int=0)
Q = rrange(A, n+p)
svdfact_restricted(A, Q, n)
svd_restricted(A, Q, n)
end

"""
Expand Down Expand Up @@ -80,7 +80,7 @@ algorithm.
This function calls `rrange`, which uses naive randomized rangefinding to
compute a basis for a subspace of dimension `n` (Algorithm 4.1 of
[^Halko2011]), followed by `svdfact_restricted()`, which computes the
[^Halko2011]), followed by `svd_restricted()`, which computes the
exact SVD factorization on the restriction of `A` to this randomly selected
subspace (Algorithm 5.1 of [^Halko2011]).
Expand Down Expand Up @@ -278,7 +278,7 @@ function rrange_f(A, l::Int)
end

"""
svdfact_restricted(A, Q, n)
svd_restricted(A, Q, n)
Compute the SVD factorization of `A` restricted to the subspace spanned by `Q`
using exact projection.
Expand All @@ -297,7 +297,7 @@ using exact projection.
Algorithm 5.1 of [^Halko2011]
"""
function svdfact_restricted(A, Q, n::Int)
function svd_restricted(A, Q, n::Int)
B=Q'A
S=svd!(B)
SVD((Q*S.U)[:, 1:n], S.S[1:n], S.Vt[1:n, :])
Expand Down Expand Up @@ -329,7 +329,7 @@ function svdvals_restricted(A, Q, n::Int)
end

"""
svdfact_re(A, Q)
svd_re(A, Q)
Compute the SVD factorization of `A` restricted to the subspace spanned by `Q`
using row extraction.
Expand All @@ -349,15 +349,15 @@ where `Ω` is a sample computed by `randn(n,l)` or even `srft(l)`.
# See also
A faster but less accurate variant of [`svdfact_restricted`](@ref) which uses the
interpolative decomposition `idfact`.
A faster but less accurate variant of [`svd_restricted`](@ref) which uses the
interpolative decomposition `id`.
# References
Algorithm 5.2 of [^Halko2011]
"""
function svdfact_re(A, Q)
F = idfact(Q)
function svd_re(A, Q)
F = id(Q)
X, J = F[:B], F[:P]
R′, W′= qr(A[J, :])
Z = X*R′
Expand All @@ -366,7 +366,7 @@ function svdfact_re(A, Q)
end

"""
eigfact_restricted(A, Q)
eigen_restricted(A, Q)
Compute the spectral (`Eigen`) factorization of `A` restricted to the subspace
spanned by `Q` using row extraction.
Expand All @@ -385,14 +385,14 @@ spanned by `Q` using row extraction.
Algorithm 5.3 of [^Halko2011]
"""
function eigfact_restricted(A::Hermitian, Q)
function eigen_restricted(A::Hermitian, Q)
B = Q'A*Q
E = eigen!(B)
Eigen(E.values, Q*E.vectors)
end

"""
eigfact_re(A, Q)
eigen_re(A, Q)
Compute the spectral (`Eigen`) factorization of `A` restricted to the subspace
spanned by `Q` using row extraction.
Expand All @@ -412,15 +412,15 @@ where `Ω` is a sample computed by `randn(n,l)` or even `srft(l)`.
# See also
A faster but less accurate variant of `eigfact_restricted()` which uses the
interpolative decomposition `idfact()`.
A faster but less accurate variant of `eigen_restricted()` which uses the
interpolative decomposition `id()`.
# References
Algorithm 5.4 of [^Halko2011]
"""
function eigfact_re(A::Hermitian, Q)
X, J = idfact(Q)
function eigen_re(A::Hermitian, Q)
X, J = id(Q)
F = qr!(X)
V, R = F.Q, F.R
Z=R*A[J, J]*R'
Expand All @@ -429,7 +429,7 @@ function eigfact_re(A::Hermitian, Q)
end

"""
eigfact_nystrom(A, Q)
eigen_nystrom(A, Q)
Compute the spectral (`Eigen`) factorization of `A` restricted to the subspace
spanned by `Q` using the Nyström method.
Expand All @@ -446,18 +446,18 @@ spanned by `Q` using the Nyström method.
# See also
More accurate than [`eigfact_restricted`](@ref) but is restricted to matrices
More accurate than [`eigen_restricted`](@ref) but is restricted to matrices
that can be Cholesky decomposed.
# References
Algorithm 5.5 of [^Halko2011]
"""
function eigfact_nystrom(A, Q)
function eigen_nystrom(A, Q)
B₁=A*Q
B₂=Q'*B₁
C=cholfact!(B₂)
F=B₁*inv(C)
C=cholesky!(Hermitian(B₂))
F=B₁/C
S=svd!(F)
Eigen(S.S.^2, S.U)
end
Expand All @@ -483,26 +483,26 @@ product involving `A`.
Algorithm 5.6 of [^Halko2011]
"""
function eigfact_onepass(A::Hermitian, Ω)
function eigen_onepass(A::Hermitian, Ω)
Y=A*Ω; Q = Matrix(qr!(Y).Q)
B=(Q'Y)\(Q'Ω)
E=eigfact!(B)
Eigen(E[:values], Q*E[:vectors])
E=eigen!(B)
Eigen(E.values, Q*E.vectors)
end
function eigfact_onepass(A, Ω, Ω̃; At=A')
function eigen_onepass(A, Ω, Ω̃; At=A')
Y=A *Ω; Q = Matrix(qr!(Y).Q)
=At*Ω; Q̃ = Matrix(qr!(Ỹ).Q)
#Want least-squares solution to (5.14 and 5.15)
B=(Q'Y)\(Q̃'Ω)
=(Q̃'Ỹ)\(Q'Ω̃)
#Here is a very very very hacky way to solve the problem
B=0.5(B +')
E=eigfact!(B)
Eigen(E[:values], Q*E[:vectors])
E=eigen!(B)
Eigen(E.values, Q*E.vectors)
end

"""
reig(A, l)
reigen(A, l)
Compute the spectral (`Eigen`) decomposition of `A` using a randomized
algorithm.
Expand All @@ -518,8 +518,8 @@ algorithm.
# Implementation note
This is a wrapper around `eigfact_onepass()` which uses the randomized
This is a wrapper around `eigen_onepass()` which uses the randomized
samples found using `srft(l)`.
"""
reig(A::Hermitian, l::Int) = eigfact_onepass(A, srft(l))
reig(A, l::Int) = eigfact_onepass(A, srft(l), srft(l))
reigen(A::Hermitian, l::Int) = eigen_onepass(A, srft(l))
reigen(A, l::Int) = eigen_onepass(A, srft(l), srft(l))
4 changes: 2 additions & 2 deletions test/factorization.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using RandomizedLinAlg
using Test, Random, LinearAlgebra

@testset "IDfact" begin
@testset "ID" begin
Random.seed!(1)
M = randn(4,5)
k = 3

F = idfact(M, k, 3)
F = id(M, k, 3)
@test norm(F.B * F.P - M) 2svdvals(M)[k + 1]
end
6 changes: 3 additions & 3 deletions test/rsvd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Test, LinearAlgebra, Random
@testset "Small wide rectangular" begin
A = [1. 2 3 4; 5 6 7 8]
S1 = svd(A)
S2 = rsvdfact(A, 2, 0)
S2 = rsvd(A, 2, 0)

@test norm(abs.(S1.U) - abs.(S2.U)) (eps())
@test norm(abs.(S1.Vt) - abs.(S2.Vt)) (eps())
Expand All @@ -18,7 +18,7 @@ using Test, LinearAlgebra, Random
@testset "Small tall rectangular" begin
A = [1. 2; 3 4; 5 6; 7 8]
S1 = svd(A)
S2 = rsvdfact(A, 2, 0)
S2 = rsvd(A, 2, 0)

@test norm(abs.(S1.U) .- abs.(S2.U)) (eps())
@test norm(abs.(S1.Vt) .- abs.(S2.Vt)) (eps())
Expand All @@ -28,7 +28,7 @@ using Test, LinearAlgebra, Random
@testset "Small square" begin
A = [1. 2 3; 4 5 6; 7 8 9]
S1 = svd(A)
S2 = rsvdfact(A, 3, 0)
S2 = rsvd(A, 3, 0)

@test norm(abs.(S1.U) - abs.(S2.U)) (eps())
@test norm(abs.(S1.Vt) - abs.(S2.Vt)) (eps())
Expand Down

0 comments on commit e411c14

Please sign in to comment.