ebook img

Sage Beginner's Guide PDF

358 Pages·2011·24.292 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Sage Beginner's Guide

Sage Beginner's Guide Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: May 2011 Production Reference: 1250411 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849514-46-0 www.packtpub.com Cover Image by Ed Maclean ([email protected]) About the Author Craig Finch is a Ph. D. Candidate in the Modeling and Simulation program at the University of Central Florida (UCF). He earned a Bachelor of Science degree from the University of Illinois at Urbana-Champaign and a Master of Science degree from UCF, both in electrical engineering. Craig worked as a design engineer for TriQuint Semiconductor, and currently works as a research assistant in the Hybrid Systems Lab at the UCF NanoScience Technology Center. Craig's professional goal is to develop tools for computational science and engineering and use them to solve difficult problems. In particular, he is interested in developing tools to help biologists study living systems. Craig is committed to using, developing, and promoting open-source software. He provides documentation and "how-to" examples on his blog at http://www.shocksolution.com. I would like to thank my advisers, Dr. J. Hickman and Dr. Tom Clarke, for giving me the opportunity to pursue my doctorate. I would also like to thank my parents for buying the Apple IIGS computer that started it all. About the Reviewers Dr. David Kirkby is a chartered engineer living in Essex, England. David has a B.Sc. in Electrical and Electronic Engineering, an M.Sc. in Microwaves and OptoElectronics, and a Ph.D. in Medical Physics. Despite David's Ph.D. being in Medical Physics, it was primarily an engineering project, measuring the optical properties of human tissue, with a mixture of Monte Carlo modeling, radio frequency design, and laser optics. David was awarded his Ph.D. in 1999 from University College London. Although not a mathematician, Dr. Kirkby has made extensive use of mathematical software. Most of his experience has been with MathematicaTM from Wolfram Research, although he has used both MATLABTM and SimulinkTM too. David is the author of a number of open-source projects, including software for modeling transmission lines using finite difference (http://atlc.sourceforge.net/), design of Yagi-Uda antennas (http://www.g8wrb.org/yagi/) which can use a genetic algorithm for optimization, as well as software for data collection and analysis from electronic test equipment. David once wrote a web-based interface to MathematicaTM (http://witm. sourceforge.net/) which allows MathematicaTM to be used from a personal computer, PDA or smartphone. Soon after the Sage project was started by Professor William Stein, Dr. Kirkby joined the development of Sage. He primarily worked on the successful port of Sage to the Solaris and OpenSolaris operating systems and encourages other developers to write portable code, conforming to POSIX standard, avoiding GNUisms. Professionally, David's skill sets include computer modeling, radio frequency design, analogue circuit design, electromagnetic compatibility and optics—both free space and integrated. David has also been a Solaris system administrator for the University of Washington where the Sage project is based. When not working on writing software, David enjoys playing chess, gardening, and spending time with his wife Lin and dog Smudge. Readers wishing to contact Dr. Kirkby can do so via his website http://www.drkirkby. co.uk/ where details of his consulting services may be found. Minh Nguyen has been a contributor to the Sage project since December 2007. Over the years, he has worked on various aspects of Sage ranging from the standard documentation and modules such as cryptography, number theory, and graph theory to the Sage build system. He regularly maintains the Sage website and works on book projects that aim to provide in-depth documentation on using Sage to study cryptography and mathematics. More of his rantings can be found at http://mvngu.wordpress.com. Table of Contents 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 ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.