The stdlib SparseArrays.jl is mainly about SparseMatrixCSC
and SparseVector
.
From my point of view, its doc reads a bit immature (Edit: this word was used improperly. I hadn’t learnt its meaning. I’m not an native speaker, please don’t take it serious ), due in part to
- there are not very many examples which can showcase its expertise.
- relations to LinearAlgebra.jl is not elaborate. (They shouldn’t be separate packages, but should work well together in tandem.) (an exemplary question is to ask why we need
sparse(I, 2, 2)
given the existence ofDiagonal(ones(2))
.)
And particularly, I have a suggestion I don’t know if it is correct and insightful:
I find that the intro of SparseVector
here is somewhat abrupt. A better intro could be
using LinearAlgebra, SparseArrays
function get_a_SparseMatrixCSC(N)
A, v = spzeros(N, N), rand(N)
A[1, :], A[:, 1] = v, v
A
end
julia> A = get_a_SparseMatrixCSC(3)
3×3 SparseMatrixCSC{Float64, Int64} with 5 stored entries:
0.728406 0.595737 0.00605678
0.595737 ⋅ ⋅
0.00605678 ⋅ ⋅
julia> sv = A[:, 1]
3-element SparseVector{Float64, Int64} with 3 stored entries:
[1] = 0.728406
[2] = 0.595737
[3] = 0.00605678
Thus we notice that a SparseVector
arises naturally as a slice of a SparseMatrixCSC
.
Furthermore, I assume that if the slice is intended to be read-only, then it could had been better to do sv = view(A, :, 1)
, am I right?
Additionally, we might testify the assertion that column slice is faster:
using BenchmarkTools
function rv_dot_u(A, u) return dot(view(A, 1, :), u) end
function cv_dot_u(A, u) return dot(view(A, :, 1), u) end
N = Int(1e6);
A, u = get_a_SparseMatrixCSC(N), rand(N);
julia> @btime rv_dot_u(A, u)
2.005 ms (1 allocation: 16 bytes)
249983.8160420952
julia> @btime cv_dot_u(A, u)
1.113 ms (1 allocation: 16 bytes)
249983.8160420952
(I wonder if this is the standard way to do dot product. Are there ways faster in this context?)
hope to hear about some comments