[Numpy-discussion] A case for rank-0 arrays

Sasha ndarray at mac.com
Thu Feb 23 19:28:03 EST 2006

On 2/23/06, Francesc Altet <faltet at carabos.com> wrote:
> It's a bit late, but I want to support your proposal (most of it).

You are not late -- you are the first to reply! When you say "most of
it," is there anything in particular that you don't like?

> I've also come to the conclusion that scalars and rank-0 arrays should
> coexist. This is something that appears as a natural fact when you
> have to deal regularly with general algorithms for treat objects with
> different shapes. And I think you have put this very well.

Thanks for your kind words.  If we agree to legitimize rank-0 arrays,
maybe we should start by removing conversion to scalars from ufuncs. 

>>> type(array(2)*2)
<type 'int32scalar'>

I believe it should result in a rank-0 array instead.

I've recently wrote ndarray round function and that code illustrates
the problem of implicite scalar conversion:

                ret = PyNumber_Multiply((PyObject *)a, f);
                if (ret==NULL) {Py_DECREF(f); return NULL;}
                if (PyArray_IsScalar(ret, Generic)) {
                        /* array scalars cannot be modified inplace */
                        PyObject *tmp;
                        tmp = PyObject_CallFunction(n_ops.rint, "O", ret);
                        ret = PyObject_CallFunction(n_ops.divide, "OO",
                                                    tmp, f);
                } else {
                        PyObject_CallFunction(n_ops.rint, "OO", ret, ret);
                        PyObject_CallFunction(n_ops.divide, "OOO", ret,
                                              f, ret);

More information about the NumPy-Discussion mailing list