evalf.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. """
  2. Adaptive numerical evaluation of SymPy expressions, using mpmath
  3. for mathematical functions.
  4. """
  5. from __future__ import annotations
  6. from typing import Tuple as tTuple, Optional, Union as tUnion, Callable, List, Dict as tDict, Type, TYPE_CHECKING, \
  7. Any, overload
  8. import math
  9. import mpmath.libmp as libmp
  10. from mpmath import (
  11. make_mpc, make_mpf, mp, mpc, mpf, nsum, quadts, quadosc, workprec)
  12. from mpmath import inf as mpmath_inf
  13. from mpmath.libmp import (from_int, from_man_exp, from_rational, fhalf,
  14. fnan, finf, fninf, fnone, fone, fzero, mpf_abs, mpf_add,
  15. mpf_atan, mpf_atan2, mpf_cmp, mpf_cos, mpf_e, mpf_exp, mpf_log, mpf_lt,
  16. mpf_mul, mpf_neg, mpf_pi, mpf_pow, mpf_pow_int, mpf_shift, mpf_sin,
  17. mpf_sqrt, normalize, round_nearest, to_int, to_str)
  18. from mpmath.libmp import bitcount as mpmath_bitcount
  19. from mpmath.libmp.backend import MPZ
  20. from mpmath.libmp.libmpc import _infs_nan
  21. from mpmath.libmp.libmpf import dps_to_prec, prec_to_dps
  22. from .sympify import sympify
  23. from .singleton import S
  24. from sympy.external.gmpy import SYMPY_INTS
  25. from sympy.utilities.iterables import is_sequence
  26. from sympy.utilities.lambdify import lambdify
  27. from sympy.utilities.misc import as_int
  28. if TYPE_CHECKING:
  29. from sympy.core.expr import Expr
  30. from sympy.core.add import Add
  31. from sympy.core.mul import Mul
  32. from sympy.core.power import Pow
  33. from sympy.core.symbol import Symbol
  34. from sympy.integrals.integrals import Integral
  35. from sympy.concrete.summations import Sum
  36. from sympy.concrete.products import Product
  37. from sympy.functions.elementary.exponential import exp, log
  38. from sympy.functions.elementary.complexes import Abs, re, im
  39. from sympy.functions.elementary.integers import ceiling, floor
  40. from sympy.functions.elementary.trigonometric import atan
  41. from .numbers import Float, Rational, Integer, AlgebraicNumber, Number
  42. LG10 = math.log(10, 2)
  43. rnd = round_nearest
  44. def bitcount(n):
  45. """Return smallest integer, b, such that |n|/2**b < 1.
  46. """
  47. return mpmath_bitcount(abs(int(n)))
  48. # Used in a few places as placeholder values to denote exponents and
  49. # precision levels, e.g. of exact numbers. Must be careful to avoid
  50. # passing these to mpmath functions or returning them in final results.
  51. INF = float(mpmath_inf)
  52. MINUS_INF = float(-mpmath_inf)
  53. # ~= 100 digits. Real men set this to INF.
  54. DEFAULT_MAXPREC = 333
  55. class PrecisionExhausted(ArithmeticError):
  56. pass
  57. #----------------------------------------------------------------------------#
  58. # #
  59. # Helper functions for arithmetic and complex parts #
  60. # #
  61. #----------------------------------------------------------------------------#
  62. """
  63. An mpf value tuple is a tuple of integers (sign, man, exp, bc)
  64. representing a floating-point number: [1, -1][sign]*man*2**exp where
  65. sign is 0 or 1 and bc should correspond to the number of bits used to
  66. represent the mantissa (man) in binary notation, e.g.
  67. """
  68. MPF_TUP = tTuple[int, int, int, int] # mpf value tuple
  69. """
  70. Explanation
  71. ===========
  72. >>> from sympy.core.evalf import bitcount
  73. >>> sign, man, exp, bc = 0, 5, 1, 3
  74. >>> n = [1, -1][sign]*man*2**exp
  75. >>> n, bitcount(man)
  76. (10, 3)
  77. A temporary result is a tuple (re, im, re_acc, im_acc) where
  78. re and im are nonzero mpf value tuples representing approximate
  79. numbers, or None to denote exact zeros.
  80. re_acc, im_acc are integers denoting log2(e) where e is the estimated
  81. relative accuracy of the respective complex part, but may be anything
  82. if the corresponding complex part is None.
  83. """
  84. TMP_RES = Any # temporary result, should be some variant of
  85. # tUnion[tTuple[Optional[MPF_TUP], Optional[MPF_TUP],
  86. # Optional[int], Optional[int]],
  87. # 'ComplexInfinity']
  88. # but mypy reports error because it doesn't know as we know
  89. # 1. re and re_acc are either both None or both MPF_TUP
  90. # 2. sometimes the result can't be zoo
  91. # type of the "options" parameter in internal evalf functions
  92. OPT_DICT = tDict[str, Any]
  93. def fastlog(x: Optional[MPF_TUP]) -> tUnion[int, Any]:
  94. """Fast approximation of log2(x) for an mpf value tuple x.
  95. Explanation
  96. ===========
  97. Calculated as exponent + width of mantissa. This is an
  98. approximation for two reasons: 1) it gives the ceil(log2(abs(x)))
  99. value and 2) it is too high by 1 in the case that x is an exact
  100. power of 2. Although this is easy to remedy by testing to see if
  101. the odd mpf mantissa is 1 (indicating that one was dealing with
  102. an exact power of 2) that would decrease the speed and is not
  103. necessary as this is only being used as an approximation for the
  104. number of bits in x. The correct return value could be written as
  105. "x[2] + (x[3] if x[1] != 1 else 0)".
  106. Since mpf tuples always have an odd mantissa, no check is done
  107. to see if the mantissa is a multiple of 2 (in which case the
  108. result would be too large by 1).
  109. Examples
  110. ========
  111. >>> from sympy import log
  112. >>> from sympy.core.evalf import fastlog, bitcount
  113. >>> s, m, e = 0, 5, 1
  114. >>> bc = bitcount(m)
  115. >>> n = [1, -1][s]*m*2**e
  116. >>> n, (log(n)/log(2)).evalf(2), fastlog((s, m, e, bc))
  117. (10, 3.3, 4)
  118. """
  119. if not x or x == fzero:
  120. return MINUS_INF
  121. return x[2] + x[3]
  122. def pure_complex(v: 'Expr', or_real=False) -> tuple['Number', 'Number'] | None:
  123. """Return a and b if v matches a + I*b where b is not zero and
  124. a and b are Numbers, else None. If `or_real` is True then 0 will
  125. be returned for `b` if `v` is a real number.
  126. Examples
  127. ========
  128. >>> from sympy.core.evalf import pure_complex
  129. >>> from sympy import sqrt, I, S
  130. >>> a, b, surd = S(2), S(3), sqrt(2)
  131. >>> pure_complex(a)
  132. >>> pure_complex(a, or_real=True)
  133. (2, 0)
  134. >>> pure_complex(surd)
  135. >>> pure_complex(a + b*I)
  136. (2, 3)
  137. >>> pure_complex(I)
  138. (0, 1)
  139. """
  140. h, t = v.as_coeff_Add()
  141. if t:
  142. c, i = t.as_coeff_Mul()
  143. if i is S.ImaginaryUnit:
  144. return h, c
  145. elif or_real:
  146. return h, S.Zero
  147. return None
  148. # I don't know what this is, see function scaled_zero below
  149. SCALED_ZERO_TUP = tTuple[List[int], int, int, int]
  150. @overload
  151. def scaled_zero(mag: SCALED_ZERO_TUP, sign=1) -> MPF_TUP:
  152. ...
  153. @overload
  154. def scaled_zero(mag: int, sign=1) -> tTuple[SCALED_ZERO_TUP, int]:
  155. ...
  156. def scaled_zero(mag: tUnion[SCALED_ZERO_TUP, int], sign=1) -> \
  157. tUnion[MPF_TUP, tTuple[SCALED_ZERO_TUP, int]]:
  158. """Return an mpf representing a power of two with magnitude ``mag``
  159. and -1 for precision. Or, if ``mag`` is a scaled_zero tuple, then just
  160. remove the sign from within the list that it was initially wrapped
  161. in.
  162. Examples
  163. ========
  164. >>> from sympy.core.evalf import scaled_zero
  165. >>> from sympy import Float
  166. >>> z, p = scaled_zero(100)
  167. >>> z, p
  168. (([0], 1, 100, 1), -1)
  169. >>> ok = scaled_zero(z)
  170. >>> ok
  171. (0, 1, 100, 1)
  172. >>> Float(ok)
  173. 1.26765060022823e+30
  174. >>> Float(ok, p)
  175. 0.e+30
  176. >>> ok, p = scaled_zero(100, -1)
  177. >>> Float(scaled_zero(ok), p)
  178. -0.e+30
  179. """
  180. if isinstance(mag, tuple) and len(mag) == 4 and iszero(mag, scaled=True):
  181. return (mag[0][0],) + mag[1:]
  182. elif isinstance(mag, SYMPY_INTS):
  183. if sign not in [-1, 1]:
  184. raise ValueError('sign must be +/-1')
  185. rv, p = mpf_shift(fone, mag), -1
  186. s = 0 if sign == 1 else 1
  187. rv = ([s],) + rv[1:]
  188. return rv, p
  189. else:
  190. raise ValueError('scaled zero expects int or scaled_zero tuple.')
  191. def iszero(mpf: tUnion[MPF_TUP, SCALED_ZERO_TUP, None], scaled=False) -> Optional[bool]:
  192. if not scaled:
  193. return not mpf or not mpf[1] and not mpf[-1]
  194. return mpf and isinstance(mpf[0], list) and mpf[1] == mpf[-1] == 1
  195. def complex_accuracy(result: TMP_RES) -> tUnion[int, Any]:
  196. """
  197. Returns relative accuracy of a complex number with given accuracies
  198. for the real and imaginary parts. The relative accuracy is defined
  199. in the complex norm sense as ||z|+|error|| / |z| where error
  200. is equal to (real absolute error) + (imag absolute error)*i.
  201. The full expression for the (logarithmic) error can be approximated
  202. easily by using the max norm to approximate the complex norm.
  203. In the worst case (re and im equal), this is wrong by a factor
  204. sqrt(2), or by log2(sqrt(2)) = 0.5 bit.
  205. """
  206. if result is S.ComplexInfinity:
  207. return INF
  208. re, im, re_acc, im_acc = result
  209. if not im:
  210. if not re:
  211. return INF
  212. return re_acc
  213. if not re:
  214. return im_acc
  215. re_size = fastlog(re)
  216. im_size = fastlog(im)
  217. absolute_error = max(re_size - re_acc, im_size - im_acc)
  218. relative_error = absolute_error - max(re_size, im_size)
  219. return -relative_error
  220. def get_abs(expr: 'Expr', prec: int, options: OPT_DICT) -> TMP_RES:
  221. result = evalf(expr, prec + 2, options)
  222. if result is S.ComplexInfinity:
  223. return finf, None, prec, None
  224. re, im, re_acc, im_acc = result
  225. if not re:
  226. re, re_acc, im, im_acc = im, im_acc, re, re_acc
  227. if im:
  228. if expr.is_number:
  229. abs_expr, _, acc, _ = evalf(abs(N(expr, prec + 2)),
  230. prec + 2, options)
  231. return abs_expr, None, acc, None
  232. else:
  233. if 'subs' in options:
  234. return libmp.mpc_abs((re, im), prec), None, re_acc, None
  235. return abs(expr), None, prec, None
  236. elif re:
  237. return mpf_abs(re), None, re_acc, None
  238. else:
  239. return None, None, None, None
  240. def get_complex_part(expr: 'Expr', no: int, prec: int, options: OPT_DICT) -> TMP_RES:
  241. """no = 0 for real part, no = 1 for imaginary part"""
  242. workprec = prec
  243. i = 0
  244. while 1:
  245. res = evalf(expr, workprec, options)
  246. if res is S.ComplexInfinity:
  247. return fnan, None, prec, None
  248. value, accuracy = res[no::2]
  249. # XXX is the last one correct? Consider re((1+I)**2).n()
  250. if (not value) or accuracy >= prec or -value[2] > prec:
  251. return value, None, accuracy, None
  252. workprec += max(30, 2**i)
  253. i += 1
  254. def evalf_abs(expr: 'Abs', prec: int, options: OPT_DICT) -> TMP_RES:
  255. return get_abs(expr.args[0], prec, options)
  256. def evalf_re(expr: 're', prec: int, options: OPT_DICT) -> TMP_RES:
  257. return get_complex_part(expr.args[0], 0, prec, options)
  258. def evalf_im(expr: 'im', prec: int, options: OPT_DICT) -> TMP_RES:
  259. return get_complex_part(expr.args[0], 1, prec, options)
  260. def finalize_complex(re: MPF_TUP, im: MPF_TUP, prec: int) -> TMP_RES:
  261. if re == fzero and im == fzero:
  262. raise ValueError("got complex zero with unknown accuracy")
  263. elif re == fzero:
  264. return None, im, None, prec
  265. elif im == fzero:
  266. return re, None, prec, None
  267. size_re = fastlog(re)
  268. size_im = fastlog(im)
  269. if size_re > size_im:
  270. re_acc = prec
  271. im_acc = prec + min(-(size_re - size_im), 0)
  272. else:
  273. im_acc = prec
  274. re_acc = prec + min(-(size_im - size_re), 0)
  275. return re, im, re_acc, im_acc
  276. def chop_parts(value: TMP_RES, prec: int) -> TMP_RES:
  277. """
  278. Chop off tiny real or complex parts.
  279. """
  280. if value is S.ComplexInfinity:
  281. return value
  282. re, im, re_acc, im_acc = value
  283. # Method 1: chop based on absolute value
  284. if re and re not in _infs_nan and (fastlog(re) < -prec + 4):
  285. re, re_acc = None, None
  286. if im and im not in _infs_nan and (fastlog(im) < -prec + 4):
  287. im, im_acc = None, None
  288. # Method 2: chop if inaccurate and relatively small
  289. if re and im:
  290. delta = fastlog(re) - fastlog(im)
  291. if re_acc < 2 and (delta - re_acc <= -prec + 4):
  292. re, re_acc = None, None
  293. if im_acc < 2 and (delta - im_acc >= prec - 4):
  294. im, im_acc = None, None
  295. return re, im, re_acc, im_acc
  296. def check_target(expr: 'Expr', result: TMP_RES, prec: int):
  297. a = complex_accuracy(result)
  298. if a < prec:
  299. raise PrecisionExhausted("Failed to distinguish the expression: \n\n%s\n\n"
  300. "from zero. Try simplifying the input, using chop=True, or providing "
  301. "a higher maxn for evalf" % (expr))
  302. def get_integer_part(expr: 'Expr', no: int, options: OPT_DICT, return_ints=False) -> \
  303. tUnion[TMP_RES, tTuple[int, int]]:
  304. """
  305. With no = 1, computes ceiling(expr)
  306. With no = -1, computes floor(expr)
  307. Note: this function either gives the exact result or signals failure.
  308. """
  309. from sympy.functions.elementary.complexes import re, im
  310. # The expression is likely less than 2^30 or so
  311. assumed_size = 30
  312. result = evalf(expr, assumed_size, options)
  313. if result is S.ComplexInfinity:
  314. raise ValueError("Cannot get integer part of Complex Infinity")
  315. ire, iim, ire_acc, iim_acc = result
  316. # We now know the size, so we can calculate how much extra precision
  317. # (if any) is needed to get within the nearest integer
  318. if ire and iim:
  319. gap = max(fastlog(ire) - ire_acc, fastlog(iim) - iim_acc)
  320. elif ire:
  321. gap = fastlog(ire) - ire_acc
  322. elif iim:
  323. gap = fastlog(iim) - iim_acc
  324. else:
  325. # ... or maybe the expression was exactly zero
  326. if return_ints:
  327. return 0, 0
  328. else:
  329. return None, None, None, None
  330. margin = 10
  331. if gap >= -margin:
  332. prec = margin + assumed_size + gap
  333. ire, iim, ire_acc, iim_acc = evalf(
  334. expr, prec, options)
  335. else:
  336. prec = assumed_size
  337. # We can now easily find the nearest integer, but to find floor/ceil, we
  338. # must also calculate whether the difference to the nearest integer is
  339. # positive or negative (which may fail if very close).
  340. def calc_part(re_im: 'Expr', nexpr: MPF_TUP):
  341. from .add import Add
  342. _, _, exponent, _ = nexpr
  343. is_int = exponent == 0
  344. nint = int(to_int(nexpr, rnd))
  345. if is_int:
  346. # make sure that we had enough precision to distinguish
  347. # between nint and the re or im part (re_im) of expr that
  348. # was passed to calc_part
  349. ire, iim, ire_acc, iim_acc = evalf(
  350. re_im - nint, 10, options) # don't need much precision
  351. assert not iim
  352. size = -fastlog(ire) + 2 # -ve b/c ire is less than 1
  353. if size > prec:
  354. ire, iim, ire_acc, iim_acc = evalf(
  355. re_im, size, options)
  356. assert not iim
  357. nexpr = ire
  358. nint = int(to_int(nexpr, rnd))
  359. _, _, new_exp, _ = ire
  360. is_int = new_exp == 0
  361. if not is_int:
  362. # if there are subs and they all contain integer re/im parts
  363. # then we can (hopefully) safely substitute them into the
  364. # expression
  365. s = options.get('subs', False)
  366. if s:
  367. doit = True
  368. # use strict=False with as_int because we take
  369. # 2.0 == 2
  370. for v in s.values():
  371. try:
  372. as_int(v, strict=False)
  373. except ValueError:
  374. try:
  375. [as_int(i, strict=False) for i in v.as_real_imag()]
  376. continue
  377. except (ValueError, AttributeError):
  378. doit = False
  379. break
  380. if doit:
  381. re_im = re_im.subs(s)
  382. re_im = Add(re_im, -nint, evaluate=False)
  383. x, _, x_acc, _ = evalf(re_im, 10, options)
  384. try:
  385. check_target(re_im, (x, None, x_acc, None), 3)
  386. except PrecisionExhausted:
  387. if not re_im.equals(0):
  388. raise PrecisionExhausted
  389. x = fzero
  390. nint += int(no*(mpf_cmp(x or fzero, fzero) == no))
  391. nint = from_int(nint)
  392. return nint, INF
  393. re_, im_, re_acc, im_acc = None, None, None, None
  394. if ire:
  395. re_, re_acc = calc_part(re(expr, evaluate=False), ire)
  396. if iim:
  397. im_, im_acc = calc_part(im(expr, evaluate=False), iim)
  398. if return_ints:
  399. return int(to_int(re_ or fzero)), int(to_int(im_ or fzero))
  400. return re_, im_, re_acc, im_acc
  401. def evalf_ceiling(expr: 'ceiling', prec: int, options: OPT_DICT) -> TMP_RES:
  402. return get_integer_part(expr.args[0], 1, options)
  403. def evalf_floor(expr: 'floor', prec: int, options: OPT_DICT) -> TMP_RES:
  404. return get_integer_part(expr.args[0], -1, options)
  405. def evalf_float(expr: 'Float', prec: int, options: OPT_DICT) -> TMP_RES:
  406. return expr._mpf_, None, prec, None
  407. def evalf_rational(expr: 'Rational', prec: int, options: OPT_DICT) -> TMP_RES:
  408. return from_rational(expr.p, expr.q, prec), None, prec, None
  409. def evalf_integer(expr: 'Integer', prec: int, options: OPT_DICT) -> TMP_RES:
  410. return from_int(expr.p, prec), None, prec, None
  411. #----------------------------------------------------------------------------#
  412. # #
  413. # Arithmetic operations #
  414. # #
  415. #----------------------------------------------------------------------------#
  416. def add_terms(terms: list, prec: int, target_prec: int) -> \
  417. tTuple[tUnion[MPF_TUP, SCALED_ZERO_TUP, None], Optional[int]]:
  418. """
  419. Helper for evalf_add. Adds a list of (mpfval, accuracy) terms.
  420. Returns
  421. =======
  422. - None, None if there are no non-zero terms;
  423. - terms[0] if there is only 1 term;
  424. - scaled_zero if the sum of the terms produces a zero by cancellation
  425. e.g. mpfs representing 1 and -1 would produce a scaled zero which need
  426. special handling since they are not actually zero and they are purposely
  427. malformed to ensure that they cannot be used in anything but accuracy
  428. calculations;
  429. - a tuple that is scaled to target_prec that corresponds to the
  430. sum of the terms.
  431. The returned mpf tuple will be normalized to target_prec; the input
  432. prec is used to define the working precision.
  433. XXX explain why this is needed and why one cannot just loop using mpf_add
  434. """
  435. terms = [t for t in terms if not iszero(t[0])]
  436. if not terms:
  437. return None, None
  438. elif len(terms) == 1:
  439. return terms[0]
  440. # see if any argument is NaN or oo and thus warrants a special return
  441. special = []
  442. from .numbers import Float
  443. for t in terms:
  444. arg = Float._new(t[0], 1)
  445. if arg is S.NaN or arg.is_infinite:
  446. special.append(arg)
  447. if special:
  448. from .add import Add
  449. rv = evalf(Add(*special), prec + 4, {})
  450. return rv[0], rv[2]
  451. working_prec = 2*prec
  452. sum_man, sum_exp = 0, 0
  453. absolute_err: List[int] = []
  454. for x, accuracy in terms:
  455. sign, man, exp, bc = x
  456. if sign:
  457. man = -man
  458. absolute_err.append(bc + exp - accuracy)
  459. delta = exp - sum_exp
  460. if exp >= sum_exp:
  461. # x much larger than existing sum?
  462. # first: quick test
  463. if ((delta > working_prec) and
  464. ((not sum_man) or
  465. delta - bitcount(abs(sum_man)) > working_prec)):
  466. sum_man = man
  467. sum_exp = exp
  468. else:
  469. sum_man += (man << delta)
  470. else:
  471. delta = -delta
  472. # x much smaller than existing sum?
  473. if delta - bc > working_prec:
  474. if not sum_man:
  475. sum_man, sum_exp = man, exp
  476. else:
  477. sum_man = (sum_man << delta) + man
  478. sum_exp = exp
  479. absolute_error = max(absolute_err)
  480. if not sum_man:
  481. return scaled_zero(absolute_error)
  482. if sum_man < 0:
  483. sum_sign = 1
  484. sum_man = -sum_man
  485. else:
  486. sum_sign = 0
  487. sum_bc = bitcount(sum_man)
  488. sum_accuracy = sum_exp + sum_bc - absolute_error
  489. r = normalize(sum_sign, sum_man, sum_exp, sum_bc, target_prec,
  490. rnd), sum_accuracy
  491. return r
  492. def evalf_add(v: 'Add', prec: int, options: OPT_DICT) -> TMP_RES:
  493. res = pure_complex(v)
  494. if res:
  495. h, c = res
  496. re, _, re_acc, _ = evalf(h, prec, options)
  497. im, _, im_acc, _ = evalf(c, prec, options)
  498. return re, im, re_acc, im_acc
  499. oldmaxprec = options.get('maxprec', DEFAULT_MAXPREC)
  500. i = 0
  501. target_prec = prec
  502. while 1:
  503. options['maxprec'] = min(oldmaxprec, 2*prec)
  504. terms = [evalf(arg, prec + 10, options) for arg in v.args]
  505. n = terms.count(S.ComplexInfinity)
  506. if n >= 2:
  507. return fnan, None, prec, None
  508. re, re_acc = add_terms(
  509. [a[0::2] for a in terms if isinstance(a, tuple) and a[0]], prec, target_prec)
  510. im, im_acc = add_terms(
  511. [a[1::2] for a in terms if isinstance(a, tuple) and a[1]], prec, target_prec)
  512. if n == 1:
  513. if re in (finf, fninf, fnan) or im in (finf, fninf, fnan):
  514. return fnan, None, prec, None
  515. return S.ComplexInfinity
  516. acc = complex_accuracy((re, im, re_acc, im_acc))
  517. if acc >= target_prec:
  518. if options.get('verbose'):
  519. print("ADD: wanted", target_prec, "accurate bits, got", re_acc, im_acc)
  520. break
  521. else:
  522. if (prec - target_prec) > options['maxprec']:
  523. break
  524. prec = prec + max(10 + 2**i, target_prec - acc)
  525. i += 1
  526. if options.get('verbose'):
  527. print("ADD: restarting with prec", prec)
  528. options['maxprec'] = oldmaxprec
  529. if iszero(re, scaled=True):
  530. re = scaled_zero(re)
  531. if iszero(im, scaled=True):
  532. im = scaled_zero(im)
  533. return re, im, re_acc, im_acc
  534. def evalf_mul(v: 'Mul', prec: int, options: OPT_DICT) -> TMP_RES:
  535. res = pure_complex(v)
  536. if res:
  537. # the only pure complex that is a mul is h*I
  538. _, h = res
  539. im, _, im_acc, _ = evalf(h, prec, options)
  540. return None, im, None, im_acc
  541. args = list(v.args)
  542. # see if any argument is NaN or oo and thus warrants a special return
  543. has_zero = False
  544. special = []
  545. from .numbers import Float
  546. for arg in args:
  547. result = evalf(arg, prec, options)
  548. if result is S.ComplexInfinity:
  549. special.append(result)
  550. continue
  551. if result[0] is None:
  552. if result[1] is None:
  553. has_zero = True
  554. continue
  555. num = Float._new(result[0], 1)
  556. if num is S.NaN:
  557. return fnan, None, prec, None
  558. if num.is_infinite:
  559. special.append(num)
  560. if special:
  561. if has_zero:
  562. return fnan, None, prec, None
  563. from .mul import Mul
  564. return evalf(Mul(*special), prec + 4, {})
  565. if has_zero:
  566. return None, None, None, None
  567. # With guard digits, multiplication in the real case does not destroy
  568. # accuracy. This is also true in the complex case when considering the
  569. # total accuracy; however accuracy for the real or imaginary parts
  570. # separately may be lower.
  571. acc = prec
  572. # XXX: big overestimate
  573. working_prec = prec + len(args) + 5
  574. # Empty product is 1
  575. start = man, exp, bc = MPZ(1), 0, 1
  576. # First, we multiply all pure real or pure imaginary numbers.
  577. # direction tells us that the result should be multiplied by
  578. # I**direction; all other numbers get put into complex_factors
  579. # to be multiplied out after the first phase.
  580. last = len(args)
  581. direction = 0
  582. args.append(S.One)
  583. complex_factors = []
  584. for i, arg in enumerate(args):
  585. if i != last and pure_complex(arg):
  586. args[-1] = (args[-1]*arg).expand()
  587. continue
  588. elif i == last and arg is S.One:
  589. continue
  590. re, im, re_acc, im_acc = evalf(arg, working_prec, options)
  591. if re and im:
  592. complex_factors.append((re, im, re_acc, im_acc))
  593. continue
  594. elif re:
  595. (s, m, e, b), w_acc = re, re_acc
  596. elif im:
  597. (s, m, e, b), w_acc = im, im_acc
  598. direction += 1
  599. else:
  600. return None, None, None, None
  601. direction += 2*s
  602. man *= m
  603. exp += e
  604. bc += b
  605. while bc > 3*working_prec:
  606. man >>= working_prec
  607. exp += working_prec
  608. bc -= working_prec
  609. acc = min(acc, w_acc)
  610. sign = (direction & 2) >> 1
  611. if not complex_factors:
  612. v = normalize(sign, man, exp, bitcount(man), prec, rnd)
  613. # multiply by i
  614. if direction & 1:
  615. return None, v, None, acc
  616. else:
  617. return v, None, acc, None
  618. else:
  619. # initialize with the first term
  620. if (man, exp, bc) != start:
  621. # there was a real part; give it an imaginary part
  622. re, im = (sign, man, exp, bitcount(man)), (0, MPZ(0), 0, 0)
  623. i0 = 0
  624. else:
  625. # there is no real part to start (other than the starting 1)
  626. wre, wim, wre_acc, wim_acc = complex_factors[0]
  627. acc = min(acc,
  628. complex_accuracy((wre, wim, wre_acc, wim_acc)))
  629. re = wre
  630. im = wim
  631. i0 = 1
  632. for wre, wim, wre_acc, wim_acc in complex_factors[i0:]:
  633. # acc is the overall accuracy of the product; we aren't
  634. # computing exact accuracies of the product.
  635. acc = min(acc,
  636. complex_accuracy((wre, wim, wre_acc, wim_acc)))
  637. use_prec = working_prec
  638. A = mpf_mul(re, wre, use_prec)
  639. B = mpf_mul(mpf_neg(im), wim, use_prec)
  640. C = mpf_mul(re, wim, use_prec)
  641. D = mpf_mul(im, wre, use_prec)
  642. re = mpf_add(A, B, use_prec)
  643. im = mpf_add(C, D, use_prec)
  644. if options.get('verbose'):
  645. print("MUL: wanted", prec, "accurate bits, got", acc)
  646. # multiply by I
  647. if direction & 1:
  648. re, im = mpf_neg(im), re
  649. return re, im, acc, acc
  650. def evalf_pow(v: 'Pow', prec: int, options) -> TMP_RES:
  651. target_prec = prec
  652. base, exp = v.args
  653. # We handle x**n separately. This has two purposes: 1) it is much
  654. # faster, because we avoid calling evalf on the exponent, and 2) it
  655. # allows better handling of real/imaginary parts that are exactly zero
  656. if exp.is_Integer:
  657. p: int = exp.p # type: ignore
  658. # Exact
  659. if not p:
  660. return fone, None, prec, None
  661. # Exponentiation by p magnifies relative error by |p|, so the
  662. # base must be evaluated with increased precision if p is large
  663. prec += int(math.log(abs(p), 2))
  664. result = evalf(base, prec + 5, options)
  665. if result is S.ComplexInfinity:
  666. if p < 0:
  667. return None, None, None, None
  668. return result
  669. re, im, re_acc, im_acc = result
  670. # Real to integer power
  671. if re and not im:
  672. return mpf_pow_int(re, p, target_prec), None, target_prec, None
  673. # (x*I)**n = I**n * x**n
  674. if im and not re:
  675. z = mpf_pow_int(im, p, target_prec)
  676. case = p % 4
  677. if case == 0:
  678. return z, None, target_prec, None
  679. if case == 1:
  680. return None, z, None, target_prec
  681. if case == 2:
  682. return mpf_neg(z), None, target_prec, None
  683. if case == 3:
  684. return None, mpf_neg(z), None, target_prec
  685. # Zero raised to an integer power
  686. if not re:
  687. if p < 0:
  688. return S.ComplexInfinity
  689. return None, None, None, None
  690. # General complex number to arbitrary integer power
  691. re, im = libmp.mpc_pow_int((re, im), p, prec)
  692. # Assumes full accuracy in input
  693. return finalize_complex(re, im, target_prec)
  694. result = evalf(base, prec + 5, options)
  695. if result is S.ComplexInfinity:
  696. if exp.is_Rational:
  697. if exp < 0:
  698. return None, None, None, None
  699. return result
  700. raise NotImplementedError
  701. # Pure square root
  702. if exp is S.Half:
  703. xre, xim, _, _ = result
  704. # General complex square root
  705. if xim:
  706. re, im = libmp.mpc_sqrt((xre or fzero, xim), prec)
  707. return finalize_complex(re, im, prec)
  708. if not xre:
  709. return None, None, None, None
  710. # Square root of a negative real number
  711. if mpf_lt(xre, fzero):
  712. return None, mpf_sqrt(mpf_neg(xre), prec), None, prec
  713. # Positive square root
  714. return mpf_sqrt(xre, prec), None, prec, None
  715. # We first evaluate the exponent to find its magnitude
  716. # This determines the working precision that must be used
  717. prec += 10
  718. result = evalf(exp, prec, options)
  719. if result is S.ComplexInfinity:
  720. return fnan, None, prec, None
  721. yre, yim, _, _ = result
  722. # Special cases: x**0
  723. if not (yre or yim):
  724. return fone, None, prec, None
  725. ysize = fastlog(yre)
  726. # Restart if too big
  727. # XXX: prec + ysize might exceed maxprec
  728. if ysize > 5:
  729. prec += ysize
  730. yre, yim, _, _ = evalf(exp, prec, options)
  731. # Pure exponential function; no need to evalf the base
  732. if base is S.Exp1:
  733. if yim:
  734. re, im = libmp.mpc_exp((yre or fzero, yim), prec)
  735. return finalize_complex(re, im, target_prec)
  736. return mpf_exp(yre, target_prec), None, target_prec, None
  737. xre, xim, _, _ = evalf(base, prec + 5, options)
  738. # 0**y
  739. if not (xre or xim):
  740. if yim:
  741. return fnan, None, prec, None
  742. if yre[0] == 1: # y < 0
  743. return S.ComplexInfinity
  744. return None, None, None, None
  745. # (real ** complex) or (complex ** complex)
  746. if yim:
  747. re, im = libmp.mpc_pow(
  748. (xre or fzero, xim or fzero), (yre or fzero, yim),
  749. target_prec)
  750. return finalize_complex(re, im, target_prec)
  751. # complex ** real
  752. if xim:
  753. re, im = libmp.mpc_pow_mpf((xre or fzero, xim), yre, target_prec)
  754. return finalize_complex(re, im, target_prec)
  755. # negative ** real
  756. elif mpf_lt(xre, fzero):
  757. re, im = libmp.mpc_pow_mpf((xre, fzero), yre, target_prec)
  758. return finalize_complex(re, im, target_prec)
  759. # positive ** real
  760. else:
  761. return mpf_pow(xre, yre, target_prec), None, target_prec, None
  762. #----------------------------------------------------------------------------#
  763. # #
  764. # Special functions #
  765. # #
  766. #----------------------------------------------------------------------------#
  767. def evalf_exp(expr: 'exp', prec: int, options: OPT_DICT) -> TMP_RES:
  768. from .power import Pow
  769. return evalf_pow(Pow(S.Exp1, expr.exp, evaluate=False), prec, options)
  770. def evalf_trig(v: 'Expr', prec: int, options: OPT_DICT) -> TMP_RES:
  771. """
  772. This function handles sin and cos of complex arguments.
  773. TODO: should also handle tan of complex arguments.
  774. """
  775. from sympy.functions.elementary.trigonometric import cos, sin
  776. if isinstance(v, cos):
  777. func = mpf_cos
  778. elif isinstance(v, sin):
  779. func = mpf_sin
  780. else:
  781. raise NotImplementedError
  782. arg = v.args[0]
  783. # 20 extra bits is possibly overkill. It does make the need
  784. # to restart very unlikely
  785. xprec = prec + 20
  786. re, im, re_acc, im_acc = evalf(arg, xprec, options)
  787. if im:
  788. if 'subs' in options:
  789. v = v.subs(options['subs'])
  790. return evalf(v._eval_evalf(prec), prec, options)
  791. if not re:
  792. if isinstance(v, cos):
  793. return fone, None, prec, None
  794. elif isinstance(v, sin):
  795. return None, None, None, None
  796. else:
  797. raise NotImplementedError
  798. # For trigonometric functions, we are interested in the
  799. # fixed-point (absolute) accuracy of the argument.
  800. xsize = fastlog(re)
  801. # Magnitude <= 1.0. OK to compute directly, because there is no
  802. # danger of hitting the first root of cos (with sin, magnitude
  803. # <= 2.0 would actually be ok)
  804. if xsize < 1:
  805. return func(re, prec, rnd), None, prec, None
  806. # Very large
  807. if xsize >= 10:
  808. xprec = prec + xsize
  809. re, im, re_acc, im_acc = evalf(arg, xprec, options)
  810. # Need to repeat in case the argument is very close to a
  811. # multiple of pi (or pi/2), hitting close to a root
  812. while 1:
  813. y = func(re, prec, rnd)
  814. ysize = fastlog(y)
  815. gap = -ysize
  816. accuracy = (xprec - xsize) - gap
  817. if accuracy < prec:
  818. if options.get('verbose'):
  819. print("SIN/COS", accuracy, "wanted", prec, "gap", gap)
  820. print(to_str(y, 10))
  821. if xprec > options.get('maxprec', DEFAULT_MAXPREC):
  822. return y, None, accuracy, None
  823. xprec += gap
  824. re, im, re_acc, im_acc = evalf(arg, xprec, options)
  825. continue
  826. else:
  827. return y, None, prec, None
  828. def evalf_log(expr: 'log', prec: int, options: OPT_DICT) -> TMP_RES:
  829. if len(expr.args)>1:
  830. expr = expr.doit()
  831. return evalf(expr, prec, options)
  832. arg = expr.args[0]
  833. workprec = prec + 10
  834. result = evalf(arg, workprec, options)
  835. if result is S.ComplexInfinity:
  836. return result
  837. xre, xim, xacc, _ = result
  838. # evalf can return NoneTypes if chop=True
  839. # issue 18516, 19623
  840. if xre is xim is None:
  841. # Dear reviewer, I do not know what -inf is;
  842. # it looks to be (1, 0, -789, -3)
  843. # but I'm not sure in general,
  844. # so we just let mpmath figure
  845. # it out by taking log of 0 directly.
  846. # It would be better to return -inf instead.
  847. xre = fzero
  848. if xim:
  849. from sympy.functions.elementary.complexes import Abs
  850. from sympy.functions.elementary.exponential import log
  851. # XXX: use get_abs etc instead
  852. re = evalf_log(
  853. log(Abs(arg, evaluate=False), evaluate=False), prec, options)
  854. im = mpf_atan2(xim, xre or fzero, prec)
  855. return re[0], im, re[2], prec
  856. imaginary_term = (mpf_cmp(xre, fzero) < 0)
  857. re = mpf_log(mpf_abs(xre), prec, rnd)
  858. size = fastlog(re)
  859. if prec - size > workprec and re != fzero:
  860. from .add import Add
  861. # We actually need to compute 1+x accurately, not x
  862. add = Add(S.NegativeOne, arg, evaluate=False)
  863. xre, xim, _, _ = evalf_add(add, prec, options)
  864. prec2 = workprec - fastlog(xre)
  865. # xre is now x - 1 so we add 1 back here to calculate x
  866. re = mpf_log(mpf_abs(mpf_add(xre, fone, prec2)), prec, rnd)
  867. re_acc = prec
  868. if imaginary_term:
  869. return re, mpf_pi(prec), re_acc, prec
  870. else:
  871. return re, None, re_acc, None
  872. def evalf_atan(v: 'atan', prec: int, options: OPT_DICT) -> TMP_RES:
  873. arg = v.args[0]
  874. xre, xim, reacc, imacc = evalf(arg, prec + 5, options)
  875. if xre is xim is None:
  876. return (None,)*4
  877. if xim:
  878. raise NotImplementedError
  879. return mpf_atan(xre, prec, rnd), None, prec, None
  880. def evalf_subs(prec: int, subs: dict) -> dict:
  881. """ Change all Float entries in `subs` to have precision prec. """
  882. newsubs = {}
  883. for a, b in subs.items():
  884. b = S(b)
  885. if b.is_Float:
  886. b = b._eval_evalf(prec)
  887. newsubs[a] = b
  888. return newsubs
  889. def evalf_piecewise(expr: 'Expr', prec: int, options: OPT_DICT) -> TMP_RES:
  890. from .numbers import Float, Integer
  891. if 'subs' in options:
  892. expr = expr.subs(evalf_subs(prec, options['subs']))
  893. newopts = options.copy()
  894. del newopts['subs']
  895. if hasattr(expr, 'func'):
  896. return evalf(expr, prec, newopts)
  897. if isinstance(expr, float):
  898. return evalf(Float(expr), prec, newopts)
  899. if isinstance(expr, int):
  900. return evalf(Integer(expr), prec, newopts)
  901. # We still have undefined symbols
  902. raise NotImplementedError
  903. def evalf_alg_num(a: 'AlgebraicNumber', prec: int, options: OPT_DICT) -> TMP_RES:
  904. return evalf(a.to_root(), prec, options)
  905. #----------------------------------------------------------------------------#
  906. # #
  907. # High-level operations #
  908. # #
  909. #----------------------------------------------------------------------------#
  910. def as_mpmath(x: Any, prec: int, options: OPT_DICT) -> tUnion[mpc, mpf]:
  911. from .numbers import Infinity, NegativeInfinity, Zero
  912. x = sympify(x)
  913. if isinstance(x, Zero) or x == 0.0:
  914. return mpf(0)
  915. if isinstance(x, Infinity):
  916. return mpf('inf')
  917. if isinstance(x, NegativeInfinity):
  918. return mpf('-inf')
  919. # XXX
  920. result = evalf(x, prec, options)
  921. return quad_to_mpmath(result)
  922. def do_integral(expr: 'Integral', prec: int, options: OPT_DICT) -> TMP_RES:
  923. func = expr.args[0]
  924. x, xlow, xhigh = expr.args[1]
  925. if xlow == xhigh:
  926. xlow = xhigh = 0
  927. elif x not in func.free_symbols:
  928. # only the difference in limits matters in this case
  929. # so if there is a symbol in common that will cancel
  930. # out when taking the difference, then use that
  931. # difference
  932. if xhigh.free_symbols & xlow.free_symbols:
  933. diff = xhigh - xlow
  934. if diff.is_number:
  935. xlow, xhigh = 0, diff
  936. oldmaxprec = options.get('maxprec', DEFAULT_MAXPREC)
  937. options['maxprec'] = min(oldmaxprec, 2*prec)
  938. with workprec(prec + 5):
  939. xlow = as_mpmath(xlow, prec + 15, options)
  940. xhigh = as_mpmath(xhigh, prec + 15, options)
  941. # Integration is like summation, and we can phone home from
  942. # the integrand function to update accuracy summation style
  943. # Note that this accuracy is inaccurate, since it fails
  944. # to account for the variable quadrature weights,
  945. # but it is better than nothing
  946. from sympy.functions.elementary.trigonometric import cos, sin
  947. from .symbol import Wild
  948. have_part = [False, False]
  949. max_real_term: tUnion[float, int] = MINUS_INF
  950. max_imag_term: tUnion[float, int] = MINUS_INF
  951. def f(t: 'Expr') -> tUnion[mpc, mpf]:
  952. nonlocal max_real_term, max_imag_term
  953. re, im, re_acc, im_acc = evalf(func, mp.prec, {'subs': {x: t}})
  954. have_part[0] = re or have_part[0]
  955. have_part[1] = im or have_part[1]
  956. max_real_term = max(max_real_term, fastlog(re))
  957. max_imag_term = max(max_imag_term, fastlog(im))
  958. if im:
  959. return mpc(re or fzero, im)
  960. return mpf(re or fzero)
  961. if options.get('quad') == 'osc':
  962. A = Wild('A', exclude=[x])
  963. B = Wild('B', exclude=[x])
  964. D = Wild('D')
  965. m = func.match(cos(A*x + B)*D)
  966. if not m:
  967. m = func.match(sin(A*x + B)*D)
  968. if not m:
  969. raise ValueError("An integrand of the form sin(A*x+B)*f(x) "
  970. "or cos(A*x+B)*f(x) is required for oscillatory quadrature")
  971. period = as_mpmath(2*S.Pi/m[A], prec + 15, options)
  972. result = quadosc(f, [xlow, xhigh], period=period)
  973. # XXX: quadosc does not do error detection yet
  974. quadrature_error = MINUS_INF
  975. else:
  976. result, quadrature_err = quadts(f, [xlow, xhigh], error=1)
  977. quadrature_error = fastlog(quadrature_err._mpf_)
  978. options['maxprec'] = oldmaxprec
  979. if have_part[0]:
  980. re: Optional[MPF_TUP] = result.real._mpf_
  981. re_acc: Optional[int]
  982. if re == fzero:
  983. re_s, re_acc = scaled_zero(int(-max(prec, max_real_term, quadrature_error)))
  984. re = scaled_zero(re_s) # handled ok in evalf_integral
  985. else:
  986. re_acc = int(-max(max_real_term - fastlog(re) - prec, quadrature_error))
  987. else:
  988. re, re_acc = None, None
  989. if have_part[1]:
  990. im: Optional[MPF_TUP] = result.imag._mpf_
  991. im_acc: Optional[int]
  992. if im == fzero:
  993. im_s, im_acc = scaled_zero(int(-max(prec, max_imag_term, quadrature_error)))
  994. im = scaled_zero(im_s) # handled ok in evalf_integral
  995. else:
  996. im_acc = int(-max(max_imag_term - fastlog(im) - prec, quadrature_error))
  997. else:
  998. im, im_acc = None, None
  999. result = re, im, re_acc, im_acc
  1000. return result
  1001. def evalf_integral(expr: 'Integral', prec: int, options: OPT_DICT) -> TMP_RES:
  1002. limits = expr.limits
  1003. if len(limits) != 1 or len(limits[0]) != 3:
  1004. raise NotImplementedError
  1005. workprec = prec
  1006. i = 0
  1007. maxprec = options.get('maxprec', INF)
  1008. while 1:
  1009. result = do_integral(expr, workprec, options)
  1010. accuracy = complex_accuracy(result)
  1011. if accuracy >= prec: # achieved desired precision
  1012. break
  1013. if workprec >= maxprec: # can't increase accuracy any more
  1014. break
  1015. if accuracy == -1:
  1016. # maybe the answer really is zero and maybe we just haven't increased
  1017. # the precision enough. So increase by doubling to not take too long
  1018. # to get to maxprec.
  1019. workprec *= 2
  1020. else:
  1021. workprec += max(prec, 2**i)
  1022. workprec = min(workprec, maxprec)
  1023. i += 1
  1024. return result
  1025. def check_convergence(numer: 'Expr', denom: 'Expr', n: 'Symbol') -> tTuple[int, Any, Any]:
  1026. """
  1027. Returns
  1028. =======
  1029. (h, g, p) where
  1030. -- h is:
  1031. > 0 for convergence of rate 1/factorial(n)**h
  1032. < 0 for divergence of rate factorial(n)**(-h)
  1033. = 0 for geometric or polynomial convergence or divergence
  1034. -- abs(g) is:
  1035. > 1 for geometric convergence of rate 1/h**n
  1036. < 1 for geometric divergence of rate h**n
  1037. = 1 for polynomial convergence or divergence
  1038. (g < 0 indicates an alternating series)
  1039. -- p is:
  1040. > 1 for polynomial convergence of rate 1/n**h
  1041. <= 1 for polynomial divergence of rate n**(-h)
  1042. """
  1043. from sympy.polys.polytools import Poly
  1044. npol = Poly(numer, n)
  1045. dpol = Poly(denom, n)
  1046. p = npol.degree()
  1047. q = dpol.degree()
  1048. rate = q - p
  1049. if rate:
  1050. return rate, None, None
  1051. constant = dpol.LC() / npol.LC()
  1052. from .numbers import equal_valued
  1053. if not equal_valued(abs(constant), 1):
  1054. return rate, constant, None
  1055. if npol.degree() == dpol.degree() == 0:
  1056. return rate, constant, 0
  1057. pc = npol.all_coeffs()[1]
  1058. qc = dpol.all_coeffs()[1]
  1059. return rate, constant, (qc - pc)/dpol.LC()
  1060. def hypsum(expr: 'Expr', n: 'Symbol', start: int, prec: int) -> mpf:
  1061. """
  1062. Sum a rapidly convergent infinite hypergeometric series with
  1063. given general term, e.g. e = hypsum(1/factorial(n), n). The
  1064. quotient between successive terms must be a quotient of integer
  1065. polynomials.
  1066. """
  1067. from .numbers import Float, equal_valued
  1068. from sympy.simplify.simplify import hypersimp
  1069. if prec == float('inf'):
  1070. raise NotImplementedError('does not support inf prec')
  1071. if start:
  1072. expr = expr.subs(n, n + start)
  1073. hs = hypersimp(expr, n)
  1074. if hs is None:
  1075. raise NotImplementedError("a hypergeometric series is required")
  1076. num, den = hs.as_numer_denom()
  1077. func1 = lambdify(n, num)
  1078. func2 = lambdify(n, den)
  1079. h, g, p = check_convergence(num, den, n)
  1080. if h < 0:
  1081. raise ValueError("Sum diverges like (n!)^%i" % (-h))
  1082. term = expr.subs(n, 0)
  1083. if not term.is_Rational:
  1084. raise NotImplementedError("Non rational term functionality is not implemented.")
  1085. # Direct summation if geometric or faster
  1086. if h > 0 or (h == 0 and abs(g) > 1):
  1087. term = (MPZ(term.p) << prec) // term.q
  1088. s = term
  1089. k = 1
  1090. while abs(term) > 5:
  1091. term *= MPZ(func1(k - 1))
  1092. term //= MPZ(func2(k - 1))
  1093. s += term
  1094. k += 1
  1095. return from_man_exp(s, -prec)
  1096. else:
  1097. alt = g < 0
  1098. if abs(g) < 1:
  1099. raise ValueError("Sum diverges like (%i)^n" % abs(1/g))
  1100. if p < 1 or (equal_valued(p, 1) and not alt):
  1101. raise ValueError("Sum diverges like n^%i" % (-p))
  1102. # We have polynomial convergence: use Richardson extrapolation
  1103. vold = None
  1104. ndig = prec_to_dps(prec)
  1105. while True:
  1106. # Need to use at least quad precision because a lot of cancellation
  1107. # might occur in the extrapolation process; we check the answer to
  1108. # make sure that the desired precision has been reached, too.
  1109. prec2 = 4*prec
  1110. term0 = (MPZ(term.p) << prec2) // term.q
  1111. def summand(k, _term=[term0]):
  1112. if k:
  1113. k = int(k)
  1114. _term[0] *= MPZ(func1(k - 1))
  1115. _term[0] //= MPZ(func2(k - 1))
  1116. return make_mpf(from_man_exp(_term[0], -prec2))
  1117. with workprec(prec):
  1118. v = nsum(summand, [0, mpmath_inf], method='richardson')
  1119. vf = Float(v, ndig)
  1120. if vold is not None and vold == vf:
  1121. break
  1122. prec += prec # double precision each time
  1123. vold = vf
  1124. return v._mpf_
  1125. def evalf_prod(expr: 'Product', prec: int, options: OPT_DICT) -> TMP_RES:
  1126. if all((l[1] - l[2]).is_Integer for l in expr.limits):
  1127. result = evalf(expr.doit(), prec=prec, options=options)
  1128. else:
  1129. from sympy.concrete.summations import Sum
  1130. result = evalf(expr.rewrite(Sum), prec=prec, options=options)
  1131. return result
  1132. def evalf_sum(expr: 'Sum', prec: int, options: OPT_DICT) -> TMP_RES:
  1133. from .numbers import Float
  1134. if 'subs' in options:
  1135. expr = expr.subs(options['subs'])
  1136. func = expr.function
  1137. limits = expr.limits
  1138. if len(limits) != 1 or len(limits[0]) != 3:
  1139. raise NotImplementedError
  1140. if func.is_zero:
  1141. return None, None, prec, None
  1142. prec2 = prec + 10
  1143. try:
  1144. n, a, b = limits[0]
  1145. if b is not S.Infinity or a is S.NegativeInfinity or a != int(a):
  1146. raise NotImplementedError
  1147. # Use fast hypergeometric summation if possible
  1148. v = hypsum(func, n, int(a), prec2)
  1149. delta = prec - fastlog(v)
  1150. if fastlog(v) < -10:
  1151. v = hypsum(func, n, int(a), delta)
  1152. return v, None, min(prec, delta), None
  1153. except NotImplementedError:
  1154. # Euler-Maclaurin summation for general series
  1155. eps = Float(2.0)**(-prec)
  1156. for i in range(1, 5):
  1157. m = n = 2**i * prec
  1158. s, err = expr.euler_maclaurin(m=m, n=n, eps=eps,
  1159. eval_integral=False)
  1160. err = err.evalf()
  1161. if err is S.NaN:
  1162. raise NotImplementedError
  1163. if err <= eps:
  1164. break
  1165. err = fastlog(evalf(abs(err), 20, options)[0])
  1166. re, im, re_acc, im_acc = evalf(s, prec2, options)
  1167. if re_acc is None:
  1168. re_acc = -err
  1169. if im_acc is None:
  1170. im_acc = -err
  1171. return re, im, re_acc, im_acc
  1172. #----------------------------------------------------------------------------#
  1173. # #
  1174. # Symbolic interface #
  1175. # #
  1176. #----------------------------------------------------------------------------#
  1177. def evalf_symbol(x: 'Expr', prec: int, options: OPT_DICT) -> TMP_RES:
  1178. val = options['subs'][x]
  1179. if isinstance(val, mpf):
  1180. if not val:
  1181. return None, None, None, None
  1182. return val._mpf_, None, prec, None
  1183. else:
  1184. if '_cache' not in options:
  1185. options['_cache'] = {}
  1186. cache = options['_cache']
  1187. cached, cached_prec = cache.get(x, (None, MINUS_INF))
  1188. if cached_prec >= prec:
  1189. return cached
  1190. v = evalf(sympify(val), prec, options)
  1191. cache[x] = (v, prec)
  1192. return v
  1193. evalf_table: tDict[Type['Expr'], Callable[['Expr', int, OPT_DICT], TMP_RES]] = {}
  1194. def _create_evalf_table():
  1195. global evalf_table
  1196. from sympy.concrete.products import Product
  1197. from sympy.concrete.summations import Sum
  1198. from .add import Add
  1199. from .mul import Mul
  1200. from .numbers import Exp1, Float, Half, ImaginaryUnit, Integer, NaN, NegativeOne, One, Pi, Rational, \
  1201. Zero, ComplexInfinity, AlgebraicNumber
  1202. from .power import Pow
  1203. from .symbol import Dummy, Symbol
  1204. from sympy.functions.elementary.complexes import Abs, im, re
  1205. from sympy.functions.elementary.exponential import exp, log
  1206. from sympy.functions.elementary.integers import ceiling, floor
  1207. from sympy.functions.elementary.piecewise import Piecewise
  1208. from sympy.functions.elementary.trigonometric import atan, cos, sin
  1209. from sympy.integrals.integrals import Integral
  1210. evalf_table = {
  1211. Symbol: evalf_symbol,
  1212. Dummy: evalf_symbol,
  1213. Float: evalf_float,
  1214. Rational: evalf_rational,
  1215. Integer: evalf_integer,
  1216. Zero: lambda x, prec, options: (None, None, prec, None),
  1217. One: lambda x, prec, options: (fone, None, prec, None),
  1218. Half: lambda x, prec, options: (fhalf, None, prec, None),
  1219. Pi: lambda x, prec, options: (mpf_pi(prec), None, prec, None),
  1220. Exp1: lambda x, prec, options: (mpf_e(prec), None, prec, None),
  1221. ImaginaryUnit: lambda x, prec, options: (None, fone, None, prec),
  1222. NegativeOne: lambda x, prec, options: (fnone, None, prec, None),
  1223. ComplexInfinity: lambda x, prec, options: S.ComplexInfinity,
  1224. NaN: lambda x, prec, options: (fnan, None, prec, None),
  1225. exp: evalf_exp,
  1226. cos: evalf_trig,
  1227. sin: evalf_trig,
  1228. Add: evalf_add,
  1229. Mul: evalf_mul,
  1230. Pow: evalf_pow,
  1231. log: evalf_log,
  1232. atan: evalf_atan,
  1233. Abs: evalf_abs,
  1234. re: evalf_re,
  1235. im: evalf_im,
  1236. floor: evalf_floor,
  1237. ceiling: evalf_ceiling,
  1238. Integral: evalf_integral,
  1239. Sum: evalf_sum,
  1240. Product: evalf_prod,
  1241. Piecewise: evalf_piecewise,
  1242. AlgebraicNumber: evalf_alg_num,
  1243. }
  1244. def evalf(x: 'Expr', prec: int, options: OPT_DICT) -> TMP_RES:
  1245. """
  1246. Evaluate the ``Expr`` instance, ``x``
  1247. to a binary precision of ``prec``. This
  1248. function is supposed to be used internally.
  1249. Parameters
  1250. ==========
  1251. x : Expr
  1252. The formula to evaluate to a float.
  1253. prec : int
  1254. The binary precision that the output should have.
  1255. options : dict
  1256. A dictionary with the same entries as
  1257. ``EvalfMixin.evalf`` and in addition,
  1258. ``maxprec`` which is the maximum working precision.
  1259. Returns
  1260. =======
  1261. An optional tuple, ``(re, im, re_acc, im_acc)``
  1262. which are the real, imaginary, real accuracy
  1263. and imaginary accuracy respectively. ``re`` is
  1264. an mpf value tuple and so is ``im``. ``re_acc``
  1265. and ``im_acc`` are ints.
  1266. NB: all these return values can be ``None``.
  1267. If all values are ``None``, then that represents 0.
  1268. Note that 0 is also represented as ``fzero = (0, 0, 0, 0)``.
  1269. """
  1270. from sympy.functions.elementary.complexes import re as re_, im as im_
  1271. try:
  1272. rf = evalf_table[type(x)]
  1273. r = rf(x, prec, options)
  1274. except KeyError:
  1275. # Fall back to ordinary evalf if possible
  1276. if 'subs' in options:
  1277. x = x.subs(evalf_subs(prec, options['subs']))
  1278. xe = x._eval_evalf(prec)
  1279. if xe is None:
  1280. raise NotImplementedError
  1281. as_real_imag = getattr(xe, "as_real_imag", None)
  1282. if as_real_imag is None:
  1283. raise NotImplementedError # e.g. FiniteSet(-1.0, 1.0).evalf()
  1284. re, im = as_real_imag()
  1285. if re.has(re_) or im.has(im_):
  1286. raise NotImplementedError
  1287. if re == 0.0:
  1288. re = None
  1289. reprec = None
  1290. elif re.is_number:
  1291. re = re._to_mpmath(prec, allow_ints=False)._mpf_
  1292. reprec = prec
  1293. else:
  1294. raise NotImplementedError
  1295. if im == 0.0:
  1296. im = None
  1297. imprec = None
  1298. elif im.is_number:
  1299. im = im._to_mpmath(prec, allow_ints=False)._mpf_
  1300. imprec = prec
  1301. else:
  1302. raise NotImplementedError
  1303. r = re, im, reprec, imprec
  1304. if options.get("verbose"):
  1305. print("### input", x)
  1306. print("### output", to_str(r[0] or fzero, 50) if isinstance(r, tuple) else r)
  1307. print("### raw", r) # r[0], r[2]
  1308. print()
  1309. chop = options.get('chop', False)
  1310. if chop:
  1311. if chop is True:
  1312. chop_prec = prec
  1313. else:
  1314. # convert (approximately) from given tolerance;
  1315. # the formula here will will make 1e-i rounds to 0 for
  1316. # i in the range +/-27 while 2e-i will not be chopped
  1317. chop_prec = int(round(-3.321*math.log10(chop) + 2.5))
  1318. if chop_prec == 3:
  1319. chop_prec -= 1
  1320. r = chop_parts(r, chop_prec)
  1321. if options.get("strict"):
  1322. check_target(x, r, prec)
  1323. return r
  1324. def quad_to_mpmath(q, ctx=None):
  1325. """Turn the quad returned by ``evalf`` into an ``mpf`` or ``mpc``. """
  1326. mpc = make_mpc if ctx is None else ctx.make_mpc
  1327. mpf = make_mpf if ctx is None else ctx.make_mpf
  1328. if q is S.ComplexInfinity:
  1329. raise NotImplementedError
  1330. re, im, _, _ = q
  1331. if im:
  1332. if not re:
  1333. re = fzero
  1334. return mpc((re, im))
  1335. elif re:
  1336. return mpf(re)
  1337. else:
  1338. return mpf(fzero)
  1339. class EvalfMixin:
  1340. """Mixin class adding evalf capability."""
  1341. __slots__ = () # type: tTuple[str, ...]
  1342. def evalf(self, n=15, subs=None, maxn=100, chop=False, strict=False, quad=None, verbose=False):
  1343. """
  1344. Evaluate the given formula to an accuracy of *n* digits.
  1345. Parameters
  1346. ==========
  1347. subs : dict, optional
  1348. Substitute numerical values for symbols, e.g.
  1349. ``subs={x:3, y:1+pi}``. The substitutions must be given as a
  1350. dictionary.
  1351. maxn : int, optional
  1352. Allow a maximum temporary working precision of maxn digits.
  1353. chop : bool or number, optional
  1354. Specifies how to replace tiny real or imaginary parts in
  1355. subresults by exact zeros.
  1356. When ``True`` the chop value defaults to standard precision.
  1357. Otherwise the chop value is used to determine the
  1358. magnitude of "small" for purposes of chopping.
  1359. >>> from sympy import N
  1360. >>> x = 1e-4
  1361. >>> N(x, chop=True)
  1362. 0.000100000000000000
  1363. >>> N(x, chop=1e-5)
  1364. 0.000100000000000000
  1365. >>> N(x, chop=1e-4)
  1366. 0
  1367. strict : bool, optional
  1368. Raise ``PrecisionExhausted`` if any subresult fails to
  1369. evaluate to full accuracy, given the available maxprec.
  1370. quad : str, optional
  1371. Choose algorithm for numerical quadrature. By default,
  1372. tanh-sinh quadrature is used. For oscillatory
  1373. integrals on an infinite interval, try ``quad='osc'``.
  1374. verbose : bool, optional
  1375. Print debug information.
  1376. Notes
  1377. =====
  1378. When Floats are naively substituted into an expression,
  1379. precision errors may adversely affect the result. For example,
  1380. adding 1e16 (a Float) to 1 will truncate to 1e16; if 1e16 is
  1381. then subtracted, the result will be 0.
  1382. That is exactly what happens in the following:
  1383. >>> from sympy.abc import x, y, z
  1384. >>> values = {x: 1e16, y: 1, z: 1e16}
  1385. >>> (x + y - z).subs(values)
  1386. 0
  1387. Using the subs argument for evalf is the accurate way to
  1388. evaluate such an expression:
  1389. >>> (x + y - z).evalf(subs=values)
  1390. 1.00000000000000
  1391. """
  1392. from .numbers import Float, Number
  1393. n = n if n is not None else 15
  1394. if subs and is_sequence(subs):
  1395. raise TypeError('subs must be given as a dictionary')
  1396. # for sake of sage that doesn't like evalf(1)
  1397. if n == 1 and isinstance(self, Number):
  1398. from .expr import _mag
  1399. rv = self.evalf(2, subs, maxn, chop, strict, quad, verbose)
  1400. m = _mag(rv)
  1401. rv = rv.round(1 - m)
  1402. return rv
  1403. if not evalf_table:
  1404. _create_evalf_table()
  1405. prec = dps_to_prec(n)
  1406. options = {'maxprec': max(prec, int(maxn*LG10)), 'chop': chop,
  1407. 'strict': strict, 'verbose': verbose}
  1408. if subs is not None:
  1409. options['subs'] = subs
  1410. if quad is not None:
  1411. options['quad'] = quad
  1412. try:
  1413. result = evalf(self, prec + 4, options)
  1414. except NotImplementedError:
  1415. # Fall back to the ordinary evalf
  1416. if hasattr(self, 'subs') and subs is not None: # issue 20291
  1417. v = self.subs(subs)._eval_evalf(prec)
  1418. else:
  1419. v = self._eval_evalf(prec)
  1420. if v is None:
  1421. return self
  1422. elif not v.is_number:
  1423. return v
  1424. try:
  1425. # If the result is numerical, normalize it
  1426. result = evalf(v, prec, options)
  1427. except NotImplementedError:
  1428. # Probably contains symbols or unknown functions
  1429. return v
  1430. if result is S.ComplexInfinity:
  1431. return result
  1432. re, im, re_acc, im_acc = result
  1433. if re is S.NaN or im is S.NaN:
  1434. return S.NaN
  1435. if re:
  1436. p = max(min(prec, re_acc), 1)
  1437. re = Float._new(re, p)
  1438. else:
  1439. re = S.Zero
  1440. if im:
  1441. p = max(min(prec, im_acc), 1)
  1442. im = Float._new(im, p)
  1443. return re + im*S.ImaginaryUnit
  1444. else:
  1445. return re
  1446. n = evalf
  1447. def _evalf(self, prec):
  1448. """Helper for evalf. Does the same thing but takes binary precision"""
  1449. r = self._eval_evalf(prec)
  1450. if r is None:
  1451. r = self
  1452. return r
  1453. def _eval_evalf(self, prec):
  1454. return
  1455. def _to_mpmath(self, prec, allow_ints=True):
  1456. # mpmath functions accept ints as input
  1457. errmsg = "cannot convert to mpmath number"
  1458. if allow_ints and self.is_Integer:
  1459. return self.p
  1460. if hasattr(self, '_as_mpf_val'):
  1461. return make_mpf(self._as_mpf_val(prec))
  1462. try:
  1463. result = evalf(self, prec, {})
  1464. return quad_to_mpmath(result)
  1465. except NotImplementedError:
  1466. v = self._eval_evalf(prec)
  1467. if v is None:
  1468. raise ValueError(errmsg)
  1469. if v.is_Float:
  1470. return make_mpf(v._mpf_)
  1471. # Number + Number*I is also fine
  1472. re, im = v.as_real_imag()
  1473. if allow_ints and re.is_Integer:
  1474. re = from_int(re.p)
  1475. elif re.is_Float:
  1476. re = re._mpf_
  1477. else:
  1478. raise ValueError(errmsg)
  1479. if allow_ints and im.is_Integer:
  1480. im = from_int(im.p)
  1481. elif im.is_Float:
  1482. im = im._mpf_
  1483. else:
  1484. raise ValueError(errmsg)
  1485. return make_mpc((re, im))
  1486. def N(x, n=15, **options):
  1487. r"""
  1488. Calls x.evalf(n, \*\*options).
  1489. Explanations
  1490. ============
  1491. Both .n() and N() are equivalent to .evalf(); use the one that you like better.
  1492. See also the docstring of .evalf() for information on the options.
  1493. Examples
  1494. ========
  1495. >>> from sympy import Sum, oo, N
  1496. >>> from sympy.abc import k
  1497. >>> Sum(1/k**k, (k, 1, oo))
  1498. Sum(k**(-k), (k, 1, oo))
  1499. >>> N(_, 4)
  1500. 1.291
  1501. """
  1502. # by using rational=True, any evaluation of a string
  1503. # will be done using exact values for the Floats
  1504. return sympify(x, rational=True).evalf(n, **options)
  1505. def _evalf_with_bounded_error(x: 'Expr', eps: 'Optional[Expr]' = None,
  1506. m: int = 0,
  1507. options: Optional[OPT_DICT] = None) -> TMP_RES:
  1508. """
  1509. Evaluate *x* to within a bounded absolute error.
  1510. Parameters
  1511. ==========
  1512. x : Expr
  1513. The quantity to be evaluated.
  1514. eps : Expr, None, optional (default=None)
  1515. Positive real upper bound on the acceptable error.
  1516. m : int, optional (default=0)
  1517. If *eps* is None, then use 2**(-m) as the upper bound on the error.
  1518. options: OPT_DICT
  1519. As in the ``evalf`` function.
  1520. Returns
  1521. =======
  1522. A tuple ``(re, im, re_acc, im_acc)``, as returned by ``evalf``.
  1523. See Also
  1524. ========
  1525. evalf
  1526. """
  1527. if eps is not None:
  1528. if not (eps.is_Rational or eps.is_Float) or not eps > 0:
  1529. raise ValueError("eps must be positive")
  1530. r, _, _, _ = evalf(1/eps, 1, {})
  1531. m = fastlog(r)
  1532. c, d, _, _ = evalf(x, 1, {})
  1533. # Note: If x = a + b*I, then |a| <= 2|c| and |b| <= 2|d|, with equality
  1534. # only in the zero case.
  1535. # If a is non-zero, then |c| = 2**nc for some integer nc, and c has
  1536. # bitcount 1. Therefore 2**fastlog(c) = 2**(nc+1) = 2|c| is an upper bound
  1537. # on |a|. Likewise for b and d.
  1538. nr, ni = fastlog(c), fastlog(d)
  1539. n = max(nr, ni) + 1
  1540. # If x is 0, then n is MINUS_INF, and p will be 1. Otherwise,
  1541. # n - 1 bits get us past the integer parts of a and b, and +1 accounts for
  1542. # the factor of <= sqrt(2) that is |x|/max(|a|, |b|).
  1543. p = max(1, m + n + 1)
  1544. options = options or {}
  1545. return evalf(x, p, options)