# Expression in equation

**URL:** <https://fenicsproject.discourse.group/t/expression-in-equation/640>\
**Category:** variational formulation\
**Created:** [April 21, 2019, 8:50am UTC](https://fenicsproject.discourse.group/t/expression-in-equation/640 "2019-04-21T08:50:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![111](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/111/32/104_2.png) [@111](https://fenicsproject.discourse.group/u/111)\
**Post date:** [April 21, 2019, 8:50am UTC](https://fenicsproject.discourse.group/t/expression-in-equation/640/1 "2019-04-21T08:50:09Z")

</div>

Good day! I have the next question: Is it any difference between putting in the equation expression and expression, interpolated on function space (i.e. function)? Example bellow. Thank you!

```
from dolfin import *

mesh = UnitSquareMesh(30, 30)
V = FunctionSpace(mesh, "CG", 1)

def boundary(x, on_boundary):
    return on_boundary

u_D = Expression('1 + x[0] * x[0] + 2 * x[1] * x[1]', degree=2)
bc = DirichletBC(V, u_D, boundary)

u = TrialFunction(V)
v = TestFunction(V)

f = Expression('-6.0', degree=1)
# f = interpolate(Expression('-6.0', degree=1), V) Is it matter?

a = dot(grad(u), grad(v)) * dx
L = f * v * dx

u = Function(V)
solve(a == L, u, bc)
```

---

<div class="post-metadata">

**Author:** ![bin-mech](https://avatars.discourse-cdn.com/v4/letter/b/ba9def/32.png) [@bin-mech](https://fenicsproject.discourse.group/u/bin-mech)\
**Post date:** [April 22, 2019, 3:50pm UTC](https://fenicsproject.discourse.group/t/expression-in-equation/640/2 "2019-04-22T15:50:15Z")

</div>

There is no need to interpolate the expression in your case.  
The function `interpolate` performs the interpolation of a given function upon a given finite element space.

PS. For the constant expression, the degree is 0 .

---

<div class="post-metadata">

**Author:** ![111](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/111/32/104_2.png) [@111](https://fenicsproject.discourse.group/u/111)\
**Post date:** [April 28, 2019, 9:05am UTC](https://fenicsproject.discourse.group/t/expression-in-equation/640/3 "2019-04-28T09:05:28Z")

</div>

Ok, I provided not the best example. But in general case, is it any difference between putting in the equation expression and expression, interpolated on function space (i.e. function)?

---

<div class="post-metadata">

**Author:** ![michalhabera](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/michalhabera/32/9_2.png) [@michalhabera](https://fenicsproject.discourse.group/u/michalhabera)\
**Post date:** [May 2, 2019, 1:51pm UTC](https://fenicsproject.discourse.group/t/expression-in-equation/640/4 "2019-05-02T13:51:42Z")

</div>

These two approaches should produce the same result.  
There is a little difference in performance.

Expression() approach will interpolate the c++ string locally for each cell during assembly, so it doesn’t need to store the whole function in memory. There are layers of function calls around, so this approach could be slightly less performant as the latter.

On the other hand, interpolation beforehand stores the intermediate result as a function, so needs more memory for storage, but computation time will be spent outside assembly. I would prefer this approach, memory requirements for additional function is almost never the problem.

Even better approach is to put the expression written in symbolic UFL language terms into finite element form directly. This means `-6.0 * v * dx` for your simple case. But UFL provides plenty of function, so c++ string expression could be almost always avoided.

---

<div class="post-metadata">

**Author:** ![111](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/111/32/104_2.png) [@111](https://fenicsproject.discourse.group/u/111)\
**Post date:** [May 2, 2019, 4:57pm UTC](https://fenicsproject.discourse.group/t/expression-in-equation/640/5 "2019-05-02T16:57:17Z")

</div>

Thank you very much for the detailed response!
