# Usage of PETSc's 'Mat' type

**URL:** https://community.freefem.org/t/usage-of-petscs-mat-type/835
**Category:** General Discussion
**Created:** [March 5, 2021, 8:09am UTC](https://community.freefem.org/t/usage-of-petscs-mat-type/835 "2021-03-05T08:09:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 5, 2021, 8:09am UTC](https://community.freefem.org/t/usage-of-petscs-mat-type/835/1 "2021-03-05T08:09:38Z")

</div>

Hi all,

I am new to FreeFEM and I am figuring out how to use FreeFEM with the PETSc interface. Now I am learning about the Mat type of the PETSc package, but I do not fully understand all the functions that come with it. I am following the tutorial with examples by Pierre Jolivet ([this one](http://jolivet.perso.enseeiht.fr/FreeFem-tutorial/)) and in the tutorial there are two specific opertations of which I am not sure what it does. The first one is  
`real[int] v;`  
`Mat J;`  
`J(v, v);`

What does the J(v, v) operation do? (and is there any place where I could read about this?) I saw in the tutorial something about the weighted dot product, but what would determine the weight?

The second one is a way of creating a new mat as:  
`Mat A, C;`  
`matrix Loc;`  
`Mat B(A, C, Loc);`

Again, what does this do? And how can I find out what this does? Is there any documentation on this or a way of searching for it in the source code?  
Thanks in advance for any help. If anyone has any other good tutorials that can help me in understanding the FreeFEM-PETSc interface please let me know.

Kind regards,

Koen

---

<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: [March 6, 2021, 11:32am UTC](https://community.freefem.org/t/usage-of-petscs-mat-type/835/2 "2021-03-06T11:32:55Z")

</div>

1. `J(v, v)` computes a scalar product according to the domain decomposition of `J`. It is basically a weighted local scalar product, followed by a reduction. See line 43 of [this](http://jolivet.perso.enseeiht.fr/FreeFem-tutorial/section_8/example4.edp.html) parallel example vs. line 28 of [this](http://jolivet.perso.enseeiht.fr/FreeFem-tutorial/section_8/example4_seq.edp.html) sequential example.
2. Given one distributed matrix `Mat A`, you can create a new distributed matrix `B` following the same column and row distribution using a local matrix `Loc` with the syntax `Mat B(A, Loc)`. If you have a different column and row distribution, here `A` for the rows, `C` for the columns, and `Loc` is a rectangular matrix, you create a distributed rectangular matrix using the syntax `Mat B(A, C, Loc)`. See [this](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/stokes-block-2d-PETSc.edp#L52) Stokes example where there is a different column and row distribution (for velocity and pressure unknowns).

Here is some more materials if you are interested: [Pierre Jolivet introduction to parallel FreeFEM part 1 - YouTube](https://www.youtube.com/watch?v=-Aw2O46V2bo) + [Pierre Jolivet introduction to parallel FreeFEM part 2 - YouTube](https://www.youtube.com/watch?v=Hx57gxTOnvE).  
Don’t hesitate if you have other questions.

---

<div class="post-metadata">

### Author: ![koenstrien](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@koenstrien](https://community.freefem.org/u/koenstrien)
#### Post date: [March 8, 2021, 3:28pm UTC](https://community.freefem.org/t/usage-of-petscs-mat-type/835/3 "2021-03-08T15:28:54Z")

</div>

Thank you for the swift response. This helps me out in further understanding the language!
