Skip to content

Commit

Permalink
test lambdify with mpmath: powm1, log1p, expm1
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Nov 28, 2022
1 parent 8ad4f49 commit dc95a77
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sympy/printing/pycode.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,8 @@ def _print_log2(self, e):
self._module_format('mpmath.log'), self._print(e.args[0]))

def _print_log1p(self, e):
return '{}({}+1)'.format(
self._module_format('mpmath.log'), self._print(e.args[0]))
return '{}({})'.format(
self._module_format('mpmath.log1p'), self._print(e.args[0]))

def _print_Pow(self, expr, rational=False):
return self._hprint_Pow(expr, rational=rational, sqrt='mpmath.sqrt')
Expand Down
21 changes: 20 additions & 1 deletion sympy/utilities/tests/test_lambdify.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from sympy.core.expr import UnevaluatedExpr
from sympy.codegen.cfunctions import expm1, log1p, exp2, log2, log10, hypot
from sympy.codegen.numpy_nodes import logaddexp, logaddexp2
from sympy.codegen.scipy_nodes import cosm1
from sympy.codegen.scipy_nodes import cosm1, powm1
from sympy.functions.elementary.complexes import re, im, arg
from sympy.functions.special.polynomials import \
chebyshevt, chebyshevu, legendre, hermite, laguerre, gegenbauer, \
Expand Down Expand Up @@ -190,6 +190,22 @@ def test_mpmath_lambda():
raises(TypeError, lambda: f(x))
# if this succeeds, it can't be a mpmath function

ref2 = (mpmath.mpf("1e-30")
- mpmath.mpf("1e-45")/2
+ 5*mpmath.mpf("1e-60")/6
- 3*mpmath.mpf("1e-75")/4
+ 33*mpmath.mpf("1e-90")/40
)
f2a = lambdify((x, y), x**y - 1, "mpmath")
f2b = lambdify((x, y), powm1(x, y), "mpmath")
f2c = lambdify((x,), expm1(x*log1p(x)), "mpmath")
ans2a = f2a(mpmath.mpf("1")+mpmath.mpf("1e-15"), mpmath.mpf("1e-15"))
ans2b = f2b(mpmath.mpf("1")+mpmath.mpf("1e-15"), mpmath.mpf("1e-15"))
ans2c = f2c(mpmath.mpf("1e-15"))
assert abs(ans2a - ref2) < 1e-51
assert abs(ans2b - ref2) < 1e-67
assert abs(ans2c - ref2) < 1e-80


@conserve_mpmath_dps
def test_number_precision():
Expand Down Expand Up @@ -1469,6 +1485,9 @@ def test_scipy_special_math():
cm1 = lambdify((x,), cosm1(x), modules='scipy')
assert abs(cm1(1e-20) + 5e-41) < 1e-200

cm2 = lambdify((x, y), powm1(x, y), modules='scipy')
assert abs(cm2(1.2, 1e-9) - 1.82321557e-10) < 1e-17


def test_scipy_bernoulli():
if not scipy:
Expand Down

0 comments on commit dc95a77

Please sign in to comment.