Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Alternate answer : use the .function() method of a expression :

sage: str="x**3"
sage: g=eval(str).function(x)
sage: g
x |--> x^3
sage: g(3)
27

Alternate to this alternate : ûexec (you will also need preparse to be able to define a symbolic function) :

sage: str2="h(x)=x**2"
sage: exec(preparse(str2))
sage: h
x |--> x^2
sage: h(3)
9

BTW :

sage: preparse(str2)
'__tmp__=var("x"); h = symbolic_expression(x**Integer(2)).function(x)'

HTH,

Alternate answer : use the .function() method of a expression :

sage: str="x**3"
sage: g=eval(str).function(x)
sage: g
x |--> x^3
sage: g(3)
27

Alternate to this alternate : ûexecexec (you will also need preparse to be able to define a symbolic function) :

sage: str2="h(x)=x**2"
sage: exec(preparse(str2))
sage: h
x |--> x^2
sage: h(3)
9

BTW :

sage: preparse(str2)
'__tmp__=var("x"); h = symbolic_expression(x**Integer(2)).function(x)'

HTH,