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

Allow which symbols like in Arpack.jl and KrylovKit.jl #132

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion bench/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using Arpack

function helloworld(n = 40, nev = 4)
A = randn(n, n)
@time S, hist = partialschur(A, nev = nev, which = LR(), tol = 1e-6)
@time S, hist = partialschur(A, nev = nev, which = :LR, tol = 1e-6)
@time eigs(A, nev = nev, which = :LR, tol = 1e-6)
@show norm(A * S.Q - S.Q * S.R)
println(hist)
Expand Down
2 changes: 1 addition & 1 deletion bench/partial_schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end
function bencharnoldi()
A = mymatrix(6000)
A = construct_linear_map(mymatrix(6000))
target = LM()
target = :LM

@benchmark partialschur(
B,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/usage/01_getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ julia> A = spdiagm(
0 => fill(2.0, 100),
1 => fill(-1.0, 99)
);
julia> decomp, history = partialschur(A, nev=10, tol=1e-6, which=SR());
julia> decomp, history = partialschur(A, nev=10, tol=1e-6, which=:SR);
julia> decomp
PartialSchur decomposition (Float64) of dimension 10
eigenvalues:
Expand Down
4 changes: 2 additions & 2 deletions docs/src/usage/02_spectral_transformations.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function construct_linear_map(A)
end

# Target the largest eigenvalues of the inverted problem
decomp, = partialschur(construct_linear_map(A), nev=4, tol=1e-5, restarts=100, which=LM())
decomp, = partialschur(construct_linear_map(A), nev=4, tol=1e-5, restarts=100, which=:LM)
λs_inv, X = partialeigen(decomp)

# Eigenvalues have to be inverted to find the smallest eigenvalues of the non-inverted problem.
Expand Down Expand Up @@ -65,7 +65,7 @@ function construct_linear_map(A,B)
end

# Target the largest eigenvalues of the inverted problem
decomp, = partialschur(construct_linear_map(A, B), nev=4, tol=1e-5, restarts=100, which=LM())
decomp, = partialschur(construct_linear_map(A, B), nev=4, tol=1e-5, restarts=100, which=:LM)
λs_inv, X = partialeigen(decomp)

# Eigenvalues have to be inverted to find the smallest eigenvalues of the non-inverted problem.
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ julia> A = spdiagm(
1 => fill(-1.0, 99)
);

julia> decomp, history = partialschur(A, nev=10, tol=1e-6, which=SR());
julia> decomp, history = partialschur(A, nev=10, tol=1e-6, which=:SR);

julia> decomp
PartialSchur decomposition (Float64) of dimension 10
Expand Down
32 changes: 20 additions & 12 deletions src/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ The most important keyword arguments:
| Keyword | Type | Default | Description |
|------:|:-----|:----|:------|
| `nev` | `Int` | `min(6, size(A, 1))` |Number of eigenvalues |
| `which` | `Target` | `LM()` | One of `LM()`, `LR()`, `SR()`, `LI()`, `SI()`, see below. |
| `which` | `Symbol` or `Target` | `:LM` | One of `:LM`, `:LR`, `:SR`, `:LI`, `:SI`, see below. |
| `tol` | `Real` | `√eps` | Tolerance for convergence: ‖Ax - xλ‖₂ < tol * ‖λ‖ |

The target `which` can be any of `subtypes(ArnoldiMethod.Target)`:
The target `which` can be any of:

| Target | Description |
|------:|:-----|
| `LM()` | Largest magnitude: `abs(λ)` is largest |
| `LR()` | Largest real part: `real(λ)` is largest |
| `SR()` | Smallest real part: `real(λ)` is smallest |
| `LI()` | Largest imaginary part: `imag(λ)` is largest|
| `SI()` | Smallest imaginary part: `imag(λ)` is smallest|
| Target | Description |
|----------------:|:-----------------------------------------------|
| `:LM` or `LM()` | Largest magnitude: `abs(λ)` is largest |
| `:LR` or `LR()` | Largest real part: `real(λ)` is largest |
| `:SR` or `SR()` | Smallest real part: `real(λ)` is smallest |
| `:LI` or `LI()` | Largest imaginary part: `imag(λ)` is largest |
| `:SI` or `SI()` | Smallest imaginary part: `imag(λ)` is smallest |

!!! note

The targets `LI()` and `SI()` only make sense in complex arithmetic. In real
The targets `:LI` and `:SI` only make sense in complex arithmetic. In real
arithmetic `λ` is an eigenvalue iff `conj(λ)` is an eigenvalue and this
conjugate pair converges simultaneously.

Expand Down Expand Up @@ -94,7 +94,7 @@ is usually about two times `mindim`.
function partialschur(
A;
nev::Int = min(6, size(A, 1)),
which::Target = LM(),
which::Union{Target,Symbol} = LM(),
tol::Real = sqrt(eps(real(vtype(A)))),
mindim::Int = min(max(10, nev), size(A, 1)),
maxdim::Int = min(max(20, 2nev), size(A, 1)),
Expand All @@ -107,9 +107,17 @@ function partialschur(
nev ≤ mindim ≤ maxdim ≤ s || throw(
ArgumentError("nev ≤ mindim ≤ maxdim does not hold, got $nev ≤ $mindim ≤ $maxdim"),
)
_partialschur(A, vtype(A), mindim, maxdim, nev, tol, restarts, which)
_which = which isa Target ? which : _symbol_to_target(which)
_partialschur(A, vtype(A), mindim, maxdim, nev, tol, restarts, _which)
end

_symbol_to_target(sym::Symbol) =
sym == :LM ? LM() :
sym == :LR ? LR() :
sym == :SR ? SR() :
sym == :LI ? LI() : sym == :SI ? SI() : throw(ArgumentError("Unknown target: $sym"))


"""
IsConverged(ritz, tol)

Expand Down
2 changes: 1 addition & 1 deletion test/partial_schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
@testset "Target non-dominant eigenvalues" begin
# Dominant eigenvalues 50, 51, 52, 53, but we target the smallest real part
A = Diagonal([1:0.1:10; 50:53])
S, hist = partialschur(A, which = SR())
S, hist = partialschur(A, which = :SR)
@test all(x -> real(x) ≤ 10, eigenvalues(S.R))
end

Expand Down
Loading