Discussion: new operators for numerical computation

Paul Svensson paul at svensson.org
Mon Jul 24 14:34:35 EDT 2000


Tim Hochberg wrote:

(summarized)
>At this point there seems to be main proposals. There are variations
>on these basic themes, but these two capture the main points:

proposal 1:
>New Operators: .+, .-, ./, .*

proposal 2:
>New Operators: (+), (-), (/), (*)[outer], (.)[dot], (|)[solve], (^)[power] 

Unfortunarely, neither of these seem very esthetically pleasing to me,
and I don't mean the graphics.  May I instead suggest that we add no
new operators at all, just a modicum of new syntax.

Logically, elementwise operators are defined in terms of operations on the
elements, and thus should need no new __foobar__ methods, only some way
(i.e. syntax) to indicate that the operators are operating on the elements
(with the usual NumPy broadcasting applied) instead of on the whole.

Matrix operators on the other hand, operate on the whole objects, which is
the natural way for operators to be defined in Python, so it would make more
sense to use the usual (+, -, *, /, **, ...) symbols for these.  This is
doable only as long as we don't usurp them for elementwise operations.

As for what syntax to use for elementwise operations,  X [+] Y (etc)
immediately evokes the right image to me; naturally combining + and [].
The extension from matrices to any and all Python containers comes naturally.
(This should also mesh well with (augmented) assignment, for example,
X [=] Y making a (shallow) copy into the existing object; however, I haven't
quite thought thru the implications of this...)

The up side:
+ clean, easy to understand syntax, with no surprises
+ the natural object-orientedness of python operators is preserved
+ no new operators or __foobar__ methods needed

The down side:
- no new operator for left division; use solve(x, y)
- no new operators for various different multiplications
- the NumPy people are not likely to be pleased

	/Paul



More information about the Python-list mailing list