SymPy

Last updated
SymPy
Developer(s) SymPy Development Team
Initial release2007;17 years ago (2007)
Stable release
1.12 [1] / 10 May 2023;8 months ago (2023-05-10)
Repository
Written in Python
Operating system Cross-platform
Type Computer algebra system
License New BSD License
Website www.sympy.org   OOjs UI icon edit-ltr-progressive.svg

SymPy is an open-source Python library for symbolic computation. It provides computer algebra capabilities either as a standalone application, as a library to other applications, or live on the web as SymPy Live [2] or SymPy Gamma. [3] SymPy is simple to install and to inspect because it is written entirely in Python with few dependencies. [4] [5] [6] This ease of access combined with a simple and extensible code base in a well known language make SymPy a computer algebra system with a relatively low barrier to entry.

Contents

SymPy includes features ranging from basic symbolic arithmetic to calculus, algebra, discrete mathematics, and quantum physics. It is capable of formatting the result of the computations as LaTeX code. [4] [5]

SymPy is free software and is licensed under the New BSD license. The lead developers are Ondřej Čertík and Aaron Meurer. It was started in 2005 by Ondřej Čertík. [7]

Features

The SymPy library is split into a core with many optional modules.

Currently, the core of SymPy has around 260,000 lines of code [8] (it also includes a comprehensive set of self-tests: over 100,000 lines in 350 files as of version 0.7.5), and its capabilities include: [4] [5] [9] [10] [11]

Core capabilities

Polynomials

Calculus

Solving equations

Discrete math

Matrices

Geometry

Plotting

Note, plotting requires the external Matplotlib or Pyglet module.

Physics

Statistics

Combinatorics

Printing

Dependencies

Since version 1.0, SymPy has the mpmath package as a dependency.

There are several optional dependencies that can enhance its capabilities:

Usage examples

Pretty-printing

Sympy allows outputs to be formatted into a more appealing format through the pprint function. Alternatively, the init_printing() method will enable pretty-printing, so pprint need not be called. Pretty-printing will use unicode symbols when available in the current environment, otherwise it will fall back to ASCII characters.

>>> fromsympyimportpprint,init_printing,Symbol,sin,cos,exp,sqrt,series,Integral,Function>>>>>> x=Symbol("x")>>> y=Symbol("y")>>> f=Function("f")>>> # pprint will default to unicode if available>>> pprint(x**exp(x)) ⎛ x⎞ ⎝ℯ ⎠x   >>> # An output without unicode>>> pprint(Integral(f(x),x),use_unicode=False)  /        |         | f(x) dx |        /        >>> # Compare with same expression but this time unicode is enabled>>> pprint(Integral(f(x),x),use_unicode=True)⎮ f(x) dx>>> # Alternatively, you can call init_printing() once and pretty-print without the pprint function.>>> init_printing()>>> sqrt(sqrt(exp(x)))   ____4 ╱  x ╲╱  ℯ  >>> (1/cos(x)).series(x,0,10)     2      4       6        8             x    5⋅x    61⋅x    277⋅x     ⎛ 10⎞1 + ── + ──── + ───── + ────── + O⎝x  ⎠    2     24     720     8064

Expansion

>>> fromsympyimportinit_printing,Symbol,expand>>> init_printing()>>>>>> a=Symbol("a")>>> b=Symbol("b")>>> e=(a+b)**3>>> e(a + b)³>>> e.expand()a³ + 3⋅a²⋅b + 3⋅a⋅b²  + b³

Arbitrary-precision example

>>> fromsympyimportRational,pprint>>> e=2**50/Rational(10)**50>>> pprint(e)1/88817841970012523233890533447265625

Differentiation

>>> fromsympyimportinit_printing,symbols,ln,diff>>> init_printing()>>> x,y=symbols("x y")>>> f=x**2/y+2*x-ln(y)>>> diff(f,x) 2⋅x     ─── + 2  y >>> diff(f,y)    2       x    1 - ── - ─    2   y   y>>> diff(diff(f,x),y) -2⋅x ────   2   y

Plotting

Output of the plotting example Sympy plot screenshot.png
Output of the plotting example
>>> fromsympyimportsymbols,cos>>> fromsympy.plottingimportplot3d>>> x,y=symbols("x y")>>> plot3d(cos(x*3)*cos(y*5)-y,(x,-1,1),(y,-1,1))<sympy.plotting.plot.Plot object at 0x3b6d0d0>

Limits

