Ask Your Question

callable symbolic expression from python script

asked 2011-06-01 16:21:15 +0100

tdstephens3 gravatar image

updated 2011-06-16 14:59:48 +0100

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):

      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

answered 2011-06-01 16:59:44 +0100

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)
sage: func.differentiate(x)
x |--> D[0](func)(x)
sage: func.differentiate(x)(3)

Using a concrete function:

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

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


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 17:15:37 +0100 )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 17:32:20 +0100 )edit

answered 2011-06-01 16:48:26 +0100

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):
      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


Asked: 2011-06-01 16:21:15 +0100

Seen: 754 times

Last updated: Jun 01 '11