Skip to content

Commit e1884b9

Browse files
authored
Merge pull request #124 from devmotion/indexing
Fix linear indexing of `PDiagMat` and `ScalMat`
2 parents d50e35a + 01cfddf commit e1884b9

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

src/pdiagmat.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ LinearAlgebra.cholesky(a::PDiagMat) = cholesky(Diagonal(a.diag))
3030

3131
### Inheriting from AbstractMatrix
3232

33-
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)
33+
function Base.getindex(a::PDiagMat, i::Integer)
34+
ncol, nrow = fldmod1(i, a.dim)
35+
ncol == nrow ? a.diag[nrow] : zero(eltype(a))
36+
end
3537
Base.getindex(a::PDiagMat{T},i::Integer,j::Integer) where {T} = i == j ? a.diag[i] : zero(T)
3638

3739
### Arithmetics

src/pdmat.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ LinearAlgebra.cholesky(a::PDMat) = a.chol
3434

3535
### Inheriting from AbstractMatrix
3636

37-
Base.size(a::PDMat) = size(a.mat)
3837
Base.getindex(a::PDMat, i::Int) = getindex(a.mat, i)
3938
Base.getindex(a::PDMat, I::Vararg{Int, N}) where {N} = getindex(a.mat, I...)
4039

src/pdsparsemat.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ LinearAlgebra.cholesky(a::PDSparseMat) = a.chol
3232

3333
### Inheriting from AbstractMatrix
3434

35-
Base.size(a::PDSparseMat) = (a.dim, a.dim)
3635
Base.getindex(a::PDSparseMat,i::Integer) = getindex(a.mat, i)
3736
Base.getindex(a::PDSparseMat,I::Vararg{Int, N}) where {N} = getindex(a.mat, I...)
3837

src/scalmat.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ LinearAlgebra.cholesky(a::ScalMat) = cholesky(Diagonal(fill(a.value, a.dim)))
2222

2323
### Inheriting from AbstractMatrix
2424

25-
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)
25+
function Base.getindex(a::ScalMat, i::Integer)
26+
ncol, nrow = fldmod1(i, a.dim)
27+
ncol == nrow ? a.value : zero(eltype(a))
28+
end
2729
Base.getindex(a::ScalMat{T}, i::Integer, j::Integer) where {T} = i == j ? a.value : zero(T)
2830

2931
### Arithmetics

test/addition.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ for T in [Float64,Float32]
77
printstyled("Testing addition with eltype = $T\n", color=:blue)
88
M = convert(Array{T,2},[4. -2. -1.; -2. 5. -1.; -1. -1. 6.])
99
V = convert(Array{T,1},[1.5, 2.5, 2.0])
10-
X = convert(T,2.0)
10+
local X = convert(T,2.0)
1111

1212
pm1 = PDMat(M)
1313
pm2 = PDiagMat(V)

test/testutils.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ function pdtest_basics(C::AbstractPDMat, Cmat::Matrix, d::Int, verbose::Int)
7878

7979
_pdt(verbose, "eltype")
8080
@test eltype(C) == eltype(Cmat)
81-
# @test eltype(typeof(C)) == eltype(typeof(Cmat))
81+
# @test eltype(typeof(C)) == eltype(typeof(Cmat))
82+
83+
_pdt(verbose, "index")
84+
@test all(C[i] == Cmat[i] for i in 1:(d^2))
85+
@test all(C[i, j] == Cmat[i, j] for j in 1:d, i in 1:d)
8286
end
8387

8488

@@ -180,9 +184,15 @@ function pdtest_mul(C::AbstractPDMat, Cmat::Matrix, X::Matrix, verbose::Int)
180184
_pdt(verbose, "multiply")
181185
@test C * X Cmat * X
182186

187+
y = similar(C * X, size(C, 1))
188+
ymat = similar(Cmat * X, size(Cmat, 1))
183189
for i = 1:size(X,2)
184190
xi = vec(copy(X[:,i]))
185191
@test C * xi Cmat * xi
192+
193+
mul!(y, C, xi)
194+
mul!(ymat, Cmat, xi)
195+
@test y ymat
186196
end
187197
end
188198

0 commit comments

Comments
 (0)