# In matlab, we calculate inv(A)\* B by A\\B. How about Julia? note that A&B are two large sparse matrices

**URL:** https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322
**Category:** New to Julia
**Tags:** question
**Created:** [September 30, 2019, 12:26pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322 "2019-09-30T12:26:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![NITISH\_KUMAR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitish_kumar/32/10546_2.png) [@NITISH\_KUMAR](https://discourse.julialang.org/u/NITISH_KUMAR)
#### Post date: [September 30, 2019, 12:26pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/1 "2019-09-30T12:26:38Z")

</div>

Does anybody have experience with the calculation of inv(A)\*B in Julia, where A and B are large sparse matrices?

When I used zz = A\B in Julia. It shows the following error in Julia

````julia
MethodError: no method matching ldiv!(::SuiteSparse.UMFPACK.UmfpackLU{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64})
Closest candidates are:
  ldiv!(!Matched::Number, ::AbstractArray) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\generic.jl:152
  ldiv!(!Matched::LinearAlgebra.LU{T,LinearAlgebra.Tridiagonal{T,V}}, ::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T) where {T, V} at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\lu.jl:523
  ldiv!(!Matched::LinearAlgebra.Transpose{#s617,#s616} where #s616<:LinearAlgebra.LU{T,LinearAlgebra.Tridiagonal{T,V}} where #s617, ::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T) where {T, V} at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\lu.jl:555
  ...

Stacktrace:
 [1] \(::SuiteSparse.UMFPACK.UmfpackLU{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\factorization.jl:87
 [2] \(::SparseArrays.SparseMatrixCSC{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64}) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.2\SparseArrays\src\linalg.jl:1374
 [3] top-level scope at In[5]:1
```![wktBM|592x499](upload://pFiCUnwBi5tsGuciDEaMhO8sxqw.png)![wktBM|592x499](upload://pFiCUnwBi5tsGuciDEaMhO8sxqw.png)
````

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 30, 2019, 12:31pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/2 "2019-09-30T12:31:13Z")

</div>

Just make `B` dense? The result will be a dense matrix of that size anyway.

---

<div class="post-metadata">

### Author: ![NITISH\_KUMAR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitish_kumar/32/10546_2.png) [@NITISH\_KUMAR](https://discourse.julialang.org/u/NITISH_KUMAR)
#### Post date: [September 30, 2019, 12:55pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/3 "2019-09-30T12:55:23Z")

</div>

Thank you 🙂 I’ll try. Wondering if full() command is not supported in Julia 1.2?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 30, 2019, 1:07pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/4 "2019-09-30T13:07:10Z")

</div>

> [@NITISH\_KUMAR](#):
>
> Wondering if full() command is not supported in Julia 1.2?

You can just do `Matrix(B)`.

---

<div class="post-metadata">

### Author: ![NITISH\_KUMAR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitish_kumar/32/10546_2.png) [@NITISH\_KUMAR](https://discourse.julialang.org/u/NITISH_KUMAR)
#### Post date: [September 30, 2019, 1:12pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/5 "2019-09-30T13:12:28Z")

</div>

Unfortunately, Same error.

MethodError: no method matching ldiv!(::SuiteSparse.UMFPACK.UmfpackLU{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64})  
Closest candidates are:  
ldiv!(!Matched::Number, ::AbstractArray) at C:\cygwin\home\Administrator\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\generic.jl:152  
ldiv!(!Matched::LinearAlgebra.LU{T,LinearAlgebra.Tridiagonal{T,V}}, ::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T) where {T, V} at C:\cygwin\home\Administrator\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\lu.jl:523  
ldiv!(!Matched::LinearAlgebra.Transpose{#s617,#s616} where #s616\<:LinearAlgebra.LU{T,LinearAlgebra.Tridiagonal{T,V}} where #s617, ::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T) where {T, V} at C:\cygwin\home\Administrator\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\lu.jl:555  
…

Stacktrace:  
[1] (::SuiteSparse.UMFPACK.UmfpackLU{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64}) at C:\cygwin\home\Administrator\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.2\LinearAlgebra\src\factorization.jl:87  
[2] (::SparseArrays.SparseMatrixCSC{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64}) at C:\cygwin\home\Administrator\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.2\SparseArrays\src\linalg.jl:1374  
[3] top-level scope at In[5]:1

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 30, 2019, 1:45pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/6 "2019-09-30T13:45:03Z")

</div>

> [@NITISH\_KUMAR](#):
>
> MethodError: no method matching ldiv!(::SuiteSparse.UMFPACK.UmfpackLU{Float64,Int64}, ::SparseArrays.SparseMatrixCSC{Float64,Int64})

Looks like you’re stll calling `ldiv!` with the sparse matrix 🤔

Can you show the code that produces this error?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 30, 2019, 1:45pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/7 "2019-09-30T13:45:09Z")

</div>

Can you give an example of what you are doing?

```julia
julia> A = lu(sprand(5,5,0.5) + I)
UMFPACK LU Factorization of a (5, 5) sparse matrix
Ptr{Nothing} @0x00007fbedec06620

julia> B = sprand(5,5,0.4);

julia> A \ Matrix(B)
5×5 Array{Float64,2}:
 -0.247253 -0.458155 0.331186 -0.179299 0.222382
 -0.812489 -2.25484 1.4221 -0.251258 1.58024
  1.17403 2.29645 -0.930615 0.164422 -1.03411
  0.338484 0.0 0.0 0.0 0.0
 -0.18574 -0.0906186 0.0367224 0.660449 0.844956

```

---

<div class="post-metadata">

### Author: ![NITISH\_KUMAR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitish_kumar/32/10546_2.png) [@NITISH\_KUMAR](https://discourse.julialang.org/u/NITISH_KUMAR)
#### Post date: [September 30, 2019, 2:41pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/8 "2019-09-30T14:41:57Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/0/30b316ec8d68ab6c36b86c6941fa15609b6d3d57.png)

I tried with another problem, it worked. However, stucked with the memory issue.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 30, 2019, 3:02pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/9 "2019-09-30T15:02:54Z")

</div>

Yes, it would require a lot of memory to store that. What is the reason for doing this computation though? If this is some type of discretized problem where K is a “stiffness matrix” and M a “mass matrix” then I don’t see why you are doing K \ M.

---

<div class="post-metadata">

### Author: ![NITISH\_KUMAR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitish_kumar/32/10546_2.png) [@NITISH\_KUMAR](https://discourse.julialang.org/u/NITISH_KUMAR)
#### Post date: [September 30, 2019, 3:19pm UTC](https://discourse.julialang.org/t/in-matlab-we-calculate-inv-a-b-by-a-b-how-about-julia-note-that-a-b-are-two-large-sparse-matrices/29322/10 "2019-09-30T15:19:08Z")

</div>

Developing formulation for modal reduction in Multiphysics problems. It works in matlab if I consider both matrix as sparse and implement K \ M .
