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

The complex numbers extend the real numbers to allow negative numbers to have a square root. Every complex number can be expressed in the form x + y * i where x and y are real numbers and i² = -1; this is called the rectangular form of the number. The rectangular form leads to an interpretation of complex numbers as points on a plane, the same way real numbers are akin to points on a line. If y = 0, the number is a real number; if x = 0, the number is called an imaginary number.

A nonzero complex number has a family of representations in the form r exp(i φ) with r > 0, called the polar representation.

Representation and manipulation in programming languages

Floating point complex numbers

1395 questions
15
votes
4 answers

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 seems to work always cases where the conversion is…
nos
  • 19,875
  • 27
  • 98
  • 134
15
votes
3 answers

Problem casting STL complex to fftw_complex

The FFTW manual says that its fftw_complex type is bit compatible to std::complex class in STL. But that doesn't work for me: #include #include int main() { std::complex x(1,0); fftw_complex fx; fx =…
galadog
  • 960
  • 2
  • 10
  • 20
15
votes
2 answers

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 root finding algorithm that builds the companion…
Virginie
  • 289
  • 3
  • 11
15
votes
3 answers

Creating complex infinity with std::complex 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 #include using std; ... complex attempt1 = complex( numeric_limits::infinity(), …
14
votes
4 answers

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 this? Some examples of similar plots: Several…
endolith
  • 25,479
  • 34
  • 128
  • 192
13
votes
3 answers

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 "interleaved complex". (Previously such matrices had…
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
13
votes
1 answer

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

When I run this code on ideone.com, it prints (2,3): #include #include int main() { std::complex val = 2 + 3i; std::cout << val << std::endl; return 0; } But when I use clang on macOS 10.11.6, I get no…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
13
votes
4 answers

Division by complex 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 #include int main() { std::complex c = {1.e-162,0}; std::cout << 1.0/c << std::endl; return…
13
votes
3 answers

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 initialization". Does anybody know why? #include…
iloveponies
  • 175
  • 1
  • 1
  • 8
12
votes
2 answers

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 the first loop, looping over X0 - Xn-1... …
Timo Willemsen
  • 8,717
  • 9
  • 51
  • 82
12
votes
1 answer

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 numpy as np a = np.zeros(1000000, dtype=np.int16) b…
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
12
votes
1 answer

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 application. Does anyone know what I can do to keep…
user34028
  • 143
  • 7
12
votes
1 answer

typeid(complex(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(0.0,1.0)) != typeid(1.0i) Am I making a mistake here? Is this a compiler bug or…
roland
  • 123
  • 6
12
votes
2 answers

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 #include int main(void) { double…
Lebannen
  • 154
  • 1
  • 6
11
votes
5 answers

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 == complex(): print("Error 05: Complex…
user1786283
1 2
3
92 93