# Revision history [back]

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

 2 retagged Kelvin Li 503 ●5 ●12 ●17

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