Skip to content

Commit d8ac9fb

Browse files
committed
Fix linear indexing of PDiagMat and ScalMat
1 parent d50e35a commit d8ac9fb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/pdiagmat.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ LinearAlgebra.cholesky(a::PDiagMat) = cholesky(Diagonal(a.diag))
3131
### Inheriting from AbstractMatrix
3232

3333
Base.size(a::PDiagMat) = (a.dim, a.dim)
34-
Base.getindex(a::PDiagMat{T},i::Integer) where {T} = i % a.dim == (i ÷ a.dim + 1) ? a.diag[i % a.dim] : zero(T)
34+
function Base.getindex(a::PDiagMat, i::Integer)
35+
ncol, nrow = fldmod1(i, a.dim)
36+
ncol == nrow ? a.diag[nrow] : zero(eltype(a))
37+
end
3538
Base.getindex(a::PDiagMat{T},i::Integer,j::Integer) where {T} = i == j ? a.diag[i] : zero(T)
3639

3740
### Arithmetics

src/scalmat.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ LinearAlgebra.cholesky(a::ScalMat) = cholesky(Diagonal(fill(a.value, a.dim)))
2323
### Inheriting from AbstractMatrix
2424

2525
Base.size(a::ScalMat) = (a.dim, a.dim)
26-
Base.getindex(a::ScalMat{T}, i::Integer) where {T} = i%a.dim == (i ÷ a.dim + 1) ? a.value : zero(T)
26+
function Base.getindex(a::ScalMat, i::Integer)
27+
ncol, nrow = fldmod1(i, a.dim)
28+
ncol == nrow ? a.value : zero(eltype(a))
29+
end
2730
Base.getindex(a::ScalMat{T}, i::Integer, j::Integer) where {T} = i == j ? a.value : zero(T)
2831

2932
### Arithmetics

0 commit comments

Comments
 (0)