|
| 1 | +# module GetIndex |
| 2 | + |
| 3 | +# using ..LinearMaps: LinearMap, AdjointMap, TransposeMap, FillMap, LinearCombination, |
| 4 | +# ScaledMap, UniformScalingMap, WrappedMap |
| 5 | + |
| 6 | +# required in Base.to_indices for [:]-indexing |
| 7 | +Base.eachindex(::IndexLinear, A::LinearMap) = (Base.@_inline_meta; Base.OneTo(length(A))) |
| 8 | +Base.lastindex(A::LinearMap) = (Base.@_inline_meta; last(eachindex(IndexLinear(), A))) |
| 9 | +Base.firstindex(A::LinearMap) = (Base.@_inline_meta; first(eachindex(IndexLinear(), A))) |
| 10 | + |
| 11 | +function Base.checkbounds(A::LinearMap, i, j) |
| 12 | + Base.@_inline_meta |
| 13 | + Base.checkbounds_indices(Bool, axes(A), (i, j)) || throw(BoundsError(A, (i, j))) |
| 14 | + nothing |
| 15 | +end |
| 16 | +# Linear indexing is explicitly allowed when there is only one (non-cartesian) index |
| 17 | +function Base.checkbounds(A::LinearMap, i) |
| 18 | + Base.@_inline_meta |
| 19 | + Base.checkindex(Bool, Base.OneTo(length(A)), i) || throw(BoundsError(A, i)) |
| 20 | + nothing |
| 21 | +end |
| 22 | + |
| 23 | +# main entry point |
| 24 | +Base.@propagate_inbounds function Base.getindex(A::LinearMap, I...) |
| 25 | + # TODO: introduce some sort of switch? |
| 26 | + Base.@_inline_meta |
| 27 | + @boundscheck checkbounds(A, I...) |
| 28 | + @inbounds _getindex(A, Base.to_indices(A, I)...) |
| 29 | +end |
| 30 | +# quick pass forward |
| 31 | +Base.@propagate_inbounds Base.getindex(A::ScaledMap, I...) = A.λ .* getindex(A.lmap, I...) |
| 32 | +Base.@propagate_inbounds Base.getindex(A::WrappedMap, I...) = A.lmap[I...] |
| 33 | +Base.@propagate_inbounds Base.getindex(A::WrappedMap, i::Integer) = A.lmap[i] |
| 34 | +Base.@propagate_inbounds Base.getindex(A::WrappedMap, i::Integer, j::Integer) = A.lmap[i,j] |
| 35 | + |
| 36 | +######################## |
| 37 | +# linear indexing |
| 38 | +######################## |
| 39 | +Base.@propagate_inbounds function _getindex(A::LinearMap, i::Integer) |
| 40 | + Base.@_inline_meta |
| 41 | + i1, i2 = Base._ind2sub(axes(A), i) |
| 42 | + return _getindex(A, i1, i2) |
| 43 | +end |
| 44 | +Base.@propagate_inbounds _getindex(A::LinearMap, I::AbstractVector{<:Integer}) = |
| 45 | + [_getindex(A, i) for i in I] |
| 46 | +_getindex(A::LinearMap, ::Base.Slice) = vec(Matrix(A)) |
| 47 | + |
| 48 | +######################## |
| 49 | +# Cartesian indexing |
| 50 | +######################## |
| 51 | +Base.@propagate_inbounds _getindex(A::LinearMap, i::Integer, j::Integer) = |
| 52 | + _getindex(A, Base.Slice(axes(A)[1]), j)[i] |
| 53 | +Base.@propagate_inbounds function _getindex(A::LinearMap, i::Integer, J::AbstractVector{<:Integer}) |
| 54 | + try |
| 55 | + return (basevec(A, i)'A)[J] |
| 56 | + catch |
| 57 | + x = zeros(eltype(A), size(A, 2)) |
| 58 | + y = similar(x, eltype(A), size(A, 1)) |
| 59 | + r = similar(x, eltype(A), length(J)) |
| 60 | + for (ind, j) in enumerate(J) |
| 61 | + x[j] = one(eltype(A)) |
| 62 | + _unsafe_mul!(y, A, x) |
| 63 | + r[ind] = y[i] |
| 64 | + x[j] = zero(eltype(A)) |
| 65 | + end |
| 66 | + return r |
| 67 | + end |
| 68 | +end |
| 69 | +function _getindex(A::LinearMap, i::Integer, J::Base.Slice) |
| 70 | + try |
| 71 | + return vec(basevec(A, i)'A) |
| 72 | + catch |
| 73 | + return vec(_getindex(A, i:i, J)) |
| 74 | + end |
| 75 | +end |
| 76 | +Base.@propagate_inbounds _getindex(A::LinearMap, I::AbstractVector{<:Integer}, j::Integer) = |
| 77 | + _getindex(A, Base.Slice(axes(A)[1]), j)[I] # = A[:,j][I] w/o bounds check |
| 78 | +_getindex(A::LinearMap, ::Base.Slice, j::Integer) = A*basevec(A, j) |
| 79 | +Base.@propagate_inbounds function _getindex(A::LinearMap, Is::Vararg{AbstractVector{<:Integer},2}) |
| 80 | + shape = Base.index_shape(Is...) |
| 81 | + dest = zeros(eltype(A), shape) |
| 82 | + I, J = Is |
| 83 | + for (ind, ij) in zip(eachindex(dest), Iterators.product(I, J)) |
| 84 | + i, j = ij |
| 85 | + dest[ind] = _getindex(A, i, j) |
| 86 | + end |
| 87 | + return dest |
| 88 | +end |
| 89 | +Base.@propagate_inbounds function _getindex(A::LinearMap, I::AbstractVector{<:Integer}, ::Base.Slice) |
| 90 | + x = zeros(eltype(A), size(A, 2)) |
| 91 | + y = similar(x, eltype(A), size(A, 1)) |
| 92 | + r = similar(x, eltype(A), (length(I), size(A, 2))) |
| 93 | + @views for j in axes(A)[2] |
| 94 | + x[j] = one(eltype(A)) |
| 95 | + _unsafe_mul!(y, A, x) |
| 96 | + r[:,j] .= y[I] |
| 97 | + x[j] = zero(eltype(A)) |
| 98 | + end |
| 99 | + return r |
| 100 | +end |
| 101 | +Base.@propagate_inbounds function _getindex(A::LinearMap, ::Base.Slice, J::AbstractVector{<:Integer}) |
| 102 | + x = zeros(eltype(A), size(A, 2)) |
| 103 | + y = similar(x, eltype(A), (size(A, 1), length(J))) |
| 104 | + for (i, j) in enumerate(J) |
| 105 | + x[j] = one(eltype(A)) |
| 106 | + _unsafe_mul!(selectdim(y, 2, i), A, x) |
| 107 | + x[j] = zero(eltype(A)) |
| 108 | + end |
| 109 | + return y |
| 110 | +end |
| 111 | +_getindex(A::LinearMap, ::Base.Slice, ::Base.Slice) = Matrix(A) |
| 112 | + |
| 113 | +# specialized methods |
| 114 | +_getindex(A::FillMap, ::Integer, ::Integer) = A.λ |
| 115 | +Base.@propagate_inbounds _getindex(A::LinearCombination, i::Integer, j::Integer) = |
| 116 | + sum(a -> A.maps[a][i, j], eachindex(A.maps)) |
| 117 | +Base.@propagate_inbounds _getindex(A::AdjointMap, i::Integer, j::Integer) = |
| 118 | + adjoint(A.lmap[j, i]) |
| 119 | +Base.@propagate_inbounds _getindex(A::TransposeMap, i::Integer, j::Integer) = |
| 120 | + transpose(A.lmap[j, i]) |
| 121 | +_getindex(A::UniformScalingMap, i::Integer, j::Integer) = ifelse(i == j, A.λ, zero(eltype(A))) |
| 122 | + |
| 123 | +# helpers |
| 124 | +function basevec(A, i::Integer) |
| 125 | + x = zeros(eltype(A), size(A, 2)) |
| 126 | + @inbounds x[i] = one(eltype(A)) |
| 127 | + return x |
| 128 | +end |
| 129 | + |
| 130 | +nogetindex_error() = error("indexing not allowed for LinearMaps; consider setting `LinearMaps.allowgetindex = true`") |
| 131 | + |
| 132 | +# end # module |
0 commit comments