diff --git a/src/identity.jl b/src/identity.jl index 72e4b0b2..b23ab7e0 100644 --- a/src/identity.jl +++ b/src/identity.jl @@ -98,6 +98,8 @@ function Base.setindex!(饾惣::IdentityMultiple, X, inds...) end Base.:(-)(饾惣::IdentityMultiple) = IdentityMultiple(-饾惣.M, 饾惣.n) +Base.:(+)(饾惣::IdentityMultiple, M::AbstractMatrix) = 饾惣.M + M +Base.:(+)(M::AbstractMatrix, 饾惣::IdentityMultiple) = M + 饾惣.M Base.:(*)(x::Number, 饾惣::IdentityMultiple) = IdentityMultiple(x * 饾惣.M, 饾惣.n) Base.:(*)(饾惣::IdentityMultiple, x::Number) = IdentityMultiple(x * 饾惣.M, 饾惣.n) Base.:(/)(饾惣::IdentityMultiple, x::Number) = IdentityMultiple(饾惣.M / x, 饾惣.n) @@ -166,3 +168,7 @@ LinearAlgebra.I(n::Int, N::DataType=Float64) = IdentityMultiple(one(N)*I, n) # callable identity matrix given the scaling factor and the size IdentityMultiple(位::Number, n::Int) = IdentityMultiple(位*I, n) LinearAlgebra.I(位::Number, n::Int) = IdentityMultiple(位*I, n) + +function LinearAlgebra.Hermitian(饾惣::IdentityMultiple) + return Hermitian(Diagonal(fill(饾惣.M.位, 饾惣.n))) +end diff --git a/test/identity.jl b/test/identity.jl index 221fdb7b..b716f89d 100644 --- a/test/identity.jl +++ b/test/identity.jl @@ -51,4 +51,11 @@ end @test I(2) / I == IdentityMultiple(1.0, 2) @test I(2) - I(2) == 0.0I(2) + + @test I(2) + [3 3; 1 2] == [4 3; 1 3.] + @test [3 3; 1 2] + I(2) == [4 3; 1 3.] +end + +@testset "Specific methods for IdentityMultiple" begin + @test Hermitian(I(2)) == Hermitian([1. 0; 0 1]) end