[Matrix-SIG] I have working code that implements active-casting concept.

Travis Oliphant Oliphant.Travis@mayo.edu
Tue, 18 Jan 2000 23:19:17 -0600 (CST)

I'm pleased to relate that I've been able to successfully implement the
active-casting concept briefly discussed in a previous post, using the
current Numerical Python.  Preliminary testing shows it to work quite well
and seems to "remove the single-precision problem." if used.

I can patch the current tree as soon as I receive approval from Paul
Dubois, but he will likely want to hear opinions from this list, so speak
up if you hate the idea or love the idea.  If no-one responds I will
assume that means a show of favor :-) 

Here is the idea that I implemented:

Every array has an active attribute that defaults to 0, but can be set
in Python to 1 (if approved I would change code so the flag could be set
as a keyword to array()).

When a universal function involving NumPy arrays is encountered, if all
arrays are passive (active flag = 0) then the current coercion rules are
used.  If any of the arguments to the function are active arrays, then the
active array with the largest typecode (enumerated types in arrayobject.h)
determines the output types.  All other arrays are automatically cast
(even if it means loss of precision) to that type, before the operation
begins.  The output arrays from the function are all active arrays.


a = array([1,2,3,4],'f')
b = a*4     # default casting rules make b double

a.active = 1
c = a*4     # now a governs the casting and c is float

The changes are not extensive, and should not break any old
code, so the only real question is does this provide a suitable fix?

Comments, critiques are welcome,