On Wednesday, 3 July 2013 10:47:04 UTC-4, Prasoon Shukla wrote:
>
> Back to my question. So, when a user does:
>
>
>    - Vector*Vector, the Vector.__mul__ method will raise an error saying 
>    that a user cannot multiply two vectors.
>    - Vector*scalar, the Vector.__mul__ will be called and a VectMul will 
>    be returned.
>    - *scalar*Vector : *Now this is the problem - here let us say that 
>    scalar is a Add - then the method Add.__mul__ will be called instead of 
>    what I want to be called  - in this case, Vector.__mul__ - and return a 
> Mul 
>    object instead of a VectMul.
>
> This is the problem I face right now. I googled around a bit - apparently, 
> we can use the __rmul__ method. But, for that to work - the Add.__mul__ (in 
> the third point above) should return NotImplemented (what does that even 
> mean? Raise an exception?). Obviously, I shouldn't have to change 
> Add.__add__ for this. So, how can I handle this problem?
>
>
_op_priority and the @call_highest_priority are there exactly for this 
purpose. Define it in your class with a value above 10.0, which is Expr's 
default value for it. Also define __rmul__, using the decorator 
@call_highest_priority. This will allow your class to control what happens 
during multiplication (left or right).

Cheers,
Julien 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to