- •Credits
- •About the Author
- •About the Reviewers
- •www.PacktPub.com
- •Preface
- •Getting started
- •More advanced graphics
- •Summary
- •Start Sage
- •Installing Sage
- •Starting Sage
- •Prerequisites
- •Installation
- •Summary
- •Command history
- •Working with code
- •Arithmetic operators
- •Strings
- •Functions
- •Functions with keyword arguments
- •Objects
- •Summary
- •Python 2 and Python 3
- •Running scripts
- •Strings
- •List comprehensions
- •Storing data in a dictionary
- •Summary
- •Vectors and vector spaces
- •Creating a vector space
- •Vector operators and methods
- •Decomposing matrices
- •Summary
- •Using graphics primitives
- •Summary
- •Substitutions
- •Finding roots
- •Derivatives
- •Integrals
- •Series and summations
- •Summary
- •Computing gradients
- •Constrained optimization
- •Probability
- •Summary
- •Making our tanks move
- •Unit testing
- •Summary
- •Introducing Python decorators
- •Making interactive graphics
- •Summary
- •Index
www.PacktPub.com
Supportfiles,eBooks,discountoffersandmore
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com,youcanalsoreadacollectionoffreetechnicalarticles,signupfora rangeoffreenewslettersandreceiveexclusivediscountsandoffersonPacktbooksandeBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy & paste, print and bookmark content
On demand and accessible via web browser
FreeAccessforPacktaccountholders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
TableofContents
Preface |
1 |
Chapter 1: What Can You Do with Sage? |
9 |
Getting started |
9 |
Using Sage as a powerful calculator |
12 |
Symbolic mathematics |
14 |
Linear algebra |
17 |
Solving an ordinary differential equation |
18 |
More advanced graphics |
19 |
Visualising a three-dimensional surface |
20 |
Typesetting mathematical expressions |
21 |
A practical example: analysing experimental data |
22 |
Time for action – fitting the standard curve |
22 |
Time for action – plotting experimental data |
24 |
Time for action – fitting a growth model |
25 |
Summary |
26 |
Chapter 2: Installing Sage |
29 |
Before you begin |
29 |
Installing a binary version of Sage on Windows |
30 |
Downloading VMware Player |
30 |
Installing VMWare Player |
30 |
Downloading and extracting Sage |
30 |
Launching the virtual machine |
31 |
Start Sage |
32 |
Installing a binary version of Sage on OS X |
33 |
Downloading Sage |
34 |
Installing Sage |
34 |
Starting Sage |
34 |
Table of Contents
Installing a binary version of Sage on GNU/Linux |
35 |
Downloading and decompressing Sage |
35 |
Running Sage from your user account |
36 |
Installing for multiple users |
37 |
Building Sage from source |
38 |
Prerequisites |
38 |
Downloading and decompressing source tarball |
39 |
Building Sage |
39 |
Installation |
39 |
Summary |
39 |
Chapter 3: Getting Started with Sage |
41 |
How to get help with Sage |
41 |
Starting Sage from the command line |
42 |
Using the interactive shell |
43 |
Time for action – doing calculations on the command line |
43 |
Getting help |
45 |
Command history |
46 |
Tab completion |
47 |
Interactively tracing execution |
48 |
Using the notebook interface |
48 |
Starting the notebook interface |
49 |
Time for action – doing calculations with the notebook interface |
52 |
Getting help in the notebook interface |
54 |
Working with cells |
54 |
Working with code |
55 |
Closing the notebook interface |
55 |
Displaying results of calculations |
56 |
Operators and variables |
56 |
Arithmetic operators |
57 |
Numerical types |
58 |
Integers and rational numbers |
58 |
Real numbers |
59 |
Complex numbers |
60 |
Symbolic expressions |
60 |
Defining variables on rings |
61 |
Combining types in expressions |
62 |
Strings |
62 |
Time for action – using strings |
62 |
Callable symbolic expressions |
63 |
Time for action – defining callable symbolic expressions |
64 |
Automatically typesetting expressions |
65 |
[ ii ]
|
Table of Contents |
Functions |
66 |
Time for action – calling functions |
66 |
Built-in functions |
68 |
Numerical approximations |
68 |
The reset and restore functions |
69 |
Defining your own functions |
70 |
Time for action – defining and using your own functions |
70 |
Functions with keyword arguments |
72 |
Time for action – defining a function with keyword arguments |
72 |
Objects |
73 |
Time for action – working with objects |
74 |
Getting help with objects |
75 |
Summary |
77 |
Chapter 4: Introducing Python and Sage |
79 |
Python 2 and Python 3 |
79 |
Writing code for Sage |
80 |
Long lines of code |
81 |
Running scripts |
81 |
Sequence types: lists, tuples, and strings |
82 |
Time for action – creating lists |
82 |
Getting and setting items in lists |
85 |
Time for action – accessing items in a list |
85 |
List functions and methods |
87 |
Tuples: read-only lists |
87 |
Time for action – returning multiple values from a function |
87 |
Strings |
89 |
Time for action – working with strings |
90 |
Other sequence types |
92 |
For loops |
92 |
Time for action – iterating over lists |
92 |
Time for action – computing a solution to the diffusion equation |
94 |
List comprehensions |
99 |
Time for action – using a list comprehension |
99 |
While loops and text file I/O |
101 |
Time for action – saving data in a text file |
101 |
Time for action – reading data from a text file |
103 |
While loops |
105 |
Parsing strings and extracting data |
105 |
Alternative approach to reading from a text file |
106 |
If statements and conditional expressions |
107 |
Storing data in a dictionary |
108 |
[ iii ]
Table of Contents
Time for action – defining and accessing dictionaries |
108 |
Lambda forms |
110 |
Time for action – using lambda to create an anonymous function |
110 |
Summary |
111 |
Chapter 5: Vectors, Matrices, and Linear Algebra |
113 |
Vectors and vector spaces |
113 |
Time for action – working with vectors |
114 |
Creating a vector space |
115 |
Creating and manipulating vectors |
116 |
Time for action – manipulating elements of vectors |
116 |
Vector operators and methods |
117 |
Matrices and matrix spaces |
118 |
Time for action – solving a system of linear equations |
118 |
Creating matrices and matrix spaces |
120 |
Accessing and manipulating matrices |
120 |
Time for action – accessing elements and parts of a matrix |
120 |
Manipulating matrices |
122 |
Time for action – manipulating matrices |
122 |
Matrix algebra |
124 |
Time for action – matrix algebra |
124 |
Other matrix methods |
125 |
Time for action – trying other matrix methods |
126 |
Eigenvalues and eigenvectors |
127 |
Time for action – computing eigenvalues and eigenvectors |
127 |
Decomposing matrices |
129 |
Time for action – computing the QR factorization |
129 |
Time for action – computing the singular value decomposition |
131 |
An introduction to NumPy |
133 |
Time for action – creating NumPy arrays |
133 |
Creating NumPy arrays |
134 |
NumPy types |
135 |
Indexing and selection with NumPy arrays |
136 |
Time for action – working with NumPy arrays |
136 |
NumPy matrices |
137 |
Time for action – creating matrices in NumPy |
137 |
Learning more about NumPy |
139 |
Summary |
139 |
Chapter 6: Plotting with Sage |
141 |
Confusion alert: Sage plots and matplotlib |
141 |
Plotting in two dimensions |
141 |
[ iv ]
|
Table of Contents |
Plotting symbolic expressions with Sage |
142 |
Time for action – plotting symbolic expressions |
142 |
Time for action – plotting a function with a pole |
144 |
Time for action – plotting a parametric function |
146 |
Time for action – making a polar plot |
147 |
Time for action – plotting a vector field |
149 |
Plotting data in Sage |
150 |
Time for action – making a scatter plot |
150 |
Time for action – plotting a list |
151 |
Using graphics primitives |
153 |
Time for action – plotting with graphics primitives |
153 |
Using matplotlib |
155 |
Time for action – plotting functions with matplotlib |
155 |
Using matplotlib to "tweak" a Sage plot |
157 |
Time for action – getting the matplotlib figure object |
158 |
Time for action – improving polar plots |
159 |
Plotting data with matplotlib |
161 |
Time for action – making a bar chart |
161 |
Time for action – making a pie chart |
163 |
Time for action – plotting a histogram |
164 |
Plotting in three dimensions |
165 |
Time for action – make an interactive 3D plot |
166 |
Higher quality output |
167 |
Parametric 3D plotting |
168 |
Time for action – parametric plots in 3D |
168 |
Contour plots |
169 |
Time for action – making some contour plots |
169 |
Summary |
171 |
Chapter 7: Making Symbolic Mathematics Easy |
173 |
Using the notebook interface |
174 |
Defining symbolic expressions |
174 |
Time for action – defining callable symbolic expressions |
174 |
Relational expressions |
176 |
Time for action – defining relational expressions |
176 |
Time for action – relational expressions with assumptions |
178 |
Manipulating expressions |
179 |
Time for action – manipulating expressions |
179 |
Manipulating rational functions |
180 |
Time for action – working with rational functions |
181 |
Substitutions |
182 |
[ v ]
Table of Contents
Time for action – substituting symbols in expressions |
182 |
Expanding and factoring polynomials |
184 |
Time for action – expanding and factoring polynomials |
184 |
Manipulating trigonometric expressions |
186 |
Time for action – manipulating trigonometric expressions |
186 |
Logarithms, rational functions, and radicals |
187 |
Time for action – simplifying expressions |
187 |
Solving equations and finding roots |
190 |
Time for action – solving equations |
190 |
Finding roots |
191 |
Time for action – finding roots |
191 |
Differential and integral calculus |
193 |
Time for action – calculating limits |
193 |
Derivatives |
195 |
Time for action – calculating derivatives |
195 |
Integrals |
198 |
Time for action – calculating integrals |
198 |
Series and summations |
199 |
Time for action – computing sums of series |
199 |
Taylor series |
200 |
Time for action – finding Taylor series |
200 |
Laplace transforms |
202 |
Time for action – computing Laplace transforms |
202 |
Solving ordinary differential equations |
204 |
Time for action – solving an ordinary differential equation |
204 |
Summary |
206 |
Chapter 8: Solving Problems Numerically |
207 |
Sage and NumPy |
208 |
Solving equations and finding roots numerically |
208 |
Time for action – finding roots of a polynomial |
208 |
Finding minima and maxima of functions |
210 |
Time for action – minimizing a function of one variable |
210 |
Functions of more than one variable |
211 |
Time for action – minimizing a function of several variables |
211 |
Numerical approximation of derivatives |
213 |
Time for action – approximating derivatives with differences |
213 |
Computing gradients |
215 |
Time for action – computing gradients |
215 |
Numerical integration |
217 |
Time for action – numerical integration |
217 |
[ vi ]
|
Table of Contents |
Numerical integration with NumPy |
219 |
Time for action – numerical integration with NumPy |
219 |
Discrete Fourier transforms |
220 |
Time for action – computing discrete Fourier transforms |
220 |
Window functions |
223 |
Time for action – plotting window functions |
223 |
Solving ordinary differential equations |
224 |
Time for action – solving a first-order ODE |
224 |
Solving a system of ODEs |
226 |
Time for action – solving a higher-order ODE |
226 |
Solving the system using the GNU Scientific Library |
229 |
Time for action – alternative method of solving a system of ODEs |
229 |
Numerical optimization |
231 |
Time for action – linear programming |
231 |
Fitting a function to a noisy data set |
233 |
Time for action – least squares fitting |
233 |
Constrained optimization |
235 |
Time for action – a constrained optimization problem |
235 |
Probability |
236 |
Time for action – accessing probability distribution functions |
236 |
Summary |
238 |
Chapter 9: Learning Advanced Python Programming |
241 |
How to write good software |
242 |
Object-oriented programming |
243 |
Time for action – defining a class that represents a tank |
244 |
Making our tanks move |
249 |
Time for action – making the tanks move |
249 |
Creating a module for our classes |
253 |
Time for action – creating your first module |
253 |
Expanding our simulation to other kinds of vehicles |
258 |
Time for action – creating a vehicle base class |
258 |
Creating a package for our simulation |
263 |
Time for action – creating a combat simulation package |
263 |
Potential pitfalls when working with classes and instances |
268 |
Time for action – using class and instance attributes |
269 |
Time for action – more about class and instance attributes |
270 |
Creating empty classes and functions |
272 |
Time for action – creating empty classes and functions |
272 |
Handling errors gracefully |
273 |
Time for action – raising and handling exceptions |
274 |
[ vii ]
Table of Contents
Exception types |
278 |
Creating your own exception types |
279 |
Time for action – creating custom exception types |
279 |
Unit testing |
284 |
Time for action – creating unit tests for the Tank class |
284 |
Strategies for unit testing |
288 |
Summary |
289 |
Chapter 10: Where to go from here |
291 |
Typesetting equations with LaTeX |
291 |
Installing LaTeX |
292 |
Time for action – PDF output from the notebook interface |
293 |
The view function in the interactive shell |
297 |
LaTeX mark-up in the notebook interface |
297 |
Time for action – working with LaTeX markup in the notebook interface |
297 |
Time for action – putting it all together |
300 |
Speeding up execution |
304 |
Time for action – detecting collisions between spheres |
304 |
Time for action – detecting collisions: command-line version |
307 |
Tips for measuring runtimes |
309 |
Optimizing our algorithm |
309 |
Time for action – faster collision detection |
309 |
Optimizing with NumPy |
311 |
Time for action – using NumPy |
311 |
More about NumPy |
314 |
Optimizing with Cython |
314 |
Time for action – optimizing collision detection with Cython |
314 |
Calling Sage from Python |
316 |
Time for action – calling Sage from a Python script |
316 |
Introducing Python decorators |
319 |
Time for action – introducing the Python decorator |
319 |
Making interactive graphics |
322 |
Time for action – making interactive controls |
322 |
Using interactive controls |
328 |
Time for action – an interactive example |
328 |
Summary |
330 |
Index |
333 |
[ viii ]
