Skip to content

Commit 48f50d8

Browse files
authored
Merge pull request #16 from gridap/implementing_la_fillstored
Implementing LinearAlgebra.fillstored! for CSR data types
2 parents de63e5c + a767c2a commit 48f50d8

4 files changed

+32
-13
lines changed

src/SparseMatrixCSR.jl

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ end
120120

121121
_copy_and_increment(x) = copy(x) .+ 1
122122

123+
function LinearAlgebra.fillstored!(a::SparseMatrixCSR,v)
124+
fill!(a.nzval,v)
125+
end
126+
123127
function LinearAlgebra.lu(a::SparseMatrixCSR{0})
124128
rowptr = _copy_and_increment(a.rowptr)
125129
colval = _copy_and_increment(a.colval)

src/SymSparseMatrixCSR.jl

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Matrix type for storing symmetric sparse matrices in the
88
Compressed Sparse Row format with `Bi`-based indexing (typically 0 or 1).
99
Only the upper triangle is stored (including the non zero diagonal entries),
1010
which is represented by a `SparseMatrixCSR`.
11-
The standard way of constructing a `SymSparseMatrixCSR` is through the
11+
The standard way of constructing a `SymSparseMatrixCSR` is through the
1212
[`symsparsecsr`](@ref) function.
1313
"""
1414
struct SymSparseMatrixCSR{Bi,T,Ti<:Integer} <: AbstractSparseMatrix{T,Ti}
@@ -90,10 +90,10 @@ nnz(S::SymSparseMatrixCSR) = nnz(S.uppertrian)
9090
"""
9191
nonzeros(S::SymSparseMatrixCSR)
9292
93-
Return a vector (1-based) of the structural nonzero values in sparse array S.
93+
Return a vector (1-based) of the structural nonzero values in sparse array S.
9494
This includes zeros that are explicitly stored in the sparse array,
9595
which correspond to the nonzero entries in the upper triangle and diagonal.
96-
The returned vector points directly to the internal nonzero storage of S,
96+
The returned vector points directly to the internal nonzero storage of S,
9797
and any modifications to the returned vector will mutate S as well.
9898
"""
9999
nonzeros(S::SymSparseMatrixCSR) = nonzeros(S.uppertrian)
@@ -104,17 +104,17 @@ nonzeros(S::SymSparseMatrixCSR) = nonzeros(S.uppertrian)
104104
Return a vector of the col indices of `S`. The stored values are indexes to arrays
105105
with `Bi`-based indexing, but the `colvals(S)` array itself is a standard 1-based
106106
Julia `Vector`.
107-
Any modifications to the returned vector will mutate S as well.
108-
Providing access to how the col indices are stored internally
109-
can be useful in conjunction with iterating over structural
107+
Any modifications to the returned vector will mutate S as well.
108+
Providing access to how the col indices are stored internally
109+
can be useful in conjunction with iterating over structural
110110
nonzero values. See also [`nonzeros`](@ref) and [`nzrange`](@ref).
111111
"""
112112
colvals(S::SymSparseMatrixCSR) = colvals(S.uppertrian)
113113

114114
"""
115115
nzrange(S::SymSparseMatrixCSR, row::Integer)
116116
117-
Return the range of indices to the structural nonzero values of a
117+
Return the range of indices to the structural nonzero values of a
118118
sparse matrix row section being in the diagonal or upper triangle.
119119
The returned range of indices is always 1-based even for `Bi != 1`.
120120
"""
@@ -123,8 +123,8 @@ nzrange(S::SymSparseMatrixCSR, row::Integer) = nzrange(S.uppertrian, row)
123123
"""
124124
findnz(S::SymSparseMatrixCSR)
125125
126-
Return a tuple `(I, J, V)` where `I` and `J` are the row and column 1-based indices
127-
of the stored ("structurally non-zero in diagonal + upper trianle") values in sparse matrix A,
126+
Return a tuple `(I, J, V)` where `I` and `J` are the row and column 1-based indices
127+
of the stored ("structurally non-zero in diagonal + upper trianle") values in sparse matrix A,
128128
and V is a vector of the values. The returned vectors are newly allocated
129129
and are unrelated to the internal storage of matrix `S`.
130130
"""
@@ -134,12 +134,16 @@ findnz(S::SymSparseMatrixCSR) = findnz(S.uppertrian)
134134
count(pred, S::SymSparseMatrixCSR)
135135
count(S::SymSparseMatrixCSR)
136136
137-
Count the number of elements in `nonzeros(S)` for which predicate `pred` returns `true`.
137+
Count the number of elements in `nonzeros(S)` for which predicate `pred` returns `true`.
138138
If `pred` not given, it counts the number of `true` values.
139139
"""
140140
count(pred, S::SymSparseMatrixCSR) = count(pred, S.uppertrian)
141141
count(S::SymSparseMatrixCSR) = count(i->true, S)
142142

143+
function LinearAlgebra.fillstored!(a::SymSparseMatrixCSR,v)
144+
LinearAlgebra.fillstored!(a.uppertrian,v)
145+
end
146+
143147
function mul!(y::AbstractVector,A::SymSparseMatrixCSR,v::AbstractVector, α::Number, β::Number)
144148
A.uppertrian.n == size(v, 1) || throw(DimensionMismatch())
145149
A.uppertrian.m == size(y, 1) || throw(DimensionMismatch())
@@ -177,7 +181,7 @@ end
177181

178182
function show(io::IO, ::MIME"text/plain", S::SymSparseMatrixCSR)
179183
xnnz = nnz(S)
180-
print(io, S.uppertrian.m, "×", S.uppertrian.n, " ",
184+
print(io, S.uppertrian.m, "×", S.uppertrian.n, " ",
181185
typeof(S), " with ", xnnz, " stored ",
182186
xnnz == 1 ? "entry" : "entries")
183187
if xnnz != 0

test/SparseMatrixCSR.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ function test_csr(Bi,Tv,Ti)
7676
mul!(y,CSR,x)
7777
mul!(z,CSC,x)
7878
@test y z
79+
@test CSR*x CSC*x
7980

8081
mul!(y,CSR,x,1,2)
8182
mul!(z,CSC,x,1,2)
8283
@test y z
8384

84-
@test CSR*x CSC*x
85+
LinearAlgebra.fillstored!(CSR,3.33)
86+
LinearAlgebra.fillstored!(CSC,3.33)
87+
mul!(y,CSR,x)
88+
mul!(z,CSC,x)
89+
@test y z
90+
8591
end
8692

8793
function test_lu(Bi,I,J,V)

test/SymSparseMatrixCSR.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ function test_csr(Bi,Tv,Ti)
7878

7979
@test CSR*x CSC*x
8080

81+
LinearAlgebra.fillstored!(CSR,3.33)
82+
LinearAlgebra.fillstored!(CSC,3.33)
83+
mul!(y,CSR,x)
84+
mul!(z,CSC,x)
85+
@test y z
86+
8187
end
8288

8389
for Bi in (0,1)
@@ -89,4 +95,3 @@ for Bi in (0,1)
8995
end
9096

9197
end # module
92-

0 commit comments

Comments
 (0)