Skip to content

Commit 3dfc99f

Browse files
authored
Add square checks for project_(anti)hermitian and eigenvalue decompositions (#408)
1 parent 5fcd236 commit 3dfc99f

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎src/factorizations/factorizations.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ function MAK.isantihermitian(t::AbstractTensorMap; kwargs...)
7676
end
7777
LinearAlgebra.ishermitian(t::AbstractTensorMap) = MAK.ishermitian(t)
7878

79+
function LinearAlgebra.checksquare(t::AbstractTensorMap)
80+
dom = domain(t)
81+
cod = codomain(t)
82+
dom == cod || throw(SpaceMismatch(lazy"tensor is not square: codomain $cod ≠ domain $cod"))
83+
return dom
84+
end
85+
7986
function LinearAlgebra.isposdef(t::AbstractTensorMap)
8087
return isposdef!(copy_oftype(t, factorisation_scalartype(isposdef, t)))
8188
end

‎src/factorizations/matrixalgebrakit.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ for f! in (
3131
:left_polar!, :right_polar!,
3232
)
3333
@eval function MAK.$f!(t::AbstractTensorMap, F, alg::AbstractAlgorithm)
34+
$(f! in (:eig_full!, :eigh_full!) && :(LinearAlgebra.checksquare(t)))
3435
foreachblock(t, F...) do _, (tblock, Fblocks...)
3536
Fblocks′ = $f!(tblock, Fblocks, alg)
3637
# deal with the case where the output is not in-place
@@ -50,6 +51,7 @@ for f! in (
5051
:project_hermitian!, :project_antihermitian!, :project_isometric!,
5152
)
5253
@eval function MAK.$f!(t::AbstractTensorMap, N, alg::AbstractAlgorithm)
54+
$(f! in (:eig_vals!, :eigh_vals!, :project_hermitian!, :project_antihermitian!) && :(LinearAlgebra.checksquare(t)))
5355
foreachblock(t, N) do _, (tblock, Nblock)
5456
Nblock′ = $f!(tblock, Nblock, alg)
5557
# deal with the case where the output is not the same as the input

‎test/factorizations/projections.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ for V in spacelist
4848
@test !isantihermitian(ta_approx)
4949
@test isantihermitian(ta_approx; atol = 10 * noisefactor) || (T <: Real && t isa DiagonalTensorMap)
5050
end
51+
52+
@test_throws SpaceMismatch project_hermitian(rand(V1, V1^2))
53+
@test_throws SpaceMismatch project_antihermitian(rand(V1, V1^2))
54+
if V1 != V1'
55+
@test_throws SpaceMismatch project_hermitian(rand(V1, V1'))
56+
@test_throws SpaceMismatch project_antihermitian(rand(V1, V1'))
57+
end
5158
end
5259

5360
@testset "Isometric projections" begin

0 commit comments

Comments
 (0)