How to build a callable symbolic expression into python code.

111 views
Skip to first unread message

sageuser

unread,
Oct 11, 2012, 7:46:44 AM10/11/12
to sage-s...@googlegroups.com
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

class C:

    def __init__(self):
         self.__f = SR(1) # An integer, for example. It could be "pi", etc.

    def f(self, x):
         return self.__f(x)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now the Sage session :

> from C import C
> c = C()

< c.f(2)
< ... 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 ?

Thanks.

sageuser

unread,
Oct 12, 2012, 6:10:30 AM10/12/12
to sage-s...@googlegroups.com
To elaborate...

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' ?

Thanks.


# -*- coding: utf-8 -*-

from sage.functions.piecewise import Piecewise
from sage.symbolic.constants import pi
from sage.symbolic.ring import var

class C:
 
   
def __init__(self):
        x
= var('x')
       
self.__f = lambda x: pi
       
self.__g = Piecewise((((0, 1), x**2), ((1, 2), lambda x: 10)))

   
def get_f_value(self, x):
       
return self.__f(x)

   
def get_g_value(self, x):
       
return self.__g(x)

   
def get_functions(self):
       
return (self.__f, self.__g)

   
def info(self):
       
print 'funct 1:', self.__f
       
print 'funct 2:', self.__g

Sage session :

> from C import C
> c = C()

> c.get_f_value(1), c.get_g_value(0.5), c.get_g_value(1.5) # OK, it works.
< (pi, 0.250000000000000, 10)

> c.info()
< funct 1: <function <lambda> at 0x4acef50>
< funct 2: Piecewise defined function with 2 parts, [[(0, 1), x^2], [(1, 2), <function <lambda> at 0x4acf848>]]

> c.get_functions()
< (<function <lambda> at 0x4acef50>, Piecewise defined function with 2 parts, [[(0, 1), x^2], [(1, 2), <function <lambda> at 0x4acf848>]])




Jason Grout

unread,
Oct 12, 2012, 6:21:24 AM10/12/12
to sage-s...@googlegroups.com
On 10/11/12 6:46 AM, sageuser wrote:
> *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.

You'd just have to do what the preparser does:

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

So take your symbolic expression S and do S.function(x).

For example:

sage: f = SR(1).function(x)
sage: f(4)
1

Thanks,

Jason


sageuser

unread,
Oct 12, 2012, 7:14:38 AM10/12/12
to sage-s...@googlegroups.com
Thank you !
Reply all
Reply to author
Forward
0 new messages