PacktLib: Sage Beginner's Guide

Sage Beginner's Guide

Credits

About the Author

About the Reviewers

www.PacktPub.com

Preface

What Can You Do with Sage?

Getting started

Using Sage as a powerful calculator

More advanced graphics

A practical example: analysing experimental data

Time for action – fitting the standard curve

Time for action – plotting experimental data

Time for action – fitting a growth model

Summary

Installing Sage

Before you begin

Installing a binary version of Sage on Windows

Installing a binary version of Sage on OS X

Installing a binary version of Sage on GNU/Linux

Building Sage from source

Summary

Getting Started with Sage

How to get help with Sage

Starting Sage from the command line

Using the interactive shell

Time for action – doing calculations on the command line

Using the notebook interface

Time for action – doing calculations with the notebook interface

Displaying results of calculations

Operators and variables

Time for action – using strings

Callable symbolic expressions

Time for action – defining callable symbolic expressions

Functions

Time for action – calling functions

Time for action – defining and using your own functions

Time for action – defining a function with keyword arguments

Objects

Time for action – working with objects

Summary

Introducing Python and Sage

Python 2 and Python 3

Writing code for Sage

Sequence types: lists, tuples, and strings

Time for action – creating lists

Time for action – accessing items in a list

Time for action – returning multiple values from a function

Time for action – working with strings

For loops

Time for action – iterating over lists

Time for action – computing a solution to the diffusion equation

Time for action – using a list comprehension

While loops and text file I/O

Time for action – saving data in a text file

Time for action – reading data from a text file

If statements and conditional expressions

Storing data in a dictionary

Time for action – defining and accessing dictionaries

Lambda forms

Time for action – using lambda to create an anonymous function

Summary

Vectors, Matrices, and Linear Algebra

Vectors and vector spaces

Time for action – working with vectors

Time for action – manipulating elements of vectors

Matrices and matrix spaces

Time for action – solving a system of linear equations

Time for action – accessing elements and parts of a matrix

Time for action – manipulating matrices

Time for action – matrix algebra

Time for action – trying other matrix methods

Time for action – computing eigenvalues and eigenvectors

Time for action – computing the QR factorization

Time for action – computing the singular value decomposition

An introduction to NumPy

Time for action – creating NumPy arrays

Time for action – working with NumPy arrays

Time for action – creating matrices in NumPy

Summary

Plotting with Sage

Confusion alert: Sage plots and matplotlib

Plotting in two dimensions

Time for action – plotting symbolic expressions

Time for action – plotting a function with a pole

Time for action – plotting a parametric function

Time for action – making a polar plot

Time for action – plotting a vector field

Time for action – making a scatter plot

Time for action – plotting a list

Time for action – plotting with graphics primitives

Using matplotlib

Time for action – plotting functions with matplotlib

Time for action – getting the matplotlib figure object

Time for action – improving polar plots

Time for action – making a bar chart

Time for action – making a pie chart

Time for action – plotting a histogram

Plotting in three dimensions

Time for action – make an interactive 3D plot

Time for action – parametric plots in 3D

Time for action – making some contour plots

Summary

Making Symbolic Mathematics Easy

Using the notebook interface

Defining symbolic expressions

Time for action – defining callable symbolic expressions

Time for action – defining relational expressions

Time for action – relational expressions with assumptions

Manipulating expressions

Time for action – manipulating expressions

Time for action – working with rational functions

Time for action – substituting symbols in expressions

Time for action – expanding and factoring polynomials

Time for action – manipulating trigonometric expressions

Time for action – simplifying expressions

Solving equations and finding roots

Time for action – solving equations

Time for action – finding roots

Differential and integral calculus

Time for action – calculating limits

Time for action – calculating derivatives

Time for action – calculating integrals

Series and summations

Time for action – computing sums of series

Time for action – finding Taylor series

Laplace transforms

Time for action – computing Laplace transforms

Solving ordinary differential equations

Time for action – solving an ordinary differential equation

Summary

Solving Problems Numerically

Sage and NumPy

Solving equations and finding roots numerically

Time for action – finding roots of a polynomial

Finding minima and maxima of functions

Time for action – minimizing a function of one variable

Time for action – minimizing a function of several variables

Numerical approximation of derivatives

Time for action – approximating derivatives with differences

Time for action – computing gradients

Numerical integration

Time for action – numerical integration

Time for action – numerical integration with NumPy

Discrete Fourier transforms

Time for action – computing discrete Fourier transforms

Time for action – plotting window functions

Solving ordinary differential equations

Time for action – solving a first-order ODE

Time for action – solving a higher-order ODE

Time for action – alternative method of solving a system of ODEs

Numerical optimization

Time for action – linear programming

Time for action – least squares fitting

Time for action – a constrained optimization problem

Probability

Time for action – accessing probability distribution functions

Summary

Learning Advanced Python Programming

How to write good software

Object-oriented programming

Time for action – defining a class that represents a tank

Time for action – making the tanks move

Time for action – creating your first module

Time for action – creating a vehicle base class

Time for action – creating a combat simulation package

Potential pitfalls when working with classes and instances

Time for action – using class and instance attributes

Time for action – more about class and instance attributes

Time for action – creating empty classes and functions

Handling errors gracefully

Time for action – raising and handling exceptions

Time for action – creating custom exception types

Unit testing

Time for action – creating unit tests for the Tank class

Summary

Where to go from here

Typesetting equations with LaTeX

Time for action – PDF output from the notebook interface

Time for action – working with LaTeX markup in the notebook interface

Time for action – putting it all together

Speeding up execution

Time for action – detecting collisions between spheres

Time for action – detecting collisions: command-line version

Time for action – faster collision detection

Time for action – using NumPy

Time for action – optimizing collision detection with Cython

Calling Sage from Python

Time for action – calling Sage from a Python script

Introducing Python decorators

Time for action – introducing the Python decorator

Making interactive graphics

Time for action – making interactive controls

Using interactive controls

Summary

Index