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

1215 questions
130
votes
2 answers

Complex numbers in python

Are complex numbers a supported data-type in python? If so, how do you use them?
I159
  • 24,762
  • 27
  • 88
  • 124
129
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
  • 18,389
  • 21
  • 77
  • 120
75
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,789
  • 4
  • 19
  • 23
48
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,385
  • 3
  • 14
  • 16
41
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,617
  • 2
  • 16
  • 36
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
  • 328,018
  • 92
  • 813
  • 992
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,190
  • 8
  • 33
  • 67
32
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!
28
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
  • 16,448
  • 25
  • 85
  • 133
27
votes
2 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
  • 7,483
  • 19
  • 63
  • 131
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
  • 873
  • 1
  • 8
  • 33
23
votes
4 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…
22
votes
3 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 …
Simon
  • 14,450
  • 4
  • 37
  • 97
22
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…
atomh33ls
  • 23,172
  • 18
  • 91
  • 149
21
votes
5 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…
1
2 3
80 81