# Solve system given function instead of matrix

**URL:** <https://community.freefem.org/t/solve-system-given-function-instead-of-matrix/2290>\
**Category:** General Discussion\
**Created:** [February 23, 2023, 1:18pm UTC](https://community.freefem.org/t/solve-system-given-function-instead-of-matrix/2290 "2023-02-23T13:18:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BeniaminBogosel](https://avatars.discourse-cdn.com/v4/letter/b/3ec8ea/32.png) [@BeniaminBogosel](https://community.freefem.org/u/BeniaminBogosel)\
**Post date:** [February 23, 2023, 1:18pm UTC](https://community.freefem.org/t/solve-system-given-function-instead-of-matrix/2290/1 "2023-02-23T13:18:48Z")

</div>

Typically, when solving a classical problem like the Laplace equation, we may construct the matrix `A` starting from the bilinear form, build the right-hand side `b` and solve the system using `A^(-1)*b`. When the matrix `A` is sparse there is no problem. However, if some non-local terms are in the bilinear form, the matrix of the bilinear form may be full so I would like to avoid constructing it.

On the other hand, constructing a function which computes `A*v` where `v` is a vector is quite efficient in my case. Is there a functionality in FreeFEM which allows to solve a system of equations `A*x=b` if instead of the matrix `A` we have a function computing the product `A*v` for an arbitrary `v`? (I know such a functionality exists for Eigenvalue problems, but I cannot find it quickly in the documentation for a linear system).

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![BeniaminBogosel](https://avatars.discourse-cdn.com/v4/letter/b/3ec8ea/32.png) [@BeniaminBogosel](https://community.freefem.org/u/BeniaminBogosel)\
**Post date:** [February 23, 2023, 2:54pm UTC](https://community.freefem.org/t/solve-system-given-function-instead-of-matrix/2290/2 "2023-02-23T14:54:18Z")

</div>

I found the solution in the examples file: examples/tutorial/algo.edp

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [February 23, 2023, 3:15pm UTC](https://community.freefem.org/t/solve-system-given-function-instead-of-matrix/2290/3 "2023-02-23T15:15:32Z")

</div>

You can also do the same thing using PETSc, e.g., [FreeFem-tutorial - Section 8 - example11.edp](https://joliv.et/FreeFem-tutorial/section_8/example11.edp.html)

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [February 24, 2023, 8:32pm UTC](https://community.freefem.org/t/solve-system-given-function-instead-of-matrix/2290/4 "2023-02-24T20:32:02Z")

</div>

you can use l

```auto
// the grad of the bilinear part of J (the RHS in remove)
  LinearCG(DJ,u,eps=1.e-6,nbiter=20,precon=matId,verbosity=verb);
  cout << "LinearGC (Affine) : J(u) = " << J(u) << " err=" << error(u,b) << endl;
  LinearCG(DJ,u,eps=1.e-15,nbiter=20,precon=matId,verbosity=50,stop=stop);
  cout << "LinearGC (Affine with stop) : J(u) = " << J(u) << " err=" << error(u,b) << endl;
  LinearCG(DJ0,u,b,eps=1.e-6,nbiter=20,precon=matId,verbosity=verb);
  cout << "LinearGC (Linear) : J(u) = " << J(u) << " err=" << error(u,b) << endl;
  cout << "LinearGMRES (Affine) : J(u) = " << J(u) << " err=" << error(u,b) << endl;
  LinearGMRES(DJ0,u,b,eps=1.e-6,nbiter=20,precon=matId,verbosity=verb);
  cout << "LinearGMRES (Linear) : J(u) = " << J(u) << " err=" << error(u,b) << endl;
{ // --- a real non linear test ---

```

form  
[algo.edp](https://community.freefem.org/uploads/short-url/aoetywdJjXHtmLqOwns5A41n0Ug.edp) (6.0 KB)
