fr., 03.09.2010 kl. 11.27 +0530, skrev Rahul Siddharthan:
> Aaron, Chris,
> Thanks for the replies!
> 
> Aaron wrote:
> > This is great.  SymPy code is *MUCH* easier to read than some lisp, in my 
> > opinion.
> 
> So far, yes and no.  The main thing is that I needed to implement
> structured "tuples" as defined in SICM, and functions that take tuples
> as arguments, and derivatives/partial derivatives of those functions.
> I defined my own class for those things, but with higher-order
> functions (functions that return functions, which is done all the time
> in SICM) it gets a bit inelegant.  Still, I think most of the
> infrastructure is done: I will put up what I have, and ask about
> prettyfication, shortly.
> 
> Chris wrote:
> 
> > what you could try doing is "masking" the derivatives--replace them
> > with symbols of your own
> 
> That's exactly what I ended up doing, but in a much more longwinded
> way than what you wrote.
> 

> The other issue that came up is, I see from the archives, again a
> known problem: if I want to define multiplication for "tuples", a
> multiplication of a tuple by a scalar (on the left or the right) is
> the tuple of each component multiplied by the scalar.  I define the
> __mul__ and __rmul__ methods, but __rmul__ fails to get called when
> the scalar is a sympy object, because scalar defines a __mul__ that
> can handle other sympy objects and I get, eg,
> r * (1,2)
> instead of
> (r, 2*r)
> (I am using the Basic class for my tuples).  If I don't know that r is
> a scalar, of course, the first answer is right and the second need not
> be right.  But even when r is a sympified number, it fails to expand:
> 
> a = up(1,2)  # (up and down are tuple-generating functions in my library)
> b = 3
> b*a
> (output)=> (3, 6) (desired answer)
> b = sympify(3)
> b*a
> (output)=>  3⋅(1, 2) (not the desired answer!)
> 
> I am currently using the workaround of unsympifying all numbers...

In the current master branch you can use the _op_priority class
attribute to make sure your own __mul__() overrides the default.  You
could do it like this:

from sympy.core.containers import Tuple
from sympy.core.expr import Expr

class ET(Tuple, Expr):

    _op_priority = 20

    def __mul__(self, other):
        other = sympify(other)
        if other.is_Atom:
            return ET(*[a*other for a in self.args])

    def __rmul__(self, other):
        other = sympify(other)
        if other.is_Atom:
            return ET(*[other*a for a in self.args])


Using this class in isympy, I get:

In [1]: t = ET(x,y,z)
In [2]: x*t
Out[2]: ET(x**2, x*y, x*z)
In [3]: t*x
Out[3]: ET(x**2, x*y, x*z)


Øyvind

> 
> Rahul
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
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/sympy?hl=en.

Reply via email to