Operators for matrix: current choices (Was Matlab vs Python ...)

Huaiyu Zhu hzhu at localhost.localdomain
Tue Jul 18 22:25:43 EDT 2000


On Wed, 19 Jul 2000 01:21:10 GMT, Tim Hochberg <tim.hochberg at ieee.org> wrote:
>
>There is a problem with adding new operators to mean elementwise: the
>current operators are already elementwise in NumPy. Either all current
>NumPy code would have to be broken to fit in with this rule, or the
>rule would have to become "elementwise, except when it's not", which
>is somewhat less satisfying. I would suggest that it would be more
>productive to add operators that are not elementwise, except that
>conflicts with the rest of Python. TNSTAAFL.

I would have prefered compatibility with NumPy, except for two reasons:
1. MatPy was started because there is a real need for using infix operator
   for matrix multiplication and the only way available was to define a new
   class.  So for MatPy users, at least, * already means matrixwise.
2. The current list add is not componentwise.

I don't know of a way that satisfies every compatibility demand.  Because
the supply of binary operators has been very limited, each package assigns
them to their most deserving ones instead of according to a better overall
plan.  Had there been more this could have been better coordinated.

>Personally I think that this an overlarge crop of operators to be
>adding. Since I prefer something like:
>
>A.inv * B 
>
>to 
>
>A \ B

They are equal in math, but not in practice.  The former takes O(n^3)
computation while the latter is O(n^2).  The former is also less accurate
for near singular A because of rounding error.

>anyway, the only operator I feel is missing is the matrix
>multiplication. And, conveniently, there is an appropriate opperator

The other operators are also useful. For example, the + and .+ do different
things.  If you add a row and column vector with + it will complain about
shape mismatch, as defined in linear algebra. With .+ it will give a matrix
properly extended to both rows and columns.  This has been asked for in this
thread. I've seen it been called prolongation, continuation or cross
extension, among others.  In fact, in MatPy, the .+ .- .* ./ do exactly what
+ - * / do in NumPy.

>> names .+ "is" .+ types
>> map(lambda x,y: x+"is"+y, names, types)
>
>There's a bit of a problem here since "is" is a sequence. I'm not sure
>how you propose to have sequences of unequal length work with .X, but
>I'd think this would have to be:
>
>names .+ ["is"]*len(names) .+ types

Oops!  It should be

names .+ ["is"] .+ types

Note the extension mechanism is used.  Sequences with different length raise
exceptions, except when one of them has length one, which is extended to the
other length.

Huaiyu



More information about the Python-list mailing list