# X+a\*Y with PETSc matrix (Mat)

**URL:** <https://community.freefem.org/t/x-a-y-with-petsc-matrix-mat/940>\
**Category:** General Discussion\
**Created:** [April 23, 2021, 5:05pm UTC](https://community.freefem.org/t/x-a-y-with-petsc-matrix-mat/940 "2021-04-23T17:05:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pmollo](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/pmollo/32/500_2.png) [@Pmollo](https://community.freefem.org/u/Pmollo)\
**Post date:** [April 23, 2021, 5:05pm UTC](https://community.freefem.org/t/x-a-y-with-petsc-matrix-mat/940/1 "2021-04-23T17:05:48Z")

</div>

Hi,  
As said in the title, I am trying to compute `X=X+a*Y` where `X` and `Y` are `Mat` PETSc matrices and `a` is a scalar. Is there a way to do this ? Here a stupid example :

```
load "PETSc"
mesh Th = square(10,10);
fespace Vh(Th,P1);

varf vX(u,v) = int2d(Th)(u*v) + on(1,u=0);

matrix X = vX(Vh,Vh);
matrix Y = vX(Vh,Vh);

Mat Xp(X);
Mat Yp(Y);
Mat Zp;
real a=2.0;

Yp = a*Xp + Yp;

```

I see something called `MatAXPY` in PETSc doc but I don’t know if it is available in FreeFem ? (I see it in `PETSc-code.hpp`)  
I can do it with `matrix` format and then import the results in `Mat` but I loose the advantage of distributed matrices in large problem ?  
Thanks for your help

---

<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 23, 2021, 7:49pm UTC](https://community.freefem.org/t/x-a-y-with-petsc-matrix-mat/940/2 "2021-04-23T19:49:00Z")

</div>

You are in luck, this feature has been very recently added to FreeFEM `develop` branch, and it is available in FreeFEM 4.9 (released earlier this week).  
You can simply do:

```auto
Yp += a*Xp;

```

Let me know if you need something else.

---

<div class="post-metadata">

**Author:** ![Pmollo](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/pmollo/32/500_2.png) [@Pmollo](https://community.freefem.org/u/Pmollo)\
**Post date:** [April 26, 2021, 7:20am UTC](https://community.freefem.org/t/x-a-y-with-petsc-matrix-mat/940/3 "2021-04-26T07:20:15Z")

</div>

Thank you, I’ve install the 4.9 and now it works.
