[Python-Dev] Matrix product
Steven D'Aprano
steve at pearwood.info
Sat Jul 26 19:11:16 CEST 2008
On Sun, 27 Jul 2008 02:23:11 am daniel.stutzbach at gmail.com wrote:
> How about just making a matrix multiply function that can take many
> arguments? I think this is pretty readable:
>
> mmul(a, b, c, d)
>
> Additionally, mmul could then optimize the order of the
> multiplications (e.g., depending the dimensions of the matrices, it
> may be much more efficient to perform a*((b*c)*d) rather than
> ((a*b)*c)*d).
But be careful there: matrix multiplication is associative, so that
a*b*c = (a*b)*c = a*(b*c), but that doesn't necessarily apply once the
elements of the matrices are floats. For instance, a*(b*c) might
underflow some elements to zero, while multiplying (a*b)*c does not. As
a general rule, compilers should not mess with the order of floating
point calculations.
Also, some classes that people might want to multiply may not be
associative even in principle. E.g. the vector cross product:
(a*b)*c != a*(b*c) in general.
I think a product() function that multiplies the arguments from left to
right would be useful. But it won't solve the problems that people want
custom operators to solve. I'm not even sure if it will solve the
problem of matrix multiplication.
--
Steven.
More information about the Python-Dev
mailing list