# Solve linear system A\*X=b

**URL:** <https://community.freefem.org/t/solve-linear-system-a-x-b/203>\
**Category:** General Discussion\
**Created:** [December 17, 2019, 4:14pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203 "2019-12-17T16:14:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)\
**Post date:** [December 17, 2019, 4:14pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/1 "2019-12-17T16:14:54Z")

</div>

Hi every one,

I have a linear system A\*X=b…  
Is there any command to solve it directly in FreeFem++ ?

Thanks.

---

<div class="post-metadata">

**Author:** ![AFourmont](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/afourmont/32/56_2.png) [@AFourmont](https://community.freefem.org/u/AFourmont)\
**Post date:** [December 17, 2019, 4:46pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/2 "2019-12-17T16:46:48Z")

</div>

Hello,  
Read the do please and look a simple example like examples/tutorial/LaplaceP1.edp  
Axel

---

<div class="post-metadata">

**Author:** ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)\
**Post date:** [December 17, 2019, 5:00pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/3 "2019-12-17T17:00:12Z")

</div>

Thanks, but there is no linear system to solve in this example.

I am searching to solve a general linear system A\*X=b  
In FreeFem, I have tried to use the command:  
X=A^-1 \* b;  
But it doesn’t work.

---

<div class="post-metadata">

**Author:** ![AFourmont](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/afourmont/32/56_2.png) [@AFourmont](https://community.freefem.org/u/AFourmont)\
**Post date:** [December 17, 2019, 5:20pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/4 "2019-12-17T17:20:55Z")

</div>

sorry, the previous example uses a problem type to define the variational formulation.  
This example examples/tutorial/ex-vf.edp builds a linear system from a varf.

Axel

---

<div class="post-metadata">

**Author:** ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)\
**Post date:** [December 17, 2019, 5:30pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/5 "2019-12-17T17:30:42Z")

</div>

> [@AFourmont](#):
>
> /ex-vf.ed

Thanks again, but my system is not related to a variational form.

---

<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:** [December 17, 2019, 7:45pm UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/6 "2019-12-17T19:45:06Z")

</div>

You can still use `A^-1 * b` even if `A` does not come from a `varf`.

---

<div class="post-metadata">

**Author:** ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)\
**Post date:** [December 18, 2019, 7:47am UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/7 "2019-12-18T07:47:29Z")

</div>

Thank you very much.

It was only necessary to declare A as “matrix” and not as “real{int,int]”.  
But there is still an error. If you can help to solve it.  
Please look at the attached file.

 ![erreur](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/a47aa0fd14817ac913e53146eb0b4a4a39a600a5.jpeg)

---

<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:** [December 18, 2019, 8:03am UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/8 "2019-12-18T08:03:12Z")

</div>

You need to call `set(A, solver = sparsesolver)`.

---

<div class="post-metadata">

**Author:** ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)\
**Post date:** [December 18, 2019, 10:20am UTC](https://community.freefem.org/t/solve-linear-system-a-x-b/203/9 "2019-12-18T10:20:39Z")

</div>

> [@prj](#):
>
> You need to call `set(A, solver = sparsesolver)` .

Thank you very much, it works well now.
