IPython has a sample SymPy profile. SymPy is a Python-symbolic mathematics library. We can simplify algebraic expressions or differentiate functions, similar to Mathematica and Maple. SymPy is obviously a fun piece of software, but is not necessary for our journey through the NumPy landscape. Consider this as an optional or bonus recipe. Like a dessert, feel free to skip it, although you might miss out on the sweetest piece of this chapter.
Install SymPy using either easy_install
or pip
:
$ sudo easy_install sympy $ sudo pip install sympy
The following steps will help you explore the SymPy profile:
Look at the configuration file, which can be found at
~/.ipython/profile_sympy/ipython_config.py
. The content is as follows:c = get_config() app = c.InteractiveShellApp # This can be used at any point in a config file to load a sub config # and merge it into the current one. load_subconfig('ipython_config.py', profile='default') lines = """ from __future__ import division from sympy import * x, y, z, t = symbols('x y z t') k, m, n = symbols('k m n', integer=True) f, g, h = symbols('f g h', cls=Function) """ # You have to make sure that attributes that are containers already # exist before using them. Simple assigning a new list will override # all previous values. if hasattr(app, 'exec_lines'): app.exec_lines.append(lines) else: app.exec_lines = [lines] # Load the sympy_printing extension to enable nice printing of sympy expr's. if hasattr(app, 'extensions'): app.extensions.append('sympyprinting') else: app.extensions = ['sympyprinting']
This code accomplishes the following:
Loads the default profile
Imports the SymPy packages
Defines symbols
Start IPython with the SymPy profile using this command:
$ ipython --profile=sympy
Expand an algebraic expression using the command shown in the following screenshot:
The SymPy homepage at http://sympy.org/en/index.html