Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Exploring the SymPy profile


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.

Getting ready

Install SymPy using either easy_install or pip:

$ sudo easy_install sympy
$ sudo pip install sympy

How to do it...

The following steps will help you explore the SymPy profile:

  1. 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

  2. Start IPython with the SymPy profile using this command:

    $ ipython --profile=sympy
    
  3. Expand an algebraic expression using the command shown in the following screenshot:

See also