Skip to content
Open
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: 2 additions & 0 deletions docs/src/LAPACK.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ LinearAlgebra.LAPACK.orgqr!(::QRWs, ::AbstractMatrix, ::Integer)
```@docs
LinearAlgebra.LAPACK.gees!(::SchurWs, ::AbstractChar, ::AbstractMatrix)
LinearAlgebra.LAPACK.gges!(::GeneralizedSchurWs, ::AbstractChar, ::AbstractChar, ::AbstractMatrix, ::AbstractMatrix)
LinearAlgebra.LAPACK.tgsen!(::GeneralizedOrdSchurWs, ::AbstractVector{LinearAlgebra.BlasInt}, ::AbstractMatrix, ::AbstractMatrix, ::AbstractMatrix, ::AbstractMatrix)
FastLapackInterface.tgevc!(::GeneralizedEigenvectorsWs, ::AbstractChar, ::AbstractChar, ::AbstractVector{LinearAlgebra.BlasInt}, ::AbstractMatrix, ::AbstractMatrix, ::AbstractMatrix, ::AbstractMatrix)
FastLapackInterface.SCHURORDER
```

Expand Down
2 changes: 2 additions & 0 deletions docs/src/workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ QROrgWs
```@docs
SchurWs
GeneralizedSchurWs
GeneralizedOrdSchurWs
GeneralizedEigenvectorsWs
```

## [SVD](@id SVD-id)
Expand Down
2 changes: 1 addition & 1 deletion src/FastLapackInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export LUWs
include("qr.jl")
export QRWs, QRWYWs, QRPivotedWs, QROrmWs, QROrgWs
include("schur.jl")
export SchurWs, GeneralizedSchurWs
export SchurWs, GeneralizedSchurWs, GeneralizedOrdSchurWs, GeneralizedEigenvectorsWs
include("eigen.jl")
export EigenWs, HermitianEigenWs, GeneralizedEigenWs
include("bunch_kaufman.jl")
Expand Down
286 changes: 276 additions & 10 deletions src/schur.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LinearAlgebra.LAPACK: gees!, gges!
import LinearAlgebra.LAPACK: gees!, gges!, tgsen!
## Schur and generalized Schur decomposition with reordering

# SELECT functions
Expand All @@ -8,8 +8,8 @@ import LinearAlgebra.LAPACK: gees!, gges!
# Keywords
- `lp`: Left plane (real(eigenvalue) < criterium)
- `rp`: Right plane (real(eigenvalue) >= criterium)
- `id`: Interior of disk (abs(eigenvalue)^2 < criterium)
- `ed`: Exterior of disk (abs(eigenvalue)^2 >= criterium)
- `id`: Interior of disk (abs(eigenvalue)^2 < criterium)
- `ed`: Exterior of disk (abs(eigenvalue)^2 >= criterium)

# Note
- the left half-plane is obtained with criterium = 0
Expand Down Expand Up @@ -304,14 +304,14 @@ end
gees!(ws, jobvs, A; select=nothing, criterium=0.0, resize=true) -> (A, vs, ws.eigen_values)

Computes the eigenvalues (`jobvs = N`) or the eigenvalues and Schur
vectors (`jobvs = V`) of matrix `A`, using the preallocated [`SchurWs`](@ref) worspace `ws`. If `ws` is not of the appropriate size and `resize==true` it will be resized for `A`.
vectors (`jobvs = V`) of matrix `A`, using the preallocated [`SchurWs`](@ref) worspace `ws`. If `ws` is not of the appropriate size and `resize==true` it will be resized for `A`.
`A` is overwritten by its Schur form, and `ws.eigen_values` is overwritten with the eigenvalues.

