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

Multiplying complex with constant in C++

The following code fails to compile #include #include #include using namespace std; int main(void) { const double b=3; complex i(0, 1), comp; comp = b*i; comp = 3*i; return 0; } with …
Atilla Filiz
  • 2,383
  • 8
  • 29
  • 47
9
votes
1 answer

How to convert a wave file into numbers in R Studio

I have loaded audio files into R and would now like to get a list of the complex number samples so I can use FFT and Wavelet transforms on the samples. How do I get the list of numbers to work with whilst in R? I've tried 'audio$data', but get an…
EmmaL
  • 95
  • 1
  • 5
9
votes
2 answers

Why std::complex is not an arithmetic type?

I created the following Matrix class: template class Matrix { static_assert(std::is_arithmetic::value,""); public: Matrix(size_t n_rows, size_t n_cols); Matrix(size_t n_rows, size_t n_cols, const T& value); void…
user1434698
9
votes
3 answers

Efficient complex arithmetic in x86 assembly for a Mandelbrot loop

Consider the following program: for i=1 to 10000000 do z <- z*z + c where z and c are complex numbers. What are efficient x86 assembler implementations of this program using x87 vs SSE and single vs double precision arithmetic? EDIT I know I can…
J D
  • 48,105
  • 13
  • 171
  • 274
8
votes
2 answers

reinterpreting array of doubles as array of std::complex

While C++11 standard says this about reinterpreting std::complex as doubles: For any pointer to an element of an array of complex named p and any valid array index i, reinterpret_cast(p)[2*i] is the real part of the complex number…
Drobot Viktor
  • 323
  • 2
  • 11
8
votes
1 answer

Laplace transform using numerical integration in Python has very poor precision

I have written a function to compute the Laplace transform of a function using scipy.integrate.quad. It is not a very sophisticated function and currently performs poorly on the probability density function of an Erlang distribution. I have included…
8
votes
2 answers

GDB - Accessing real and imaginary parts of a complex number

When debugging a program which uses (either C or C++), gdb displays complex numbers as _M_value = xxx + yyy*I (with a type of complex double). While debugging, I need to print that number multiplied by a factor. The following does not work: print…
nimrodm
  • 23,081
  • 7
  • 58
  • 59
8
votes
5 answers

How to convert a string to a complex number in Python?

I'm trying to convert an input string to a float but when I do it I keep getting some kind of error, as shown in the sample below. >>> a = "3 + 3j" >>> b = complex(a) Traceback (most recent call last): File "", line 1, in…
Adeniregun Dipo
  • 115
  • 1
  • 1
  • 3
8
votes
2 answers

How to square two complex doubles with 256-bit AVX vectors?

Matt Scarpino gives a good explanation (although he admits he's not sure it's the optimal algorithm, I offer him my gratitude) for how to multiply two complex doubles with Intel's AVX intrinsics. Here's his method, which I've verified: __m256d vec1…
sacheie
  • 768
  • 6
  • 16
8
votes
4 answers

How to directly assign complex numbers to a variable?

Using the complex class and library, how do I assign a complex number to a variable? I understand that I can set the value when I first instantiate the complex number. I also understand that I can assign one instantiated complex number to…
kmiklas
  • 13,085
  • 22
  • 67
  • 103
8
votes
1 answer

Why does this key class for sorting heterogeneous sequences behave oddly?

Python 3.x's sorted() function cannot be relied on to sort heterogeneous sequences, because most pairs of distinct types are unorderable (numeric types like int, float, decimal.Decimal etc. being an exception): Python 3.4.2 (default, Oct 8 2014,…
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
8
votes
1 answer

Plot complex numbers in R with ggplot2

Is there a way to plot complex number in an elegant way with ggplot2? plot {graphics} does it for my snowflake vector of values, but I would prefer to have it in ggplot2. The ggplot2 tutorials I came across do not mention a complex word. snowflake…
Marta Karas
  • 4,967
  • 10
  • 47
  • 77
8
votes
1 answer

Why is .NET's Complex type broken?

I was astounded to find that the System.Numerics.Complex data type in .NET doesn't yield mathematically accurate results. Complex.Sqrt(-1) != Complex.ImaginaryOne Instead of (0, 1), I get (6.12303176911189E-17, 1), which looks a lot like a rounding…
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
7
votes
5 answers

C99: can imaginary part of complex be a negative zero

Is it possible to store negative zero in imaginary part of C99 complex float? How I should statically initialize complex constants with signed imaginary part? I have a small example, but I can't understand, why a and c are same and why -std=c99…
osgx
  • 90,338
  • 53
  • 357
  • 513
7
votes
4 answers

Why is __muldc3 called, when two std::complex are multiplied?

I naively assumed, that the complex number multiplication would be inlined by the compiler, for example for this function: #include void mult(std::complex &a, std::complex &b){ a*=b; } However, when complied by gcc…
ead
  • 32,758
  • 6
  • 90
  • 153