I swear this should be so easy... Why is it not? :(
我发誓这应该很容易……为什么不呢?:(
In fact, I want to combine 2 parts of the same array to make a complex array:
实际上,我想把相同数组的两个部分组合成一个复杂的数组:
Data[:,:,:,0] , Data[:,:,:,1]
These don't work:
这些不工作:
x = np.complex(Data[:,:,:,0], Data[:,:,:,1])
x = complex(Data[:,:,:,0], Data[:,:,:,1])
Am I missing something? Does numpy not like performing array functions on complex numbers? Here's the error:
我遗漏了什么东西?numpy不喜欢在复数上执行数组函数吗?这是一个错误:
TypeError: only length-1 arrays can be converted to Python scalars
7 个解决方案
#1
54
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):
PS:如果你想保存内存(没有中间数组):
result = 1j*Data[...,1]; result += Data[...,0]
devS' solution below is also fast.
devS的解决方案也很快。
#2
28
There's of course the rather obvious:
当然有一个相当明显的现象:
Data[...,0] + 1j * Data[...,1]
#3
14
If your real and imaginary parts are the slices along the last dimension and your array is contiguous along the last dimension, you can just do
如果你的实部和虚部是沿最后一个维度的切片,你的数组在最后一个维度上是连续的,你可以这样做。
A.view(dtype=np.complex128)
If you are using single precision floats, this would be
如果您使用的是单精度浮点数,这将是
A.view(dtype=np.complex64)
Here is a fuller example
这里有一个更完整的例子
import numpy as np
from numpy.random import rand
# Randomly choose real and imaginary parts.
# Treat last axis as the real and imaginary parts.
A = rand(100, 2)
# Cast the array as a complex array
# Note that this will now be a 100x1 array
A_comp = A.view(dtype=np.complex128)
# To get the original array A back from the complex version
A = A.view(dtype=np.float64)
If you want to get rid of the extra dimension that stays around from the casting, you could do something like
如果你想摆脱多余的维数,你可以做一些类似的事情
A_comp = A.view(dtype=np.complex128)[...,0]
This works because, in memory, a complex number is really just two floating point numbers. The first represents the real part, and the second represents the imaginary part. The view method of the array changes the dtype of the array to reflect that you want to treat two adjacent floating point values as a single complex number and updates the dimension accordingly.
这是因为,在内存中,复数实际上只是两个浮点数。第一个表示实部,第二个表示虚部。数组的视图方法更改数组的dtype,以反映您希望将两个相邻的浮点值作为单个复数来处理,并相应地更新维度。
This method does not copy any values in the array or perform any new computations, all it does is create a new array object that views the same block of memory differently. That makes it so that this operation can be performed much faster than anything that involves copying values. It also means that any changes made in the complex-valued array will be reflected in the array with the real and imaginary parts.
此方法不复制数组中的任何值或执行任何新的计算,它所做的只是创建一个新数组对象,该对象以不同的方式查看相同的内存块。这使得这个操作比任何涉及复制值的操作都要快得多。它还意味着在复值数组中所做的任何更改都将反映在具有实部和虚部的数组中。
It may also be a little trickier to recover the original array if you remove the extra axis that is there immediately after the type cast. Things like A_comp[...,np.newaxis].view(np.float64)
do not currently work because, as of this writing, NumPy doesn't detect that the array is still C-contiguous when the new axis is added. See this issue. A_comp.view(np.float64).reshape(A.shape)
seems to work in most cases though.
如果在类型转换之后立即删除多余的轴,那么恢复原始数组可能也会更加困难。像A_comp[…,np.newaxis].view(np.float64)这样的东西目前不工作,因为在编写本文时,NumPy没有检测到添加新轴时数组仍然是C-contiguous。看到这个问题。不过,在大多数情况下,重塑(A.shape)似乎是可行的。
#4
13
This is what your are looking for:
这就是你想要的:
from numpy import array
a=array([1,2,3])
b=array([4,5,6])
a + 1j*b
->array([ 1.+4.j, 2.+5.j, 3.+6.j])
#5
7
I am python novice so this may not be the most efficient method but, if I understand the intent of the question correctly, steps listed below worked for me.
我是python新手,所以这可能不是最有效的方法,但是,如果我正确地理解了问题的意图,下面列出的步骤对我很有用。
>>> import numpy as np
>>> Data = np.random.random((100, 100, 1000, 2))
>>> result = np.empty(Data.shape[:-1], dtype=complex)
>>> result.real = Data[...,0]; result.imag = Data[...,1]
>>> print Data[0,0,0,0], Data[0,0,0,1], result[0,0,0]
0.0782889873474 0.156087854837 (0.0782889873474+0.156087854837j)
#6
2
import numpy as np
n = 51 #number of data points
# Suppose the real and imaginary parts are created independently
real_part = np.random.normal(size=n)
imag_part = np.random.normal(size=n)
# Create a complex array - the imaginary part will be equal to zero
z = np.array(real_part, dtype=complex)
# Now define the imaginary part:
z.imag = imag_part
print(z)
#7
0
That worked for me:
这工作对我来说:
input:
输入:
from scipy import *
array([[1,2],[3,2]]).astype(complex)
output:
输出:
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 2.+0.j]])
#1
54
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):
PS:如果你想保存内存(没有中间数组):
result = 1j*Data[...,1]; result += Data[...,0]
devS' solution below is also fast.
devS的解决方案也很快。
#2
28
There's of course the rather obvious:
当然有一个相当明显的现象:
Data[...,0] + 1j * Data[...,1]
#3
14
If your real and imaginary parts are the slices along the last dimension and your array is contiguous along the last dimension, you can just do
如果你的实部和虚部是沿最后一个维度的切片,你的数组在最后一个维度上是连续的,你可以这样做。
A.view(dtype=np.complex128)
If you are using single precision floats, this would be
如果您使用的是单精度浮点数,这将是
A.view(dtype=np.complex64)
Here is a fuller example
这里有一个更完整的例子
import numpy as np
from numpy.random import rand
# Randomly choose real and imaginary parts.
# Treat last axis as the real and imaginary parts.
A = rand(100, 2)
# Cast the array as a complex array
# Note that this will now be a 100x1 array
A_comp = A.view(dtype=np.complex128)
# To get the original array A back from the complex version
A = A.view(dtype=np.float64)
If you want to get rid of the extra dimension that stays around from the casting, you could do something like
如果你想摆脱多余的维数,你可以做一些类似的事情
A_comp = A.view(dtype=np.complex128)[...,0]
This works because, in memory, a complex number is really just two floating point numbers. The first represents the real part, and the second represents the imaginary part. The view method of the array changes the dtype of the array to reflect that you want to treat two adjacent floating point values as a single complex number and updates the dimension accordingly.
这是因为,在内存中,复数实际上只是两个浮点数。第一个表示实部,第二个表示虚部。数组的视图方法更改数组的dtype,以反映您希望将两个相邻的浮点值作为单个复数来处理,并相应地更新维度。
This method does not copy any values in the array or perform any new computations, all it does is create a new array object that views the same block of memory differently. That makes it so that this operation can be performed much faster than anything that involves copying values. It also means that any changes made in the complex-valued array will be reflected in the array with the real and imaginary parts.
此方法不复制数组中的任何值或执行任何新的计算,它所做的只是创建一个新数组对象,该对象以不同的方式查看相同的内存块。这使得这个操作比任何涉及复制值的操作都要快得多。它还意味着在复值数组中所做的任何更改都将反映在具有实部和虚部的数组中。
It may also be a little trickier to recover the original array if you remove the extra axis that is there immediately after the type cast. Things like A_comp[...,np.newaxis].view(np.float64)
do not currently work because, as of this writing, NumPy doesn't detect that the array is still C-contiguous when the new axis is added. See this issue. A_comp.view(np.float64).reshape(A.shape)
seems to work in most cases though.
如果在类型转换之后立即删除多余的轴,那么恢复原始数组可能也会更加困难。像A_comp[…,np.newaxis].view(np.float64)这样的东西目前不工作,因为在编写本文时,NumPy没有检测到添加新轴时数组仍然是C-contiguous。看到这个问题。不过,在大多数情况下,重塑(A.shape)似乎是可行的。
#4
13
This is what your are looking for:
这就是你想要的:
from numpy import array
a=array([1,2,3])
b=array([4,5,6])
a + 1j*b
->array([ 1.+4.j, 2.+5.j, 3.+6.j])
#5
7
I am python novice so this may not be the most efficient method but, if I understand the intent of the question correctly, steps listed below worked for me.
我是python新手,所以这可能不是最有效的方法,但是,如果我正确地理解了问题的意图,下面列出的步骤对我很有用。
>>> import numpy as np
>>> Data = np.random.random((100, 100, 1000, 2))
>>> result = np.empty(Data.shape[:-1], dtype=complex)
>>> result.real = Data[...,0]; result.imag = Data[...,1]
>>> print Data[0,0,0,0], Data[0,0,0,1], result[0,0,0]
0.0782889873474 0.156087854837 (0.0782889873474+0.156087854837j)
#6
2
import numpy as np
n = 51 #number of data points
# Suppose the real and imaginary parts are created independently
real_part = np.random.normal(size=n)
imag_part = np.random.normal(size=n)
# Create a complex array - the imaginary part will be equal to zero
z = np.array(real_part, dtype=complex)
# Now define the imaginary part:
z.imag = imag_part
print(z)
#7
0
That worked for me:
这工作对我来说:
input:
输入:
from scipy import *
array([[1,2],[3,2]]).astype(complex)
output:
输出:
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 2.+0.j]])