Questions tagged [complex-numbers]

Questions about complex numbers (numbers in the form of x + y∙i where i² = -1), types to represent them in programming languages, and libraries to manipulate them

Filter by
Sorted by
Tagged with
144votes
2answers
311kviews

Complex numbers in python

Are complex numbers a supported data-type in python? If so, how do you use them?
user avatar
  • 27k
136votes
5answers
206kviews

How to work with complex numbers in C?

How can I work with complex numbers in C? I see there is a complex.h header file, but it doesn't give me much information about how to use it. How to access real and imaginary parts in an efficient ...
user avatar
77votes
9answers
128kviews

Numpy: Creating a complex array from 2 real ones?

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[:,:,:,...
user avatar
  • 1,837
50votes
4answers
35kviews

C complex number and printf

How to print ( with printf ) complex number? For example, if I have this code: #include <stdio.h> #include <complex.h> int main(void) { double complex dc1 = 3 + 2*I; double ...
user avatar
  • 1,445
41votes
2answers
2kviews

Why does C++ mandate that complex only be instantiated for float, double, or long double?

According to the C++ ISO spec, §26.2/2: The effect of instantiating the template complex for any type other than float, double or long double is unspecified. Why would the standard authors ...
user avatar
41votes
1answer
6kviews

Complex numbers in Cython

What is the correct way to work with complex numbers in Cython? I would like to write a pure C loop using a numpy.ndarray of dtype np.complex128. In Cython, the associated C type is defined in ...
user avatar
  • 1,708
34votes
2answers
1kviews

Complex number arithmetic in Tcl?

Is there an equivalent to the expr command which works for complex numbers (represented lists of two doubles)? This library provides functions for complex number arithmetic, and this seems to be a ...
user avatar
  • 4,410
33votes
2answers
19kviews

What's a nice method to factor gaussian integers?

I already have prime factorization (for integers), but now I want to implement it for gaussian integers but how should I do it? thanks!
user avatar
  • 333
30votes
2answers
29kviews

Use scipy.integrate.quad to integrate complex numbers

I'm using right now the scipy.integrate.quad to successfully integrate some real integrands. Now a situation appeared that I need to integrate a complex integrand. quad seems not be able to do it, as ...
user avatar
  • 18k
29votes
2answers
79kviews

Equivalent of j in NumPy

What is the equivalent of Octave's j in NumPy? How can I use j in Python? In Octave: octave:1> j ans = 0 + 1i octave:1> j*pi/4 ans = 0.00000 + 0.78540i But in Python: >>> import ...
user avatar
  • 7,839
26votes
5answers
45kviews

Python Numpy - Complex Numbers - Is there a function for Polar to Rectangular conversion?

Is there a built-in Numpy function to convert a complex number in polar form, a magnitude and an angle (degrees) to one in real and imaginary components? Clearly I could write my own but it seems ...
user avatar
  • 25.7k
24votes
3answers
4kviews

Converting double to BigDecimal in Java

I wrote a Java program that calculates values for the Riemann Zeta Function. Inside the program, I made a library to calculate necessary complex functions such as atan, cos, etc. Everything inside ...
user avatar
  • 913
23votes
5answers
13kviews

Most memory-efficient way to compute abs()**2 of complex numpy ndarray

I'm looking for the most memory-efficient way to compute the absolute squared value of a complex numpy ndarray arr = np.empty((250000, 150), dtype='complex128') # common size I haven't found a ...
user avatar
22votes
6answers
34kviews

Formatting Complex Numbers

For a project in one of my classes we have to output numbers up to five decimal places.It is possible that the output will be a complex number and I am unable to figure out how to output a complex ...
user avatar
22votes
4answers
20kviews

Plot a complex function in Mathematica

How can I make a Mathematica graphics that copies the behaviour of complex_plot in sage? i.e. ... takes a complex function of one variable, and plots output of the function over the specified xrange ...
user avatar
  • 14.6k
20votes
4answers
68kviews

How to plot complex numbers (Argand Diagram) using matplotlib

I'd like to create an Argand Diagram from a set of complex numbers using matplotlib. Are there any pre-built functions to help me do this? Can anyone recommend an approach? Image by LeonardoG, CC-SA-...
user avatar
  • 25.7k
20votes
4answers
18kviews

Format of complex number in Python

I am wondering about the way Python (3.3.0) prints complex numbers. I am looking for an explanation, not a way to change the print. Example: >>> complex(1,1)-complex(1,1) 0j Why doesn't ...
user avatar
  • 4,263
20votes
2answers
18kviews

How to save and load an array of complex numbers using numpy.savetxt?

I want to use numpy.savetxt() to save an array of complex numbers to a text file. Problems: If you save the complex array with the default format string, the imaginary part is discarded. If you use ...
user avatar
  • 53.5k
19votes
4answers
37kviews

FFT real/imaginary/abs parts interpretation

I'm currently learning about discret Fourier transform and I'm playing with numpy to understand it better. I tried to plot a "sin x sin x sin" signal and obtained a clean FFT with 4 non-zero points. ...
user avatar
  • 12.8k
19votes
1answer
1kviews

Complex literal 'i' used in function argument

There seems to be a problem, using the literal i in C++ with std::complex. Consider the following code: std::complex<double> a = -1.0i * 42.0; std::complex<double> b = a + 1.0i; The ...
user avatar
  • 3,469
18votes
13answers
5kviews

As a programmer how would you explain imaginary numbers?

As a programmer I think it is my job to be good at math but I am having trouble getting my head round imaginary numbers. I have tried google and wikipedia with no luck so I am hoping a programmer can ...
user avatar
  • 4,923
18votes
2answers
2kviews

Why does abs(complex<int>) always return zero?

The following code with VS2010 prints 0, contrary to my expectations: #include <complex> #include <iostream> using namespace std; int main(void) { complex<int> z(20, 200); ...
user avatar
  • 4,837
18votes
2answers
623views

Is there a reason why implementations allow instantiation of std::complex with unsupported types

edit note: originally question said illegal where now it says unspecified. Thanks to video comment section of Jason Turner video recently I learned that std::complex<int> is unspecified. But all(...
user avatar
  • 26.3k
17votes
2answers
772views

Does ICC satisfy C99 specs for multiplication of complex numbers?

Consider this simple code: #include <complex.h> complex float f(complex float x) { return x*x; } If you compile it with -O3 -march=core-avx2 -fp-model strict using the Intel Compiler you ...
user avatar
  • 17.6k
16votes
6answers
4kviews

Fastest way to calculate the abs()-values of a complex array

I want to calculate the absolute values of the elements of a complex array in C or C++. The easiest way would be for(int i = 0; i < N; i++) { b[i] = cabs(a[i]); } But for large vectors that ...
user avatar
  • 3,623
16votes
5answers
13kviews

matplotlib imshow complex 2D array

Is there any good way how to plot 2D array of complex numbers as image in mathplotlib ? It makes very much sense to map magnitude of complex number as "brightness" or "saturation" and phase as "Hue" (...
user avatar
16votes
2answers
14kviews

Square root of complex numbers in python

I have run across some confusing behaviour with square roots of complex numbers in python. Running this code: from cmath import sqrt a = 0.2 b = 0.2 + 0j print(sqrt(a / (a - 1))) print(sqrt(b / (b - ...
user avatar
15votes
3answers
11kviews

Problem casting STL complex<double> to fftw_complex

The FFTW manual says that its fftw_complex type is bit compatible to std::complex<double> class in STL. But that doesn't work for me: #include <complex> #include <fftw3.h> int main()...
user avatar
  • 950
15votes
4answers
5kviews

Python augmented assignment issue

i ran into something interesting about the python augmented assignment += it seems to be automatic data type conversion is not always done for a += b if a is a 'simpler' data type, while a = a + b ...
user avatar
  • 17.9k
15votes
2answers
1kviews

Complex-coefficient polynomial root finding in Java

I'm trying to find a way to compute roots of a polynomial with complex coefficients in Java (i.e. an equivalent of what is ridiculously easily done with roots() in MATLAB). I'm ready to recode a ...
user avatar
  • 289
14votes
5answers
13kviews

Complex numbers product using only three multiplications

We do complex number multiplication as follows: (a + i * b) * (c + i * d) = (a * c - b * d) + i * (a * d + b * c) The real and imaginary parts of the result are real part = (a * c - b * d) imag ...
user avatar
14votes
4answers
3kviews

Is there any way to use bivariate colormaps in matplotlib?

In other words, I want to make a heatmap (or surface plot) where the color varies as a function of 2 variables. (Specifically, luminance = magnitude and hue = phase.) Is there any native way to do ...
user avatar
  • 23.1k
14votes
3answers
1kviews

Creating complex infinity with std::complex<T> in C++

I'm trying to create a complex infinity equal to Inf+Inf*j where j is the complex variable. When I do this : #include <complex> #include <limits> using std; ... complex<double> ...
user avatar
13votes
4answers
1kviews

Division by complex<double> in clang++ versus g++

When I compile the following code with g++ (4.8.1 or 4.9.0) or clang++ (3.4) I get different outputs. #include <iostream> #include <complex> int main() { std::complex<double> c = ...
user avatar
13votes
1answer
3kviews

Why don't complex-number literals work in clang?

When I run this code on ideone.com, it prints (2,3): #include <iostream> #include <complex> int main() { std::complex<double> val = 2 + 3i; std::cout << val << ...
user avatar
  • 110k
13votes
3answers
1kviews

Casting complex to real without data copy in MATLAB R2018a and newer

Since MATLAB R2018a, complex-valued matrices are stored internally as a single data block, with the real and imaginary component of each matrix element stored next to each other -- they call this "...
user avatar
  • 48.4k
12votes
1answer
666views

typeid(complex<double>(0.0,1.0)) != typeid(1.0i)

Using gcc 4.9 I found that types generated with type literal for complex numbers are not the same as when created by conventional means, i.e.: typeid(complex<double>(0.0,1.0)) != typeid(1.0i) ...
user avatar
  • 123
12votes
2answers
6kviews

Compiling C code in Visual Studio 2013 with complex.h library

http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx C99 support added visual studio 2013, but I cant use complex.h in my "C" code. #include <stdio.h&...
user avatar
  • 154
12votes
2answers
2kviews

What is wrong with this fourier transform implementation

I'm trying to implement a discrete fourier transform, but it's not working. I'm probably have written a bug somewhere, but I haven't found it yet. Based on the following formula: This function does ...
user avatar
12votes
1answer
1kviews

numpy.arctanh(x) for x >= 1 returns NaN but I want complex

When I perform the operation numpy.arctanh(x) for x >= 1, it returns nan, which is odd because when I perform the operation in Wolfram|alpha, it returns complex values, which is what I need for my ...
user avatar
  • 143
11votes
3answers
10kviews

C Complex Numbers in C++?

The following code compiles and runs just fine in C (at least according to 'gcc -std=gnu99'), but it fails to compile under C++, giving "line 5: error: cannot convert 'double' to 'double complex' in ...
user avatar
11votes
3answers
9kviews

Complex Mul and Div using sse Instructions

Is performing complex multiplication and division beneficial through SSE instructions? I know that addition and subtraction perform better when using SSE. Can someone tell me how I can use SSE to ...
user avatar
  • 484
11votes
3answers
4kviews

Scipy: Speeding up calculation of a 2D complex integral

I want to repeatedly calculate a two-dimensional complex integral using dblquad from scipy.integrate. As the number of evaluations will be quite high I would like to increase the evaluation speed of ...
user avatar
  • 351
11votes
2answers
1kviews

More on using i and j as variables in Matlab: speed

The Matlab documentation says that For speed and improved robustness, you can replace complex i and j by 1i. For example, instead of using a = i; use a = 1i; The robustness part is clear, ...
user avatar
  • 109k
11votes
2answers
1kviews

Complex numbers passed by-value from C++ to C does not seem to work on powerpc

When I'm passing a complex float(complex.h) from a c++ caller to a c library, the value does not pass correctly when running on a 32 bit power pc. I was using two different open source software ...
user avatar
  • 119
10votes
5answers
24kviews

Identifying a complex number

I am creating a calculator application for all types of mathematical algorithms. However, I want to identify if a root is complex and then have an exception for it. I came up with this: if x == ...
user avatar
10votes
1answer
4kviews

What's the fastest way to convert an interleaved NumPy integer array to complex64?

I have a stream of incoming data that has interleaved real and imaginary integers. Converting these to complex64 values is the slowest operation in my program. This is my current approach: import ...
user avatar
  • 12.7k
10votes
3answers
10kviews

Unwrap angle to have continuous phase

Let's say I have an array of phases similar to this: import numpy as np import matplotlib.pyplot as plt phase = np.linspace(0., 100., 1000) % np.pi plt.plot(phase) plt.show() (with many ...
user avatar
  • 35.6k
10votes
2answers
6kviews

Why is complex<double> * int not defined in C++?

The C++ program #include <complex> #include <iostream> int main() { std::complex<double> z(0,2); int n = 3; std::cout << z * n << std::endl; } yields an error: ...
user avatar
  • 4,362
10votes
2answers
192views

Do variables contain extra hidden metadata - aka When is zero not zero (but still is)

I hate having to ask this because I assume the answer must be simple, but I cannot for the life of me seem to track down the source. While trying to rewrite a function I ran across this problem: a = -...
user avatar
  • 103

15 30 50 per page
1
2 3 4 5
26