Here is one example of class overload that works:

In [1]: from sympy import Matrix, MatrixSymbol

In [2]: k = MatrixSymbol('k', 2, 2)

In [3]: m = MatrixSymbol('m', 2, 2)

In [4]: K = Matrix(k)

In [5]: M = Matrix(m)

In [6]: K * M
Out[6]:
Matrix([
[k[0, 0]*m[0, 0] + k[0, 1]*m[1, 0], k[0, 0]*m[0, 1] + k[0, 1]*m[1, 1]],
[k[1, 0]*m[0, 0] + k[1, 1]*m[1, 0], k[1, 0]*m[0, 1] + k[1, 1]*m[1, 1]]])

In [7]: class MyMatrix(Matrix):
    pass
   ...:

In [8]: MyMatrix.__mul__ = K.multiply_elementwise

In [9]: K = MyMatrix(k)

In [10]: M = MyMatrix(m)

In [11]: K * M
Out[11]:
Matrix([
[k[0, 0]*m[0, 0], k[0, 1]*m[0, 1]],
[k[1, 0]*m[1, 0], k[1, 1]*m[1, 1]]])



Jason
moorepants.info
+01 530-601-9791


On Sun, Feb 2, 2014 at 12:16 PM, Aaron Meurer <[email protected]> wrote:

> 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.
>

-- 
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