You are previewing Sage Beginner's Guide.
O'Reilly logo
Sage Beginner's Guide

Book Description

Unlock the full potential of Sage for simplifying and automating mathematical computing

  • The best way to learn Sage which is a open source alternative to Magma, Maple, Mathematica, and Matlab

  • Learn to use symbolic and numerical computation to simplify your work and produce publication-quality graphics

  • Numerically solve systems of equations, find roots, and analyze data from experiments or simulations

  • Save time on algebra by automatically simplifying symbolic expressions, performing calculus operations, and manipulating vectors and matrices

  • Use the Python programming language to write and debug code more quickly than traditional compiled languages like C++ or Fortran

  • Key features of Sage are explained using practical examples from engineering, science, and applied mathematics

In Detail

Your work demands results, and you don't have time for tedious, repetitive mathematical tasks. Sage is a free, open-source software package that automates symbolic and numerical calculations with the power of the Python programming language, so you can focus on the analytical and creative aspects of your work or studies.

Sage Beginner's Guide shows you how to do calculations with Sage. Each concept is illustrated with a complete example that you can use as a starting point for your own work. You will learn how to use many of the functions that are built in to Sage, and how to use Python to write sophisticated programs that utilize the power of Sage.

This book starts by showing you how to download and install Sage, and introduces the command-line interface and the graphical notebook interface. It also includes an introduction to Python so you can start programming in Sage. Every major concept is illustrated with a practical example.

After learning the fundamentals of variables and functions in Sage, you will learn how to symbolically simplify expressions, solve equations, perform integrals and derivatives, and manipulate vectors and matrices. You will learn how Sage can produce numerous kinds of plots and graphics. The book will demonstrate numerical methods in Sage, and explain how to use object-oriented programming to improve your code.

Sage Beginner's Guide will give you the tools you need to unlock the full potential of Sage for simplifying and automating mathematical computing.

Effectively use Sage to eliminate tedious algebra, speed up numerical calculations, implement algorithms and data structures, and illustrate your work with publication-quality plots and graphics

