Dear all, I'm looking for a way to convert Sympy expressions into UFL expressions, where the symbols are Python functions. Any tips? Cheers Cjc
On 7 Oct 2014, at 21:34, Colin Cotter <colin.cotter@imperial.ac.uk> wrote:
Dear all, I'm looking for a way to convert Sympy expressions into UFL expressions, where the symbols are Python functions. Any tips?
The sympy expression printers are tree visitors. You could write one that visits the tree and produces a UFL tree. Or, ugly but simpler in the short term: stringify, replace the functions with the names of the appropriate UFL functions. eval the resulting string. urgh. i.e. x = Symbol('x') string = str(exp(-x**2)) from firedrake import * x = Function(V) ufl_expr = eval(string) Lawrence
Just to be clear, does eval being to ufl? Cjc On 7 Oct 2014 21:52, "Lawrence Mitchell" <lawrence.mitchell@imperial.ac.uk> wrote:
On 7 Oct 2014, at 21:34, Colin Cotter <colin.cotter@imperial.ac.uk> wrote:
Dear all, I'm looking for a way to convert Sympy expressions into UFL expressions, where the symbols are Python functions. Any tips?
The sympy expression printers are tree visitors. You could write one that visits the tree and produces a UFL tree.
Or, ugly but simpler in the short term:
stringify, replace the functions with the names of the appropriate UFL functions. eval the resulting string. urgh.
i.e.
x = Symbol('x')
string = str(exp(-x**2))
from firedrake import *
x = Function(V)
ufl_expr = eval(string)
Lawrence
_______________________________________________ firedrake mailing list firedrake@imperial.ac.uk https://mailman.ic.ac.uk/mailman/listinfo/firedrake
On 7 Oct 2014, at 22:21, Colin Cotter <colin.cotter@imperial.ac.uk> wrote:
Just to be clear, does eval being to ufl?
Presuming you mean belong. No, it's Python that evaluates a string in the current environment. Eg x=5 print eval("x") => 5
On 7 Oct 2014 21:52, "Lawrence Mitchell" <lawrence.mitchell@imperial.ac.uk> wrote:
On 7 Oct 2014, at 21:34, Colin Cotter <colin.cotter@imperial.ac.uk> wrote:
Dear all, I'm looking for a way to convert Sympy expressions into UFL expressions, where the symbols are Python functions. Any tips?
The sympy expression printers are tree visitors. You could write one that visits the tree and produces a UFL tree.
Or, ugly but simpler in the short term:
stringify, replace the functions with the names of the appropriate UFL functions. eval the resulting string. urgh.
i.e.
x = Symbol('x')
string = str(exp(-x**2))
from firedrake import *
x = Function(V)
ufl_expr = eval(string)
Lawrence
_______________________________________________ firedrake mailing list firedrake@imperial.ac.uk https://mailman.ic.ac.uk/mailman/listinfo/firedrake
firedrake mailing list firedrake@imperial.ac.uk https://mailman.ic.ac.uk/mailman/listinfo/firedrake
On 07/10/14 22:27, Lawrence Mitchell wrote:
On 7 Oct 2014, at 22:21, Colin Cotter <colin.cotter@imperial.ac.uk <mailto:colin.cotter@imperial.ac.uk>> wrote:
Just to be clear, does eval being to ufl?
Presuming you mean belong. No, it's Python that evaluates a string in the current environment.
Eg
x=5 print eval("x") => 5
Note that eval only works for *right-hand side expressions* i.e. the following yields a SyntaxError: eval("x = 5") For such code strings or multi-line statements you need to use exec. Aside: in Python 2, eval is a __builtin__ function, whereas eval is a statement - that has been fixed in Python 3
On 7 Oct 2014 21:52, "Lawrence Mitchell" <lawrence.mitchell@imperial.ac.uk <mailto:lawrence.mitchell@imperial.ac.uk>> wrote:
On 7 Oct 2014, at 21:34, Colin Cotter <colin.cotter@imperial.ac.uk <mailto:colin.cotter@imperial.ac.uk>> wrote:
> Dear all, > I'm looking for a way to convert Sympy expressions into UFL expressions, where the symbols are Python functions. Any tips?
The sympy expression printers are tree visitors. You could write one that visits the tree and produces a UFL tree.
Or, ugly but simpler in the short term:
stringify, replace the functions with the names of the appropriate UFL functions. eval the resulting string. urgh.
i.e.
x = Symbol('x')
string = str(exp(-x**2))
from firedrake import *
x = Function(V)
ufl_expr = eval(string)
Lawrence
Great, thanks. I'm using this to properly evaluate the hedgehog map to get second order convergence. Will try this out on Thursday. all the best --cjc On 07/10/14 22:27, Lawrence Mitchell wrote:
On 7 Oct 2014, at 22:21, Colin Cotter <colin.cotter@imperial.ac.uk <mailto:colin.cotter@imperial.ac.uk>> wrote:
Just to be clear, does eval being to ufl?
Presuming you mean belong. No, it's Python that evaluates a string in the current environment.
Eg
x=5 print eval("x") => 5
On 7 Oct 2014 21:52, "Lawrence Mitchell" <lawrence.mitchell@imperial.ac.uk <mailto:lawrence.mitchell@imperial.ac.uk>> wrote:
On 7 Oct 2014, at 21:34, Colin Cotter <colin.cotter@imperial.ac.uk <mailto:colin.cotter@imperial.ac.uk>> wrote:
> Dear all, > I'm looking for a way to convert Sympy expressions into UFL expressions, where the symbols are Python functions. Any tips?
The sympy expression printers are tree visitors. You could write one that visits the tree and produces a UFL tree.
Or, ugly but simpler in the short term:
stringify, replace the functions with the names of the appropriate UFL functions. eval the resulting string. urgh.
i.e.
x = Symbol('x')
string = str(exp(-x**2))
from firedrake import *
x = Function(V)
ufl_expr = eval(string)
Lawrence
_______________________________________________ firedrake mailing list firedrake@imperial.ac.uk <mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake
_______________________________________________ firedrake mailing list firedrake@imperial.ac.uk <mailto:firedrake@imperial.ac.uk> https://mailman.ic.ac.uk/mailman/listinfo/firedrake
_______________________________________________ firedrake mailing list firedrake@imperial.ac.uk https://mailman.ic.ac.uk/mailman/listinfo/firedrake
participants (4)
- 
                
                Colin Cotter
- 
                
                Florian Rathgeber
- 
                
                Lawrence Mitchell
- 
                
                Lawrence Mitchell