Saturday, October 29, 2011

Install Sympy: A symbolic math software package in pure Python!

What is nice about Sympy?

It aims to be a simple but full-featured, extensible computer algebra system and it is written in pure Python. There is no need for external libraries!

Here are some information extracted from the home page of sympy.
What are the available features in Sympy?

The current core of Sympy have the following capabilities:

  • basic arithmetics *,/,+,-,**
  • basic simplification (like a*b*b + 2*b*a*b -> 3*a*b**2)
  • expansion (like (a+b)**2 -> a**2 + 2*a*b + b**2)
  • functions (exp, ln, ...)
  • complex numbers (like exp(I*x).expand(complex=True) -> cos(x)+I*sin(x))
  • differentiation
  • taylor (laurent) series
  • substitution (like x -> ln(x), or sin -> cos)
  • arbitrary precision integers, rationals and floats
  • noncommutative symbols
  • pattern matching


What are the avaialable modules aside from the core?

  • more functions (sin, cos, tan, atan, asin, acos, factorial, zeta, legendre)
  • limits (like limit(x*log(x), x, 0) -> 0)
  • integration using extended Risch-Norman heuristic
  • polynomials (division, gcd, square free decomposition, groebner bases, factorization)
  • solvers (algebraic, difference and differential equations, and systems of equations)
  • symbolic matrices (determinants, LU decomposition...)
  • mpmath (multiprecision floating-point arithmetic)
  • geometric algebra (GA)
  • Pauli and Dirac algebra
  • quantum physics
  • geometry module
  • plotting (2D and 3D)
  • code generation (C, Fortran, LaTeX)
We wish that Sympy will become standard and integrate with the other package called Symbolic. It is a long time coming. This will not supplant the need for Maxima and will enable Python loving scientists and applied and research mathematicians. But I got the following error on installing Sympy in Oneiric:
sudo apt-get install sympy [sudo] password for toto: Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package sympy toto@toto-Aspire-4520:~$ sudo apt-get install python-sympy Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: python-sympy 0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded. Need to get 1,694 kB of archives. After this operation, 9,490 kB of additional disk space will be used. Get:1 http://ph.archive.ubuntu.com/ubuntu/ oneiric/universe python-sympy all 0.6.7-1.1 [1,694 kB] Fetched 1,694 kB in 19s (84.8 kB/s) Selecting previously deselected package python-sympy. (Reading database ... 552034 files and directories currently installed.) Unpacking python-sympy (from .../python-sympy_0.6.7-1.1_all.deb) ... Processing triggers for man-db ... Processing triggers for menu ... Setting up python-sympy (0.6.7-1.1) ... Processing triggers for menu ... Processing triggers for python-support ... Compiling /usr/lib/pymodules/python2.6/mpmath/libmp/exec_py3.py ... SyntaxError: ('invalid syntax', ('/usr/lib/pymodules/python2.6/mpmath/libmp/exec_py3.py', 1, 12, 'exec_ = exec\n')) Compiling /usr/lib/pymodules/python2.7/mpmath/libmp/exec_py3.py ... SyntaxError: ('invalid syntax', ('/usr/lib/pymodules/python2.7/mpmath/libmp/exec_py3.py', 1, 12, 'exec_ = exec\n')) Compiling /usr/lib/pymodules/python2.7/mpmath/tests/extratest_gamma.py ... SyntaxError: ('invalid syntax', ('/usr/lib/pymodules/python2.7/mpmath/tests/extratest_gamma.py', 50, 35, ' print("%s ok;" % name, end=\' \')\n')) Compiling /usr/lib/pymodules/python2.7/mpmath/tests/torture.py ... SyntaxError: ('invalid syntax', ('/usr/lib/pymodules/python2.7/mpmath/tests/torture.py', 80, 27, ' print(".", end=\' \')\n'))
Yet when I fired up ipython and imported Sympy, there were no errors. But lets spend more time with this package on our blog. Oh, I removed sympy and reinstalled the package using sudo easy_install-2.7 sympy Here are some simple examples.
>>> sympy.factor("(x^2 + 2*a*x + a^2)")
(a + x)**2

>>> sympy.diff("x^2*y", "x")
2*x*y

>>> sympy.diff("log(y)")
1/y

>>> sympy.simplify("(x-a)/(x-a)")
1
Of course we just cannot trust the results blindly. Integration is somewhat convoluted. Here is the help documentation for integrate:
integrate(expr, *args, **kwargs)
    integrate(f, var, ...)
    
    Compute definite or indefinite integral of one or more variables
    using Risch-Norman algorithm and table lookup. This procedure is
    able to handle elementary algebraic and transcendental functions
    and also a huge class of special functions, including Airy,
    Bessel, Whittaker and Lambert.
    
    var can be:
    
    - a symbol                   -- indefinite integration
    - a tuple (symbol, a, b)     -- definite integration
    
    Several variables can be specified, in which case the result is multiple
    integration.
    
    Also, if no var is specified at all, then the full anti-derivative of f is
    returned. This is equivalent to integrating f over all its variables.
    
    **Examples**
    
    >>> from sympy import integrate, log
    >>> from sympy.abc import a, x, y
    
    >>> integrate(x*y, x)
    x**2*y/2
    
    >>> integrate(log(x), x)
    x*log(x) - x

This is module worth having in your system. We hope that this will be more robust as time goes by.



No comments:

Post a Comment