[Numpy-discussion] A little help please?

Neal Becker ndbecker2@gmail....
Tue Feb 26 05:40:41 CST 2008


My user-defined type project has mostly gone well, but I'm stuck on
mixed-type arithmetic.

I have 2 types: cmplx_int32 and cmplx_int64.  I have added basic arithmetic
for those types, and for mix of those arrays and their respective scalars. 
But mixed arithmetic only partly works.


In [2]: a
Out[2]: array([(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0),
(8,0), (9,0)], dtype=cmplx_int32)

In [3]: b
Out[3]: array([(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0),
(8,0), (9,0)], dtype=cmplx_int64)

In [4]: a+b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/nbecker/numpy/<ipython console> in <module>()

TypeError: function not supported for these types, and can't coerce safely
to supported types

In [5]: b+a
Out[5]: 
array([(0,0), (2,0), (4,0), (6,0), (8,0), (10,0), (12,0), (14,0), (16,0),
       (18,0)], dtype=cmplx_int64)

What I did:
d1 is dtype cmplx_int32, d2 is dtype cmplx_int64.

1. PyArray_RegisterCastFunc (d1, d2->type_num,
&cmplx_to_cmplx<cmplx_int64_t,cmplx_int32_t>);

That registers a conversion from cmplx_int32->cmplx_int64.
(Docs never explain when this conversion is used, BTW)


2.  PyArray_RegisterCanCast (d1, d2->type_num, PyArray_NOSCALAR);

3. d1->f->castdict = PyDict_New();
  PyObject *key = PyInt_FromLong (d2->type_num);
  PyObject *cobj = PyCObject_FromVoidPtr ((void*)(void(*
(void*,void*,npy_intp,void*,void*))&cmplx_to_cmplx<cmplx_int64_t,cmplx_int32_t>,
0);

  PyDict_SetItem (d1->f->castdict, key, cobj);

So now add (cmplx_int64, cmplx_int32) is OK, the 2nd arg is converted,  but
no attempt is made at radd.  Obviously, I don't want to convert 64->32.

Any clues what's missing here?




More information about the Numpy-discussion mailing list