Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/functions/powers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Exponentiation
..............

.. autofunction:: mpmath.exp
.. autofunction:: mpmath.exp2
.. autofunction:: mpmath.power
.. autofunction:: mpmath.expj
.. autofunction:: mpmath.expjpi
Expand All @@ -27,6 +28,7 @@ Logarithms

.. autofunction:: mpmath.log
.. autofunction:: mpmath.ln
.. autofunction:: mpmath.log2
.. autofunction:: mpmath.log10
.. autofunction:: mpmath.log1p(x)

Expand Down
2 changes: 2 additions & 0 deletions mpmath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@
pi = mp.pi
ln2 = mp.ln2
ln10 = mp.ln10
exp2 = mp.exp2
log2 = mp.log2
phi = mp.phi
e = mp.e
euler = mp.euler
Expand Down
9 changes: 9 additions & 0 deletions mpmath/function_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,15 @@
is equivalent to ``log(x, 10)``.
"""

log2 = r"""
Computes the base-2 logarithm of `x`, `\log_{2}(x)`. ``log2(x)``
is equivalent to ``log(x, 2)``.
"""

exp2 = """
Computes 2 raised to the power `x`.
"""

fmod = r"""
Converts `x` and `y` to mpmath numbers and returns `x \mod y`.
For mpmath numbers, this is equivalent to ``x % y``.
Expand Down
8 changes: 8 additions & 0 deletions mpmath/functions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ def log(ctx, x, b=None):
def log10(ctx, x):
return ctx.log(x, 10)

@defun
def log2(ctx, x):
return ctx.log(x, 2)

@defun
def exp2(ctx, x):
return ctx.power(2, x)

@defun
def fmod(ctx, x, y):
return ctx.convert(x) % ctx.convert(y)
Expand Down
10 changes: 10 additions & 0 deletions mpmath/libmp/libelefun.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,16 @@ def mpf_log(x, prec, rnd=round_fast):
m -= n*ln2_fixed(wp)
return from_man_exp(m, -wp, prec, rnd)

def mpf_log1p(x, prec, rnd=round_fast):
"""
Computes log(1+x) accurately.
"""
wp = prec + 10
u = mpf_add(fone, x, wp*2)
return mpf_mul(mpf_log(u, wp),
mpf_div(x, mpf_sub(u, fone, wp),
wp), prec, rnd)

def mpf_log_hypot(a, b, prec, rnd):
"""
Computes log(sqrt(a^2+b^2)) accurately.
Expand Down
8 changes: 4 additions & 4 deletions mpmath/libmp/libmpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from .libelefun import (mpf_acos, mpf_acosh, mpf_asin, mpf_atan, mpf_atan2,
mpf_cos, mpf_cos_pi, mpf_cos_sin, mpf_cos_sin_pi,
mpf_cosh, mpf_cosh_sinh, mpf_exp, mpf_fibonacci,
mpf_log, mpf_log_hypot, mpf_nthroot, mpf_phi, mpf_pi,
mpf_pow_int, mpf_sin, mpf_sin_pi, mpf_sinh, mpf_tan,
mpf_tanh)
mpf_log, mpf_log1p, mpf_log_hypot, mpf_nthroot,
mpf_phi, mpf_pi, mpf_pow_int, mpf_sin, mpf_sin_pi,
mpf_sinh, mpf_tan, mpf_tanh)
from .libintmath import giant_steps, lshift, rshift
from .libmpf import (ComplexResult, fhalf, finf, fnan, fninf, fnone, fone,
from_float, from_int, from_man_exp, ftwo, fzero, mpf_abs,
Expand Down Expand Up @@ -719,7 +719,7 @@ def acos_asin(z, prec, rnd, n):
Am1 = mpf_shift(mpf_add(c1, c2, wp), -1)
# im = log(1 + Am1 + sqrt(Am1*(alpha+1)))
im = mpf_mul(Am1, mpf_add(alpha, fone, wp), wp)
im = mpf_log(mpf_add(fone, mpf_add(Am1, mpf_sqrt(im, wp), wp), wp), wp)
im = mpf_log1p(mpf_add(Am1, mpf_sqrt(im, wp), wp), wp)
else:
# im = log(alpha + sqrt(alpha*alpha - 1))
im = mpf_sqrt(mpf_sub(mpf_mul(alpha, alpha, wp), fone, wp), wp)
Expand Down
18 changes: 13 additions & 5 deletions mpmath/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
asech, asin, asinh, atan, atan2, atanh, catalan, cbrt,
ceil, conj, cos, cos_sin, cosh, cospi, cospi_sinpi, cot,
coth, csc, csch, cyclotomic, degree, degrees, e, eps,
euler, exp, expj, expjpi, expm1, fabs, fadd, fib,
euler, exp, exp2, expj, expjpi, expm1, fabs, fadd, fib,
fibonacci, floor, fmod, fp, frexp, glaisher, hypot, im,
inf, isnan, j, khinchin, ldexp, linspace, ln, ln2, ln10,
log, log1p, log10, mertens, mp, mpc, mpf, nan, nthroot,
phi, pi, power, powm1, radians, rand, re, root, sec, sech,
sign, sin, sinc, sincpi, sinh, sinpi, sqrt, tan, tanh,
twinprime, unitroots)
log, log1p, log2, log10, mertens, mp, mpc, mpf, nan,
nthroot, phi, pi, power, powm1, radians, rand, re, root,
sec, sech, sign, sin, sinc, sincpi, sinh, sinpi, sqrt, tan,
tanh, twinprime, unitroots)
from mpmath.libmp import (MPZ, ComplexResult, from_int, mpf_gt, mpf_lt,
mpf_mul, mpf_pow_int, mpf_rand, mpf_sqrt,
round_ceiling, round_down, round_nearest, round_up)
Expand Down Expand Up @@ -326,6 +326,12 @@ def test_asin():
assert asin(mpc(+2, 0)).ae(mpc(+pi2, -log(2 + sqrt(3))))
assert asin(mpc(0.5, 0)).ae(pi/6)

# issue 787
assert asin(mpc(0, 1e-22)).ae(1e-22j)
mp.prec = 700
assert asin(mpc(0, 1e-220)).ae(1e-220j)
mp.prec = 53

def test_acos():
pi4 = pi/4
assert acos(mpc(+inf, +inf)) == mpc(+pi4, -inf)
Expand Down Expand Up @@ -540,6 +546,8 @@ def test_frexp():
def test_aliases():
assert ln(7) == log(7)
assert log10(3.75) == log(3.75,10)
assert log2(1.25) == log(1.25,2)
assert exp2(-0.5) == power(2, -0.5)
assert degrees(5.6) == 5.6 / degree
assert radians(5.6) == 5.6 * degree
assert power(-1,0.5) == j
Expand Down