
- •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

Installing Sage
Start Sage
Once the virtual machine is running, you will see three icons. Double-clicking the Sage Notebook icon starts the Sage notebook interface, while the Sage icon starts the commandline interface. The first time you run Sage, you will have to wait while it regenerates files. When it finishes, you are ready to go.
You may get the warning "External network not set up" when launching the notebook interface. This does not cause any problems.
[ 32 ]

Chapter 2
When you are done using Sage, choose Shut Down… from the System menu at the top of the window, and a dialog will appear. Click the Shut Down button to close the virtual machine.
InstallingabinaryversionofSageonOSX
On Mac OS X, you have the option of installing a pre-built binary application, or downloading the source code and compiling Sage yourself. One advantage of the pre-built binary is
that it is very easy to install, because it contains everything you need to run Sage. Another advantage of the binary is that building Sage from source requires a lot of computational resources, and may take a long time on older machines. However, there are a number of disadvantages to prebuilt binaries. The binary download is quite large, and the installed files take up a lot of disk space. Many of the tools in the binary may be duplicates of tools you already have on your system. Pre-built binaries cannot be tuned to take advantage of the hardware features of a particular platform, so building Sage from source is preferred if you are looking for the best performance on CPU-intensive tasks. You will have to choose which method is right for you.
[ 33 ]

Installing Sage
DownloadingSage
Download Sage by following the Download link from http://www.sagemath.org. The site should automatically detect that you are using OS X, and direct you to the right download page. Choose a mirror site close to you. Select your architecture (Intel for new Macs, or PowerPC for older G4 and G5 macs). Then, click the link for the correct .dmg file for you version of Mac OS X. If you aren't sure, click the Apple menu on the far left side of the menu bar and choose About This Mac.
InstallingSage
Once the download is complete, double-click the .dmg file to mount the disk image. Drag the Sage folder from the disk image to the desired location on your hard drive (such as the Apps folder).
If the copy procedure fails, you will need to do it from the command line. Open the Terminal application and enter the following commands. Be sure to change the name sage-4.5- OSX-64bit-10.6-i386-Darwin.dmg to the name of the file you just downloaded:
$ cd /Applications
$ cp -R -P /Volumes/sage-4.5-OSX-64bit-10.6-i386-Darwin.dmg /sage .
After the copy process is complete, right-click on the icon for the disk image, and choose
Eject.
Starting Sage
Use the Finder to visit the Sage folder that you just created. Double-click on the icon called Sage. It should open with the Terminal application. If it doesn't start, right-click on the icon, go to the Open With submenu and choose Terminal.app. The Sage command line will now be running in a Terminal window. The first time you run Sage, you will have to wait while it regenerates files. When it finishes, you are ready to go.
[ 34 ]

Chapter 2
There are three ways to exit Sage: type exit or quit at the Sage command prompt, or press Ctrl-D in the Terminal window. You can then quit the Terminal application.
InstallingabinaryversionofSageonGNU/Linux
As with Mac OS X, you have the option of installing a pre-built binary application for your version of Linux, or downloading the source code and compiling Sage yourself. The same trade-offs apply to Linux. Keep in mind that the Sage team only distributes pre-build binaries for a few popular distributions. If you are using a different distribution, you'll have to compile Sage from source anyway. The following instructions will assume you are downloading a binary application. I will use Ubuntu as an example, but other versions of Linux should be very similar.
Most modern Linux distributions use a package manager to install and remove software. Sage is not available as an officially supported package for any Linux distribution at this time. "Unofficial" packages have been created for Debian, Mandriva, Ubuntu, and possibly others, but they are unlikely to be up to date and may not work properly. An effort to integrate Sage with Gentoo Linux can be found at https://github.com/cschwan/sage-on-gentoo.
DownloadinganddecompressingSage
Download the appropriate pre-built binary from http://www.sagemath.org/downloadlinux.html. Choose the closest mirror, and then choose the appropriate architecture for your operating system. If you're not sure whether your operating system is built for 32 or 64 bit operation, open a terminal and type the following on the command line:
$ uname –m
If the output contains 64, then your system is probably a 64-bit system. If not, it's a 32-bit.
An alternative way to check is with the following command:
$ file /usr/bin/bin
If the file type contains 64, your kernel probably supports 64 bit applications. If not, you need the 32 bit version. Select the appropriate prebuilt binary and save it to your computer.
[ 35 ]

Installing Sage
Once the download is done, uncompress the archive. You can use the graphical archiving tool for your version of Linux (the Ubuntu archiver is shown in the following screenshot). If you prefer the command line, type the following:
tar --lzma -xvf sage-*...tar.lzma
RunningSagefromyouruseraccount
After decompression, you will have a single directory. This directory is self-contained, so no further installation is necessary. You can simply move it to a convenient location within your home directory. This is a good option if you don't have administrator privileges on the system, or if you are the only person who uses the system. To run Sage, open a terminal and
change to the Sage directory (you will have to modify the command below, depending on the version you installed and where you installed it):
cfinch@ubuntu:$ cd sage-4.5.3-linux-32bit-ubuntu_10.04_lts-i686-Linux
Run Sage by typing the following:
cfinch@ubuntu:$ ./sage
Don't forget the period before the slash! The first time you run Sage, you will have to wait while it regenerates files, as shown in the following screenshot. When it finishes, you are ready to go.
[ 36 ]

Chapter 2
There are three ways to exit Sage: type exit or quit at the Sage command prompt, or press Ctrl-D in the terminal window.
Installingformultipleusers
If you are the administrator of a shared system, you may want to install Sage so that everyone can use it. Since Sage consists of one self-contained directory, I suggest moving it to the /opt directory:
sudo mv sage-4.5.3-linux-32bit-ubuntu_10.04_lts-i686-Linux /opt [sudo] password for cfinch:
To make it easy for everyone to run Sage, make a symbolic link from /usr/bin to the actual location:
cfinch@ubuntu:/usr/bin$ sudo ln -s /opt/sage-4.5.3-linux-32bit- ubuntu_10.04_lts-i686-Linux/sage sage
[sudo] password for cfinch:
As before, Sage will have to regenerate its internal files the first time it runs after moving. You should run Sage once as a user with administrative privileges, because other users won't have the necessary write permissions to save the files. Once this is completed, any user will be able to use Sage by typing Sage at the command prompt.
[ 37 ]