[SciPy-user] Setting the real part of a complex array element

Scott Ransom sransom at nrao.edu
Sun Jan 15 18:11:42 EST 2006


Hi Travis,

Hmmm.  Very strange.  The code that Paul was mentioning (which
was written by me, BTW), used to run on 4-byte floats (i.e.
typecode 'F').  And it worked fine.  But you are right that it
doesn't work for old typecode 'D':

In [1]:Numeric.__version__
Out[1]:'24.2'

In [2]:c = Numeric.zeros(10,'F')

In [3]:c[0].real = 1.0

In [4]:c
Out[4]: array([ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
0.+0.j,  0.+0.j,  0.+0.j,0.+0.j,  0.+0.j,  0.+0.j],'F')
	     
In [5]:c = Numeric.zeros(10,'D')

In [6]:c[0].real = 1.0

---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback
(most recent call last)

/home/sransom/<console>

TypeError: 'complex' object has only read-only attributes
(assign to .real)

Very strange....

Scott

On Sun, Jan 15, 2006 at 04:01:08PM -0700, Travis Oliphant wrote:
> Paul Ray wrote:
> 
> >Hi,
> >
> >I have some old code that tries to write to the real part of a  
> >complex array element, but the syntax now seems to fail in the new  
> >NumPy.  Looking in the new NumPy book (section 3.1.3) , it appears  
> >that the .real attribute should be writable, but it does not seem to  
> >work.  Does anyone know how to do this?
> >
> >In [1]: import numpy
> >
> >In [2]: numpy.__version__
> >Out[2]: '0.9.2'
> >
> >In [4]: c = numpy.zeros(10,dtype=numpy.complex64)
> >
> >  
> >
> >In [5]: c[0].real = 1.0
> >  
> >
> 
> c.real[0] = 1.0
> 
> The problem is that
> 
> c[0] is not an array it is a scalar.    This would not have worked with 
> typecode='D' with Numeric either...
> 
> c.real[0] = 1.0 will work.
> 
> -Travis
> 
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

-- 
-- 
Scott M. Ransom            Address:  NRAO
Phone:  (434) 296-0320               520 Edgemont Rd.
email:  sransom at nrao.edu             Charlottesville, VA 22903 USA
GPG Fingerprint: 06A9 9553 78BE 16DB 407B  FFCA 9BFA B6FF FFD3 2989




More information about the SciPy-User mailing list