gridap / gridap/SparseMatricesCSR.jl

Slicing the matrix should give a sparse array

Open
#11 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
44
Forks
15
PR merge metrics
No merged PRs in 30d

Description

```julia
julia> m = 10 # Number rows
10

julia> n = 10 # Number columns
10

julia> nz = floor(Int, m * 1.5) # Number of non-zero elements
15

julia> rowptr = vcat(sort!(rand(0:nz, m)), nz);

julia> colval = rand(0:(n-1), nz);

julia> nzval = randn(nz);

julia> A = SparseMatrixCSR{0}(m, n, rowptr, colval, nzval)
10×10 SparseMatrixCSR{0, Float64, Int64} with 15 stored entries:
[1, 5] = 0.48371
[2, 3] = 1.72291
[3, 8] = 0.707065
[3, 5] = 0.622569
[3, 7] = 1.57136
[4, 8] = -0.262692
[4, 5] = -0.00643953
[5, 6] = -1.15259
[6, 3] = 0.0652688
[6, 2] = 1.02735
[7, 2] = 0.169683
[9, 6] = -0.648538
[9, 4] = -0.647156
[10, 6] = -1.28313
[10, 7] = 0.116641

julia> A[1:3, :]
3×10 Matrix{Float64}:
0.0 0.0 0.0 0.0 0.48371 0.0 0.0 0.0 0.0 0.0
0.0 0.0 1.72291 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 1.57136 0.0 0.0 0.0
```
This isn't very convenient, especially since this data structure should allow having a compact representation of a slice of rows. The standard `SparseMatrixCSC` has compact slicing for both rows and columns:
```julia
julia> B = sprandn(rng, 10, 10, 0.2)
10×10 SparseMatrixCSC{Float64, Int64} with 19 stored entries:
⋅ ⋅ ⋅ ⋅ -0.0684167 0.0414316 ⋅ ⋅ 1.79099 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.451416
⋅ ⋅ ⋅ ⋅ ⋅ 0.245215 ⋅ ⋅ ⋅ ⋅
⋅ -0.880614 -0.149248 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ -0.673498 ⋅ ⋅ ⋅ ⋅ ⋅ -0.48382 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -0.679016 -1.93712 -0.468799 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
0.0442601 0.0607505 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
-0.839911 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.715919 0.151704
⋅ ⋅ ⋅ ⋅ ⋅ -1.83668 0.267189 ⋅ ⋅ ⋅

julia> B[1:3, :]
3×10 SparseMatrixCSC{Float64, Int64} with 5 stored entries:
⋅ ⋅ ⋅ ⋅ -0.0684167 0.0414316 ⋅ ⋅ 1.79099 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.451416
⋅ ⋅ ⋅ ⋅ ⋅ 0.245215 ⋅ ⋅ ⋅ ⋅

julia> B[:, 1:3]
10×3 SparseMatrixCSC{Float64, Int64} with 6 stored entries:
⋅ ⋅ ⋅
⋅ ⋅ ⋅
⋅ ⋅ ⋅
⋅ -0.880614 -0.149248
⋅ ⋅ -0.673498
⋅ ⋅ ⋅
⋅ ⋅ ⋅
0.0442601 0.0607505 ⋅
-0.839911 ⋅ ⋅
⋅ ⋅ ⋅
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.