Re: [Numpy-discussion] ndarray of object dtype

2010-10-06 Thread Ioan Ferencik
Thanks Robert,

that worked just great.


I was wandering whether anyone has experience with calling Fortran 90  
functions that return derived types and cast them to C/Python objects.

I have found excellent reference in a couple of websites, however all  
examples deal with returning at best 2D arrays of fundamental/basic  
types. When it comes to derived types/structures I have following  
dilemma:

should I use Fortran subroutines, allocate memory in C and pass the  
structures as arguments or should I use functions, allocate memory in  
Fortran and return the data to C/Python.

I should mention I have little experience in C/Fortran.


All thoughts are welcome.





Quoting Robert Kern robert.k...@gmail.com:

 On Mon, Oct 4, 2010 at 00:41, Ioan Ferencik ioan.feren...@tkk.fi wrote:
 Hello list,

 I am trying to pass elements of a custom defined with C API.
 I have successfully wrapped a type around a C struct.
 I intent to create a list with these objects and process it using   
 numpy C API.
 So i  create an array:

 array = (PyArrayObject *)PyArray_ContiguousFromObject(input,
 PyArray_OBJECT, 0, 0);

 to my understanding each el. in this array is a pointer to my type so
 a cast to this type
 should work.


 this is my custom type

 typedef struct val{
         PyObject_HEAD
         float q;
         float wl;
         int cssid;
         int br;
 }hm1dval;

 I am passing only one element for testing purposes.

 in python
 a = [hm1d1.hm1dval() for i in range(0,1)]
 for c in a:
         c.set_values(q=3.0, wl=2.5, cssid=6, br=7)


 So following code should be valid in C:

 hm1dval s* = PyArray_DATA(array);

 but the members are 0 after casting in spite they were set previously.

 The elements of a dtype=object array are PyObject* pointers. In C, an
 array of pointers would look like this:

 hm1dval **s = PyArray_DATA(array);

 --
 Robert Kern

 I have come to believe that the whole world is an enigma, a harmless
 enigma that is made terrible by our own mad attempt to interpret it as
 though it had an underlying truth.
   -- Umberto Eco
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion




Ioan Ferencik
PhD student
Aalto University
School of Science and Technology
Faculty Of Civil and Env. Engineering
Lahti Center
Tel: +358505122707


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Please help on compilation fortran module using f2py

2010-10-06 Thread Pearu Peterson
Hi,

On 10/06/2010 04:57 AM, Jing wrote:
   Hi, everyone:

 I am new to the python numpy and f2py. I really need help on compiling
 FORTRAN module using f2py. I have been searched internet without any
 success. Here is my setup: I have a Ubuntu 10.04 LTS with python 2.6,
 numpy 1.3.0 and f2py 2 (installed from ubuntu) and gfortran compiler
 4.4. I have a simple Fortran subroutine (in CSM_CH01_P1_1a_F.f95 file)
 as shown below:
 
 !

 subroutine sim_model_1(ts, n, a)

 !

 !f2py integer, intent(in) :: ts, n

 !f2py real,dimension(n), intent(inout) :: a

 implicit none

The problem is in Fortran code. The ts, n, and a variables need to be 
declared for fortran compiler too. The f2py directives are invisible
to the fortran compiler, they are just comments that are used by f2py.
So, try adding these lines to the Fortran code:

   integer, intent(in) :: ts, n
   real, dimension(n) :: a

HTH,
Pearu
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] index of a value in an array

2010-10-06 Thread Chris Withers
Hi All,

Given an array such as:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

How can I find the index of a particular number in the array?

(ie: if it was a list, I'd do [1,2,3,4].index(3))

cheers,

Chris
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] alter_code1.py doesn't convert (any more)?

2010-10-06 Thread Frank Thommen
Hi,

I'm trying to help a user to migrate from Numeric - which he used up to 
now - to numpy.  I found reference to alter_code1.py on 
http://numpy.scipy.org/old_array_packages.html.


First I tried the conversion script:

# python2.6 /path/to/alter_code1.py test.py

and alternatively

# python2.6 /path/to/alter_code1.py ./

This didn't modify test.py at all.  Then I tried to convert from within 
Python:

# python2.6
Python 2.6.2 (r262:71600, Aug  5 2010, 14:21:11)
[GCC 4.4.4] on linux2
Type help, copyright, credits or license for more information.
  import numpy.numarray.alter_code1 as noa
  noa.convertfile(test.py)
  ^D
