# Sparse to dense matrix

**URL:** https://discourse.julialang.org/t/sparse-to-dense-matrix/8380
**Category:** General Usage
**Created:** [January 15, 2018, 10:49am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380 "2018-01-15T10:49:57Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [January 15, 2018, 10:49am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/1 "2018-01-15T10:49:57Z")

</div>

For some numerical optimization I need to get the inverse of a sparse matrix. How do I compute it efficiently? Julia throws me an error when I try to use either `inv()` or `A\B` and tells me to convert my sparse matrix first to a dense one. How can I do that?

---

<div class="post-metadata">

### Author: ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)
#### Post date: [January 15, 2018, 10:56am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/2 "2018-01-15T10:56:33Z")

</div>

`full` or `Matrix` can solve your problem.

```julia
julia> a = speye(5,5)
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
  [1, 1] = 1.0
  [2, 2] = 1.0
  [3, 3] = 1.0
  [4, 4] = 1.0
  [5, 5] = 1.0

julia> b = full(a)
5×5 Array{Float64,2}:
 1.0 0.0 0.0 0.0 0.0
 0.0 1.0 0.0 0.0 0.0
 0.0 0.0 1.0 0.0 0.0
 0.0 0.0 0.0 1.0 0.0
 0.0 0.0 0.0 0.0 1.0

julia> c = Matrix(a)
5×5 Array{Float64,2}:
 1.0 0.0 0.0 0.0 0.0
 0.0 1.0 0.0 0.0 0.0
 0.0 0.0 1.0 0.0 0.0
 0.0 0.0 0.0 1.0 0.0
 0.0 0.0 0.0 0.0 1.0

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [January 15, 2018, 10:57am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/3 "2018-01-15T10:57:30Z")

</div>

Also take a look at  
[https://github.com/JuliaMath/IterativeSolvers.jl](https://github.com/JuliaMath/IterativeSolvers.jl)

---

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [January 15, 2018, 10:58am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/4 "2018-01-15T10:58:34Z")

</div>

Thanks a lot, I’ll have a look! 🙂

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 15, 2018, 11:11am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/5 "2018-01-15T11:11:51Z")

</div>

> [@IljaK91](#):
>
> For some numerical optimization I need to get the inverse of a sparse matrix.

Are you sure you do need the inverse? Textbook equations often use an inverse where the numerical implementation does without one. Examples include linear regression (b=(X'X)^{-1}X'y, but you use a factorization and never form the inverse), BFGS update (work with the inverse of the approximate Hessian instead), etc.

(_in case you have given this question due consideration and I am preaching to the choir, please ignore_)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 15, 2018, 11:22am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/6 "2018-01-15T11:22:17Z")

</div>

You want to use an iterative solver or a sparse factorization on sparse matrices. I’m surprised `\` doesn’t automatically fallback to some polyalgorithm over SuiteSparse for that?

---

<div class="post-metadata">

### Author: ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)
#### Post date: [January 15, 2018, 11:36am UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/7 "2018-01-15T11:36:51Z")

</div>

👍 for factorizations. For efficiency and stability purposes, you should also have a look at the documentation of some factorization routines available in `julia` by issuing `?lufact`, `?qrfact` and `?schurfact`, the first two of which already have methods defined for sparse matrices. For the rest, you should check an iterative solver suitable for your purpose as mentioned by Chris and Sebastian above.

---

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [January 15, 2018, 2:18pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/8 "2018-01-15T14:18:38Z")

</div>

Thank you guys for the many answers. Indeed, if you use `A\B`, `A` might be sparse. My problem was instead that `A` is actually singular and thus does not allow me to calculate the inverse. I am looking for the mistake on my side in calculating `A`, but if anyone of you have experience using CompEcon.jl, please let me know!

---

<div class="post-metadata">

### Author: ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)
#### Post date: [January 15, 2018, 2:26pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/9 "2018-01-15T14:26:24Z")

</div>

> [@IljaK91](#):
>
> I am looking for the mistake on my side in calculating A, but if anyone of you have experience using CompEcon.jl, please let me know!

It might be better to open a new thread specific to your issue, which preferably also includes a minimal working example. In this way, you would be highlighting the problem better (in a more concrete way), and people here would be helping you more easily. Otherwise, we tend to guess the issue ourselves 🙂

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 15, 2018, 2:31pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/10 "2018-01-15T14:31:55Z")

</div>

> [@IljaK91](#):
>
> My problem was instead that A is actually singular and thus does not allow me to calculate the inverse.

You can instead use `svdfact!` for the pseudoinverse of the matrix if that’s necessary. Of course, first check analytically if it’s supposed to be singular before going down this route.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [January 15, 2018, 3:21pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/11 "2018-01-15T15:21:58Z")

</div>

> [@ChrisRackauckas](#):
>
> You can instead use svdfact! for the pseudoinverse of the matrix

That’s not available for sparse matrices in v0.6 (and even the v0.7 I have now), but `qrfact` is available on both and is equally insensitive to `A` being singular.

---

<div class="post-metadata">

### Author: ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)
#### Post date: [November 20, 2018, 9:41pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/12 "2018-11-20T21:41:20Z")

</div>

full was not defined in Julia 1.0.2

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 20, 2018, 10:39pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/13 "2018-11-20T22:39:50Z")

</div>

Did you try the other thing that was suggested?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 20, 2018, 10:44pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/14 "2018-11-20T22:44:26Z")

</div>

There is no `full` in Julia 1.0. Use `Matrix(...)` instead to do the conversion.

---

<div class="post-metadata">

### Author: ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)
#### Post date: [November 20, 2018, 11:38pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/15 "2018-11-20T23:38:09Z")

</div>

Array(a) or Matrix(a) do the trick.

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [June 3, 2020, 5:34pm UTC](https://discourse.julialang.org/t/sparse-to-dense-matrix/8380/16 "2020-06-03T17:34:15Z")

</div>

Hi all, reviving this old thread to ask if there is a **generic** way of converting sparse arrays to dense, in particular when using sparse gpu arrays (e.g. `CuSparseMatrixCSR` → `CuMatrix`). Thanks!
