-1

im trying to use the Chudnovsky algorithm in mpmath for python and so i wrote this code:

from mpmath import *
import csv
mp.dps = 100; mp.pretty = True
x = mpf(426880)*sqrt(10005)*(nsum(lambda q: ((mpf(-1))^(q)*fac((mpf(6)*q))*(mpf(545140134*q+13591409)))/((fac(mpf(3*q)))*(fac(q)^(mpf(3)))*
(mpf(-262537412640768000))^(q)), [0, inf]))^(mpf(-1))

with open('main.txt', 'w', newline='') as txt:
    txt.write(str(x))

but i keep getting this error:

Traceback (most recent call last):
  File "C:\Coding Adventures\Python\millionpi\V2\main.py", line 4, in <module>
    x = mpf(426880)*sqrt(10005)*(nsum(lambda q: ((mpf(-1))^(q)*fac((mpf(6)*q))*(mpf(545140134*q+13591409)))/((fac(mpf(3*q)))*(fac(q)^(mpf(3)))*(mpf(-262537412640768000))^(q)), [0, inf]))^mpf(-1)
  File "C:\Coding Adventures\Python\millionpi\venv\lib\site-packages\mpmath\calculus\extrapolation.py", line 1718, in nsum
    return +ctx.adaptive_extrapolation(update, emfun, options)
  File "C:\Coding Adventures\Python\millionpi\venv\lib\site-packages\mpmath\calculus\extrapolation.py", line 1165, in adaptive_extrapolation     
    update(partial, xrange(index, index+step))
  File "C:\Coding Adventures\Python\millionpi\venv\lib\site-packages\mpmath\calculus\extrapolation.py", line 1706, in update
    psum = psum + g(ctx.mpf(k))
  File "C:\Coding Adventures\Python\millionpi\venv\lib\site-packages\mpmath\calculus\extrapolation.py", line 1753, in g
    return f(*args)
  File "C:\Coding Adventures\Python\millionpi\venv\lib\site-packages\mpmath\calculus\extrapolation.py", line 1808, in g
    return f(*args)
  File "C:\Coding Adventures\Python\millionpi\V2\main.py", line 4, in <lambda>
    x = mpf(426880)*sqrt(10005)*(nsum(lambda q: ((mpf(-1))^(q)*fac((mpf(6)*q))*(mpf(545140134*q+13591409)))/((fac(mpf(3*q)))*(fac(q)^(mpf(3)))*(mpf(-262537412640768000))^(q)), [0, inf]))^mpf(-1)
TypeError: unsupported operand type(s) for ^: 'mpf' and 'mpf'

and im completely stumped as how to solve it so any help would be great

1 Answers1

0

A simpler example:

In [84]: import mpmath as mp

In [85]: mp.mpf(2)
Out[85]: mpf('2.0')

In [86]: mp.mpf(2)^3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [86], in <cell line: 1>()
----> 1 mp.mpf(2)^3

TypeError: unsupported operand type(s) for ^: 'mpf' and 'int'

Correct power expression:

In [87]: mp.mpf(2)**3
Out[87]: mpf('8.0')

Compare that with Python integers:

In [88]: 2**3
Out[88]: 8

In [89]: 2^3          # not power
Out[89]: 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353