#

This modified test.py, but only partially.  E.g. the import line was 
still untouched, meaning that it still imported Numeric instead of numpy:


# diff ../test-original.py test.py
0a1,2
  ## Automatically adapted for numpy.numarray Oct 06, 2010 by
 
49c51
   pyfid.byteswap()
---
   pyfid.byteswap(True)
74c76
   databin.byteswap()
---
   databin.byteswap(True)
#


After I've changed 'import os, string, array, Numeric' to 'import os, 
string, array, numpy' I realized, that the conversion had introduced errors:

# python2.6 ./test.py command line parameters
Traceback (most recent call last):
   File ./test.py, line 115, in module
 combineser3D(td3,td2,td1,ser,ser1,fidnum)
   File ./test.py, line 21, in combineser3D
 allser1=readBrukerSer(tdall,td3,ser)
   File ./test.py, line 51, in readBrukerSer
 pyfid.byteswap(True)
TypeError: byteswap() takes no arguments (1 given)
#

So either this method doesn't work (any more) or I am doing something 
wrong.  The numpy version we are using is 1.4.1 with Python 2.6.2 from 
python.org.

Any help regarding this is highly appreciated.


Thanks in advance

 frank
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] index of a value in an array

2010-10-06 Thread Nicolas Bigaouette
On Wed, Oct 6, 2010 at 5:26 AM, Chris Withers ch...@simplistix.co.ukwrote:

 Hi All,

 Given an array such as:

 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

 How can I find the index of a particular number in the array?

 (ie: if it was a list, I'd do [1,2,3,4].index(3))

 cheers,

 Chris
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion


Try with numpy.where(array == value) and plain array == value
IIRC, the first will return an array the size of the found elements and the
second will return an array (or list?) of True/False.
Either can be used as index = numpy.where() and index = (array == value)
and then array[index].
N
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] index of a value in an array

2010-10-06 Thread Thomas, Brian (GE Energy)
Chris,

You can use where() command to find index of a particular number in the
array.

For e.g. to find index of number 1 in array a, you can say 
idx = where(a==1)

Regards,
Brian

-Original Message-
From: numpy-discussion-boun...@scipy.org
[mailto:numpy-discussion-boun...@scipy.org] On Behalf Of Chris Withers
Sent: Wednesday, October 06, 2010 5:27 AM
To: numpy-discussion@scipy.org
Cc: snezana.pe...@glcuk.com
Subject: [Numpy-discussion] index of a value in an array

Hi All,

Given an array such as:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

How can I find the index of a particular number in the array?

(ie: if it was a list, I'd do [1,2,3,4].index(3))

cheers,

Chris
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] index of a value in an array

2010-10-06 Thread Chris Withers
On 06/10/2010 14:53, Thomas, Brian (GE Energy) wrote:
 For e.g. to find index of number 1 in array a, you can say
 idx = where(a==1)

Exactly what we were looking for, thanks!

Chris
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] index of a value in an array

2010-10-06 Thread Benjamin Root
On Wed, Oct 6, 2010 at 7:23 AM, Nicolas Bigaouette nbigaoue...@gmail.comwrote:

 On Wed, Oct 6, 2010 at 5:26 AM, Chris Withers ch...@simplistix.co.ukwrote:

 Hi All,

 Given an array such as:

 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

 How can I find the index of a particular number in the array?

 (ie: if it was a list, I'd do [1,2,3,4].index(3))

 cheers,

 Chris
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion


 Try with numpy.where(array == value) and plain array == value
 IIRC, the first will return an array the size of the found elements and the
 second will return an array (or list?) of True/False.
 Either can be used as index = numpy.where() and index = (array ==
 value) and then array[index].
 N


Wouldn't it be more technically correct to use numpy.nonzero()?  Returning
the index is a special behavior of not specifying the second argument to
numpy.where().

Ben Root
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] bug in ceil()

2010-10-06 Thread Christian Fischer
Hi All,

I use numpy 1.4.1 on Debian squeeze amd64.

I noticed that ceil() is not working properly.

If the input to ceil() is a float I expect a float to be returned but for inputs
in (-1.0, 0.0) the result is of type integer.

In [65]: np.__version__
Out[65]: '1.4.1'

In [66]: np.ceil(-1.1)
Out[66]: -1.0

In [67]: np.ceil(-0.734)
Out[67]: -0

