diff --git a/docs/src/LAPACK.md b/docs/src/LAPACK.md index 9675262..e74fea2 100644 --- a/docs/src/LAPACK.md +++ b/docs/src/LAPACK.md @@ -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 ``` diff --git a/docs/src/workspaces.md b/docs/src/workspaces.md index e703f46..eff486d 100644 --- a/docs/src/workspaces.md +++ b/docs/src/workspaces.md @@ -53,6 +53,8 @@ QROrgWs ```@docs SchurWs GeneralizedSchurWs +GeneralizedOrdSchurWs +GeneralizedEigenvectorsWs ``` ## [SVD](@id SVD-id) diff --git a/src/FastLapackInterface.jl b/src/FastLapackInterface.jl index 1666aa3..b340ee7 100644 --- a/src/FastLapackInterface.jl +++ b/src/FastLapackInterface.jl @@ -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") diff --git a/src/schur.jl b/src/schur.jl index bfae7a0..a8f1407 100644 --- a/src/schur.jl +++ b/src/schur.jl @@ -1,4 +1,4 @@ -import LinearAlgebra.LAPACK: gees!, gges! +import LinearAlgebra.LAPACK: gees!, gges!, tgsen! ## Schur and generalized Schur decomposition with reordering # SELECT functions @@ -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 @@ -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. @@ -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: @@ -529,16 +529,16 @@ 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 @@ -546,3 +546,269 @@ 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...) diff --git a/src/workspace.jl b/src/workspace.jl index 7865739..c9b5671 100644 --- a/src/workspace.jl +++ b/src/workspace.jl @@ -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...) @@ -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...) diff --git a/test/schur_test.jl b/test/schur_test.jl index 3c46bb8..4f74348 100644 --- a/test/schur_test.jl +++ b/test/schur_test.jl @@ -170,3 +170,94 @@ end @test count(abs.(ws.eigen_values) .>= CRITERIUM) == ws.sdim[] end end + +@testset "GeneralizedOrdSchurWs" begin + n = 10 + @testset "Complex, square" begin + for T in (ComplexF64, ComplexF32) + A0 = randn(T, n, n) + B0 = randn(T, n, n) + F = schur(A0, B0) + select = LinearAlgebra.BlasInt[isodd(i) for i in 1:n] + ws = GeneralizedOrdSchurWs(copy(A0)) + + S1, T1, α1, β1, Q1, Z1 = LAPACK.tgsen!(select, copy(F.S), copy(F.T), copy(F.Q), copy(F.Z)) + S2, T2, α2, β2, Q2, Z2 = LAPACK.tgsen!(ws, select, copy(F.S), copy(F.T), copy(F.Q), copy(F.Z)) + @test isapprox(S1, S2) + @test isapprox(T1, T2) + @test isapprox(α1, α2) + @test isapprox(β1, β2) + @test isapprox(Q1, Q2) + @test isapprox(Z1, Z2) + + # Using Workspace, factorize! + ws = Workspace(LAPACK.tgsen!, copy(A0)) + S2, T2, α2, β2, Q2, Z2 = factorize!(ws, select, copy(F.S), copy(F.T), copy(F.Q), copy(F.Z)) + @test isapprox(S1, S2) + @test isapprox(Z1, Z2) + show(devnull, "text/plain", ws) + + for div in (-1, 1) + F2 = schur(randn(T, n + div, n + div), randn(T, n + div, n + div)) + select2 = LinearAlgebra.BlasInt[isodd(i) for i in 1:(n + div)] + @test_throws FastLapackInterface.WorkspaceSizeError factorize!( + ws, select2, F2.S, F2.T, F2.Q, F2.Z; resize = false) + factorize!(ws, select2, F2.S, F2.T, F2.Q, F2.Z) + @test length(ws.α) == n + div + end + end + end +end + +@testset "GeneralizedEigenvectorsWs" begin + n = 10 + @testset "Complex, square" begin + for T in (ComplexF64, ComplexF32) + A0 = randn(T, n, n) + B0 = randn(T, n, n) + F = schur(A0, B0) + λs = F.α ./ F.β + ws = GeneralizedEigenvectorsWs(copy(A0)) + tol = sqrt(eps(real(T))) + + # back-transformed right and left eigenvectors of (A0, B0) + VL, VR, m = FastLapackInterface.tgevc!(ws, 'B', 'B', LinearAlgebra.BlasInt[], + F.S, F.T, copy(F.Q), copy(F.Z)) + @test m == n + for j in 1:n + v, u = VR[:, j], VL[:, j] + @test norm(A0 * v - λs[j] * B0 * v) < tol * norm(A0) * norm(v) + @test norm(u' * A0 - λs[j] * u' * B0) < tol * norm(A0) * norm(u) + end + + # selected right eigenvectors of (S, T), written into a view + select = LinearAlgebra.BlasInt[i <= n ÷ 2 for i in 1:n] + VRall = FastLapackInterface.tgevc!(ws, 'R', 'A', select, F.S, F.T, + similar(F.S, 0, n), similar(F.S))[2] + VRsel = zeros(T, n, n) + _, _, m = FastLapackInterface.tgevc!(ws, 'R', 'S', select, F.S, F.T, + similar(F.S, 0, n), view(VRsel, :, 1:(n ÷ 2))) + @test m == n ÷ 2 + @test isapprox(VRsel[:, 1:(n ÷ 2)], VRall[:, 1:(n ÷ 2)]) + @test iszero(VRsel[:, (n ÷ 2 + 1):n]) + + # Using Workspace, factorize! + ws = Workspace(FastLapackInterface.tgevc!, copy(A0)) + _, VR2, _ = factorize!(ws, 'R', 'A', select, F.S, F.T, similar(F.S, 0, n), similar(F.S)) + @test isapprox(VRall, VR2) + show(devnull, "text/plain", ws) + + @test_throws ArgumentError FastLapackInterface.tgevc!(ws, 'X', 'A', select, + F.S, F.T, similar(F.S, 0, n), similar(F.S)) + for div in (-1, 1) + F2 = schur(randn(T, n + div, n + div), randn(T, n + div, n + div)) + args2 = ('R', 'A', LinearAlgebra.BlasInt[], F2.S, F2.T, + similar(F2.S, 0, n + div), similar(F2.S)) + @test_throws FastLapackInterface.WorkspaceSizeError factorize!(ws, args2...; + resize = false) + factorize!(ws, args2...) + @test length(ws) == n + div + end + end + end +end