- •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
Where to go from here
Play with the sliders to adjust two of the lines that constrain the minimization problem. Notice that the coordinates of the minimum are printed every time you move a slider, and the plots change to reflect the new constraints and the location of the minimum.
What just happened?
We started with an example from Chapter 8, and made it interactive. We defined a function with a single underscore as its name, and used the decorator syntax to decorate the function with interact. We created four slider controls, using the syntax described in the previous example. We then pasted the code from Chapter 8 into the function body and made a few changes. The linear constraints are represented as a matrix G and a vector h. We changed four of the entries in the matrix to be variables rather than hard-coded values. The slider controls set the values for these variables. When you move a slider, the constraints change, the minimum is recalculated, and the plots are redrawn. Note that this example is a good choice for user interaction because the linear program can be solved quickly; if the code took minutes or hours to run, there would be no point in making it interactive!
The complete documentation of the interact module can be found at:
http://sagemath.org/doc/reference/sagenb/notebook/interact.html
Many interactive examples, along with source code, can be found at:
http://wiki.sagemath.org/interact
Haveagohero–Taylorseries
In Chapter 7, we used an example to demonstrate a Taylor series converges to a function in a region as the number of terms in the series increases. Convert this example so that it is interactive. Allow the user to change the number of terms in the series, and update the graph to show how the series converges to the function.
Summary
This chapter provided you with advanced tools that will help you get the most out of Sage.
We learned about:
Exporting mathematical expressions in PDF files and PNG bitmaps
Generating LaTeX mark-up that describes a mathematical expression
Incorporating LaTeX mark-up into a text cell in a workbook
Processing LaTeX mark-up in a workbook
Using NumPy to improve the execution speed of your code
[ 330 ]
Chapter 10
Using Sage from a stand-alone Python script
Creating interactive graphical examples in the notebook interface
I hope you have found Sage to be a useful tool that takes much of the pain out of mathematics. This book has only scratched the surface of its capabilities, especially if you are interested in advanced mathematics. Refer to the online documentation for Sage and Python to learn more. The Sage worksheets published at http://www.sagenb.org/pub/ can also be an excellent resource.
[ 331 ]