Efficient way to create a complex array from two real arrays
See original GitHub issueDear NumPy developers,
This may be more like a question. I have two real arrays (a
and b
), and I would like create a complex array (c
) which takes the two real arrays as its real and imaginary parts respectively.
The simplest one would be
c = a + b * 1.0j
However, since my data size is quite large, such code is not very efficient.
We can also do the following,
c = np.empty(data_shape, dtype=np.complex128)
c.real = a
c.imag = b
I am wondering is there a better way to do that (e.g. using buffer
or something)?
Thank you very much! Zhihao
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Numpy: Creating a complex array from 2 real ones?
The first represents the real part, and the second represents the imaginary part. The view method of the array changes the dtype of...
Read more >Create complex array - MATLAB complex - MathWorks
This MATLAB function creates a complex output, z, from two real inputs, such that z = a + bi.
Read more >How to create an array of complex numbers in Python - YouTube
How to create an array of complex numbers in Python. ... Ultimate Guide to NumPy Arrays - VERY DETAILED TUTORIAL for beginners!
Read more >PYTHON : Numpy: Creating a complex array from 2 real ones?
PYTHON : Numpy: Creating a complex array from 2 real ones? [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] ...
Read more >Extracting the real and imaginary parts of an NumPy array of ...
real () : To find real part of the complex number ... creating a NumPy array. complex_num = np.array([ - 1 + 9j...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your approach using
empty
is as optimal as you can get, short of avoiding the intermediatea
andb
arrays completely.I see, I guess this is the reason why we can view the
(n, 2)
double array to a complex array.