# Unable to compile C++ code with dijitso, error in expression

**URL:** <https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818>\
**Category:** Uncategorized\
**Created:** [October 14, 2021, 8:30am UTC](https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818 "2021-10-14T08:30:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adnan](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@adnan](https://fenicsproject.discourse.group/u/adnan)\
**Post date:** [October 14, 2021, 8:30am UTC](https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818/1 "2021-10-14T08:30:55Z")

</div>

A= Expression(‘0.5\*(1+tanh(100\*(cos(0.4_pi )/sin(0.4_pi )- cos(pi_0.1_phi )/sin(pi_0.1_phi))))+1’,degree=2,pi=pi, phi=phi, element = V.ufl\_element())

This is how phi is defined.  
q,dq = Function(V),TestFunction(V)

```
pii,phi, qe = split(q)

```

I am a newbie may be I am asking very basic. The phi in the argument of cos and sin generating this error. phi is a function. I want to know how to insert phi in the expression.

---

<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:** [October 14, 2021, 12:13pm UTC](https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818/2 "2021-10-14T12:13:51Z")

</div>

Please add a full minimal working code example that reproduces the error message, and make sure that you use 3x` encapsulation to ensure appropriate formatting.

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [October 14, 2021, 6:03pm UTC](https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818/3 "2021-10-14T18:03:19Z")

</div>

You can index into a mixed `Function` in an `Expression` instead of passing the `Function` components returned by `split`, e.g.,

```python
from dolfin import *
mesh = UnitIntervalMesh(1)
V = VectorFunctionSpace(mesh,"CG",1,dim=2)
v = project(Constant((1,2)),V)
v1,v2 = split(v)

# Does not work:
#expr1 = Expression("v1",v1=v1,degree=1)
#expr2 = Expression("v2",v2=v2,degree=1)

# Works:
expr1 = Expression("v[0]",v=v,degree=1)
expr2 = Expression("v[1]",v=v,degree=1)

# Verify (should print 1 and 2):
print(assemble(expr1*dx(domain=mesh)))
print(assemble(expr2*dx(domain=mesh)))

```

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gebauer](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/emanuel_gebauer/32/3004_2.png) [@Emanuel\_Gebauer](https://fenicsproject.discourse.group/u/Emanuel_Gebauer)\
**Post date:** [November 9, 2021, 11:51am UTC](https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818/4 "2021-11-09T11:51:41Z")

</div>

Thank you, this solution worked for me @kamensky!

> [@kamensky](#):
>
> ```auto
> expr1 = Expression("v[0]",v=v,degree=1)
> expr2 = Expression("v[1]",v=v,degree=1)
> 
> ```

However I would change it a tiny bit to

```auto
expr1 = Expression("v",v=v.sub(0),degree=1)

```

This has the advantage, that the ‘0’ can easily exchanged by a variable, making a larger code more readable and easier to maintain.

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [November 9, 2021, 2:43pm UTC](https://fenicsproject.discourse.group/t/unable-to-compile-c-code-with-dijitso-error-in-expression/6818/5 "2021-11-09T14:43:37Z")

</div>

Whether this is more readable is perhaps subjective, but the following eliminates the redundant lines that differ only by index and is easier to generalize to arbitrary dimension:

```python
# Works:
#expr1 = Expression("v[0]",v=v,degree=1)
#expr2 = Expression("v[1]",v=v,degree=1)
d=2
expr = [Expression("v["+str(i)+"]",v=v,degree=1)
        for i in range(0,d)]

# Verify (should print 1 and 2):
#print(assemble(expr1*dx(domain=mesh)))
#print(assemble(expr2*dx(domain=mesh)))
for i in range(0,d):
    print(assemble(expr[i]*dx(domain=mesh)))

```
