# About new sparse matrix structure

**URL:** https://community.freefem.org/t/about-new-sparse-matrix-structure/88
**Category:** General Discussion
**Created:** [June 28, 2019, 8:07am UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88 "2019-06-28T08:07:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![e-aranda](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/e-aranda/32/59_2.png) [@e-aranda](https://community.freefem.org/u/e-aranda)
#### Post date: [June 28, 2019, 8:07am UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88/1 "2019-06-28T08:07:33Z")

</div>

I had a c++ code linked with FreeFem version 3, in which I built a matrix using

```
MatriceMorse<double> * ret = new MatriceMorse<double>(np,np,newnnz,false,newa,newlg,newcl);

```

However, in version 4, I get:

```
error: no matching function for call to ‘HashMatrix<int, double>::HashMatrix(int&, int&, int&, bool, double*&, int*&, int*&)’

```

How can I adapt my code to the new sparse matrix structure?

---

<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: [June 28, 2019, 8:33am UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88/2 "2019-06-28T08:33:08Z")

</div>

The new matrix have completely change

the new code can be like :

```auto
MatriceMorse<double> * ret = new MatriceMorse<double>(np);
// build 
for (int k=0; k< newnnz; ++k);
{
 int i = 
 int j = 
 double aij = 
  (*ret)( i,j) = aijj;
}

       sparce_mat->Uh = UniqueffId();
	sparce_mat->Vh = UniqueffId();
	sparce_mat->A.master(ret);
        sparce_mat->typemat = 0;

return sparce_mat;

```

---

<div class="post-metadata">

### Author: ![e-aranda](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/e-aranda/32/59_2.png) [@e-aranda](https://community.freefem.org/u/e-aranda)
#### Post date: [July 2, 2019, 4:32pm UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88/3 "2019-07-02T16:32:13Z")

</div>

Sorry @frederichecht but I don’t understand very well what I have to do.  
My code was very similar to that you wrote, except that MatriceMorse had to be created using the usual arrays [I,J,A] of sparse matrices. I mean, before I wrote

```auto
MatriceMorse<double> * ret = new MatriceMorse<double>(np,np,newnnz,false,newa,newlg,newcl);

```

where A = newa, I=newlg and J=newcl.

And now, I have to write:

```auto
   MatriceMorse<double> * ret = new MatriceMorse<double>(np);
    for (int k=0; k< newnnz; ++k)
 		(*ret)( newlg[k],newcl[k]) = newa[k];
 

```

but it does not work. What I am doing wrong?

---

<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: [July 3, 2019, 4:42pm UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88/4 "2019-07-03T16:42:00Z")

</div>

No because your previous matrix is a CSR format so your core is wrong

> ```auto
> MatriceMorse<double> * mA = new MatriceMorse<double>(np);
> 
> ```

```auto

```

mA-\>j=0;

mA-\>p=0;

mA-\>aij=0;

```
mA->set(np,np,0,newnnz,newlg,newcl,newa,1,1);

```

remarque the army newlg, ewel, new can be freed after.

---

<div class="post-metadata">

### Author: ![e-aranda](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/e-aranda/32/59_2.png) [@e-aranda](https://community.freefem.org/u/e-aranda)
#### Post date: [July 4, 2019, 2:28pm UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88/5 "2019-07-04T14:28:15Z")

</div>

Thanks @frederichecht, just a little remark,  
`mA->set(np,np,0,newnnz,newlg,newcl,newa,1,1);`  
gives an error:  
`free(): invalid next size (normal)`

I need to put  
`mA->set(np,np,0,newnnz,newlg,newcl,newa,0,1);`  
and it seems ok.

---

<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: [July 4, 2019, 2:52pm UTC](https://community.freefem.org/t/about-new-sparse-matrix-structure/88/6 "2019-07-04T14:52:51Z")

</div>

ok, sorry for the mistake.
