Hi Burcin
I am still in sage-4.0... The 4.0.1 is currently building.
I am writing a piece of software whose purpose is to analyse a set of
given equations to transform them into Cellular Automata to solve
differential problems right from the formal expression. You can get
details there : http://intercell.metz.supelec.fr and there :
http://intercell.metz.supelec.fr/depot/InterCell/escapade2.handout.pdf
I have written in sage.3.4 a class that inherited from
SymbolicArithmetic (and used the __init__ as follows) :
_____________________
if is_SymbolicExpression(eq):
eq=(eq==0)
# SymbolicArithmetic.__init__(self,[eq,
0],operator.add)
SymbolicArithmetic.__init__(self,[eq.lhs
(),eq.rhs()],operator.sub)
elif is_SymbolicEquation(eq):
SymbolicArithmetic.__init__(self,[eq.lhs
(),eq.rhs()],operator.sub)
elif ((eq==0)|(type(eq)==bool)):# a special treatment
for the 0==0 equation
SymbolicArithmetic.__init__(self,
[x,x],operator.sub)
else:
print('arguments must be SymbolicExpression or
SymbolicEquation')
____________________
As you see, I have used a few dirty tricks to work around a few bugs
but it worked.
My purpose is to add to this inherited class a few methods that will
analyse this equation (count the functions in, determine their
arguments, translate them...), but I definitely need to use the base
functions of symbolic arithmetic. I must thus find a way to correctly
init a subclass of Expression.
By the way, I also found a few other things that did not work with
this __init__ method, see :
________________________
sage: a=(x==1)
sage: b=(x==2)
sage: a+b
2*x == 3
sage: a=test(x==1)
sage: b=test(x==2)
sage: a+b
2*x == 3
sage: a=(x==1)
sage: b=test(x==2)
sage: a+b
x == (x + 1 == 3)
________________________
strange, huh ?
I will try out your patch. Why do you say that does not solve my
problem ?
Thanks
Nicolas
On 11 juin, 17:12, Burcin Erocal <[email protected]> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---