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

Implement vector indexing for SDiagonal with statically sized indices #926

Open
wants to merge 3 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
6 changes: 6 additions & 0 deletions src/SDiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ SDiagonal(a::StaticMatrix{N,N,T}) where {N,T} = Diagonal(diag(a))
size(::Type{<:SDiagonal{N}}) where {N} = (N,N)
size(::Type{<:SDiagonal{N}}, d::Int) where {N} = d > 2 ? 1 : N

Base.axes(D::SDiagonal, d) = d <= 2 ? axes(D)[d] : SOneTo(1)
mateuszbaran marked this conversation as resolved.
Show resolved Hide resolved

Base.reshape(a::SDiagonal, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as the generic fallback:

@inline reshape(a::AbstractArray, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
.
Do we really need this method?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than this small point, the PR looks nice 👍 .


# define specific methods to avoid allocating mutable arrays
\(D::SDiagonal, b::AbstractVector) = D.diag .\ b
\(D::SDiagonal, b::StaticVector) = D.diag .\ b # catch ambiguity
Expand Down Expand Up @@ -56,3 +60,5 @@ function inv(D::SDiagonal)
check_singular(D)
SDiagonal(inv.(D.diag))
end

Base.copy(D::SDiagonal) = Diagonal(copy(diag(D)))
18 changes: 18 additions & 0 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,21 @@ Base.unsafe_view(A::AbstractArray, i1::StaticIndexing, indices::StaticIndexing..
# the tuple indices has to have at least one element to prevent infinite
# recursion when viewing a zero-dimensional array (see issue #705)
Base.SubArray(A::AbstractArray, indices::Tuple{StaticIndexing, Vararg{StaticIndexing}}) = Base.SubArray(A, map(unwrap, indices))

###########################################################
# SDiagonal
###########################################################

# SDiagonal uses Cartesian indexing, and the canonical indexing methods shadow getindex for Diagonal
# these are needed for ambiguity resolution
@inline function getindex(D::SDiagonal, i::Int, j::Int)
jishnub marked this conversation as resolved.
Show resolved Hide resolved
invoke(getindex, Tuple{Diagonal, Int, Int}, D, i, j)
end
@inline function getindex(D::SDiagonal, i::Int...)
invoke(getindex, Tuple{Diagonal, Vararg{Int}}, D, i...)
end
# Ensure that vector indexing with static types lead to SArrays
@propagate_inbounds function getindex(a::SDiagonal, inds::Union{Int, StaticArray{<:Tuple, Int}, SOneTo, Colon}...)
ar = reshape(a, Val(length(inds)))
_getindex(ar, index_sizes(Size(ar), inds...), inds)
end
11 changes: 11 additions & 0 deletions test/SDiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ using StaticArrays, Test, LinearAlgebra

@test length(m) === 4*4

m2 = SMatrix{4,4}(m)
@test (@inferred axes(m)) === axes(m2)
@test axes(m, 1) === axes(m2, 1)
@test axes(m, 3) == SOneTo(1)

@test m[:, 1] === SVector{4}(m[1,1], 0, 0, 0)
@test m[:, :] === m2
@test m[2, 2, 1] === m[2, 2]

@test_throws Exception m[1] = 1

b = @SVector [2,-1,2,1]
Expand Down Expand Up @@ -114,5 +123,7 @@ using StaticArrays, Test, LinearAlgebra

@test m + zero(m) == m
@test m + zero(typeof(m)) == m

@test copy(m) === m
end
end