Formatting Complex Numbers

You could do it as is shown below using the str.format() method: >>> n = 3.4+2.3j >>> n (3.4+2.3j) >>> ‘({0.real:.2f} + {0.imag:.2f}i)’.format(n) ‘(3.40 + 2.30i)’ >>> ‘({c.real:.2f} + {c.imag:.2f}i)’.format(c=n) ‘(3.40 + 2.30i)’ To make it handle both positive and negative imaginary portions properly, you would need a (even more) complicated formatting operation: >>> n … Read more

Numpy: Creating a complex array from 2 real ones?

This seems to do what you want: numpy.apply_along_axis(lambda args: [complex(*args)], 3, Data) Here is another solution: # The ellipsis is equivalent here to “:,:,:”… numpy.vectorize(complex)(Data[…,0], Data[…,1]) And yet another simpler solution: Data[…,0] + 1j * Data[…,1] PS: If you want to save memory (no intermediate array): result = 1j*Data[…,1]; result += Data[…,0] devS’ solution below … Read more

Use scipy.integrate.quad to integrate complex numbers

What’s wrong with just separating it out into real and imaginary parts? scipy.integrate.quad requires the integrated function return floats (aka real numbers) for the algorithm it uses. import scipy from scipy.integrate import quad def complex_quadrature(func, a, b, **kwargs): def real_func(x): return scipy.real(func(x)) def imag_func(x): return scipy.imag(func(x)) real_integral = quad(real_func, a, b, **kwargs) imag_integral = quad(imag_func, … Read more

Complex numbers in python

In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex literals easily: >>> 1j 1j >>> 1J 1j >>> 1j * 1j (-1+0j) The ‘j’ suffix comes from electrical engineering, where the variable ‘i’ is usually used for current. (Reasoning found here.) The type of … Read more

How to work with complex numbers in C?

This code will help you, and it’s fairly self-explanatory: #include <stdio.h> /* Standard Library of Input and Output */ #include <complex.h> /* Standard Library of Complex Numbers */ int main() { double complex z1 = 1.0 + 3.0 * I; double complex z2 = 1.0 – 4.0 * I; printf(“Working with complex numbers:\n\v”); printf(“Starting values: … Read more