I'm making a custom "BandedMatrix" data type that overrides setindex!, and
I want to use @inbounds:
@inbounds A[k,j]=4
The issue is that @inbounds doesn't seem to go "inside" the setindex! call.
Any idea to allow @inbounds to work? My solution is to add another call
ibsetindex!, but this makes the set syntax uglier. Code is below.
immutable BandedMatrix{T}
data::Matrix{T}
a::Int
b::Int
n::Int #Number of rows
end
function BandedMatrix{T}(data::Matrix{T},a,b,n)
@assert size(data,1)==b-a+1
BandedMatrix{data,a,b,n}
end
bazeros{T}(::Type{T},a::Integer,b,n,m)=BandedMatrix(zeros(T,b-a+1,m),a,b,n)
bazeros{T}(::Type{T},a::Integer,b,n)=bazeros(T,a,b,n,n)
bazeros(a::Integer,b,n,m)=bazeros(Float64,a,b,n,m)
bazeros(a::Integer,b,n)=bazeros(Float64,a,b,n,n)
Base.size(A::BandedMatrix,k)=ifelse(k==1,A.n,size(A.data,2))
Base.size(A::BandedMatrix)=A.n,size(A.data,2)
usgetindex(A::BandedMatrix,k,j::Integer)=A.data[k-j+A.b+1,j]
getindex{T}(A::BandedMatrix{T},k::Integer,j::Integer)=(A.a≤j-k≤A.b)?usgetindex(A,k,j):(k≤A.n?zero(T):throw(BoundsError()))
getindex(A::BandedMatrix,kr::Range,j::Integer)=A.a≤j-kr[end]≤j-kr[1]≤A.b?usgetindex(A,kr,j):[A[k,j]
for k=kr]
getindex(A::BandedMatrix,k::Integer,jr::Range)=[A[k,j] for j=jr]
getindex(A::BandedMatrix,kr::Range,jr::Range)=[A[k,j] for k=kr,j=jr]
Base.full(A::BandedMatrix)=A[1:size(A,1),1:size(A,2)]
ibsetindex!(A::BandedMatrix,v,k,j::Integer)=(@inbounds
A.data[k-j+A.b+1,j]=v)
setindex!(A::BandedMatrix,v,k,j::Integer)=(A.data[k-j+A.b+1,j]=v)
function setindex!(A::BandedMatrix,v,kr::Range,jr::Range)
for j in jr
A[kr,j]=slice(v,:,j)
end
end
function setindex!(A::BandedMatrix,v,k::Integer,jr::Range)
for j in jr
A[k,j]=v[j]
end
end