It is possible to select the eigenvalues appearing in the top left corner of the Schur form:
- by setting the `select` option to one of
- by setting the `select` option to one of
- `lp`: Left plane (real(eigenvalue) < criterium)
- `rp`: Right plane (real(eigenvalue) >= criterium)
- `id`: Interior of disk (abs(eigenvalue)^2 < criterium)
- `id`: Interior of disk (abs(eigenvalue)^2 < criterium)
- `ed`: Exterior of disk (abs(eigenvalue)^2 >= criterium)
and setting `criterium`.
- by setting `select` equal to a function used to sort the eigenvalues during the decomponsition. In this case, the `criterium` keyword isn't used. The function should have the signature `f(wr::T, wi::T) -> Bool`, where `wr` and `wi` are the real and imaginary parts of the eigenvalue, and `T == eltype(A)`. This is not available on aarch64 architecture.
Expand Down Expand Up @@ -353,7 +353,7 @@ GeneralizedSchurWs{Float64}
sdim: Base.RefValue{Int64}
bwork: 2-element Vector{Int64}
eigen_values: 2-element Vector{ComplexF64}

julia> t = GeneralizedSchur(LAPACK.gges!(ws, 'V','V', A, B)...)
GeneralizedSchur{Float64, Matrix{Float64}, Vector{ComplexF64}, Vector{Float64}}
S factor:
Expand Down Expand Up @@ -529,20 +529,286 @@ vectors (`jobsvl = V`), or right Schur vectors (`jobvsr = V`) of `A` and
If `ws` is not of the right size, and `resize==true` it will be resized appropriately.

It is possible to select the eigenvalues appearing in the top left corner of the Schur form:
- by setting the `select` option to one of
- by setting the `select` option to one of
- `lp`: Left plane (real(eigenvalue) < criterium)
- `rp`: Right plane (real(eigenvalue) >= criterium)
- `id`: Interior of disk (abs(eigenvalue)^2 < criterium)
- `id`: Interior of disk (abs(eigenvalue)^2 < criterium)
- `ed`: Exterior of disk (abs(eigenvalue)^2 >= criterium)
and setting `criterium`.
- by setting `select` equal to a function used to sort the eigenvalues during the decomposition. In this case, the `criterium` keyword isn't used. The function should have the signature `f(αr::T, αi::T, β::T) -> Bool` where `T == eltype(A)`. An eigenvalue `(αr[j]+αi[j])/β[j]` is selected if `f(αr[j],αi[j],β[j])` is true, i.e. if either one of a complex conjugate pair of eigenvalues is selected, then both complex eigenvalues are selected. This is not available on aarch64 architecture.

The generalized eigenvalues components are returned in `ws.α` and `ws.β` where `ws.α` is a complex vector and `ẁs.β`, a real vector.
The generalized eigenvalues (`ws.α./ws.β`) are returned in `ws.eigen_values`, a complex vector.
The generalized eigenvalues (`ws.α./ws.β`) are returned in `ws.eigen_values`, a complex vector.
The left Schur vectors are returned in `ws.vsl` and the right Schur vectors are returned in `ws.vsr`.

See also FastLapackInterface.SCHURORDER
"""
gges!(
ws::GeneralizedSchurWs, jobvsl::AbstractChar, jobvsr::AbstractChar, A::AbstractMatrix,
B::AbstractMatrix; kwargs...)

"""
GeneralizedOrdSchurWs

Workspace to reorder a generalized Schur decomposition, as
[`LinearAlgebra.ordschur!`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.ordschur!)
does for a `GeneralizedSchur`, using the [`LAPACK.tgsen!`](@ref) function.
Supports `Complex` matrices.

