# Methods for computing the inverse of large sparse matrix

**URL:** https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864
**Category:** General Discussion
**Created:** [April 15, 2025, 2:56pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864 "2025-04-15T14:56:06Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Nana](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nana](https://community.freefem.org/u/Nana)
#### Post date: [April 15, 2025, 2:56pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/1 "2025-04-15T14:56:06Z")

</div>

Dear Freefem users,I developed a set of finite element programs using FreeFEM,within the time-stepping loop, it is necessary to compute the inverse of a large sparse matrix M,Then, the inverse of M is left-multiplied with a sparse matrix L to obtain a new sparse matrix 𝑀^{-1}\*𝐿(denoted as MinvL).The original design of my program was inefficient.As the spatial mesh is refined, the runtime of the program becomes excessively long.The matrix MinvL is used repeatedly and must be computed inside the time-stepping loop, so computing the matrix inverse is unavoidable.  
I hope to find a more efficient programming approach to compute the inverse of large sparse matrices more effectively.I hope to receive some help. Thank you very much!  
The following is the original design of my program.

varf a (psiany, w) = int2d(Th, qforder=10) (psiany \* conj(w));  
matrix M = a(Vh, Vh);  
set(M, solver=UMFPACK);  
int m = M.n;  
complex[int, int] MinvDense(m, m);  
for (int j = 0; j \< m; j++) {  
complex[int] b(m);  
b = 0;  
b[j] = 1.0 + 0i;  
complex[int] p = M^-1 \* b;  
MinvDense(:, j) = p;  
}  
matrix Minv = MinvDense;

varf l(psiany, w)  
=int2d(Th,qforder=10)(  
1/(k^2)\*(dx(psiany)\*dx(conj(w))+dy(psiany)_dy(conj(w)))  
+1i/k (dx(psiany)A1x+dy(psiany)A1y)conj(w)  
+conj(1i/k)(A1xdx(conj(w))+A1ydy(conj(w)))psiany  
+(A1x^2+A1y^2)psiany conj(w)  
+ mu \* psiany \* conj(w)  
);  
matrix L = l(Vh, Vh);// matrix of (1i/kgrad+A)^2psi+mu_psi  
matrix MinvL = Minv \* L ;

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 15, 2025, 3:02pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/2 "2025-04-15T15:02:21Z")

</div>

What do you apply `MinvL` to?

---

<div class="post-metadata">

### Author: ![Nana](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nana](https://community.freefem.org/u/Nana)
#### Post date: [April 15, 2025, 3:10pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/3 "2025-04-15T15:10:20Z")

</div>

Yes,I aim to compute the matrix MinvL

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 15, 2025, 3:14pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/4 "2025-04-15T15:14:06Z")

</div>

Why? What do you compute with `MinvL`?

---

<div class="post-metadata">

### Author: ![Nana](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nana](https://community.freefem.org/u/Nana)
#### Post date: [April 15, 2025, 3:27pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/5 "2025-04-15T15:27:02Z")

</div>

In the equation I am solving, the matrix MinvL is an essential component in the construction of the unknown.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 15, 2025, 3:59pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/6 "2025-04-15T15:59:27Z")

</div>

What number of degrees of freedom would you ideally want in the finite element space `Vh`?

---

<div class="post-metadata">

### Author: ![Nana](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nana](https://community.freefem.org/u/Nana)
#### Post date: [April 16, 2025, 2:32am UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/8 "2025-04-16T02:32:03Z")

</div>

[test-A(4.16).edp](https://community.freefem.org/uploads/short-url/8lrBmiGd98Th1NXcJlmmYLE3thA.edp) (6.7 KB)

 ![0e98c1b94c6806875e4d091b3f4e90d](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/1/1d465322d8cdbc311e4c5fcaa3be1790d9f2f26a.png)  
The attachment contains my complete program, and the photo shows the part where the finite element spaces are defined.Later in the program, when computing the inverse of the matrix Mom (denoted as Mominv), I encountered the same issue.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 16, 2025, 5:05am UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/9 "2025-04-16T05:05:42Z")

</div>

You are not answering my question: what size `nn` would you like to use?

---

<div class="post-metadata">

### Author: ![Nana](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nana](https://community.freefem.org/u/Nana)
#### Post date: [April 16, 2025, 5:53am UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/10 "2025-04-16T05:53:20Z")

</div>

Thank you for your patience.  
I intend to set nn = 16, 32, or 64,  
so that“ mesh Th = square(nn, nn)” corresponds to a relatively fine mesh.  
I will later use this high-density mesh to study the temporal convergence order.  
Is my explanation clear? If there is anything that’s not clear, please feel free to let me know.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 16, 2025, 6:53am UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/11 "2025-04-16T06:53:57Z")

</div>

So, for `nn` set to 64, with P3 finite elements, you need about 11 GB of RAM just to store a dense matrix. Your problem is not tractable, it is still not clear to me why you need to actually form `MinvL`.

---

<div class="post-metadata">

### Author: ![Nana](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nana](https://community.freefem.org/u/Nana)
#### Post date: [April 16, 2025, 10:28am UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/12 "2025-04-16T10:28:50Z")

</div>

I’m looking for ways to optimize this part of the algorithm to speed up the computation.Are you suggesting that we should avoid forming the inverse matrix explicitly?If we can’t avoid computing the inverse and restrict ourselves to nn = 16, is there a more efficient way to write this part of the code?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [April 16, 2025, 12:05pm UTC](https://community.freefem.org/t/methods-for-computing-the-inverse-of-large-sparse-matrix/3864/13 "2025-04-16T12:05:33Z")

</div>

Could you please write down the math? What is your algorithm computing? You should never invert a matrix and just compute a sequence of forward and backward solves.
