[Matrix-SIG] Conversion of scalars.

Travis Oliphant Oliphant.Travis@mayo.edu
Wed, 23 Jun 1999 10:44:07 -0500 (CDT)


After looking at the source code it seems like it would be "somewhat"
straightforward thing to implement Tim Hochberg's suggestion of defining a
function (or an attribute) to set the desired behavior for the precision
of scalars.

It seems like what needs to be changed is of code in
PyArray_ObjectType, which all the coercion section of code look to in
order to determine what type to make a Python Object.  In particular the
last part of this code is called whenever the Python Object is one of the
Numeric types:

Here is the code:

int PyArray_ObjectType(PyObject *op, int minimum_type) {

<snip>

	if (PyInt_Check(op)) {
		return max(minimum_type, (int)PyArray_LONG);
	} else {
		if (PyFloat_Check(op)) {
			return max(minimum_type, (int)PyArray_DOUBLE);
		} else {
			if (PyComplex_Check(op)) {
				return max(minimum_type,
(int)PyArray_CDOUBLE);
			} else {
				return (int)PyArray_OBJECT;
			}
		}
	}
}


As you can see right now the default is to return PyArray_LONG,
PyArray_DOUBLE, and PyArray_CDOUBLE.  It seems this could be changed quite
straightforwardly to return the lower precision versions.

Setting up some way to alter these return types would also have the effect
of changing the way sequences of these types were returned so
array([1.,3.,4.]) would become an array of type 'f'.

I like the idea of having this return type be user settable.  It would be
easy to do by just having this code return what the user settable type is
for the integer, float, and complex objects of Python.

Asside from introducing a global variable floating around I don't see how
to do this without an extra argument to PyArrayObject_Type, which isn't
too bad except it requires all Numeric extensions which use this function
to be changed (I'd be willing to change all of mine, though.)

This seems like a pretty easy feature to add and as I use Float32 all of
the time (the big reason I left from MATLAB), I would like to see it
added.

I would submit a patch myself, except it sounds like Paul has been making
some changes to the source and is going to release a new version at some
point...

Just for fun, it would be interesting to see the effect of a hard-coded
PyArray_SHORT, PyArray_FLOAT, and PyArray_CFLOAT here...


Travis