|How to build a callable symbolic expression into python code.||sageuser||10/11/12 4:46 AM|
Hello Sage team.
I would like to build callable symbolic expression into some python code file an load (import) it. Since it's before Sage parse it, expression like f(x) = x**2 is of course forbidden.
Look at this code :
# -*- coding: utf-8 -*-
from sage.symbolic.expression_conversions import SR
self.__f = SR(1) # An integer, for example. It could be "pi", etc.
def f(self, x):
Now the Sage session :
> from C import C
> c = C()
< ... ValueError: the number of arguments must be less than or equal to 0 ...What tool can I use to convert this constant an make it a constant function ?
|Re: How to build a callable symbolic expression into python code.||sageuser||10/12/12 3:10 AM|
I've half solved my problem, but here is a more detailled example. I would like the user to have the possibility to get (get_functions method) and to see (info method) the explicit expressions of the created functions into the class, but <lambda + adress> is somewhat cryptic. The problem is now : how can I convert a lamba expression into a : 'sage.symbolic.expression.Expression' ?
> c.get_f_value(1), c.get_g_value(0.5), c.get_g_value(1.5) # OK, it works.
< (pi, 0.250000000000000, 10)
< funct 1: <function <lambda> at 0x4acef50> < funct 2: Piecewise defined function with 2 parts, [[(0, 1), x^2], [(1, 2), <function <lambda> at 0x4acf848>]]
< (<function <lambda> at 0x4acef50>, Piecewise defined function with 2 parts, [[(0, 1), x^2], [(1, 2), <function <lambda> at 0x4acf848>]])
|Re: How to build a callable symbolic expression into python code.||Jason Grout||10/12/12 3:21 AM|
On 10/11/12 6:46 AM, sageuser wrote:
> *Hello Sage team.
>You'd just have to do what the preparser does:
'__tmp__=var("x"); f = symbolic_expression(x**Integer(2)).function(x)'
So take your symbolic expression S and do S.function(x).
sage: f = SR(1).function(x)
|Re: How to build a callable symbolic expression into python code.||sageuser||10/12/12 4:14 AM|
Thank you !