>>> fromsympyimportinit_printing,Symbol,limit,sqrt,oo>>> init_printing()>>> >>> x=Symbol("x")>>> limit(sqrt(x**2-5*x+6)-x,x,oo)-5/2>>> limit(x*(sqrt(x**2+1)-x),x,oo)1/2>>> limit(1/x**2,x,0)>>> limit(((x-1)/(x+1))**x,x,oo) -2

Differential equations

>>> fromsympyimportinit_printing,Symbol,Function,Eq,dsolve,sin,diff>>> init_printing()>>>>>> x=Symbol("x")>>> f=Function("f")>>>>>> eq=Eq(f(x).diff(x),f(x))>>> eqd              ──(f(x)) = f(x)dx         >>> >>> dsolve(eq,f(x))           xf(x) = C₁⋅ℯ>>>>>> eq=Eq(x**2*f(x).diff(x),-3*x*f(x)+sin(x)/x)>>> eq 2 d                      sin(x)x ⋅──(f(x)) = -3⋅x⋅f(x) + ──────   dx                       x   >>>>>> dsolve(eq,f(x))       C₁ - cos(x)f(x) = ───────────   

Integration

>>> fromsympyimportinit_printing,integrate,Symbol,exp,cos,erf>>> init_printing()>>> x=Symbol("x")>>> # Polynomial Function>>> f=x**2+x+1>>> f 2        x  + x + 1>>> integrate(f,x) 3    2    x    x     ── + ── + x3    2     >>> # Rational Function>>> f=x/(x**2+2*x+1)>>> f     x      ──────────── 2          x  + 2⋅x + 1>>> integrate(f,x)               1  log(x + 1) + ─────             x + 1>>> # Exponential-polynomial functions>>> f=x**2*exp(x)*cos(x)>>> f 2  x       x ⋅ℯ ⋅cos(x)>>> integrate(f,x) 2  x           2  x                         x           x       x ⋅ℯ ⋅sin(x)   x ⋅ℯ ⋅cos(x)      x          ℯ ⋅sin(x)   ℯ ⋅cos(x)──────────── + ──────────── - x⋅ℯ ⋅sin(x) + ───────── - ─────────     2              2                           2           2    >>> # A non-elementary integral>>> f=exp(-(x**2))*erf(x)>>> f   2        -x        ℯ   ⋅erf(x)>>> integrate(f,x)  ___    2   ╲╱ π ⋅erf (x)─────────────      4

Series

>>> fromsympyimportSymbol,cos,sin,pprint>>> x=Symbol("x")>>> e=1/cos(x)>>> pprint(e)  1   ──────cos(x)>>> pprint(e.series(x,0,10))     2      4       6        8             x    5⋅x    61⋅x    277⋅x     ⎛ 10⎞1 + ── + ──── + ───── + ────── + O⎝x  ⎠    2     24     720     8064          >>> e=1/sin(x)>>> pprint(e)  1   ──────sin(x)>>> pprint(e.series(x,0,4))           3        1   x   7⋅x     ⎛ 4⎞─ + ─ + ──── + O⎝x ⎠x   6   360

Logical reasoning

Example 1

>>> fromsympyimport*>>> x=Symbol("x")>>> y=Symbol("y")>>> facts=Q.positive(x),Q.positive(y)>>> withassuming(*facts):... print(ask(Q.positive(2*x+y)))True

Example 2

>>> fromsympyimport*>>> x=Symbol("x")>>> # Assumption about x>>> fact=[Q.prime(x)]>>> withassuming(*fact):... print(ask(Q.rational(1/x)))True

See also

Related Research Articles

<span class="mw-page-title-main">Arithmetic–geometric mean</span> Mathematical function of two positive real arguments

In mathematics, the arithmetic–geometric mean of two positive real numbers x and y is the mutual limit of a sequence of arithmetic means and a sequence of geometric means:

<span class="mw-page-title-main">Complex number</span> Number with a real and an imaginary part

In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted i, called the imaginary unit and satisfying the equation ; every complex number can be expressed in the form , where a and b are real numbers. Because no real number satisfies the above equation, i was called an imaginary number by René Descartes. For the complex number ,a is called the real part, and b is called the imaginary part. The set of complex numbers is denoted by either of the symbols or C. Despite the historical nomenclature "imaginary", complex numbers are regarded in the mathematical sciences as just as "real" as the real numbers and are fundamental in many aspects of the scientific description of the natural world.

<span class="mw-page-title-main">Imaginary unit</span> Principal square root of −1

The imaginary unit or unit imaginary number is a solution to the quadratic equation x2 + 1 = 0. Although there is no real number with this property, i can be used to extend the real numbers to what are called complex numbers, using addition and multiplication. A simple example of the use of i in a complex number is 2 + 3i.

