Exploring the SymPy profile
IPython has a sample SymPy profile. SymPy is a Python symbolic, mathematics library. For instance, we can simplify algebraic expressions or differentiate, similar to Mathematica and Maple. SymPy is obviously a fun piece of software, but is not directly necessary for our journey through the NumPy landscape. Consider this as an optional bonus recipe. Like dessert, feel free to skip, although you might miss out on the sweetest piece of this chapter.
Getting ready
Install
SymPy using either easy_install
, or pip
:
easy_install sympy sudo pip install sympy
How to do it...
Look at the configuration file, which can be found at
~/.ipython/profile_sympy/ipython_config.py
. The contents are 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:
Loading the default profile
Importing the SymPy packages
Defining symbols
Start IPython with the SymPy profile using the following command:
ipython --profile=sympy
Expand an algebraic expression using the command shown in the following screenshot: