0

I'm trying to lower the error of my floating point calculation, to this end I wanted to use python mpmath to utilize the arbitrary precision of the library. But I notice that it is much slower than the math.

Consider the program

import math
import numpy as np
mathsin = np.vectorize(math.sin)
mathsin(np.arange(1,10**7)

this take around a second or two, if I do this with mpmath

import mpmath as mp
import numpy as np
mpsin = np.vectorize(mp.sin)
mpsin(np.arange(1,10**7)

this takes around forty second even if I lower the default settings of precision with mp.prec and mp.dps. Is there a way to speed up such process for mpmath? And are there any general tips to making mpmath faster when using it also with numpy?

Nitaa a
  • 121
  • 4
  • 1
    Any reason for not using `np.sin` in your first code snippet? As far as the rest of your question goes, multiprecsion calculations are done in software but floating point calculations have hardware support. An order of magnitude difference isn't surprising. – John Coleman Dec 10 '22 at 14:28
  • 2
    It's not `vectorize` (which is little better than a list comprehension). It's that `mpmath.sin` is much slower. In general don't use `mpmath` if you are worried about speed. – hpaulj Dec 10 '22 at 17:08
  • Ok, thank you both! np.sin wasn't used because this was just an example (not a good one) – Nitaa a Dec 10 '22 at 17:37

0 Answers0