Skip to content

Commit

Permalink
Merge pull request #213 from nep-pack/eigsolver_refactor
Browse files Browse the repository at this point in the history
Eigsolver refactor
  • Loading branch information
jarlebring authored Sep 11, 2019
2 parents 5abe5f9 + 05f9c34 commit 43968a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
6 changes: 0 additions & 6 deletions docs/src/deflation.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ NEP-solver to the deflated NEP.
More elaborate deflation examples can be found in [the tutorial on deflation](deflate_tutorial.md).








## Theory

The theory follows the presentation of the technique
Expand Down
File renamed without changes.
46 changes: 23 additions & 23 deletions src/LinSolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module LinSolvers
# Eigenvalue solvers
export EigSolver
export DefaultEigSolver
export NativeEigSolver
export NativeEigSSolver
export EigenEigSolver
export ArnoldiEigSolver
export eig_solve

import Base.eltype
Expand Down Expand Up @@ -269,21 +269,21 @@ eigenproblem is solved.
See also: [`EigSolver`](@ref) and [`eig_solve`](@ref)
"""
struct NativeEigSolver{T_A,T_B} <: EigSolver
struct EigenEigSolver{T_A,T_B} <: EigSolver
A::T_A
B::T_B

function NativeEigSolver(A)
function EigenEigSolver(A)
return new{typeof(A),Missing}(A,missing)
end
function NativeEigSolver(A,B)
function EigenEigSolver(A,B)
return new{typeof(A),typeof(B)}(A,B)
end
end



function eig_solve(solver::NativeEigSolver; nev = 1, target = 0)
function eig_solve(solver::EigenEigSolver; nev = 1, target = 0)
D,V = inner_eig_solve(solver)

#Sort the eigenvalues wrt distance from target, and permute
Expand All @@ -292,20 +292,20 @@ See also: [`EigSolver`](@ref) and [`eig_solve`](@ref)
return D[1:nev],V[:,1:nev];
end

function inner_eig_solve(solver::NativeEigSolver{T_A,T_B}) where {T_A, T_B}
function inner_eig_solve(solver::EigenEigSolver{T_A,T_B}) where {T_A, T_B}
D,V = eigen(solver.A,solver.B)
end
function inner_eig_solve(solver::NativeEigSolver{T_A,T_B}) where {T_A, T_B<:Missing}
function inner_eig_solve(solver::EigenEigSolver{T_A,T_B}) where {T_A, T_B<:Missing}
D,V = eigen(solver.A)
end

"""
struct NativeEigSSolver <: EigSolver
struct ArnoldiEigSolver <: EigSolver
A linear eigenvalueproblem solver for large and sparse problems that calls
Julia's in-built eigs()
A linear eigenproblem solver for large and sparse problems that calls
the Arnoldi method implemented in the Julia package ArnoldiMethod.jl.
Constructed as `NativeEigSSolver(A, [B,])`, and solves the problem
Constructed as `ArnoldiEigSolver(A, [B,])`, and solves the problem
```math
Ax = λBx
```
Expand All @@ -314,26 +314,26 @@ eigenproblem is solved.
See also: [`EigSolver`](@ref) and [`eig_solve`](@ref)
"""
struct NativeEigSSolver{T_A,T_B} <: EigSolver
struct ArnoldiEigSolver{T_A,T_B} <: EigSolver
A::T_A
B::T_B

function NativeEigSSolver(A)
function ArnoldiEigSolver(A)
return new{typeof(A),Missing}(A,missing)
end
function NativeEigSSolver(A,B)
function ArnoldiEigSolver(A,B)
return new{typeof(A),typeof(B)}(A,B)
end

end


function eig_solve(solver::NativeEigSSolver; nev=6, target=0)
function eig_solve(solver::ArnoldiEigSolver; nev=6, target=0)
D,V = inner_eigs_solve(solver, nev, target)
return D,V
end

function inner_eigs_solve(solver::NativeEigSSolver{T_A,T_B}, nev, target) where {T_A, T_B}
function inner_eigs_solve(solver::ArnoldiEigSolver{T_A,T_B}, nev, target) where {T_A, T_B}
if (T_B <: Missing)
C=target*I-solver.A;
Cfact=factorize(C);
Expand Down Expand Up @@ -365,26 +365,26 @@ See also: [`EigSolver`](@ref) and [`eig_solve`](@ref)
A linear eigenvalueproblem solver that calls checks for sparsity and accordingly
assigns an appropriate solver.
See also: [`EigSolver`](@ref), [`eig_solve`](@ref), [`NativeEigSolver`](@ref), [`NativeEigSSolver`](@ref)
See also: [`EigSolver`](@ref), [`eig_solve`](@ref), [`EigenEigSolver`](@ref), [`ArnoldiEigSolver`](@ref)
"""
struct DefaultEigSolver{T_sub} <: EigSolver
subsolver::T_sub

function DefaultEigSolver(A,B)
local subsolver
if(issparse(A))
subsolver = NativeEigSSolver(A,B)
subsolver = ArnoldiEigSolver(A,B)
else
subsolver = NativeEigSolver(A,B)
subsolver = EigenEigSolver(A,B)
end
return new{typeof(subsolver)}(subsolver)
end
function DefaultEigSolver(A)
local subsolver
if(issparse(A))
subsolver = NativeEigSSolver(A)
subsolver = ArnoldiEigSolver(A)
else
subsolver = NativeEigSolver(A)
subsolver = EigenEigSolver(A)
end
return new{typeof(subsolver)}(subsolver)
end
Expand All @@ -409,6 +409,6 @@ way of solving linear eigenvalue problems. See [`EigSolver`](@ref) for examples.
return eig_solve(solver.subsolver,nev=nev,target=target)
end

include("NewLinSolvers.jl");
include("LinSolverCreators.jl");

end
12 changes: 6 additions & 6 deletions test/linsolver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ using LinearAlgebra
end
end

@bench @testset "NativeEigSolver" begin
@bench @testset "EigenEigSolver" begin
Random.seed!(0)
n = 20
A = rand(ComplexF64, n, n)
B = rand(ComplexF64, n, n)

eigsolver1 = NativeEigSolver(A,B)
eigsolver2 = NativeEigSolver(B\A)
eigsolver1 = EigenEigSolver(A,B)
eigsolver2 = EigenEigSolver(B\A)

λ1,v1 = eig_solve(eigsolver1, nev=3)
λ2,v2 = eig_solve(eigsolver2, nev=3)
Expand All @@ -153,14 +153,14 @@ using LinearAlgebra
end
end

@bench @testset "NativeEigSSolver" begin
@bench @testset "ArnoldiEigSolver" begin
Random.seed!(0)
n = 20
A = sprand(ComplexF64, n, n, 0.25) + I
B = sprand(ComplexF64, n, n, 0.25) + I

eigsolver1 = NativeEigSSolver(A,B)
eigsolver2 = NativeEigSSolver(sparse(Matrix(B)\Matrix(A)))
eigsolver1 = ArnoldiEigSolver(A,B)
eigsolver2 = ArnoldiEigSolver(sparse(Matrix(B)\Matrix(A)))

λ1,v1 = eig_solve(eigsolver1, nev=3)
λ2,v2 = eig_solve(eigsolver2, nev=3)
Expand Down

0 comments on commit 43968a4

Please sign in to comment.