Skip to content

Slicing the matrix should give a sparse array #11

@giordano

Description

@giordano
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> 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                 
                         

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions