[PEP draft 2] Adding new math operators

Perry Greenfield perry at eclipse.stsci.edu
Wed Aug 9 18:12:47 EDT 2000


In article <m2n1imfr0q.fsf at spiff.home.com>, Tim Hochberg <tim.hochberg at ieee.org> writes:
 
[description of .E and .A approach snipped]

Some of us have been talking about an almost identical approach (though
I'm not sure we all agree even amongst ourselves which is the best
approach :-). I think it is worth some serious consideration.

|> There has been some critisism of this approach on the basis that
|> inside a function one doesn't know what types are passed in. I think
|> this is a smaller problem than it has been presented as. If the
|> function in question is not a small, throwaway function, then it is
|> both useful and easy to check or coerce the arguments at the top of
|> the function. For example:
|> 
|> def frobulate(A, B, C):
|>    A, B, C = A.E, B.E, C.E # I like working elementwise.
|>    # ....
|> 
|> or better still:
|> 
|> def fribbinate(A, B, C):
|>    A, B, C = asarray(A, Float), asarray(B, Float), asarray(C, Float)
|>    #....
|> 

But don't forget the need to coerce the result to the type submitted
as an argument. For example if the FFT module used the NumPy module
and someone called an fft with MatPy arrays, presumably we would like
the fft to return a MatPy array. So some convenience function ought to
be provided to coerce to the input type.

As mentioned below, this issue has to be addressed regardless of whether
new operators are provided if MatPy and NumPy use different meanings for
the standard operators.

|> The second option, which I commonly use, also protects against
|> inadvertent integer matrices and also allows the user of the function
|> to use lists or tuples as inputs which can often be useful:
|> 
|> D = fribbinate(A, B, [1,5,7])
|> 
|> In addition, the additional operator approach only help here if the
|> sense of the operators is the same for both MatPy and NumPy. Which
|> means that, in effect, ~X would be matrixeise for both packages. I
|> admit to having lost track of this thread for a while, but last I
|> heard the wish was for ~X to be elementwise within MatPy.
|> 
|> -tim


Perry Greenfield



More information about the Python-list mailing list