Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a Complex number in Numpy with the accuracy of a Decimal type?

I'm working in Python with NumPy arrays of complex numbers that extend well past the normal floating point limits of NumPy’s default Complex type (numbers greater than 10^500). I wanted to know if there was some way I could extend NumPy so that it will be able to handle complex numbers with this sort of magnitude. For example, is there a way to make a NumPy complex type that uses functionality from the Decimal module?

I know there are resources available such as mpmath that could probably do what I need. However, it is a requirement of my project that I use NumPy.

For anyone that's interested in why I need these enormous numbers, it's because I'm working on a numerical relativity simulation of the early universe.

like image 721
Anarki Avatar asked Feb 13 '14 09:02

Anarki


People also ask

Can NumPy do complex numbers?

Numpy library gives us functions such as real() and imag() to find real and imaginary parts of a complex number.

Do NumPy supports complex data type?

NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array's data-type. Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa.


1 Answers

Depending on your platform, you may have support for complex192 and/or complex256. These are generally not available on Intel platforms under Windows, but they are on some others—if your code is running on Solaris or on a supercomputer cluster, it may support one of these types. On Linux, you may even find them available or you could create your array using dtype=object and then use bigfloat or gmpy2.mpc.

  1. complex256 numbers up to 104932 + 104932j
  2. complex192 ditto, but with less precision
  3. I have even seen mention of NumPy and complex512...
like image 135
Steve Barnes Avatar answered Nov 15 '22 01:11

Steve Barnes