This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Any possiblity for Expression Object to take in python expression instead of c++ expression?

0 votes

Hello! everyone,

I have a long list of python expression, say:

listexpr = [   x[0]+1, x[1]**2 + x[0]*x[1],  ...... ]

And I want to use each of the expression in the list as follows:

coefficient = Expression( str(listexpr[1]),  degree=2) )

Since ** is not valid in c++ expression, it returns error. I want to know if there is way to make my "coefficient" a valid Expression object in Fenics, without one by one modification of my original long list?

Thank you in advance!

asked May 31, 2017 by chen636489 FEniCS Novice (220 points)

1 Answer

0 votes
 
Best answer

Hello!

I have found a way.

import sympy as sp
from dolfin import *

x, y, z = sp.symbols('x[0] x[1] x[2]')
u = x2 + 2*y2 + 3*z**2
sp.printing.ccode(u)

Thank you again. This is a great platform for Fenics.

answered May 31, 2017 by chen636489 FEniCS Novice (220 points)
edited Jun 6, 2017 by chen636489
...