Table of Contents

  1. Sage
    1. Sage
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Time for action - heading
        1. What just happened?
        2. Pop quiz - heading
        3. Have a go hero - heading
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. What Can You Do with Sage?
      1. Getting started
      2. Using Sage as a powerful calculator
        1. Symbolic mathematics
        2. Have a go hero -
        3. Linear algebra
        4. Solving an ordinary differential equation
      3. More advanced graphics
        1. Visualising a three-dimensional surface
        2. Typesetting mathematical expressions
      4. A practical example: analysing experimental data
      5. Time for action - fitting the standard curve
        1. What just happened?
      6. Time for action - plotting experimental data
        1. What just happened?
      7. Time for action - fitting a growth model
        1. What just happened?
      8. Summary
    8. 2. Installing Sage
      1. Before you begin
      2. Installing a binary version of Sage on Windows
        1. Downloading VMware Player
        2. Installing VMWare Player
        3. Downloading and extracting Sage
        4. Launching the virtual machine
        5. Start Sage
      3. Installing a binary version of Sage on OS X
        1. Downloading Sage
        2. Installing Sage
        3. Starting Sage
      4. Installing a binary version of Sage on GNU/Linux
        1. Downloading and decompressing Sage
        2. Running Sage from your user account
        3. Installing for multiple users
      5. Building Sage from source
        1. Prerequisites
        2. Downloading and decompressing source tarball
        3. Building Sage
        4. Installation
      6. Summary
    9. 3. Getting Started with Sage
      1. How to get help with Sage
      2. Starting Sage from the command line
      3. Using the interactive shell
      4. Time for action - doing calculations on the command line
        1. What just happened?
        2. Getting help
        3. Command history
        4. Tab completion
        5. Interactively tracing execution
      5. Using the notebook interface
        1. Starting the notebook interface
      6. Time for action - doing calculations with the notebook interface
        1. What just happened?
        2. Getting help in the notebook interface
        3. Working with cells
        4. Working with code
        5. Closing the notebook interface
        6. Have a go hero - using the notebook interface
      7. Displaying results of calculations
      8. Operators and variables
        1. Arithmetic operators
        2. Pop quiz - working with operators
        3. Numerical types
          1. Integers and rational numbers
          2. Real numbers
          3. Complex numbers
          4. Symbolic expressions
          5. Defining variables on rings
          6. Combining types in expressions
        4. Pop quiz - understanding types
        5. Strings
      9. Time for action - using strings
        1. What just happened?
      10. Callable symbolic expressions
      11. Time for action - defining callable symbolic expressions
        1. What just happened?
        2. Automatically typesetting expressions
      12. Functions
      13. Time for action - calling functions
        1. What just happened?
        2. Have a go hero - make some more plots
        3. Built-in functions
          1. Numerical approximations
          2. The reset and restore functions
        4. Defining your own functions
      14. Time for action - defining and using your own functions
        1. What just happened?
        2. Functions with keyword arguments
      15. Time for action - defining a function with keyword arguments
        1. What just happened?
      16. Objects
      17. Time for action - working with objects
        1. What just happened?
        2. Getting help with objects
      18. Summary
    10. 4. Introducing Python and Sage
      1. Python 2 and Python 3
      2. Writing code for Sage
        1. Long lines of code
        2. Running scripts
      3. Sequence types: lists, tuples, and strings
      4. Time for action - creating lists
        1. What just happened?
        2. Getting and setting items in lists
      5. Time for action - accessing items in a list
        1. What just happened?
        2. Pop quiz - lists and indexing
        3. List functions and methods
        4. Tuples: read-only lists
      6. Time for action - returning multiple values from a function
        1. What just happened?
        2. Strings
      7. Time for action - working with strings
        1. What just happened?
        2. Other sequence types
      8. For loops
      9. Time for action - iterating over lists
        1. What just happened?
      10. Time for action - computing a solution to the diffusion equation
        1. What just happened?
        2. Pop quiz - lists and for loops
        3. Have a go hero - adding another for loop
        4. List comprehensions
      11. Time for action - using a list comprehension
        1. What just happened?
      12. While loops and text file I/O
      13. Time for action - saving data in a text file
        1. What just happened?
      14. Time for action - reading data from a text file
        1. What just happened?
        2. While loops
        3. Parsing strings and extracting data
        4. Alternative approach to reading from a text file
        5. Have a go hero - define a function for reading the text file
        6. Have a go hero - replace a for loop with a while loop
      15. If statements and conditional expressions
      16. Storing data in a dictionary
      17. Time for action - defining and accessing dictionaries
        1. What just happened?
      18. Lambda forms
      19. Time for action - using lambda to create an anonymous function
        1. What just happened?
      20. Summary
    11. 5. Vectors, Matrices, and Linear Algebra
      1. Vectors and vector spaces
      2. Time for action - working with vectors
        1. What just happened?
        2. Creating a vector space
        3. Creating and manipulating vectors
      3. Time for action - manipulating elements of vectors
        1. What just happened?
        2. Vector operators and methods
      4. Matrices and matrix spaces
      5. Time for action - solving a system of linear equations
        1. What just happened?
        2. Creating matrices and matrix spaces
        3. Accessing and manipulating matrices
      6. Time for action - accessing elements and parts of a matrix
        1. What just happened?
        2. Manipulating matrices
      7. Time for action - manipulating matrices
        1. What just happened?
        2. Pop quiz - manipulating matrices
        3. Matrix algebra
      8. Time for action - matrix algebra
        1. What just happened?
        2. Other matrix methods
      9. Time for action - trying other matrix methods
        1. What just happened?
        2. Eigenvalues and eigenvectors
      10. Time for action - computing eigenvalues and eigenvectors
        1. What just happened?
        2. Have a go hero - verifying the eigenvalues and eigenvectors
        3. Decomposing matrices
      11. Time for action - computing the QR factorization
        1. What just happened?
      12. Time for action - computing the singular value decomposition
        1. What just happened?
      13. An introduction to NumPy
      14. Time for action - creating NumPy arrays
        1. What just happened?
        2. Creating NumPy arrays
        3. NumPy types
        4. Indexing and selection with NumPy arrays
      15. Time for action - working with NumPy arrays
        1. What just happened?
        2. Have a go hero - replacing lists with NumPy arrays
        3. NumPy matrices
      16. Time for action - creating matrices in NumPy
        1. What just happened?
        2. Learning more about NumPy
      17. Summary
    12. 6. Plotting with Sage
      1. Confusion alert: Sage plots and matplotlib
      2. Plotting in two dimensions
        1. Plotting symbolic expressions with Sage
      3. Time for action - plotting symbolic expressions
        1. What just happened?
      4. Time for action - plotting a function with a pole
        1. What just happened?
      5. Time for action - plotting a parametric function
        1. What just happened?
      6. Time for action - making a polar plot
        1. What just happened?
      7. Time for action - plotting a vector field
        1. What just happened?
        2. Plotting data in Sage
      8. Time for action - making a scatter plot
        1. What just happened?
      9. Time for action - plotting a list
        1. What just happened?
        2. Using graphics primitives
      10. Time for action - plotting with graphics primitives
        1. What just happened?
      11. Using matplotlib
      12. Time for action - plotting functions with matplotlib
        1. What just happened?
        2. Using matplotlib to "tweak" a Sage plot
      13. Time for action - getting the matplotlib figure object
        1. What just happened?
      14. Time for action - improving polar plots
        1. What just happened?
        2. Plotting data with matplotlib
      15. Time for action - making a bar chart
        1. What just happened?
      16. Time for action - making a pie chart
        1. What just happened?
      17. Time for action - plotting a histogram
        1. What just happened?
      18. Plotting in three dimensions
      19. Time for action - make an interactive 3D plot
        1. What just happened?
        2. Higher quality output
        3. Parametric 3D plotting
      20. Time for action - parametric plots in 3D
        1. What just happened?
        2. Contour plots
      21. Time for action - making some contour plots
        1. What just happened?
      22. Summary
    13. 7. Making Symbolic Mathematics Easy
      1. Using the notebook interface
      2. Defining symbolic expressions
      3. Time for action - defining callable symbolic expressions
        1. What just happened?
        2. Relational expressions
      4. Time for action - defining relational expressions
        1. What just happened?
      5. Time for action - relational expressions with assumptions
        1. What just happened?
      6. Manipulating expressions
      7. Time for action - manipulating expressions
        1. What just happened?
        2. Manipulating rational functions
      8. Time for action - working with rational functions
        1. What just happened?
        2. Substitutions
      9. Time for action - substituting symbols in expressions
        1. What just happened?
        2. Expanding and factoring polynomials
      10. Time for action - expanding and factoring polynomials
        1. What just happened?
        2. Manipulating trigonometric expressions
      11. Time for action - manipulating trigonometric expressions
        1. What just happened?
        2. Logarithms, rational functions, and radicals
      12. Time for action - simplifying expressions
        1. What just happened?
      13. Solving equations and finding roots
      14. Time for action - solving equations
        1. What just happened?
        2. Finding roots
      15. Time for action - finding roots
        1. What just happened?
      16. Differential and integral calculus
      17. Time for action - calculating limits
        1. What just happened?
        2. Derivatives
      18. Time for action - calculating derivatives
        1. What just happened?
        2. Integrals
      19. Time for action - calculating integrals
        1. What just happened?
      20. Series and summations
      21. Time for action - computing sums of series
        1. What just happened?
        2. Taylor series
      22. Time for action - finding Taylor series
        1. What just happened?
        2. Have a go hero - Taylor series
      23. Laplace transforms
      24. Time for action - computing Laplace transforms
        1. What just happened?
      25. Solving ordinary differential equations
      26. Time for action - solving an ordinary differential equation
        1. What just happened?
      27. Summary
    14. 8. Solving Problems Numerically
      1. Sage and NumPy
      2. Solving equations and finding roots numerically
      3. Time for action - finding roots of a polynomial
        1. What just happened?
      4. Finding minima and maxima of functions
      5. Time for action - minimizing a function of one variable
        1. What just happened?
        2. Functions of more than one variable
      6. Time for action - minimizing a function of several variables
        1. What just happened?
      7. Numerical approximation of derivatives
      8. Time for action - approximating derivatives with differences
        1. What just happened?
        2. Computing gradients
      9. Time for action - computing gradients
        1. What just happened?
      10. Numerical integration
      11. Time for action - numerical integration
        1. What just happened?
        2. Numerical integration with NumPy
      12. Time for action - numerical integration with NumPy
        1. What just happened?
      13. Discrete Fourier transforms
      14. Time for action - computing discrete Fourier transforms
        1. What just happened?
        2. Window functions
      15. Time for action - plotting window functions
        1. What just happened?
        2. Have a go hero - using window functions
      16. Solving ordinary differential equations
      17. Time for action - solving a first-order ODE
        1. What just happened?
        2. Solving a system of ODEs
      18. Time for action - solving a higher-order ODE
        1. What just happened?
        2. Solving the system using the GNU Scientific Library
      19. Time for action - alternative method of solving a system of ODEs
        1. What just happened?
      20. Numerical optimization
      21. Time for action - linear programming
        1. What just happened?
        2. Fitting a function to a noisy data set
      22. Time for action - least squares fitting
        1. What just happened?
        2. Constrained optimization
      23. Time for action - a constrained optimization problem
        1. What just happened?
      24. Probability
      25. Time for action - accessing probability distribution functions
        1. What just happened?
      26. Summary
    15. 9. Learning Advanced Python Programming
      1. How to write good software
      2. Object-oriented programming
      3. Time for action - defining a class that represents a tank
        1. What just happened?
        2. Making our tanks move
      4. Time for action - making the tanks move
        1. What just happened?
        2. Have a go hero - checking the values passed to __init__
        3. Creating a module for our classes
      5. Time for action - creating your first module
        1. What just happened?
        2. Expanding our simulation to other kinds of vehicles
      6. Time for action - creating a vehicle base class
        1. What just happened?
        2. Creating a package for our simulation
      7. Time for action - creating a combat simulation package
        1. What just happened?
        2. Have a go hero - adding another derived class
      8. Potential pitfalls when working with classes and instances
      9. Time for action - using class and instance attributes
        1. What just happened?
      10. Time for action - more about class and instance attributes
        1. What just happened?
        2. Creating empty classes and functions
      11. Time for action - creating empty classes and functions
        1. What just happened?
      12. Handling errors gracefully
      13. Time for action - raising and handling exceptions
        1. What just happened?
        2. Exception types
        3. Have a go hero - raising exceptions in the __init__ method of Tank
        4. Creating your own exception types
      14. Time for action - creating custom exception types
        1. What just happened?
      15. Unit testing
      16. Time for action - creating unit tests for the Tank class
        1. What just happened?
        2. Strategies for unit testing
        3. Have a go hero - creating some unit tests
      17. Summary
    16. 10. Where to go from here
      1. Typesetting equations with LaTeX
        1. Installing LaTeX
      2. Time for action - PDF output from the notebook interface
        1. What just happened?
        2. The view function in the interactive shell
        3. LaTeX mark-up in the notebook interface
      3. Time for action - working with LaTeX markup in the notebook interface
        1. What just happened?
      4. Time for action - putting it all together
        1. What just happened?
        2. Have a go hero - Bessel functions of the second kind
      5. Speeding up execution
      6. Time for action - detecting collisions between spheres
        1. What just happened?
      7. Time for action - detecting collisions: command-line version
        1. What just happened?
        2. Tips for measuring runtimes
        3. Optimizing our algorithm
      8. Time for action - faster collision detection
        1. What just happened?
        2. Optimizing with NumPy
      9. Time for action - using NumPy
        1. What just happened?
        2. More about NumPy
        3. Optimizing with Cython
      10. Time for action - optimizing collision detection with Cython
        1. What just happened?
        2. Have a go hero - further optimization with Cython
      11. Calling Sage from Python
      12. Time for action - calling Sage from a Python script
        1. What just happened?
        2. Have a go hero - solving an ODE symbolically from Python
      13. Introducing Python decorators
      14. Time for action - introducing the Python decorator
        1. What just happened?
        2. Pop quiz - understanding function decorators
        3. Have a go hero - improving the decorator
        4. Have a go hero - the memoize decorator
      15. Making interactive graphics
      16. Time for action - making interactive controls
        1. What just happened?
        2. Using interactive controls
      17. Time for action - an interactive example
        1. What just happened?
        2. Have a go hero - Taylor series
      18. Summary