# From python function to UFL expression

**URL:** <https://fenicsproject.discourse.group/t/from-python-function-to-ufl-expression/11810>\
**Category:** dolfinx\
**Created:** [July 24, 2023, 6:08pm UTC](https://fenicsproject.discourse.group/t/from-python-function-to-ufl-expression/11810 "2023-07-24T18:08:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Mark](https://fenicsproject.discourse.group/u/Mark)\
**Post date:** [July 24, 2023, 6:08pm UTC](https://fenicsproject.discourse.group/t/from-python-function-to-ufl-expression/11810/1 "2023-07-24T18:08:41Z")

</div>

I fear this post is poor even for my low standards, but for the life of me I could not find a solution after quite a search.  
I would like to understand a bit more about UFL, for one how to translate a function to an UFL expression.  
Looking for example at the [Poisson Tutorial v4.0](https://github.com/jorgensd/dolfinx-tutorial/blob/v0.4.0/chapter1/membrane_code.ipynb), I see this code snipped

```auto
import ufl
from petsc4py.PETSc import ScalarType
x = ufl.SpatialCoordinate(domain)
beta = fem.Constant(domain, ScalarType(12))
R0 = fem.Constant(domain, ScalarType(0.3))
p = 4 * ufl.exp(-beta**2 * (x[0]**2 + (x[1] - R0)**2))

```

to to define a Gaussian f source term.  
What to do if I wanted my source term to be say a polynomial?  
I search for an `UFL` method, could find all sorts of methods `ufl.asin`, `ufl.bessel ` but not anything I could use.  
I fear even the cheap hack of using for example

```auto
ufl.ln(ufl.exp(x[0]**2))

```

does not work, as the type of the latter expression is different from the one of the`ufl.exp()` expression above (also mysterios to me yet).

Any hints please to go from a python function to UFL, thanks a lot for your patience

---

<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:** [July 24, 2023, 6:21pm UTC](https://fenicsproject.discourse.group/t/from-python-function-to-ufl-expression/11810/2 "2023-07-24T18:21:08Z")

</div>

Why not just use `p=x[0]**2`?

---

<div class="post-metadata">

**Author:** ![Mark](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Mark](https://fenicsproject.discourse.group/u/Mark)\
**Post date:** [July 24, 2023, 6:24pm UTC](https://fenicsproject.discourse.group/t/from-python-function-to-ufl-expression/11810/3 "2023-07-24T18:24:15Z")

</div>

Oh it was that easy, thank you very much. I though anything that is used later to define the weak form but be somehow ufl-compliant. Is there then a particular reason to use the ufl.exp method in the tutorial, instead of say just defininf f as a function with np.exp()? Doesnot one lose some features with the approach you suggest, such e.g. ufl derivation and so, if ever needed? Thanks

---

<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:** [July 24, 2023, 6:29pm UTC](https://fenicsproject.discourse.group/t/from-python-function-to-ufl-expression/11810/4 "2023-07-24T18:29:36Z")

</div>

`x` is defined as a ufl object, ie

> [@Mark](#):
>
> ```auto
> x = ufl.SpatialCoordinate(domain)
> 
> ```

This means that it is interpeted as the physical coordinate in the physical cell at evaluation, which means that during assembly it is equal to the quadrature points on the physical cell (not reference element), giving exact integration of the quadrature degree is ser suffficiently high for polynomials.

Ive talked more about this at: [Numerical values from ufl.SpatialCoordinate - #2 by dokken](https://fenicsproject.discourse.group/t/numerical-values-from-ufl-spatialcoordinate/11064/2)
