# How to operate on a fem.Function and maintain type/attributes without interpolating?

**URL:** <https://fenicsproject.discourse.group/t/how-to-operate-on-a-fem-function-and-maintain-type-attributes-without-interpolating/10477>\
**Category:** General\
**Created:** [February 20, 2023, 6:36pm UTC](https://fenicsproject.discourse.group/t/how-to-operate-on-a-fem-function-and-maintain-type-attributes-without-interpolating/10477 "2023-02-20T18:36:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dg0609](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@dg0609](https://fenicsproject.discourse.group/u/dg0609)\
**Post date:** [February 20, 2023, 6:36pm UTC](https://fenicsproject.discourse.group/t/how-to-operate-on-a-fem-function-and-maintain-type-attributes-without-interpolating/10477/1 "2023-02-20T18:36:40Z")

</div>

Hello,

Quick question here. I want to operate on a fem.Function of a vector function space, like matrix multiplication, and return a function in the same space. A similar question is [https://fenicsproject.discourse.group/t/dolfinx-fem-function-function-add-with-dolfinx-fem-function-function/9328](https://fenicsproject.discourse.group/t/dolfinx-fem-function-function-add-with-dolfinx-fem-function-function/9328), however, it was a simple addition operation of another function or interpolation. I have a simple example below, and I’d like for it to work without interpolation, if possible.

Thank you.

```auto
from dolfinx import mesh, fem
import ufl

nx, ny = 10,10
domain = mesh.create_rectangle(comm=MPI.COMM_WORLD, points=((-1., -1.), (1., 1.)),n=(nx, ny), cell_type=mesh.CellType.triangle)

# Vector function space
W = fem.VectorFunctionSpace(domain, ("CG",1), dim = 4)

# create a function
u1 = fem.Function(W)

# create a function modified from u1
u2 = ufl.Identity(4)*u1 # <--- Returns a different type than u1

# try to access u2's DOFs... Attribute error
u2.x.array

```

The above returns the error

```auto
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[24], line 15
     12 u2 = ufl.Identity(4)*u1 # <--- Returns a different type than u1
     14 # try to access u2's DOFs
---> 15 u2.x.array

AttributeError: 'ComponentTensor' object has no attribute 'x'

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 20, 2023, 7:36pm UTC](https://fenicsproject.discourse.group/t/how-to-operate-on-a-fem-function-and-maintain-type-attributes-without-interpolating/10477/2 "2023-02-20T19:36:55Z")

</div>

Note that

> [@dg0609](#):
>
> `u2 = ufl.Identity(4)*u1`

Is a `ufl-expression`. To map a UFL expression to a function space, you need to use interpolate.  
Specifically, you need to use the `u2expr=dolfinx.fem.Expression(u2, W.element.interpolation_points())`  
and  
`u3 = Function(W)`  
`u3.interpolate(u2expr)`.

Is there a specific reason for trying to avoid interpolation?

And what is the actual matrix you want to use?

---

<div class="post-metadata">

**Author:** ![dg0609](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@dg0609](https://fenicsproject.discourse.group/u/dg0609)\
**Post date:** [February 20, 2023, 8:02pm UTC](https://fenicsproject.discourse.group/t/how-to-operate-on-a-fem-function-and-maintain-type-attributes-without-interpolating/10477/3 "2023-02-20T20:02:30Z")

</div>

Thank you. I want to avoid interpolating because I do not want to lose any information/accuracy, and I do not know enough about `*.interpolate()` to know if that will happen.

The actual matrix is a change of variables matrix. So it would be a function of combinations of `a,b,c,d = ufl.split(u1)`.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 20, 2023, 9:08pm UTC](https://fenicsproject.discourse.group/t/how-to-operate-on-a-fem-function-and-maintain-type-attributes-without-interpolating/10477/4 "2023-02-20T21:08:07Z")

</div>

If your matrix is a function of a,b,c,d then each component will be a product of a_a,a_b, a_c, b_a, b\*b, etc.  
This function is in a higher function space than your original function, and thus it should either:

1. be kept as an ufl expression, and sent into the appropriate variational form.
2. interpolated into a higher order function space, where these products are included.

In general, you choose the accuracy of your interpolation, as you can chose the space to interpolate into.
