# Assembling a third order tensor

**URL:** <https://fenicsproject.discourse.group/t/assembling-a-third-order-tensor/1825>\
**Category:** Uncategorized\
**Created:** [November 2, 2019, 12:08pm UTC](https://fenicsproject.discourse.group/t/assembling-a-third-order-tensor/1825 "2019-11-02T12:08:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit112amit](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@amit112amit](https://fenicsproject.discourse.group/u/amit112amit)\
**Post date:** [November 2, 2019, 12:08pm UTC](https://fenicsproject.discourse.group/t/assembling-a-third-order-tensor/1825/1 "2019-11-02T12:08:52Z")

</div>

Suppose u and v are my trial and test functions from the P1 (1st order Lagrange) space. If \phi denotes the basis functions then

u = \sum\_j \phi\_jU\_j\quad\mathrm{and}\quad v=\hat{\phi\_i}

The mass matrix M and the stiffness matrix K are

M\_{ij} = \int\hat{\phi}\_i\phi\_j\mathrm{d}x\quad\mathrm{and}\quad K\_{ij} = \int\nabla\hat{\phi}\_i\cdot\nabla\phi\_j\mathrm{d}x

I can assemble the mass matrix and the stiffness matrix in `dolfin` as

```python
a_K = inner(grad(u), grad(v))*dx
a_M = u*v*dx

M = assemble(a_M)
K = assemble(a_K)

```

Now I have another term

∫f(x)uv\mathrm{d}x\quad\mathrm{where}\quad f = \sum\_k\phi\_kF\_k

To calculate this term as tensor product I require assembling the following rank 3 tensor

Q\_{ijk} = \int\hat{\phi}\_i\phi\_j\phi\_k\mathrm{d}x

How can I assemble this third-order tensor in `dolfin`? To see the context for this question please see [this](https://fenicsproject.discourse.group/t/avoiding-assembly-in-a-non-linear-mixed-finite-element-reaction-diffusion-problem/1823) question.