<span class="mw-page-title-main">Maple (software)</span> Mathematical computing environment

Maple is a symbolic and numeric computing environment as well as a multi-paradigm programming language. It covers several areas of technical computing, such as symbolic mathematics, numerical analysis, data processing, visualization, and others. A toolbox, MapleSim, adds functionality for multidomain physical modeling and code generation.

<span class="mw-page-title-main">Maxima (software)</span> Computer algebra system

Maxima is a computer algebra system (CAS) based on a 1982 version of Macsyma and is still in development today. It is written in Common Lisp and runs on all POSIX platforms such as macOS, Unix, BSD, and Linux, as well as under Microsoft Windows and Android. It is free software released under the terms of the GNU General Public License (GPL).

<span class="mw-page-title-main">Exponentiation</span> Arithmetic operation

In mathematics, exponentiation is an operation involving two numbers: the base and the exponent or power. Exponentiation is written as bn, where b is the base and n is the power; this is pronounced as "b (raised) to the n". When n is a positive integer, exponentiation corresponds to repeated multiplication of the base: that is, bn is the product of multiplying n bases:

In mathematics, a Gaussian function, often simply referred to as a Gaussian, is a function of the base form

<span class="mw-page-title-main">Tomographic reconstruction</span> Estimate object properties from a finite number of projections

Tomographic reconstruction is a type of multidimensional inverse problem where the challenge is to yield an estimate of a specific system from a finite number of projections. The mathematical basis for tomographic imaging was laid down by Johann Radon. A notable example of applications is the reconstruction of computed tomography (CT) where cross-sectional images of patients are obtained in non-invasive manner. Recent developments have seen the Radon transform and its inverse used for tasks related to realistic object insertion required for testing and evaluating computed tomography use in airport security.

<span class="mw-page-title-main">Parabolic cylinder function</span>

In mathematics, the parabolic cylinder functions are special functions defined as solutions to the differential equation

<span class="mw-page-title-main">MayaVi</span>

MayaVi is a scientific data visualizer written in Python, which uses VTK and provides a GUI via Tkinter. MayaVi was developed by Prabhu Ramachandran, is free and distributed under the BSD License. It is cross-platform and runs on any platform where both Python and VTK are available. MayaVi is pronounced as a single name, "Ma-ya-vee", meaning "magical" in Sanskrit. The code of MayaVi has nothing in common with that of Autodesk Maya or the Vi text editor.

Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments on which they operate. Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments. Tacit programming is of theoretical interest, because the strict use of composition results in programs that are well adapted for equational reasoning. It is also the natural style of certain programming languages, including APL and its derivatives, and concatenative languages such as Forth. The lack of argument naming gives point-free style a reputation of being unnecessarily obscure, hence the epithet "pointless style".

<span class="mw-page-title-main">Transcendental equation</span> Equation whose side(s) describe a transcendental function

In applied mathematics, a transcendental equation is an equation over the real numbers that is not algebraic, that is, if at least one of its sides describes a transcendental function. Examples include:

<span class="mw-page-title-main">IPython</span> Advanced interactive shell for Python

IPython is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history. IPython provides the following features:

<span class="mw-page-title-main">Cython</span> Programming language

Cython is a superset of the programming language Python, which allows developers to write Python code that yields performance comparable to that of C.

Theano is a Python library and optimizing compiler for manipulating and evaluating mathematical expressions, especially matrix-valued ones. In Theano, computations are expressed using a NumPy-esque syntax and compiled to run efficiently on either CPU or GPU architectures.

<span class="mw-page-title-main">Wolfram Language</span> Programming language and environment developed by Wolfram Research

The Wolfram Language is a proprietary, general high-level multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary structures and data. It is the programming language of the mathematical symbolic computation program Mathematica.

<span class="mw-page-title-main">Numba</span> Open-source JIT compiler

Numba is an open-source JIT compiler that translates a subset of Python and NumPy into fast machine code using LLVM, via the llvmlite Python package. It offers a range of options for parallelising Python code for CPUs and GPUs, often with only minor code changes.

The GEKKO Python package solves large-scale mixed-integer and differential algebraic equations with nonlinear programming solvers. Modes of operation include machine learning, data reconciliation, real-time optimization, dynamic simulation, and nonlinear model predictive control. In addition, the package solves Linear programming (LP), Quadratic programming (QP), Quadratically constrained quadratic program (QCQP), Nonlinear programming (NLP), Mixed integer programming (MIP), and Mixed integer linear programming (MILP). GEKKO is available in Python and installed with pip from PyPI of the Python Software Foundation.

CuPy is an open source library for GPU-accelerated computing with Python programming language, providing support for multi-dimensional arrays, sparse matrices, and a variety of numerical algorithms implemented on top of them. CuPy shares the same API set as NumPy and SciPy, allowing it to be a drop-in replacement to run NumPy/SciPy code on GPU. CuPy supports NVIDIA CUDA GPU platform, and AMD ROCm GPU platform starting in v9.0.

References

  1. "Releases - sympy/sympy" . Retrieved 6 September 2022 via GitHub.
  2. "SymPy Live". live.sympy.org. Retrieved 2021-08-25.
  3. "SymPy Gamma". www.sympygamma.com. Retrieved 2021-08-25.
  4. 1 2 3 "SymPy homepage" . Retrieved 2014-10-13.
  5. 1 2 3 Joyner, David; Čertík, Ondřej; Meurer, Aaron; Granger, Brian E. (2012). "Open source computer algebra systems: SymPy". ACM Communications in Computer Algebra. 45 (3/4): 225–234. doi:10.1145/2110170.2110185. S2CID   44862851.
  6. Meurer, Aaron; Smith, Christopher P.; Paprocki, Mateusz; Čertík, Ondřej; Kirpichev, Sergey B.; Rocklin, Matthew; Kumar, AMiT; Ivanov, Sergiu; Moore, Jason K. (2017-01-02). "SymPy: symbolic computing in Python" (PDF). PeerJ Computer Science. 3: e103. doi: 10.7717/peerj-cs.103 . ISSN   2376-5992.
  7. "SymPy vs. Mathematica · sympy/Sympy Wiki". GitHub .
  8. "Sympy project statistics on Open HUB" . Retrieved 2014-10-13.
  9. Gede, Gilbert; Peterson, Dale L.; Nanjangud, Angadh; Moore, Jason K.; Hubbard, Mont (2013). Constrained multibody dynamics with Python: From symbolic equation generation to publication . ASME 2013 International Design Engineering Technical Conferences and Computers and Information in Engineering Conference. American Society of Mechanical Engineers. pp. V07BT10A051. doi:10.1115/DETC2013-13470. ISBN   978-0-7918-5597-3.
  10. Rocklin, Matthew; Terrel, Andy (2012). "Symbolic Statistics with SymPy". Computing in Science & Engineering. 14 (3): 88–93. Bibcode:2012CSE....14c..88R. doi:10.1109/MCSE.2012.56. S2CID   18307629.
  11. Asif, Mushtaq; Olaussen, Kåre (2014). "Automatic code generator for higher order integrators". Computer Physics Communications. 185 (5): 1461–1472. arXiv: 1310.2111 . Bibcode:2014CoPhC.185.1461M. doi:10.1016/j.cpc.2014.01.012. S2CID   42041635.
  12. "Assumptions Module — SymPy 1.4 documentation". docs.sympy.org. Retrieved 2019-07-05.
  13. "Continuum Mechanics — SymPy 1.4 documentation". docs.sympy.org. Retrieved 2019-07-05.
  14. "GitHub - symengine/symengine: SymEngine is a fast symbolic manipulation library, written in C++". GitHub. Retrieved 2021-08-25.
  15. "mpmath - Python library for arbitrary-precision floating-point arithmetic". mpmath.org. Retrieved 2021-08-25.
  16. "GitHub - pearu/sympycore: Automatically exported from code.google.com/p/sympycore". GitHub. January 2021. Retrieved 2021-08-25.
  17. Developers, SfePy. "SfePy: Simple Finite Elements in Python — SfePy version: 2021.2+git.13ca95f1 documentation". sfepy.org. Retrieved 2021-08-25.
  18. "GitHub - pygae/galgebra: Symbolic Geometric Algebra/Calculus package for SymPy". GitHub. Retrieved 2021-08-25.
  19. "Quameon - Quantum Monte Carlo in Python". quameon.sourceforge.net. Retrieved 2021-08-25.
  20. "Welcome to Lcapy's documentation! — Lcapy 0.76 documentation". 2021-01-16. Archived from the original on 2021-01-16. Retrieved 2021-08-25.
  21. "LaTeX Expression project documentation — LaTeX Expression 0.3.dev documentation". mech.fsv.cvut.cz. Retrieved 2021-08-25.
  22. "Symbolic Statistics with SymPy". ResearchGate. Retrieved 2021-08-25.
  23. "Diofant's documentation — Diofant 0.13.0a4.dev13+g8c5685115 documentation". diofant.readthedocs.io. Retrieved 2021-08-25.