Symbolic math with python

3.2. Sympy : Symbolic Mathematics in Python¶

What is SymPy? SymPy is a Python library for symbolic mathematics. It aims to be an alternative to systems such as Mathematica or Maple while keeping the code as simple as possible and easily extensible. SymPy is written entirely in Python and does not require any external libraries.

Sympy documentation and packages for installation can be found on http://www.sympy.org/

3.2.1. First Steps with SymPy¶

3.2.1.1. Using SymPy as a calculator¶

SymPy defines three numerical types: Real , Rational and Integer .

The Rational class represents a rational number as a pair of two Integers: the numerator and the denominator, so Rational(1, 2) represents 1/2, Rational(5, 2) 5/2 and so on:

>>> import sympy as sym >>> a = sym.Rational(1, 2) >>> a 1/2 >>> a*2 1 

SymPy uses mpmath in the background, which makes it possible to perform computations using arbitrary-precision arithmetic. That way, some special constants, like e, pi, oo(Infinity), are treated as symbols and can be evaluated with arbitrary precision:

>>> sym.pi**2 pi**2 >>> sym.pi.evalf() 3.14159265358979 >>> (sym.pi + sym.exp(1)).evalf() 5.85987448204884 

as you see, evalf evaluates the expression to a floating-point number.

There is also a class representing mathematical infinity, called oo :

>>> sym.oo > 99999 True >>> sym.oo + 1 oo 
  1. Calculate \sqrt<2 data-lazy-src=
Оцените статью