# How to add values to a Matrix?

**URL:** <https://fenicsproject.discourse.group/t/how-to-add-values-to-a-matrix/5042>\
**Category:** I/O\
**Created:** [February 6, 2021, 10:57pm UTC](https://fenicsproject.discourse.group/t/how-to-add-values-to-a-matrix/5042 "2021-02-06T22:57:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PNPS](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@PNPS](https://fenicsproject.discourse.group/u/PNPS)\
**Post date:** [February 6, 2021, 10:57pm UTC](https://fenicsproject.discourse.group/t/how-to-add-values-to-a-matrix/5042/1 "2021-02-06T22:57:51Z")

</div>

In fenics 2018, I was able to do

```python
A = assemble(...)
A.add(local_tensor, local_to_global_map, local_to_global_map)\

```

but with fenics 2019 I now get the error “AttributeError: ‘dolfin.cpp.la.Matrix’ object has no attribute ‘add’”

Here is the full example:

```python
from __future__ import division
import numpy as np
import logging

from dolfin import (
    FunctionSpace,
    TestFunction,
    TrialFunction,
    assemble,
    assemble_local,
    cells,
    dx,
    grad,
    inner,
    UnitSquareMesh
)

mesh = UnitSquareMesh(8,8)

V = FunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v)) * dx
A = assemble(a)
A.zero()
dof_map = V.dofmap()
dof_coord = V.tabulate_dof_coordinates()

for cell in cells(mesh):
    local_to_global_map = dof_map.cell_dofs(cell.index())
    local_tensor = assemble_local(a, cell)

    A.add(local_tensor, local_to_global_map, local_to_global_map)
    A.apply("insert")

```

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![bhaveshshrimali](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bhaveshshrimali/32/124_2.png) [@bhaveshshrimali](https://fenicsproject.discourse.group/u/bhaveshshrimali)\
**Post date:** [February 7, 2021, 3:39am UTC](https://fenicsproject.discourse.group/t/how-to-add-values-to-a-matrix/5042/2 "2021-02-07T03:39:30Z")

</div>

You could try using the `petsc4py` API to add values to your matrix as suggested here: [Add Matrix to Assembled Matrix - #2 by kamensky](https://fenicsproject.discourse.group/t/add-matrix-to-assembled-matrix/122/2)

```auto
from __future__ import division
import numpy as np
import logging

from dolfin import *
from petsc4py import PETSc

mesh = UnitSquareMesh(2,2)

V = FunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v)) * dx
A = assemble(a)

mat = as_backend_type(A).mat()
b1 = A.array() # just for a quick comparison (you shouldn't do this)
A.zero()

dof_map = V.dofmap()
dof_coord = V.tabulate_dof_coordinates()
for cell in cells(mesh):
    local_to_global_map = dof_map.cell_dofs(cell.index())
    local_tensor = assemble_local(a, cell)

    mat.setValuesLocal(local_to_global_map.tolist(), local_to_global_map.tolist(), local_tensor, PETSc.InsertMode.ADD)
A.apply("insert")
mat.assemble()

b2 = A.array()
np.testing.assert_allclose(b1,b2) # True

```
