Wednesday, March 11, 2026

Simplifying expressions in SymPy


The earlier publish checked out why Mathematica doesn’t simplify the expression Sinh[ArcCosh[x]] the best way you would possibly suppose it ought to. This publish might be a type of Python analog of the earlier publish.

SymPy is a Python library that amongst different issues will simplify mathematical expressions. As earlier than, we search to confirm the entries within the desk beneath, this time utilizing SymPy.

Right here’s the code:

from sympy import *

x = symbols('x')

print( simplify(sinh(asinh(x))) )
print( simplify(sinh(acosh(x))) )
print( simplify(sinh(atanh(x))) )
print( simplify(cosh(asinh(x))) )
print( simplify(cosh(acosh(x))) )
print( simplify(cosh(atanh(x))) )
print( simplify(tanh(asinh(x))) )
print( simplify(tanh(acosh(x))) )
print( simplify(tanh(atanh(x))) )

As earlier than, the outcomes are largely as we’d anticipate:

x
sqrt(x - 1)*sqrt(x + 1)
x/sqrt(1 - x**2)
sqrt(x**2 + 1)
x
1/sqrt(1 - x**2)
x/sqrt(x**2 + 1)
sqrt(x - 1)*sqrt(x + 1)/x
x

Additionally as earlier than, sinh(acosh(x)) and tanh(acosh(x)) return extra sophisticated expressions than within the desk above. Why doesn’t

√(x − 1) √(x + 1)

simplify to

√(x² − 1)

as you’d anticipate? As a result of the equation

√(x − 1) √(x + 1) = √(x² − 1)

doesn’t maintain for all x. See the earlier publish for the subtleties of defining arccosh and sqrt for advanced numbers. The equation above doesn’t maintain, for instance, when x = −2.

As in Mathematica, you’ll be able to specify the vary of variables in SymPy. If we specify that x ≥ 0 we get the end result we anticipate. The code

x = symbols('x', actual=True, nonnegative=True)
print( simplify(sinh(acosh(x))) )

prints

sqrt(x**2 - 1)

as anticipated.

Related Articles

Latest Articles