matrix_1 = matrix_2 * matrix_3

holger krekel pyth at devel.trillke.net
Mon Aug 26 10:05:30 EDT 2002


Anton Vredegoor wrote:
> On 25 Aug 2002 23:32:01 +0200, Chris Liechti <cliechti at gmx.net> wrote:
> 
> >anton at vredegoor.doge.nl (Anton Vredegoor) wrote in
> >news:akbhl5$m3r$1 at news.hccnet.nl: 
> >> Is there a module that uses the star operator for matrix
> >> multiplication? If not, which module would be suitable to subclass a
> >> matrix class from so that it does this?
> >
> >Nummeric has everything you need
> >http://www.pfdubois.com/numpy/
> 
> <numeric docs>
> matrixmultiply(m1, m2)
> The matrixmultiply(m1, m2) multiplies matrices or matrices and vectors as
> matrices rather than ele-mentwise.
> Compare:
> >>> print a
> [[0 1 2]
> [3 4 5]]
> >>> print b
> [1 2 3]
> >>> print a*b
> [[ 0 2 6]
> [ 3 8 15]]
> >>> print matrixmultiply(a,b)
> [ 8 26]
> </numeric docs>
> 
> From this it seems that redefining the star operator to do what is
> called matrixmultiply here could cause problems. But maybe not. Could
> someone please inform me whether it would be possible to adapt numeric
> to use the star operator for matrixmultiply.
> 
> I think numeric is a great package and if it were just for me I would
> use the standard package, but since this is not for one of my own
> projects I would rather not interfere with design decisions. In fact
> it could be that if no python module that uses the star operator for
> matrixmultiply can be found, python would not be used at all for
> this project.

Do know how easy it is in python to redefine the star operator?

>>> class a(str):
...    def __mul__(self, other):
...       return self.startswith(other) or other.startswith(self)
...
>>> a1=a("hallo")
>>> a2=a("hal")
>>> a1*a2
1

So you could inherit whatever matrix-class you like and (re)define
the __mul__ operator to mean 'matrixmultiply'.  

Ask back if you don't know what i am talking about :-)

HTH,

    holger




More information about the Python-list mailing list