Or, if you don't use matrix multiplication at all, then you could overload __mul__.
And I forgot to mention that you'll want to overload both __op__ and __rop__ for whatever operator you choose. Aaron Meurer On Sun, Feb 2, 2014 at 11:15 AM, Aaron Meurer <[email protected]> wrote: > To elaborate on what Sergey is saying, you need to understand that > SymPy is just Python, so it is limited by what is available in Python. > .* is not a valid operator in Python, so there is no way to make it > work. > > In addition to this, SymPy philosophy is to make the Python operators > mean what they mean in normal Python. % is the modulo operator, so it > may not make sense to use it for something completely different. On > the other hand, I'm not sure if % can ever make sense for matrices, so > maybe it would be possible. > > I would recommend just creating a light subclass of Matrix for your > work, and overload an operator to do elementwise multiplication. There > is a list of all Python operators here > http://docs.python.org/3.3/reference/expressions.html#operator-precedence. > You should be aware of the precedence of the operator you choose. % > actually probably is a good choice, because it has the same precedence > as *. > > Something like > > class MyMatrix(Matrix): > def __mod__(self, other): > return self.multiply_elementwise(other) > > You'll want to be sure to document this nicely in your thesis, so that > people don't think that % is SymPy behavior. > > If you come across an instance where something returns a normal Matrix > instead of a MyMatrix, that's most likely a bug that you should > report. > > Aaron Meurer > > On Sun, Feb 2, 2014 at 11:04 AM, Sergey Kirpichev <[email protected]> > wrote: >> On Sunday, February 2, 2014 8:30:45 PM UTC+4, David Kremer wrote: >>> >>> What I would simply ask, is if it wouldn't be possible to overload an >>> operator such as "%" or ".*" to >>> implement matrix elementwise multiplication in sympy. It would greatly >>> improve the readability of my >>> code. >> >> >> First, there is no ".*" operator at all: >> http://docs.python.org/dev/reference/lexical_analysis.html#operators >> >> % - stands for integer division (yields the remainder). While it's possible >> to overload >> it, I doubt if that's natural and pythonic (to include in sympy). In your >> words, it's too hackish. >> >>> >>> About the second question, it is about the point if defining a single >>> operator such as ".*" in matlab >>> could be done in sympy >> >> >> Probably, this can be done only with some syntax extension. >> >> BTW, see also PEP 225. >> >> -- >> 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. -- 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.
