Human readable output of numerical values of tensors

Dear all,

trying to implement a spectral decomposition in UFL, I would like to test the parts of my code separately.
That’s why I am wondering if there is a way to enforce printing the numerical values of UFL objects such as tensors in a human readable format.

Concrete example: I want to test the implementation of the functions te_plus(A)+te_minus(A) calculating the positive and negative part of the tensor A (see below):

A = as_tensor(((1,1),(0,1)))
print(A)

A_reass=te_plus(A)+te_minus(A)
print(A_reass)

However, the second print command does not give the expected result (which should be the same as the first command’s output) but something not understandable:

]) + ({ A | A_{i_{16}, i_{17}} = -1 * ({ A | A_{i_{14}, i_{15}} = 1.00000001 * I[i_{14}, i_{15}] })[i_{16}, i_{17}] }))[i_{18}, i_{19}] })[i_{22}, i_{23}] })

functions to test (not efficent yet, by the way…)

from dolfin import *

nuz = 5E-12 #numerical zero
rd = 2 #space dimension

numturb=1E-8 # numerical perturbations to be applied in case of equal eigenvalues
→ assuming 2d problem !

#eigenvalues
def eval1(te):
return 0.5*(te[1,1]+te[0,0]+sqrt(pow(te[0,0],2)-2.0*te[0,0]te[1,1]+4.0te[0,1]*te[1,0]+pow(te[1,1],2))) #first eigenvalue

def eval2(te):
return 0.5*(te[1,1]+te[0,0]-sqrt(pow(te[0,0],2)-2.0*te[0,0]te[1,1]+4.0te[0,1]*te[1,0]+pow(te[1,1],2))) #second eigenvalue

#positive and negative part
def te_plus(te):
eva1=eval1(te)+numturb
eva2=eval2(te)-numturb

#projection tensors
p1=(1.0/(eva1-eva2))(te-eva2Identity(rd))
p2=(1.0/(eva2-eva1))(te-eva1Identity(rd))

#“positive part” of eigenvalues
eva1_pos = (abs(eva1)+eva1)/2
eva2_pos = (abs(eva2)+eva2)/2

#assemble tensor’s positive part
return eva1_posp1+eva2_posp2

def te_minus(te):
eva1=eval1(te)+numturb
eva2=eval2(te)-numturb

#projection tensors
p1=(1.0/(eva1-eva2))(te-eva2Identity(rd))
p2=(1.0/(eva2-eva1))(te-eva1Identity(rd))

#“negative part” of eigenvalues
eva1_neg = (abs(eva1)-eva1)/2
eva2_neg = (abs(eva2)-eva2)/2

#assemble tensor’s negative part
return eva1_negp1+eva2_negp2

Please format the code so that it can be pasted and run easily. That said, you should not expect the two print statements to yield the same result. UFL is in general lazy and does not evaluate subexpressions. So the representation of the objects will be different. However, what should matter for you is if they are representations of the same (mathematical) object. Consider

from dolfin import *

A = as_tensor(((1, 2), (3, 4)))
print(A)

B = as_tensor(((1*Constant(1), 2), (3, 4)))
print(B)  # Different representation; B is 'written' differently

error = inner(A-B, A-B)*dx(domain=UnitSquareMesh(4, 4))
print(assemble(error))  # B is the same thing as A

Thanks a lot for your answer.

Actually, I don’t know how to format the code such can be run easily yet. %(
Could you give me a tip for formatting my posts, please ?

Hi, for python syntax highlighting you enclose the code block as follows

 ````python
print 1
````

This is then rendered as

print 1

See this thread for more info.