# What to do since CSR matrixes are not in Sparse Arrays?

**URL:** <https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 26, 2021, 3:49pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991 "2021-01-26T15:49:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![andfranklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andfranklin/32/20078_2.png) [@andfranklin](https://discourse.julialang.org/u/andfranklin)\
**Post date:** [January 26, 2021, 3:49pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/1 "2021-01-26T15:49:10Z")

</div>

Viral B. Shah discussed (in 2015!) why CSR matrixes were not included in Sparse Arrays: [link](https://youtu.be/Xqfa_irN69g?t=2213). The justification makes sense, but I now find myself wishing there was an easy way to access CSR sparse array. I just want to get stuff done, and I don’t care much about the elegance of the abstraction design at the current time.

Does anyone have suggestions for an easy work-around so that I can use a CSR matrix? A few “solutions” I’ve considered:

1. Bite the bullet and implement a CSR matrix from the ground up. This feels like a waste of time because Viral mentions that someone has already done this – it just wasn’t included in Sparse Arrays. (Does anyone know where I can find this implementation?)
2. Hack together a CSR matrix that is simply an interface to a CSC matrix. This might take less time to implement than option 1, and it might require less debugging. Again, this feels like a waste of time and coding effort.
3. Use a 3rd party project that hasn’t been touched since 201(whatever). A quick search on github or google will reveal a few potential candidates. This seems risky since most of the projects I’ve seen do not have testing, documentation, or a broad user base.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 26, 2021, 3:55pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/2 "2021-01-26T15:55:17Z")

</div>

Why not just use a `CSC` matrix and transpose it?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 26, 2021, 3:55pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/3 "2021-01-26T15:55:54Z")

</div>

Can you perhaps just use a transposed view of a CSC matrix? This could be as easy as:

```julia
julia> A_csr = transpose(sprand(10, 5, 0.1))
5×10 LinearAlgebra.Transpose{Float64,SparseMatrixCSC{Float64,Int64}}
...

```

`transpose` preserves the underlying sparse matrix and presents a transposed view of it, as if it were actually stored in CSR format.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 26, 2021, 3:59pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/5 "2021-01-26T15:59:29Z")

</div>

> [@andfranklin](#):
>
> (Does anyone know where I can find this implementation?)

> <https://github.com/JuliaLang/julia/pull/7029>
>
> This would introduce Sparse CSR matrix data type.
> \- \`SparseMatrixCSC\` and \`Spars…eMatrixCSR\` implement \`CompressedSparseMatrix\` 
> \- \`CompressedSparseMatrix\` is \`AbstractSparseMatrix\` and parameterized with the constants \`CSC\` or \`CSR\` ~~a \`CompressedSparseType\` (\`CSC\` or \`CSR\`)~~
> \- made the existing CSC matrix methods generic wherever possible to be used on \`CompressedSparseMatrix\` by working on the abstraction of compressed and uncompressed dimension.
> 
> All the linalg methods and a few matrix methods are yet to be implemented for CSR. This does not break any existing APIs. In brief, the API changes are:
> \- There is a \`SparseMatrixCSR\` constructor, similar to the one for CSC. 
> \- The \`sparse\` constructor and methods like \`spzeros\`, \`sprand\` can now optionally take a keyword argument \`format\` that can either be \`CSC\` (default) or \`CSR\`. ~~\`CSR\` or \`CSC\` as the first parameter.~~
> \- The \`sparse\` constructor now has a variant that takes a sparse store (\`CompressedSparseStore\` or a \`TripletSparseStore\`) specifying the data. This seemed to be a good uniform way to overload the \`sparse\` constructor for different data formats.
> ~~\\- Methods like \`spzeros\`, \`sprand\` take a keyword argument \`format\` that can either be \`CSC\` (default) or \`CSR\`.~~
> 
> Would appreciate feedbacks and whether this is in the right direction.

just for reference and probably not of too much use.

---

<div class="post-metadata">

**Author:** ![andfranklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andfranklin/32/20078_2.png) [@andfranklin](https://discourse.julialang.org/u/andfranklin)\
**Post date:** [January 30, 2021, 7:18am UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/6 "2021-01-30T07:18:05Z")

</div>

I ran some benchmarks using the suggestions of @rdeits and @Oscar_Smith . The example matrix is the 2D finite-difference laplacian on a grid of 10,000 x 10,000 points. The “CSR” matrix is the transpose of the CSC matrix. Below are the benchmark results and a conclusion. All times are the minimum benchmark time:

 ![Screen Shot 2021-01-30 at 1.09.31 AM](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f1554528e24b394cd642f51501a28b64571c382.jpeg)

Thoughts? I can provide the pluto notebook upon request.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 31, 2021, 5:01pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/7 "2021-01-31T17:01:39Z")

</div>

> **[SparseMatricesOLD.jl/src at master · JuliaAttic/SparseMatricesOLD.jl](https://github.com/JuliaAttic/SparseMatricesOLD.jl/tree/master/src)**
>
> master/src

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 31, 2022, 3:07pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/8 "2022-01-31T15:07:03Z")

</div>

Matrix-vector mult is _much_ faster with the CSR format. I use [https://github.com/gridap/SparseMatricesCSR.jl](https://github.com/gridap/SparseMatricesCSR.jl). And [https://github.com/BacAmorim/ThreadedSparseCSR.jl](https://github.com/BacAmorim/ThreadedSparseCSR.jl).

---

<div class="post-metadata">

**Author:** ![Bruno\_Amorim](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bruno\_Amorim](https://discourse.julialang.org/u/Bruno_Amorim)\
**Post date:** [January 31, 2022, 4:40pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/9 "2022-01-31T16:40:09Z")

</div>

I am glad ThreadedSparseCSR.jl is being useful for you!

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 14, 2023, 10:34pm UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/10 "2023-03-14T22:34:23Z")

</div>

What is the best way to convert a CSC matrix to CSR? I am doing:

```julia
using LinearAlgebra, SparseArrays, SparseMatricesCSR
A = sprand(10,20,0.5); # CSC
Acsr = SparseMatricesCSR.SparseMatrixCSR(transpose(sparse(transpose(A)))); # CSR
@assert(norm(Acsr-A) == 0)

```

but feel like I’m missing something…

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [November 27, 2025, 9:26am UTC](https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991/11 "2025-11-27T09:26:36Z")

</div>

I don’t know (I just started playing with this) but I used `findnz` instead. No idea about performance though:

```julia
Acsr = sparsecsr(findnz(A)..., size(A)...)

```