# Examples
```jldoctest
julia> A = [1.0+1.0im 2.0
0.5im 3.0]
2×2 Matrix{ComplexF64}:
1.0+1.0im 2.0+0.0im
0.0+0.5im 3.0+0.0im

julia> B = [2.0 1.0im
0.0 1.0]
2×2 Matrix{ComplexF64}:
2.0+0.0im 0.0+1.0im
0.0+0.0im 1.0+0.0im

julia> ws = GeneralizedOrdSchurWs(A)
GeneralizedOrdSchurWs{ComplexF64}
work: 1-element Vector{ComplexF64}
iwork: 1-element Vector{Int64}
α: 2-element Vector{ComplexF64}
β: 2-element Vector{ComplexF64}


julia> F = schur(A, B);

julia> LAPACK.tgsen!(ws, LinearAlgebra.BlasInt[0, 1], F.S, F.T, F.Q, F.Z);

julia> ws.α ./ ws.β ≈ reverse(F.α ./ F.β)
true
```
"""
struct GeneralizedOrdSchurWs{T <: Complex} <: Workspace
work::Vector{T}
iwork::Vector{BlasInt}
α::Vector{T}
β::Vector{T}
end

Base.length(ws::GeneralizedOrdSchurWs) = length(ws.α)

for (tgsen, elty) in ((:ztgsen_, :ComplexF64),
(:ctgsen_, :ComplexF32))
@eval begin
function Base.resize!(ws::GeneralizedOrdSchurWs, A::AbstractMatrix{$elty}; work = true)
chkstride1(A)
n = checksquare(A)
resize!(ws.α, n)
resize!(ws.β, n)
if work
lda = max(1, stride(A, 2))
select = zeros(BlasInt, n) # read by the workspace query
info = Ref{BlasInt}()
ccall((@blasfunc($tgsen), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ptr{BlasInt},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ref{BlasInt},
Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{$elty},
Ref{BlasInt}, Ptr{BlasInt}, Ref{BlasInt}, Ptr{BlasInt}),
0, 1, 1, select,
n, A, lda, A,
lda, ws.α, ws.β, A,
lda, A, lda, Ref{BlasInt}(),
C_NULL, C_NULL, C_NULL, ws.work,
-1, ws.iwork, -1, info)
chklapackerror(info[])
resize!(ws.work, BlasInt(real(ws.work[1])))
resize!(ws.iwork, ws.iwork[1])
end
return ws
end

GeneralizedOrdSchurWs(A::AbstractMatrix{$elty}) = resize!(
GeneralizedOrdSchurWs(Vector{$elty}(undef, 1), Vector{BlasInt}(undef, 1),
$elty[], $elty[]),
A)

function tgsen!(ws::GeneralizedOrdSchurWs, select::AbstractVector{BlasInt},
S::AbstractMatrix{$elty}, T::AbstractMatrix{$elty},
Q::AbstractMatrix{$elty}, Z::AbstractMatrix{$elty}; resize = true)
require_one_based_indexing(select, S, T, Q, Z)
chkstride1(select, S, T, Q, Z)
n = checksquare(S)
if checksquare(T) != n || checksquare(Q) != n || checksquare(Z) != n
throw(DimensionMismatch("S, T, Q and Z must all be of size ($n,$n)"))
end
if length(select) != n
throw(DimensionMismatch("length of select, $(length(select)), must be $n"))
end
nws = length(ws)
if n != nws
if resize
resize!(ws, S; work = n > nws)
else
throw(WorkspaceSizeError(nws, n))
end
end
info = Ref{BlasInt}()
ccall((@blasfunc($tgsen), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ptr{BlasInt},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ref{BlasInt},
Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{$elty},
Ref{BlasInt}, Ptr{BlasInt}, Ref{BlasInt}, Ptr{BlasInt}),
0, 1, 1, select,
n, S, max(1, stride(S, 2)), T,
max(1, stride(T, 2)), ws.α, ws.β, Q,
max(1, stride(Q, 2)), Z, max(1, stride(Z, 2)), Ref{BlasInt}(),
C_NULL, C_NULL, C_NULL, ws.work,
length(ws.work), ws.iwork, length(ws.iwork), info)
chklapackerror(info[])
return S, T, ws.α, ws.β, Q, Z
end
end
end

"""
tgsen!(ws, select, S, T, Q, Z; resize=true) -> (S, T, ws.α, ws.β, Q, Z)

Reorders the generalized Schur decomposition `(S, T, Q, Z)` of a matrix pair, so that the
eigenvalues selected by `select` (a vector of `0`s and `1`s) move to the top left corner,
using the preallocated [`GeneralizedOrdSchurWs`](@ref) workspace `ws`.
If `ws` is not of the right size, and `resize==true` it will be resized appropriately.
`S`, `T`, `Q` and `Z` are overwritten, and the reordered generalized eigenvalues are `ws.α ./ ws.β`.
This is the same as `LinearAlgebra.LAPACK.tgsen!(select, S, T, Q, Z)`, but without allocations.
"""
tgsen!(ws::GeneralizedOrdSchurWs, select::AbstractVector{BlasInt}, S::AbstractMatrix,
T::AbstractMatrix, Q::AbstractMatrix, Z::AbstractMatrix; kwargs...)

"""
GeneralizedEigenvectorsWs

Workspace to compute eigenvectors of a matrix pair in generalized Schur form `(S, P)`,
using the [`tgevc!`](@ref) function. Supports `Complex` matrices.

# Examples
```jldoctest
julia> A = [1.0+1.0im 2.0
0.5im 3.0]
2×2 Matrix{ComplexF64}:
1.0+1.0im 2.0+0.0im
0.0+0.5im 3.0+0.0im

julia> B = [2.0 1.0im
0.0 1.0]
2×2 Matrix{ComplexF64}:
2.0+0.0im 0.0+1.0im
0.0+0.0im 1.0+0.0im

julia> ws = GeneralizedEigenvectorsWs(A)
GeneralizedEigenvectorsWs{ComplexF64, Float64}
work: 4-element Vector{ComplexF64}
rwork: 4-element Vector{Float64}

julia> F = schur(A, B);

julia> side, howmny = 'R', 'B'; # Right eigenvectors, with Back-transform with F.Z

julia> VR = copy(F.Z); # back-transformed right eigenvectors will be stored here

julia> VL = similar(A, 0, 0); # placeholder, unused with side = 'R'

julia> FastLapackInterface.tgevc!(ws, side, howmny, LinearAlgebra.BlasInt[], F.S, F.T, VL, VR);

julia> A * VR ≈ B * VR * Diagonal(F.α ./ F.β)
true
```
"""
struct GeneralizedEigenvectorsWs{T <: Complex, RT <: AbstractFloat} <: Workspace
work::Vector{T}
rwork::Vector{RT}
end

Base.length(ws::GeneralizedEigenvectorsWs) = length(ws.work) ÷ 2

for (tgevc, elty, relty) in ((:ztgevc_, :ComplexF64, :Float64),
(:ctgevc_, :ComplexF32, :Float32))
@eval begin
function Base.resize!(ws::GeneralizedEigenvectorsWs, A::AbstractMatrix{$elty})
chkstride1(A)
n = checksquare(A)
resize!(ws.work, 2n)
resize!(ws.rwork, 2n)
return ws
end

GeneralizedEigenvectorsWs(A::AbstractMatrix{$elty}) =
resize!(GeneralizedEigenvectorsWs($elty[], $relty[]), A)

function tgevc!(ws::GeneralizedEigenvectorsWs, side::AbstractChar,
howmny::AbstractChar, select::AbstractVector{BlasInt},
S::AbstractMatrix{$elty}, P::AbstractMatrix{$elty},
VL::AbstractMatrix{$elty}, VR::AbstractMatrix{$elty}; resize = true)
require_one_based_indexing(select, S, P, VL, VR)
chkstride1(select, S, P, VL, VR)
if side ∉ ('L', 'R', 'B')
throw(ArgumentError("side must be 'L', 'R' or 'B', but $side was passed"))
end
if howmny ∉ ('A', 'B', 'S')
throw(ArgumentError("howmny must be 'A', 'B' or 'S', but $howmny was passed"))
end
n = checksquare(S)
if checksquare(P) != n
throw(DimensionMismatch("dimensions of S, ($n,$n), and P, $(size(P)), must match"))
end
if howmny == 'S' && length(select) != n
throw(DimensionMismatch("length of select, $(length(select)), must be $n"))
end
nws = length(ws)
if n != nws
if resize
resize!(ws, S)
else
throw(WorkspaceSizeError(nws, n))
end
end
mm = side == 'L' ? size(VL, 2) : side == 'R' ? size(VR, 2) :
min(size(VL, 2), size(VR, 2))
m = Ref{BlasInt}()
info = Ref{BlasInt}()
ccall((@blasfunc($tgevc), liblapack), Cvoid,
(Ref{UInt8}, Ref{UInt8}, Ptr{BlasInt}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ref{BlasInt}, Ptr{BlasInt}, Ptr{$elty}, Ptr{$relty},
Ptr{BlasInt}, Clong, Clong),
side, howmny, select, n,
S, max(1, stride(S, 2)), P, max(1, stride(P, 2)),
VL, max(1, stride(VL, 2)), VR, max(1, stride(VR, 2)),
mm, m, ws.work, ws.rwork,
info, 1, 1)
chklapackerror(info[])
return VL, VR, m[]
end
end
end

"""
tgevc!(ws, side, howmny, select, S, P, VL, VR; resize=true) -> (VL, VR, m)

