Actually, none of the proposed solutions worked in my case (Python 2.7.6, NumPy 1.8.2).
But I've found out, that change of dtype from complex (standard Python library) to numpy.complex_ may help:
>>> import numpy as np
>>> x = 1 + 2 * 1j
>>> C = np.zeros((2,2),dtype=np.complex_)
>>> C
array([[ 0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j]])
>>> C[0,0] = 1+1j + x
>>> C
array([[ 2.+3.j, 0.+0.j],
[ 0.+0.j, 0.+0.j]])
Answer from Lenka42 on Stack OverflowActually, none of the proposed solutions worked in my case (Python 2.7.6, NumPy 1.8.2).
But I've found out, that change of dtype from complex (standard Python library) to numpy.complex_ may help:
>>> import numpy as np
>>> x = 1 + 2 * 1j
>>> C = np.zeros((2,2),dtype=np.complex_)
>>> C
array([[ 0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j]])
>>> C[0,0] = 1+1j + x
>>> C
array([[ 2.+3.j, 0.+0.j],
[ 0.+0.j, 0.+0.j]])
To insert complex x or x + something into C, you apparently need to treat it as if it were an array, so either index into x or assign it to a slice of C:
>>> C
array([[ 0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j]])
>>> C[0, 0:1] = x
>>> C
array([[ 0.47229555+0.7957525j, 0.00000000+0.j ],
[ 0.00000000+0.j , 0.00000000+0.j ]])
>>> C[1, 1] = x[0] + 1+1j
>>> C
array([[ 0.47229555+0.7957525j, 0.00000000+0.j ],
[ 0.00000000+0.j , 1.47229555+1.7957525j]])
It looks like NumPy isn't handling this case correctly. Consider submitting a bug report.
Videos
This seems to do what you want:
numpy.apply_along_axis(lambda args: [complex(*args)], 3, Data)
Here is another solution:
# The ellipsis is equivalent here to ":,:,:"...
numpy.vectorize(complex)(Data[...,0], Data[...,1])
And yet another simpler solution:
Data[...,0] + 1j * Data[...,1]
PS: If you want to save memory (no intermediate array):
result = 1j*Data[...,1]; result += Data[...,0]
devS' solution below is also fast.
There's of course the rather obvious:
Data[...,0] + 1j * Data[...,1]
If you want to create an array containing complex values, you need to specify a complex type to numpy:
>>> A = np.zeros((3,3), dtype=complex)
>>> print A
[[ 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]]
>>> A[0,0] = 1. + 2.j
>>> print A
[[ 1.+2.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]]
You need to convert the array's type.
Example:
>>> from numpy import array, dtype
>>> A = array([1, 2, 3])
>>> A[0] = 1+2j
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to long
>>> B = A.astype(dtype("complex128"))
>>> B[0] = 1+2j
>>> B
array([ 1.+2.j, 2.+0.j, 3.+0.j])
>>>
Are you trying to compute a complex valued array A, or a real valued array A? If the former, then declare A as:
A = np.zeros((2*M + 2,nt), dtype=complex)
Otherwise, cast your complex value d[0, 0] to a real using:
A[1, :] = (1+4j).real * np.ones((1,1))
This is because by default numpy array holds zeros which is int type and you are trying to assign a complex array to integer array. If you write A = np.zeros((2*M+2, nt), dtype=np.complex) it should fix the problem. dtype parameter basically tells numpy to cast elements complex 0 which is actually 0+0.j.
Python has built-in support for complex numbers. You can just enter them like that:
>>> a = 2 + 3j # or: complex(2,3)
>>> a
(2+3j)
>>> type(a)
<type 'complex'>
>>> a.real
2.0
>>> a.imag
3.0
>>>
As for the container, in Python you can start with a list:
>>> complex_nums_list = [2+3j, 3+4j, 4+5j]
>>> complex_nums_list
[(2+3j), (3+4j), (4+5j)]
Or you can use numpy.array, which would be more suited for numerical applications.
Extremely simple:
Python's has both a native list and a native complex type, so:
c = complex(real,imag)
or just
c = 1 + 2j
does the trick of creating one;
complexes = [ complex(i, i) for i in range(100) ]
creates thousand complex values in a list complexes.
You might want to have a look at numpy:
import numpy
arr = numpy.ndarray(1000, dtype=numpy.complex128)