complex-numbers

Laplace transform using numerical integration in Python has very poor precision

Laplace transform using numerical integration in Python has very poor precision Question: 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 all my work below. I …

Total answers: 1

Complex numbers in Cython

Complex numbers in Cython Question: 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 complex complex128_t so it seems this is just a simple …

Total answers: 1

Equivalent of j in NumPy

Equivalent of j in NumPy Question: 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 <function imag at 0x2368140> >>> np.imag(3) array(0) …

Total answers: 3

How to plot complex numbers (Argand Diagram) using matplotlib

How to plot complex numbers (Argand Diagram) using matplotlib Question: 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 Asked By: Lee || Source Answers: I’m not sure exactly what …

Total answers: 5

Calculating nth Roots of Unity in Python

Calculating nth Roots of Unity in Python Question: So, I’m trying to write an algorithm croot(k, n), that returns the kth root of unity with n == n. I’m getting mostly the right answer, however it’s giving me really weird representations that seem wrong for certain numbers. Here is an example. import cmath def croot(k, …

Total answers: 4

Complex number in ctypes

Complex number in ctypes Question: This might be a bit foolish but I’m trying to use ctypes to call a function that receives a complex vector as a paramter. But in ctypes there isn’t a class c_complex. Does anybody know how to solve this? edit: I’m refering to python’s ctypes, in case there are others…. …

Total answers: 6

Complex numbers in python

Complex numbers in python Question: Are complex numbers a supported data-type in Python? If so, how do you use them? Asked By: I159 || Source Answers: The following example for complex numbers should be self explanatory including the error message at the end >>> x=complex(1,2) >>> print x (1+2j) >>> y=complex(3,4) >>> print y (3+4j) …

Total answers: 4

Use scipy.integrate.quad to integrate complex numbers

Use scipy.integrate.quad to integrate complex numbers Question: 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 there any way to integrate a complex …

Total answers: 2

Is there a library for large precision complex numbers in Python?

Is there a library for large precision complex numbers in Python? Question: Is there a library for large precision complex numbers in Python? Asked By: eddie || Source Answers: Take a look at mpmath which offers real and complex floating-point arithmetic with arbitrary precision. Answered By: David Heffernan You can also try numpy (official website). …

Total answers: 2

Numpy: Creating a complex array from 2 real ones?

Numpy: Creating a complex array from 2 real ones? Question: 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 performing array functions on complex numbers? Here’s the …

Total answers: 9