Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#189 - IdentityMultiple: restrict getindex to Integer #318

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ end
Base.IndexStyle(::Type{<:IdentityMultiple}) = IndexLinear()
Base.size(𝐼::IdentityMultiple) = (𝐼.n, 𝐼.n)

function Base.getindex(𝐼::IdentityMultiple, inds...)
function Base.getindex(𝐼::IdentityMultiple, inds::Integer...)
any(idx -> idx > 𝐼.n, inds) && throw(BoundsError(𝐼, inds))
return getindex(𝐼.M, inds...)
end

function Base.getindex(𝐼::IdentityMultiple{T}, ind) where {T}
function Base.getindex(𝐼::IdentityMultiple{T}, ind::Integer) where {T}
if 1 ≤ ind ≤ 𝐼.n^2
return rem(ind - 1, 𝐼.n + 1) == 0 ? 𝐼.M.λ : zero(T)
else
Expand All @@ -94,8 +94,6 @@ function Base.setindex!(::IdentityMultiple, ::Any, 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 @@ -193,8 +191,4 @@ end
# callable identity matrix given the scaling factor and the size
IdentityMultiple(λ::Number, n::Int) = IdentityMultiple(λ * I, n)

function LinearAlgebra.Hermitian(𝐼::IdentityMultiple)
return Hermitian(Diagonal(fill(𝐼.M.λ, 𝐼.n)))
end

Base.exp(𝐼::IdentityMultiple) = IdentityMultiple(exp(𝐼.M.λ), 𝐼.n)
1 change: 1 addition & 0 deletions test/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ end
end

@testset "Specific methods for IdentityMultiple" begin
@test Diagonal(Id(2)) == Diagonal([1.0, 1])
@test Hermitian(Id(2)) == Hermitian([1.0 0; 0 1])
@test exp(Id(3, 1)) == Id(3, exp(1))
end
Loading