Computes some or all of the right (`side = 'R'`), left (`side = 'L'`) or both (`side = 'B'`)
eigenvectors of a matrix pair `(S, P)` in generalized Schur form (upper triangular),
using the preallocated [`GeneralizedEigenvectorsWs`](@ref) workspace `ws`.
If `ws` is not of the right size, and `resize==true` it will be resized appropriately.

- `howmny = 'A'`: compute all eigenvectors.
- `howmny = 'B'`: compute all eigenvectors, back-transformed by the matrices passed in `VL`
and `VR` (e.g. `Q` and `Z` from a generalized Schur decomposition).
- `howmny = 'S'`: compute only the eigenvectors selected by `select` (a vector of `0`s and `1`s).
`select` is ignored otherwise.

`VL` and `VR` are overwritten with the eigenvectors in their first `m` columns, and returned without
resizing, so they may be views. Wraps LAPACK's `ztgevc`/`ctgevc` (`LinearAlgebra.LAPACK` doesn't.)
"""
tgevc!(ws::GeneralizedEigenvectorsWs, side::AbstractChar, howmny::AbstractChar,
select::AbstractVector{BlasInt}, S::AbstractMatrix, P::AbstractMatrix,
VL::AbstractMatrix, VR::AbstractMatrix; kwargs...)
4 changes: 4 additions & 0 deletions src/workspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ end

Workspace(::typeof(LAPACK.gees!), A::AbstractMatrix) = SchurWs(A)
Workspace(::typeof(LAPACK.gges!), A::AbstractMatrix) = GeneralizedSchurWs(A)
Workspace(::typeof(LAPACK.tgsen!), A::AbstractMatrix) = GeneralizedOrdSchurWs(A)
Workspace(::typeof(tgevc!), A::AbstractMatrix) = GeneralizedEigenvectorsWs(A)

Workspace(::typeof(LAPACK.geevx!), A::AbstractMatrix; kwargs...) = EigenWs(A; kwargs...)
function Workspace(::typeof(LAPACK.syevr!), A::AbstractMatrix; kwargs...)
Expand Down Expand Up @@ -94,6 +96,8 @@ decompose!(ws::SchurWs, args...; kwargs...) = LAPACK.gees!(ws, args...; kwargs..
function decompose!(ws::GeneralizedSchurWs, args...; kwargs...)
LAPACK.gges!(ws, args...; kwargs...)
end
decompose!(ws::GeneralizedOrdSchurWs, args...; kwargs...) = LAPACK.tgsen!(ws, args...; kwargs...)
decompose!(ws::GeneralizedEigenvectorsWs, args...; kwargs...) = tgevc!(ws, args...; kwargs...)

decompose!(ws::EigenWs, args...; kwargs...) = LAPACK.geevx!(ws, args...; kwargs...)
decompose!(ws::HermitianEigenWs, args...; kwargs...) = LAPACK.syevr!(ws, args...; kwargs...)
Expand Down
Loading