Dot product of two Tensors

Hello Everyone,

I want to compute the dot product of two Tensors. I don’t get an error message, but the result is wrong.
When I multiply an Identity-Tensor with a Tensor in which every entry is 1 I would expect the result to be the Identity tensor. When I implement this in numpy it does exactly that:

import numpy as np
I_np = np.identity(3)
ones_np = np.ones((3,3))
print('Numpy I*ones= \n',I_np*ones_np)

However, the result in Fenics is the “ones”-Tensor, I have tried it with “*”, dot(a,b) and also using indices. Can you explain to me what Iam doing wrong, and what is the right way to do this in fenics?
Thanks in advance!

Here is a MWE:

from fenics import *
from ufl import i,j,k,l

mesh = UnitCubeMesh(1,1,1)

I = Identity(3)
P_disp = VectorElement("CG", mesh.ufl_cell(), 2)  # quadratic element for displacement
V = FunctionSpace(mesh, P_disp)

WF = TensorFunctionSpace(mesh, "DG", degree=0, symmetry=True)

u = Function(V)
F = I+grad(u)

ones = as_matrix([[1, 1, 1], 
                  [1, 1, 1],
                  [1, 1, 1]])
ones_tensor = project(ones, WF)

# calculating I*ones(3,3) in different ways
prod = ones_tensor*F
dot_prod = dot(ones_tensor, F)
prod_indices = ones[i,j]*F[j,k]

# not the expected result
a = project(prod, form_compiler_parameters= {'quadrature_degree' : 4})
print('prod', a.vector()[:])
# not the expected result
b = project(dot_prod, form_compiler_parameters= {'quadrature_degree' : 4})
print('dot_prod', b.vector()[:])

# produces an error-Message:
# c = project(test, form_compiler_parameters= {'quadrature_degree' : 4})
# print('prod_indices', c.vector()[:])

You are confusing dot product with element by element product. When multiplying (dot product) any tensor with the identity you will get the tensor you had in the first place. In numpy you should do:

np.dot(I_np, ones_np)
1 Like

Thanks, you are right.
I think I am looking for the element by element product which I know implemented by hard coding it with

as_tensor[[I_np[0,0]*ones[0,0], [] , [],
...])