In [68]: np.ceil(-0.256)
Out[68]: -0

In [69]: np.ceil(-0.0)
Out[69]: -0

In [70]: np.ceil(0.2)
Out[70]: 1.0

Best wishes
Christian


-- 
  Dipl.-Ing. Christian Fischer
  Institute of Engineering and Computational Mechanics
  (name in German:  Institut für Technische und Numerische Mechanik)
  University of Stuttgart, Pfaffenwaldring 9, 70569 Stuttgart, Germany
  mailto:cfisc...@itm.uni-stuttgart.de, tel 0711-685-66565, fax -66400
  http://www.itm.uni-stuttgart.de/staff/Fischer




smime.p7s
Description: S/MIME Cryptographic Signature
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] bug in ceil()

2010-10-06 Thread Michael Droettboom

Are you sure?

In [4]: type(np.ceil(-0.0))
Out[4]: type 'numpy.float64'

Mike

On 10/06/2010 01:55 PM, Christian Fischer wrote:

Hi All,

I use numpy 1.4.1 on Debian squeeze amd64.

I noticed that ceil() is not working properly.

If the input to ceil() is a float I expect a float to be returned but for inputs
in (-1.0, 0.0) the result is of type integer.

In [65]: np.__version__
Out[65]: '1.4.1'

In [66]: np.ceil(-1.1)
Out[66]: -1.0

In [67]: np.ceil(-0.734)
Out[67]: -0

In [68]: np.ceil(-0.256)
Out[68]: -0

In [69]: np.ceil(-0.0)
Out[69]: -0

In [70]: np.ceil(0.2)
Out[70]: 1.0

Best wishes
Christian


   



___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
   



--
Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] bug in ceil()

2010-10-06 Thread Charles R Harris
On Wed, Oct 6, 2010 at 11:55 AM, Christian Fischer 
cfisc...@itm.uni-stuttgart.de wrote:

 Hi All,

 I use numpy 1.4.1 on Debian squeeze amd64.

 I noticed that ceil() is not working properly.

 If the input to ceil() is a float I expect a float to be returned but for
 inputs
 in (-1.0, 0.0) the result is of type integer.

 In [65]: np.__version__
 Out[65]: '1.4.1'

 In [66]: np.ceil(-1.1)
 Out[66]: -1.0

 In [67]: np.ceil(-0.734)
 Out[67]: -0

 In [68]: np.ceil(-0.256)
 Out[68]: -0

 In [69]: np.ceil(-0.0)
 Out[69]: -0

 In [70]: np.ceil(0.2)
 Out[70]: 1.0


The -0 is a float type, you can't get it with integers. The printing might
could be tweaked to return -0.0, which would be less misleading. The thing
that bothers me is that -0 is returned instead of 0.0, but that can be
fixed.

In [2]: ceil(-.5)
Out[2]: -0

In [3]: ceil(-.5) + 0
Out[3]: 0.0

Please open a ticket, maybe two.

Chuck
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] bug in ceil()

2010-10-06 Thread Nils Wagner
On Wed, 06 Oct 2010 19:55:00 +0200
  Christian Fischer cfisc...@itm.uni-stuttgart.de wrote:
 Hi All,
 
 I use numpy 1.4.1 on Debian squeeze amd64.
 
 I noticed that ceil() is not working properly.
 
 If the input to ceil() is a float I expect a float to be 
returned but for inputs
 in (-1.0, 0.0) the result is of type integer.
 
 In [65]: np.__version__
 Out[65]: '1.4.1'
 
 In [66]: np.ceil(-1.1)
 Out[66]: -1.0
 
 In [67]: np.ceil(-0.734)
 Out[67]: -0
 
 In [68]: np.ceil(-0.256)
 Out[68]: -0
 
 In [69]: np.ceil(-0.0)
 Out[69]: -0
 
 In [70]: np.ceil(0.2)
 Out[70]: 1.0
 
 Best wishes
 Christian
 
  
It's a float

 type(numpy.ceil(-0.4))
type 'numpy.float64'
 numpy.ceil(-0.4)
-0

Nils
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy on 64 bit x86_64

2010-10-06 Thread P Sanjey
Hi

I have a x86_64 Fedora core 3 machine where I want to install numpy
but python2.4 has been installed on this machine using Intel compilers
instead of gcc

Can someone tell me how I can install numpy on this architecture?

Thanks
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] point line distance

2010-10-06 Thread Renato Fabbri
supose you have a line defined by two points and a point. you want the distance

