# Inverting a matrix object?

**URL:** <https://community.freefem.org/t/inverting-a-matrix-object/2362>\
**Category:** General Discussion\
**Created:** [March 29, 2023, 8:48am UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362 "2023-03-29T08:48:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neutrino](https://avatars.discourse-cdn.com/v4/letter/n/bb73d2/32.png) [@Neutrino](https://community.freefem.org/u/Neutrino)\
**Post date:** [March 29, 2023, 8:48am UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/1 "2023-03-29T08:48:06Z")

</div>

Hi there,

I’m not sure if this has been answered before, and probably seems like a trivial question, but suppose that I have the following code

```auto
varf a(u,v) = int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v));
matrix K = a(Vh,Vh);

```

and that I would like to invert the matrix `K`. I’ve tried `inv(K)` from `fflapack` and `K^-1` and both did not work. I also skimmed through the [documentation](https://doc.freefem.org/pdf/FreeFEM-documentation.pdf) but with no luck. How do I invert a matrix object in general?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [March 29, 2023, 10:01am UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/2 "2023-03-29T10:01:23Z")

</div>

Why do you need to invert the matrix? If you are using the matrix to solve a linear system you should definitely not invert it.

You cannot invert a `matrix` with fflapack – you will need to first convert it to a dense matrix, i.e. `real[int,int]`.  
[https://doc.freefem.org/examples/developers.html#matrix-inversion](https://doc.freefem.org/examples/developers.html#matrix-inversion)

---

<div class="post-metadata">

**Author:** ![Neutrino](https://avatars.discourse-cdn.com/v4/letter/n/bb73d2/32.png) [@Neutrino](https://community.freefem.org/u/Neutrino)\
**Post date:** [April 3, 2023, 3:08pm UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/3 "2023-04-03T15:08:07Z")

</div>

Thank you for the message and sorry for the late reply. I’m doing a time dependent simulation where I need the inverse of `K` at each iteration. So instead of solving `K^-1*u` at each step, I thought I’d just compute `Kinv=K^-1` and then do `Kinv*u` directly to speed things up.

Also, how do you convert to a dense matrix? I tried the following _incredibly slow_ solution

```auto
int col = K.n;
int row = K.m;
real[int,int] Kin(row,col); 
for(int i=0; i<col; i++){	
	for(int j=0; j<row; j++){	
		Kin(j,i)=K(j,i);	
	}				
}					
inv(Kin); 

```

Not sure why copying takes thing long for a 4096\times 4096 matrix!

---

<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 3, 2023, 3:17pm UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/4 "2023-04-03T15:17:56Z")

</div>

Computing `Kinv` and then `Kinv*u` is in no way faster than computing `K^-1*u`. The L and U factors of the decomposition of `K` are computed only once, and then it’s just successive forward and backward solves, which are basically as fast as matrix-vector products.  
Doing `for` loop in FreeFEM is slow, you should replace your loop by simply doing `Kin = K;`.

---

<div class="post-metadata">

**Author:** ![Neutrino](https://avatars.discourse-cdn.com/v4/letter/n/bb73d2/32.png) [@Neutrino](https://community.freefem.org/u/Neutrino)\
**Post date:** [April 3, 2023, 3:28pm UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/5 "2023-04-03T15:28:07Z")

</div>

So if I understand correctly, FreeFEM stores the LU-factorization of `K` after the first use of `K^-1*u`?

Also, `Kin = K` generates an error as `K` is of `matrix` type and `Kin` is of type `real[int][int]`.

---

<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 3, 2023, 3:39pm UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/6 "2023-04-03T15:39:24Z")

</div>

You understand correctly, but it’s not specific to FreeFEM, it’s almost all sparse exact factorization solvers that behave like this (SuiteSparse, MUMPS, PARDISO…).

It does not fail with a `real[int, int]`, which is the type you used in your original snippet.

---

<div class="post-metadata">

**Author:** ![Neutrino](https://avatars.discourse-cdn.com/v4/letter/n/bb73d2/32.png) [@Neutrino](https://community.freefem.org/u/Neutrino)\
**Post date:** [April 3, 2023, 3:43pm UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/7 "2023-04-03T15:43:57Z")

</div>

Sorry I meant `real[int,int]` in my previous post. Also I confirm that

```auto
varf a(u,v) = int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v));
matrix K = a(Vh,Vh);
real[int,int] Kin(K.m,K.n);
Kin = K; 

```

creates an error.

---

<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 3, 2023, 3:45pm UTC](https://community.freefem.org/t/inverting-a-matrix-object/2362/8 "2023-04-03T15:45:59Z")

</div>

Right, this does not work, you can simply do `for[I,J,C : K] Kin(I,J) = C;`.
