question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Efficient way to create a complex array from two real arrays

See original GitHub issue

Dear 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
eric-wiesercommented, Apr 22, 2020

Your approach using empty is as optimal as you can get, short of avoiding the intermediate a and b arrays completely.

0reactions
zhcuicommented, Apr 23, 2020

The elements are contiguous and interleaved: c[0].real, c[0].imag, c[1].real, c[1].imag, not c[0].real, c[1].real, … c[n-1].real, c[0].imag, c[1].imag, …

I see, I guess this is the reason why we can view the (n, 2) double array to a complex array.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found