Hi Nicolas,
On Thu, 11 Jun 2009 07:07:50 -0700 (PDT)
Nicolas <[email protected]> wrote:
>
> Hi all,
>
> I am trying, in sage 4.0, to write a class that inherits from the new
> sage.symbolic.expression.Expression class. I have not found any
> precise signature for the __init__ method of that class so I suppose I
> am doing something wrong : things seem to work, except for the
> substitute stuff.
>
> Here is an example of what I mean :
>
> ______________________________________
> class test(Expression):
> def __init__(self,eq):
> Expression.__init__(self,SR,eq)
>
> f=function("f")
> g=function("g")
> a=f(x)
> b=test(f(x))
> testa=a.substitute_function(f,g)
> testb=b.substitute_function(f,g)
> ______________________________________
> sage: testa
> g(x)
> sage: testb
> f(x)
> sage:
>
> Anyone to help me out ?
I don't think the __init__ function in Expression is usable as it is.
Here is the code:
def __init__(self, SR, x=0):
cdef GEx exp
GEx_construct_pyobject(exp, x)
GEx_construct_ex(&self._gobj, exp)
self._parent = SR
The line with GEx_construct_pyobject() coerces the symbolic expression
you give it to a constant numeric object. Then, in your construction, b
becomes a constant.
sage: b.operator() # this returns None since it's a constant
sage: t.operator()
f
After applying the patch below, the following works:
----------------------------------------------------------------------
| Sage Version 4.0.1, Release Date: 2009-06-06 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
Loading Sage library. Current Mercurial branch is: la
sage: from sage.symbolic.expression import Expression
sage: class esub(Expression):
....: def __init__(self, parent, val):
....: Expression.__init__(self, parent, val)
....:
sage: f = function('f')
sage: g = function('g')
sage: t = f(x)
sage: b = esub(SR, t)
sage: b.substitute_function(f, g)
g(x)
This still doesn't solve your problem though, most methods of
Expression will return Expression objects.
sage: type(b.substitute_function(f, g))
<type 'sage.symbolic.expression.Expression'>
Can you explain your application a little?
Cheers,
Burcin
diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
--- a/sage/symbolic/expression.pyx
+++ b/sage/symbolic/expression.pyx
@@ -212,9 +212,8 @@
sage: sage.symbolic.expression.Expression(SR, 5)
5
"""
- cdef GEx exp
- GEx_construct_pyobject(exp, x)
- GEx_construct_ex(&self._gobj, exp)
+ cdef Expression exp = self.coerce_in(x)
+ GEx_construct_ex(&self._gobj, exp._gobj)
self._parent = SR
def __dealloc__(self):
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---