what are easiest possibilities? i am doing it, but its nasty'n ugly

-- 
GNU/Linux User #479299
skype: fabbri.renato
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] puzzle with boolean dtype

2010-10-06 Thread Alan G Isaac
Integer exponentiation fails (i.e., changes type)
with boolean dtype.  See below. Expected?

Alan Isaac


  a = np.array([[0,1,0],[0,0,1],[1,0,0]], dtype=np.bool_)
  a2 = a*a
  a3 = a2*a
  print(a3)
 [[False  True False]
  [False False  True]
  [ True False False]]
  print(a**3)
 [[0 1 0]
  [0 0 1]
  [1 0 0]]

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] point line distance

2010-10-06 Thread Zachary Pincus
Here's a good list of basic geometry algorithms:
http://www.softsurfer.com/algorithms.htm

Zach


On Oct 6, 2010, at 5:08 PM, Renato Fabbri wrote:

 supose you have a line defined by two points and a point. you want  
 the distance

 what are easiest possibilities? i am doing it, but its nasty'n ugly

 -- 
 GNU/Linux User #479299
 skype: fabbri.renato
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] puzzle with boolean dtype

2010-10-06 Thread Alan G Isaac
On 10/6/2010 5:52 PM, Charles R Harris wrote:
 What would you want in it's place? For consistancy, a**0 == all_true, a**i == 
 a, i  0?

Yes.

Alan Isaac

PS Boolean matrix powers are well defined and useful.
I found this oddity by accidentally creating an array instead of a matrix.
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] known failure test decorator not treated as expected by nose

2010-10-06 Thread Jonathan March
It appears that the numpy testing decorators for skipping and for
known failure should behave similarly to each other, at least from
their descriptions here:
http://projects.scipy.org/numpy/wiki/TestingGuidelines#known-failures-skipping-tests

Yet in the following example, run under nose, they behave quite differently:


from numpy.testing import  dec

@dec.knownfailureif( True, known failure test message)
def test_will_fail():
pass

@dec.skipif( True, skip test message)
def test_to_skip():
pass


The skipped test is marked as S as expected, but when the
KnownFailureTest exception is raised, nose handles it as an error
(marked E rather than K as expected, and printing the stack
trace.)

It's also interesting that nose has a --no-skip option but no
corresponding option for ignoring known failure decorators.

Why the discrepancy? Who is out of step with whom? This is with numpy
1.4.0 and nosetests 0.11.3

Thanks,
Jonathan March
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] known failure test decorator not treated as expected by nose

2010-10-06 Thread josef . pktd
On Wed, Oct 6, 2010 at 7:08 PM, Jonathan March jma...@enthought.com wrote:
 It appears that the numpy testing decorators for skipping and for
 known failure should behave similarly to each other, at least from
 their descriptions here:
 http://projects.scipy.org/numpy/wiki/TestingGuidelines#known-failures-skipping-tests

 Yet in the following example, run under nose, they behave quite differently:

 
 from numpy.testing import  dec

 @dec.knownfailureif( True, known failure test message)
 def test_will_fail():
    pass

 @dec.skipif( True, skip test message)
 def test_to_skip():
    pass
 

 The skipped test is marked as S as expected, but when the
 KnownFailureTest exception is raised, nose handles it as an error
 (marked E rather than K as expected, and printing the stack
 trace.)

 It's also interesting that nose has a --no-skip option but no
 corresponding option for ignoring known failure decorators.

 Why the discrepancy? Who is out of step with whom? This is with numpy
 1.4.0 and nosetests 0.11.3

just as additional information

This only happens on the command line, with nosetests packagename
but not with the function, packagename.test()

Josef


 Thanks,
 Jonathan March
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] unpack argument in loadtxt/genfromtxt does not work as documented

2010-10-06 Thread Chris Fonnesbeck
The documentation for loadtxt and genfromtxt state that the unpack
argument functions as follows:

If True, the returned array is transposed, so that arguments may be
unpacked using x, y, z = loadtxt(...).

In practice, this does not always occur. I have a csv file of mixed
data types, and try importing it via:

genfromtxt(progestogens.csv, delimiter=,, names=True,
dtype=dtype([('id', int),('study', '|S25'),('year', int),('treat',
int),('drug', '|S25'),('form', '|S10'),('ptb', int),('mgest',
int),('lab', int),('n', int),('y', int),('var', '|S5'),('wt',
int),('sdwt', int)]), unpack=True)

With unpack=True, I would expect the data to be presented by columns,
however the resulting array is by rows:


array([(1, 'Meis', 2003, 1, '17P', 'IM', 1, 0, 0, 306, 111, 'ptb'),
   (1, 'Meis', 2003, 0, '17P', 'IM', 1, 0, 0, 153, 84, 'ptb'),
   (2, 'Rai', 2009, 1, 'Progesterone', 'Oral', 1, 0, 0, 74, 29, 'ptb'),
   (2, 'Rai', 2009, 0, 'Progesterone', 'Oral', 1, 0, 0, 74, 44, 'ptb'),
...

The same behaviour occurs using loadtxt. Moreover, this array is
untransposeable, so I am stuck with having to iterate over all the
rows, making genfromtxt no better than csv.reader.
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Assigning complex value to real array

2010-10-06 Thread Andrew P. Mullhaupt


I came across this gem yesterday


   from numpy import *
   R = ones((2))
   R[0] = R[0] * 1j
   R
 ...array([ 0., 1.])
   R = ones((2), 'complex')
   R[0] = R[0] * 1j
   R
 array([ 0.+1.j, 1.+0.j])

and I read that this behavior is actually intended for some reason about 
how Python wants relations between types to be such that this mistake is 
unavoidable.


So can we have a new abstract floating type which is a complex, but is 
implemented so that the numbers where the imaginary part is zero 
represent and operate on that imaginary part implicitly? By containing 
all these semantics within one type, then presumably we avoid problems 
with ideas relationships between types.


Best regards,
Andrew Mullhaupt
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Assigning complex value to real array

2010-10-06 Thread sicre



Andrew P. Mullhaupt wrote:
 
 
 I came across this gem yesterday
 
 
 from numpy import *
 R = ones((2))
 R[0] = R[0] * 1j
 R
   ...array([ 0., 1.])
 R = ones((2), 'complex')
 R[0] = R[0] * 1j
 R
   array([ 0.+1.j, 1.+0.j])
 
 and I read that this behavior is actually intended for some reason about 
 how Python wants relations between types to be such that this mistake is 
 unavoidable.
 
 So can we have a new abstract floating type which is a complex, but is 
 implemented so that the numbers where the imaginary part is zero 
 represent and operate on that imaginary part implicitly? By containing 
 all these semantics within one type, then presumably we avoid problems 
 with ideas relationships between types.
 
 Best regards,
 Andrew Mullhaupt
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 
Sorry, but this post is in the wrong topic since it has nothing to do with
my question.
-- 
View this message in context: 
http://old.nabble.com/Meshgrid-with-Huge-Arrays-tp29902859p29903028.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Assigning complex value to real array

2010-10-06 Thread Andrew P. Mullhaupt
  On 10/7/2010 1:14 AM, sicre wrote:


 Andrew P. Mullhaupt wrote:
 (assigning complex value to real array)
 Sorry, but this post is in the wrong topic since it has nothing to do with
 my question.

How did this happen? I created a new subject.

Best regards,
Andrew Mullhaupt
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Assigning complex value to real array

2010-10-06 Thread Charles R Harris
On Wed, Oct 6, 2010 at 11:07 PM, Andrew P. Mullhaupt
d...@zen-pharaohs.comwrote:


 I came across this gem yesterday
  from numpy import *
  R = ones((2))
  R[0] = R[0] * 1j
  R
 ...array([ 0., 1.])
  R = ones((2), 'complex')
  R[0] = R[0] * 1j
  R
 array([ 0.+1.j, 1.+0.j]) and I read that this behavior is actually
 intended for some reason about how Python wants relations between types to
 be such that this mistake is unavoidable.


It's because an element of a real array only has space for a real and you
can't fit a complex in there. Some other software which is less strict about
types may allow such things, but it comes at a cost.


 So can we have a new abstract floating type which is a complex, but is
 implemented so that the numbers where the imaginary part is zero represent
 and operate on that imaginary part implicitly? By containing all these
 semantics within one type, then presumably we avoid problems with ideas
 relationships between types.


Short answer: no. If you want complex just use a complex array. Changing
types like you propose would require making a new copy or reserving space
ahead of time, which would be wasteful. It could also be done with lists or
objects, but then you would lose speed. Newer versions of numpy will warn
you that the imaginary part is going to be discarded in your first example.

Chuck
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion