Skip to content

Commit 0e811e4

Browse files
authored
Allow indexing UniformScaling with CartesianIndex{2} (JuliaLang#56461)
Since indexing with two `Integer`s is defined, we might as well define indexing with a `CartesianIndex`. This makes certain loops convenient where the index is obtained using `eachindex`.
1 parent 683da41 commit 0e811e4

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

stdlib/LinearAlgebra/src/uniformscaling.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ julia> (0.7*I)(3)
8686
eltype(::Type{UniformScaling{T}}) where {T} = T
8787
ndims(J::UniformScaling) = 2
8888
Base.has_offset_axes(::UniformScaling) = false
89+
getindex(J::UniformScaling, ind::CartesianIndex{2}) = J[Tuple(ind)...]
8990
getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ))
9091

9192
getindex(J::UniformScaling, n::Integer, m::AbstractVector{<:Integer}) = getindex(J, m, n)

stdlib/LinearAlgebra/test/uniformscaling.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Random.seed!(1234543)
2828
end
2929

3030
@testset "getindex" begin
31-
@test I[1,1] == 1
32-
@test I[1,2] == 0
31+
@test I[1,1] == I[CartesianIndex(1,1)] == 1
32+
@test I[1,2] == I[CartesianIndex(1,2)] == 0
3333

3434
J = I(15)
3535
for (a, b) in [

0 commit comments

Comments
 (0)