Taking the exponent of each entry in a Tensor

Hello Everyone,

I want to take the natural exponent of each component of a Tensor-Function, e.g. the deformation gradient.
With ufl.exp() this is not possible, since it requires a scalar. I was wondering, if there is a clever way to do this.

So far my only idea is to project my Tensor, take the exponent with numpy.exp() and then somehow transform it back to a tensor (still have to figure out the last step). But I fear that this is quite time consuming for larger meshes.

Do you have an idea to do this more efficiently? I appreciate your help!

Here is a MWE:


from fenics import *
import numpy as np

### Create mesh and define function space
mesh = BoxMesh(Point(0, 0, 0), Point(1, 1, 1), 4, 4, 4)
P = VectorElement("CG", mesh.ufl_cell(), 2)
V = FunctionSpace(mesh, P)
u = Function(V)

I = Identity(V.mesh().geometry().dim()) 
F = I + grad(u) # deformation gradient

### lets say, I want to take the exponent of every entry of F

# exp(F) # does not work

# Idea:
tmp = project(F, form_compiler_parameters= {'quadrature_degree' : 4})
tmp_vec = tmp.vector()[:]
tmp_vec_exp = np.exp(tmp_vec)
# next step: transforme it back into a 3x3 Tensor

use something as exp_F = as_matrix([[exp(F[0,0], exp(F[0, 1]), ...], ...])

Thank you for your fast reply, this is what I was looking for! :slight_smile:

Hi @Martin1,

I wrote a function here that automates this type of element-wise operation for functions of arbitrary shape so that you don’t have to type all the components of the tensor. Hope you find this helpful.

Thanks, I will look into it! :slight_smile: