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

Getting Started with Sage
Notice that the expression QQ(4/3) returns an exact rational number, but RR(4/3) returns a floating-point approximation. A useful trick is to use I to define a complex number, and then use CC to force the result to have a complex number type rather than a symbolic expression type.
Combiningtypesinexpressions
It often happens that integers, rational numbers, real numbers, and complex numbers need to be combined in a mathematical expression. Most of the time, you don't need to worry about this because Sage will automatically choose the best type for the result of the calculation, so that no information will be lost. For example, adding an integer to a real number results in a real number, to avoid losing the non-integer part of the result.
Popquiz–understandingtypes
What type will result from the following Sage commands? Check your answers with the type function in Sage.
3/2
2/3.0
sin(pi/3) sqrt(-1) sqrt(-1.0) CC(7 + 3 * i)
Strings
Strings are another fundamental type in Python and Sage. We will use strings extensively, in conjunction with the print function, to display results from our calculations. We will also use strings to document functions that we define.
Timeforaction–usingstrings
Let's practice with strings:
string_1 = 'Single quoted string'
string_2 = "Sometimes it's good to use double quotes"
multiline_string = """ This string
contains single quotes ' and double quotes " and spans multiple lines"""
print(string_1) print(string_2) print(multiline_string)
[ 62 ]

Chapter 3
numerical_value = 1.616233
print('The value is ' + str(numerical_value))
The result should look like this:
What just happened?
A string literal is an arbitrary sequence of characters enclosed in quotation marks, such as 'Single quoted string' in the example above. String literals can be assigned to variables, like any other type. Single or double quotes can be used. If you need to use a single quote within the string, you need to enclose the string in double quotes, as we did with the string literal assigned to the variable string_2. Enclosing a string in triple quotes (either single or double) allows you to include newlines and quotation marks in the string. We used triple quotes to assign a string value to the variable multiline_string.
The last two lines of the example show how we can use strings to improve the output from our calculations. The str function returns a string representation of its argument, which is a real number in this example. Every object in Sage, including functions, has a string representation, although it's not necessarily useful. We then used the + operator to concatenate (join) the two strings. This operator performs addition if used with numerical types, and concatenation if used with strings. This is known as operator overloading. We'll
use print, str, and the + operator extensively to improve the output from our calculations.
Callablesymbolicexpressions
The definition of the word "function" is a potential source of confusion in Sage because there are two types of constructs that are commonly referred to as functions. Mathematicians define a function as a relation that associates each element of a given set (called the domain) with an element of another set (the range). In computer programming, a function is a block of code within a larger program that performs a specific task. Sage supports both types of functions. In order to avoid confusion, we will use the term "callable symbolic expression" to refer to a function in the mathematical sense. The word "function" will refer to a function definition using the Python programming language, which we will learn about in the next section.
[ 63 ]

Getting Started with Sage
Timeforaction–definingcallablesymbolicexpressions
Let's say we want to define this mathematical function and perform some calculations with it:
Evaluate the following code to define the function:
var('a, x') f(x) = a * x^3
print(type(f))
print(f)
show(f)
print(f(2, a=5)) print type(f(2, a=5))
The result should look like this:
Now, let's define another function, which is the derivative of f(x):
Evaluate the following code to define g(x):
g(x) = derivative(f, x) show(g)
g(x=2, a=3)
[ 64 ]

Chapter 3
The result should look like this:
What just happened?
We started out using the var function to define some symbolic variables. Technically, we didn't need to explicitly define x as a symbolic variable, because Sage assumes that x is symbolic by default. We then used the notation f(x) = a * x^3 to define a callable symbolic expression called f, and we confirmed that f was symbolic by using the type function. We used the print function to display f, and then introduced a new function called show to display a typeset representation of f. Finally, we called f with specific values for x and a. When we evaluate f, the result always has a symbolic type, even when the result is a numerical value.
In the next section, we created a new callable symbolic expression called g that is defined as the derivative of f with respect to x. derivative is a Sage function for computing symbolic derivatives, which we'll cover in Chapter 7. We then computed the value of g for specific numerical values of a and x to verify that g is also a callable symbolic expression.
Automaticallytypesettingexpressions
Near the top of every worksheet is a check box with the label Typeset. When it's not checked, symbolic expressions are displayed on a single line:
When the box is checked, expressions are typeset:
The Typeset check box has no effect on the print or show functions; print always displays an expression as text, and show always typesets expressions.
[ 65 ]