Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

callable symbolic expression from python script

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)

click to hide/show revision 2
retagged

callable symbolic expression from python script

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)