ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 01 Jun 2011 17:32:20 +0200callable symbolic expression from python scripthttps://ask.sagemath.org/question/8140/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)
Wed, 01 Jun 2011 16:21:15 +0200https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/Answer by tdstephens3 for <p>I am trying to obtain a callable symbolic expression from a python script and having a hard time of it.</p>
<p>In my python script I would like to have something along the lines of:</p>
<pre><code>func = myFunc()
func_evaluated_at_3 = myFunc(3)
func_dx = func.differentiate()
func_dx_evaluated_at_3 = func_dx(3)
</code></pre>
<p>In the module defining <code>myFunc</code> I was hoping to have something along the lines of:</p>
<pre><code>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)
</code></pre>
<p>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)</p>
https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?answer=12397#post-id-12397Alright, I guess it works this way sometimes (ask question, five minutes later figure out the answer!)
Using the information [here](http://sage1-math.univ-lyon1.fr/media/sage/docs_files/en/reference/sage/calculus/calculus.html) (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.
Wed, 01 Jun 2011 16:48:26 +0200https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?answer=12397#post-id-12397Answer by Kelvin Li for <p>I am trying to obtain a callable symbolic expression from a python script and having a hard time of it.</p>
<p>In my python script I would like to have something along the lines of:</p>
<pre><code>func = myFunc()
func_evaluated_at_3 = myFunc(3)
func_dx = func.differentiate()
func_dx_evaluated_at_3 = func_dx(3)
</code></pre>
<p>In the module defining <code>myFunc</code> I was hoping to have something along the lines of:</p>
<pre><code>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)
</code></pre>
<p>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)</p>
https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?answer=12401#post-id-12401I 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.Wed, 01 Jun 2011 16:59:44 +0200https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?answer=12401#post-id-12401Comment by Kelvin Li for <p>I am not sure whether your overall goal is worth pursuing. From the surface, it looks like <code>MyFunc</code> is becoming a clone of <code>sage.symbolic.expression.Expression</code>. Additionally, Sage has a deprecation policy: old code must be supported for at least twelve months <em>with deprecation warnings</em> 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.</p>
<p>Just for reference, your example can be done in Sage as follows:</p>
<p>Using a symbolic function:</p>
<pre><code>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)
</code></pre>
<p>Using a concrete function:</p>
<pre><code>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
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?comment=21648#post-id-21648From my very limited knowledge, I think the parts you are using are quite stable. Have fun porting!Wed, 01 Jun 2011 17:32:20 +0200https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?comment=21648#post-id-21648Comment by tdstephens3 for <p>I am not sure whether your overall goal is worth pursuing. From the surface, it looks like <code>MyFunc</code> is becoming a clone of <code>sage.symbolic.expression.Expression</code>. Additionally, Sage has a deprecation policy: old code must be supported for at least twelve months <em>with deprecation warnings</em> 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.</p>
<p>Just for reference, your example can be done in Sage as follows:</p>
<p>Using a symbolic function:</p>
<pre><code>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)
</code></pre>
<p>Using a concrete function:</p>
<pre><code>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
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?comment=21650#post-id-21650I 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?Wed, 01 Jun 2011 17:15:37 +0200https://ask.sagemath.org/question/8140/callable-symbolic-expression-from-python-script/?comment=21650#post-id-21650