From 48e619ee8b53c0afe8392d9c7ba9b920b39d5ccd Mon Sep 17 00:00:00 2001 From: ueliwechsler Date: Thu, 27 Feb 2020 01:18:24 +0100 Subject: [PATCH] Missing method for IdentityMultiple (#187) * add addition operation for Matrix and IdentityMultiple and Hermitian method * add tests * Update src/identity.jl Co-Authored-By: Christian Schilling * Update src/identity.jl Co-Authored-By: Christian Schilling Co-authored-by: Marcelo Forets Co-authored-by: Christian Schilling --- src/identity.jl | 6 ++++++ test/identity.jl | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/identity.jl b/src/identity.jl index f96c3032..d0df89e0 100644 --- a/src/identity.jl +++ b/src/identity.jl @@ -102,6 +102,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) @@ -170,3 +172,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 35d32831..0e96b526 100644 --- a/test/identity.jl +++ b/test/identity.jl @@ -54,4 +54,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