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
24
votes
3 answers

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 both programs is accessed through the double and…
Axion004
  • 943
  • 1
  • 11
  • 37
23
votes
6 answers

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 number with five decimal places. For floats I know…
22
votes
4 answers

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 and yrange as demonstrated below. The magnitude…
Simon
  • 14,631
  • 4
  • 41
  • 101
22
votes
4 answers

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. I naively told myself : "well, if I plot a "sin +…
Cyrille
  • 13,905
  • 2
  • 22
  • 41
22
votes
4 answers

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 it just print "0"? My guess is: to keep the output of type…
cxxl
  • 4,939
  • 3
  • 31
  • 52
21
votes
2 answers

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 fmt='%s', then numpy.loadtxt() can't load it…
ptomato
  • 56,175
  • 13
  • 112
  • 165
19
votes
1 answer

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 a = -1.0i * 42.0; std::complex b = a + 1.0i; The second line fails to compile with: error: no match for ‘operator+’…
Louen
  • 3,617
  • 1
  • 29
  • 49
19
votes
2 answers

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 - 1))) gives the output 0.5j -0.5j A similar…
CleptoMarcus
  • 193
  • 1
  • 1
  • 5
19
votes
2 answers

Why does abs(complex) always return zero?

The following code with VS2010 prints 0, contrary to my expectations: #include #include using namespace std; int main(void) { complex z(20, 200); cout << abs(z) << endl; return 0; } It works fine when…
Artium
  • 5,147
  • 8
  • 39
  • 60
18
votes
2 answers

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 is unspecified. But all(AFAIK) implementations seem to happily…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
18
votes
13 answers

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 explain in to me, give me an example of a number…
Tim Matthews
  • 5,031
  • 8
  • 38
  • 45
18
votes
5 answers

How to create a phase plot for a 2D array of complex numbers with matplotlib?

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" ( anyway Hue is nothing else than phase in RBG…
Prokop Hapala
  • 2,424
  • 2
  • 30
  • 59
17
votes
2 answers

Does ICC satisfy C99 specs for multiplication of complex numbers?

Consider this simple code: #include 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 get: f: vmovsldup xmm1, xmm0 …
Simd
  • 19,447
  • 42
  • 136
  • 271
17
votes
5 answers

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 part = (a * d + b * c) This involves four real…
16
votes
6 answers

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 will be slow. Is there a way to speed that up (by…
arc_lupus
  • 3,942
  • 5
  • 45
  • 81
1
2
3
92 93