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

Complex numbers in python

Are complex numbers a supported data-type in Python? If so, how do you use them?
I159
  • 29,741
  • 31
  • 97
  • 132
145
votes
5 answers

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 way? Is there native functions to get module and…
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
86
votes
9 answers

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[:,:,:,1]) Am I missing something? Does numpy not like…
Duncan Tait
  • 1,997
  • 4
  • 20
  • 24
55
votes
3 answers

Why is log(inf + inf j) equal to (inf + 0.785398 j), In C++/Python/NumPy?

I've been finding a strange behaviour of log functions in C++ and numpy about the behaviour of log function handling complex infinite numbers. Specifically, log(inf + inf * 1j) equals (inf + 0.785398j) when I expect it to be (inf + nan * 1j). When…
Firman
  • 928
  • 7
  • 15
54
votes
2 answers

How to perform approximate structural pattern matching for floats and complex

I've read about and understand floating point round-off issues such as: >>> sum([0.1] * 10) == 1.0 False >>> 1.1 + 2.2 == 3.3 False >>> sin(radians(45)) == sqrt(2) / 2 False I also know how to work around these issues with math.isclose() and…
54
votes
4 answers

C complex number and printf

How to print ( with printf ) complex number? For example, if I have this code: #include #include int main(void) { double complex dc1 = 3 + 2*I; double complex dc2 = 4 + 5*I; double complex result; result = dc1…
gameboy
  • 1,505
  • 3
  • 15
  • 16
43
votes
1 answer

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 Cython/Includes/numpy/__init__.pxd as ctypedef double…
paugier
  • 1,838
  • 2
  • 20
  • 39
41
votes
2 answers

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 explicitly add this restriction? This makes it…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
35
votes
2 answers

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!
34
votes
2 answers

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 useful utility to define the required cexpr…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
32
votes
3 answers

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 numpy as np >>> np.imag >>>…
Programmer
  • 8,303
  • 23
  • 78
  • 162
31
votes
2 answers

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 the other scipy.integrate routines, so I ask: is…
Ivan
  • 19,560
  • 31
  • 97
  • 141
27
votes
5 answers

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 ufunc that would do exactly np.abs()**2. As an array…
27
votes
5 answers

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 like the type of thing for which there is an…
Lee
  • 29,398
  • 28
  • 117
  • 170
25
votes
5 answers

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-3.0
Lee
  • 29,398
  • 28
  • 117
  • 170
1
2 3
92 93