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

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 discontinuities like this) How to get an array of more…
Basj
  • 41,386
  • 99
  • 383
  • 673
11
votes
2 answers

Meaning and usage of complex number functor and monad?

I was kinda surprised when I read the source code of instances of Applicative Complex and Monad Complex from GHC Data.Complex module: -- | @since 4.9.0.0 instance Applicative Complex where pure a = a :+ a f :+ g <*> a :+ b = f a :+ g b liftA2…
Dannyu NDos
  • 2,458
  • 13
  • 32
11
votes
2 answers

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 libraries when I detected this problem. I've isolated…
dan
  • 119
  • 5
11
votes
3 answers

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 perform complex multiplication to get better…
pv.
  • 484
  • 1
  • 4
  • 9
11
votes
5 answers

How to round up a complex number?

How can I round up a complex number (e.g. 1.9999999999999998-2j) as 2-2j? When I tried using print(round(x,2)) it showed Traceback (most recent call last): File "C:\Python34\FFT.py", line 22, in print(round(x,2)) TypeError: type…
prav
  • 165
  • 1
  • 2
  • 8
11
votes
2 answers

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, as there might be variables called i or j. However, as…
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
11
votes
3 answers

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 my code. Dblquad does not seem to be able to…
Olaf
  • 371
  • 2
  • 11
10
votes
2 answers

Better use a tuple or numpy array for storing coordinates

I'm porting an C++ scientific application to python, and as I'm new to python, some problems come to my mind: 1) I'm defining a class that will contain the coordinates (x,y). These values will be accessed several times, but they only will be read…
Ivan
  • 19,560
  • 31
  • 97
  • 141
10
votes
2 answers

Implementing complex number comparison in Python?

I know that comparison operators with complex numbers can't be defined in general. That is why python throws a TypeError exception when trying to use out-of-the-box complex comparison. I understand why this is the case (please don't go off topic…
jorgeh
  • 1,727
  • 20
  • 32
10
votes
2 answers

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 = -j x = real(a) y = imag(a) y/x Which spits out…
Stunt
  • 103
  • 4
10
votes
6 answers

How to use complex number "i" in C++

I am coding a simple DFT algorithm now and I want to use the complex number i in complex exponential. I saw somebody use #include and #include, and then they used the overloaded symbol I such as exp(2*I) . But it seems it doesn't…
Cancan
  • 691
  • 3
  • 14
  • 23
10
votes
2 answers

why (0+0i)^{0} == (nan, nan) in c++

take a look at the code blew: #include #include int main() { std::cout << std::pow( std::complex(0,0), std::complex(0,0) ) << "\n"; std::cout << std::pow( std::complex(0,0), double(0) ) <<…
Feng Wang
  • 1,506
  • 15
  • 17
10
votes
2 answers

Why is complex * int not defined in C++?

The C++ program #include #include int main() { std::complex z(0,2); int n = 3; std::cout << z * n << std::endl; } yields an error: no match for ‘operator*’ in ‘z * n’. Why? I'm compiling with g++ 4.4.1. Perhaps…
Jitse Niesen
  • 4,492
  • 26
  • 23
10
votes
1 answer

Using metaclasses to override methods of complex builtin

As a learning exercise, I'm trying to implement a class which will emulate the behavior of python's complex builtin, but with different behavior of the __str__ and __repr__ methods: I want them to print in the format... (1.0,2.0) ...instead…
mszep
  • 410
  • 3
  • 12
9
votes
1 answer

Why does operator>> on complex not set eofbit if it reaches EOF?

I'm trying to read as many std::complex as possible from a file (or any std::istream). If the operation fails, I check for ios::eof(). If it hasn't been set, I assume that there was an error in parsing the data, and I can report to the user…
dennis
  • 192
  • 7