[PYTHON MATRIX-SIG] Type coercion two more times

Perry A. Stoll stoll@atr-sw.atr.co.jp
Fri, 26 Jan 1996 16:37:38 +0900


I'm about 12 hours behind all of you in North America, so I come to
work in the morning and read all the activity of the previous
day. Most of the debate is over by the time I read it, so I have to
spend the day working instead ;-)

Here's the situation now:
 - There is support for three scalar numerical types in Python:
     PyInteger ( C long )
     PyFloat   ( C double )
     PyComplex ( C double complex )

 - The multiarray object is introducing (providing access to)
   operations for many more C numerical types.

The question is how do we handle operations that mix C numerical types
not in Python (i.e. C float, C short) with Python supported C types (C
long,C double)?

In the following, I'm going to use Jim's term of "3 Classes" of
numerical types ( Integer, Float, Complex) to mean the set of
corresponding C types:

   Integer = any of the C byte,short,int, or long 
   Float   = either C float or C double
   Complex = either C float complex or C double complex

--------------------------------------------------
The Proposal:

We define an optional attribute for multiarrays, say, oh, I don't
know:
       array.__maintain_ctype__, or
       array.__avoid_coercion__, or
       array.__do_no_coerce_me__ (?!?)

- Coercions within a Class (Integer<->Integer,Float<->Float) are
governed by this attribute, namely,
     If this attribute exists and is TRUE:
         then the result will be the same type as the multiarray.
     If the attribute does not exists or is FALSE,
         then NORMAL Python coercion rules apply, and
         the result will always be a PyInteger, PyFloat, or PyComplex.

- Coercions to a higher Class (i.e. Integer-> Float -> Complex) always
occur if necessary. The result will be the normal Python type for that
Class, namely PyInteger, PyFloat, or PyComplex.

- If both operands have this attribute set but the array types
disagree, raise an exception. Operations involving two multiarrays can
be dealt with this way.

- Correspondingly, add a keyword to the Array creation function, i.e.
>>>  myshortarray = Array.array([1,2,3,4],'s',nocoercion=1)

--------------------------------------------------
The Explanation

Normally, this attribute would not be set, so operations will follow
normal Python rules. That makes it consistant with the rest of Python
for new and old users.

But when Jim reads in a C float file, he sets the
"__do_not_coerce_me__" attribute, letting the C multiarray code know
he expects operations involving this array and other Floats to result
in C float arrays.

--------------------------------------------------

Comments?

-Perry




=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================