Article 744XB Simplifying expressions in SymPy

Simplifying expressions in SymPy

by
John
from John D. Cook on (#744XB)

The previous post looked at why Mathematica does not simplify the expression Sinh[ArcCosh[x]] the way you might think it should. This post will be a sort of Python analog of the previous post.

SymPy is a Python library that among other things will simplify mathematical expressions. As before, we seek to verify the entries in the table below, this time using SymPy.

hyp_mult_table.svg

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 before, the results are mostly as we'd expect:

xsqrt(x - 1)*sqrt(x + 1)x/sqrt(1 - x**2)sqrt(x**2 + 1)x1/sqrt(1 - x**2)x/sqrt(x**2 + 1)sqrt(x - 1)*sqrt(x + 1)/xx

Also as before, sinh(acosh(x)) and tanh(acosh(x)) return more complicated expressions than in the table above. Why doesn't

(x - 1) (x + 1)

simplify to

(x^2 - 1)

as you'd expect? Because the equation

(x - 1) (x + 1) = (x^2 - 1)

does not hold for allx. See the previous post for the subtleties of defining arccosh and sqrt for complex numbers. The equation above does not hold, for example, whenx = -2.

As in Mathematica, you can specify the range of variables in SymPy. If we specify thatx >= 0 we get the result we expect. The code

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

prints

sqrt(x**2 - 1)

as expected.

The post Simplifying expressions in SymPy first appeared on John D. Cook.
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments