Ask Your Question
0

callable symbolic expression from python script

asked 2011-06-01 09:21:15 -0500

tdstephens3 gravatar image

updated 2011-06-16 07:59:48 -0500

Kelvin Li gravatar image

I am trying to obtain a callable symbolic expression from a python script and having a hard time of it.

In my python script I would like to have something along the lines of:

func = myFunc()
func_evaluated_at_3 = myFunc(3)

func_dx = func.differentiate()
func_dx_evaluated_at_3 = func_dx(3)

In the module defining myFunc I was hoping to have something along the lines of:

import sage.all as sage

class myFunc:

   def __init__(self):

      sage.var('x')
      self.expression = x + 1
      # maybe this should be: self.expression = sage.preparse("f(x) = x + 1")?

   def __call__(self,value):
       return self.expression(value)

   def differentiate(self):
      return self.expression.diff(x)

The goal is to keep the client code that constructs, calls, and manipulates objects of type myFunc from knowing about Sage. Perhaps I am misguided, any help would be appreciated. (also, help with the appropriate tags would be, well, helpful)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2011-06-01 09:59:44 -0500

Kelvin Li gravatar image

I am not sure whether your overall goal is worth pursuing. From the surface, it looks like MyFunc is becoming a clone of sage.symbolic.expression.Expression. Additionally, Sage has a deprecation policy: old code must be supported for at least twelve months with deprecation warnings before Sage can break it. For most purposes, I suggest keeping Sage up to date and continually testing whether your code is using a deprecated interface.

Just for reference, your example can be done in Sage as follows:

Using a symbolic function:

sage: func(x) = function('func', x)
sage: func
x |--> func(x)
sage: func(3)
func(3)
sage: func.differentiate(x)
x |--> D[0](func)(x)
sage: func.differentiate(x)(3)
D[0](func)(3)

Using a concrete function:

sage: func(x) = sinh(x) * sqrt(x)
sage: func
x |--> sqrt(x)*sinh(x)
sage: func(3)
sqrt(3)*sinh(3)
sage: RR(func(3))
17.3514683581443
sage: func.differentiate(x)
x |--> sqrt(x)*cosh(x) + 1/2*sinh(x)/sqrt(x)
sage: RR(func.differentiate(x)(3))
20.3296134831414

As far as I know, the above functionality has been around for a while without any changes, so I would not be too concerned about the stability of Sage's interface.

edit flag offensive delete link more

Comments

I agree that MyFunc is a clone of sage.symbolic.expression.Expression, but I *think* that is what I want here. I am trying to have these symbolic expressions available to a larger system written in python... I am rewriting a large amount of OO Matlab code in python. For better or worse, I am being guided by my earlier design patterns. Is there a deprecation issue with the code I have written?

tdstephens3 gravatar imagetdstephens3 ( 2011-06-01 10:15:37 -0500 )edit

From my very limited knowledge, I think the parts you are using are quite stable. Have fun porting!

Kelvin Li gravatar imageKelvin Li ( 2011-06-01 10:32:20 -0500 )edit
0

answered 2011-06-01 09:48:26 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Alright, I guess it works this way sometimes (ask question, five minutes later figure out the answer!)

Using the information here (Sage tutorial), we can build a CallableSymbolicExpression. Here's what I did for this specific example:

import sage.all as sage

class myFunc:

   def __init__(self):
      sage.var('x')
      expr = x+1
      self.expr = expr.function(x)

   def __call__(self,value):
      return self.expr(value)

   def diff(self):
      return self.expr.diff(x)

This uses the Sage function(...) method as explained in the link. It is clean and I like it.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-06-01 09:21:15 -0500

Seen: 412 times

Last updated: Jun 01 '11