[MATRIX-SIG] Bug report (and patch) Complex to PyObject

Timothy A. Hochberg hochberg@wwa.com
Wed, 20 Aug 1997 09:21:19 -0500 (CDT)


There's a bug in the Complex->PyObject casting routine (For both CFLOAT
and CDOUBLE). Here's an example:

>>> arange(5) + 0j
array([ 0.+0.j,  1.+0.j,  2.+0.j,  3.+0.j,  4.+0.j])
>>> _.astype(PyObject)
array([0j , 1j , (1+0j) , 2j , (2+0j) ],'O')
>>> 

A patch to arraytypes.c to fix this is included below. Basically, the
complex array was being stepped by 1 double (or float) instead of two, so
the result was all messed up.

 ____   
  /im  

+------------------------------------------------+
|Tim Hochberg            Research Assistant      |
|hochberg <at> wwa.com   University of Illinois  |
|                        Electrical Engineering  |
+------------------------------------------------+


*** NumPy/arraytypes.c  Sat Aug 16 08:36:09 1997
--- NumPy.old/arraytypes.c      Fri Mar 21 15:09:59 1997
***************
*** 474,480 ****
       {int i; for(i=0;i<2*n;i++,ip+=ipstep,op+=opstep) {*op =
(double)*ip;}}
  
  static void CFLOAT_to_OBJECT(float *ip, int ipstep, PyObject  **op, int
opstep, int n)
!      {int i; for(i=0;i<n;i++,ip+=2*ipstep,op+=opstep) {op[0] =
PyComplex_FromDoubles((double)((float *)ip)[0], (double)((float
*)ip)[1]);}}
  
  static PyObject * CFLOAT_getitem(char *ip) {return
PyComplex_FromDoubles((double)((float *)ip)[0], (double)((float
*)ip)[1]);}
  
--- 474,480 ----
       {int i; for(i=0;i<2*n;i++,ip+=ipstep,op+=opstep) {*op =
(double)*ip;}}
  
  static void CFLOAT_to_OBJECT(float *ip, int ipstep, PyObject  **op, int
opstep, int n)
!      {int i; for(i=0;i<n;i++,ip+=ipstep,op+=opstep) {op[0] =
PyComplex_FromDoubles((double)((float *)ip)[0], (double)((float
*)ip)[1]);}}
  
  static PyObject * CFLOAT_getitem(char *ip) {return
PyComplex_FromDoubles((double)((float *)ip)[0], (double)((float
*)ip)[1]);}
  
***************
*** 529,535 ****
       {int i; for(i=0;i<2*n;i++,ip+=ipstep,op+=opstep) {*op =
(double)*ip;}}
  
  static void CDOUBLE_to_OBJECT(double *ip, int ipstep, PyObject  **op,
int opstep, int n)
!      {int i; for(i=0;i<n;i++,ip+=2*ipstep,op+=opstep) {op[0] =
PyComplex_FromDoubles((double)((double *)ip)[0], (double)((double
*)ip)[1]);}}
  
  static PyObject * CDOUBLE_getitem(char *ip) {return
PyComplex_FromDoubles((double)((double *)ip)[0], (double)((double
*)ip)[1]);}
  
--- 529,535 ----
       {int i; for(i=0;i<2*n;i++,ip+=ipstep,op+=opstep) {*op =
(double)*ip;}}
  
  static void CDOUBLE_to_OBJECT(double *ip, int ipstep, PyObject  **op,
int opstep, int n)
!      {int i; for(i=0;i<n;i++,ip+=ipstep,op+=opstep) {op[0] =
PyComplex_FromDoubles((double)((double *)ip)[0], (double)((double
*)ip)[1]);}}
  
  static PyObject * CDOUBLE_getitem(char *ip) {return
PyComplex_FromDoubles((double)((double *)ip)[0], (double)((double
*)ip)[1]);}
  



_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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