Skip to content

Commit

Permalink
Missing method for IdentityMultiple (#187)
Browse files Browse the repository at this point in the history
* add addition operation for Matrix and IdentityMultiple and Hermitian method

* add tests

* Update src/identity.jl

Co-Authored-By: Christian Schilling <[email protected]>

* Update src/identity.jl

Co-Authored-By: Christian Schilling <[email protected]>

Co-authored-by: Marcelo Forets <[email protected]>
Co-authored-by: Christian Schilling <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2020
1 parent 71b1424 commit 48e619e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 48e619e

Please sign in to comment.