|
With some minor exceptions, Sage uses the Python programming language, so most introductory books on Python will help you to learn Sage.
Sage uses =
for assignment. It uses ==,<=,>=,<
and >
for comparison:
{{{id=1|
a = 5
///
}}}
{{{id=2|
a
///
5
}}}
{{{id=3|
2 == 3
///
False
}}}
{{{id=4|
2 < 3
///
True
}}}
{{{id=5|
a == 5
///
True
}}}
Sage provides all of the basic mathematical operations: {{{id=6| 2+4*6 /// 26 }}} {{{id=7| 2**6 # ** means exponent /// 64 }}} {{{id=8| 2^6 # ^ is a synonym for ** (unlike in Python) /// 64 }}} {{{id=9| 10 % 3 # for integer arguments, % means mod, i.e., remainder /// 1 }}} {{{id=10| 10/4 /// 5/2 }}} {{{id=11| 10//4 # for integer arguments, // returns the integer quotient /// 2 }}} {{{id=15| 4 * (10 // 4) + 10 % 4 == 10 /// True }}} {{{id=12| 2345^98 # arbitrary precision /// 1879312537156040227304800937029250945996419733488500849760174594685063601299599011178197702306691856869053751204298304807715200029567547302257341824069952997802594773365541370823811696392671323977639889323921481055923347588862647026214659502037417043419435219410766862699746022191346706887472850500131471562781371176242828369140625 }}} {{{id=17| 3^2*4 + 2%5 /// 38 }}}
The computation of an expression like 3^2*4 + 2%5
depends on
the order in which the operations are applied; this is specified in
the ``operator precedence table'' in Section A.1.
Sage also provides many familiar mathematical functions; here are just a few examples: {{{id=13| pi /// pi }}} {{{id=14| sin(pi/3) /// 1/2*sqrt(3) }}} {{{id=24| exp(1) /// e }}} {{{id=16| n(e) # n(-) returns a numerical approximation to its argument /// 2.71828182845905 }}} {{{id=25| n(pi,digits=200) /// 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820 }}}
Python is dynamically typed, so the value referred to
by each variable has a type associated with it, but a given variable
may hold values of any Python type within a given scope:
{{{id=18|
a = 5 # a is an integer
type(a)
///
Sage has extensive built-in documentation, accessible by typing the
name of a function or a constant (for example), followed by a question mark:
{{{id=36|
exp?
///
}}}
Auto Completion with TAB
{{{id=35|
di
///
}}}
{{{id=37|
diff?
///
}}}
Source code with ??
{{{id=38|
subsets??
///
}}}
The
You can solve equations for one variable in terms of others:
{{{id=28|
x, b, c = var('x b c')
solve(x^2 + b*x + c == 0, x) # quadratic equation
///
[x == -1/2*b - 1/2*sqrt(b^2 - 4*c), x == -1/2*b + 1/2*sqrt(b^2 - 4*c)]
}}}
{{{id=135|
x, b, c, d = var('x b c d')
solve(x^3 + b*x^2 + c*x + d == 0, x) # cubic equation
///
[x == -1/18*(-I*sqrt(3) + 1)*(b^2 - 3*c)/(1/6*sqrt(-1/3*b^2*c^2 + 4/3*c^3 + 2/3*(2*b^3 - 9*b*c)*d + 9*d^2) - 1/27*b^3 + 1/6*b*c - 1/2*d)^(1/3) - 1/2*(I*sqrt(3) + 1)*(1/6*sqrt(-1/3*b^2*c^2 + 4/3*c^3 + 2/3*(2*b^3 - 9*b*c)*d + 9*d^2) - 1/27*b^3 + 1/6*b*c - 1/2*d)^(1/3) - 1/3*b, x == -1/18*(I*sqrt(3) + 1)*(b^2 - 3*c)/(1/6*sqrt(-1/3*b^2*c^2 + 4/3*c^3 + 2/3*(2*b^3 - 9*b*c)*d + 9*d^2) - 1/27*b^3 + 1/6*b*c - 1/2*d)^(1/3) - 1/2*(-I*sqrt(3) + 1)*(1/6*sqrt(-1/3*b^2*c^2 + 4/3*c^3 + 2/3*(2*b^3 - 9*b*c)*d + 9*d^2) - 1/27*b^3 + 1/6*b*c - 1/2*d)^(1/3) - 1/3*b, x == -1/3*b + 1/9*(b^2 - 3*c)/(1/6*sqrt(-1/3*b^2*c^2 + 4/3*c^3 + 2/3*(2*b^3 - 9*b*c)*d + 9*d^2) - 1/27*b^3 + 1/6*b*c - 1/2*d)^(1/3) + (1/6*sqrt(-1/3*b^2*c^2 + 4/3*c^3 + 2/3*(2*b^3 - 9*b*c)*d + 9*d^2) - 1/27*b^3 + 1/6*b*c - 1/2*d)^(1/3)]
}}}
You can also solve for several variables:
{{{id=29|
x, y = var('x y')
solve([x+y == 6, x-y == 4], x, y)
///
[[x == 5, y == 1]]
}}}
Sage allows you to plot functions easily. Plots can be added.
Sage knows how to differentiate and integrate many functions. For
example, to differentiate sin(x) with
respect to x, do the following:
{{{id=49|
diff(sin(x), x)
///
cos(x)
}}}
Compute higher derivatives:
{{{id=51|
diff(sqrt(exp(sin(x^2+1))+cos(x))+log(x), x, 5)
///
1/2*(32*x^5*e^(sin(x^2 + 1))*cos(x^2 + 1)^5 - 320*x^5*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1)^3 + 480*x^5*e^(sin(x^2 + 1))*sin(x^2 + 1)^2*cos(x^2 + 1) - 320*x^5*e^(sin(x^2 + 1))*cos(x^2 + 1)^3 + 480*x^5*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1) + 160*x^3*e^(sin(x^2 + 1))*cos(x^2 + 1)^4 + 32*x^5*e^(sin(x^2 + 1))*cos(x^2 + 1) - 960*x^3*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1)^2 + 480*x^3*e^(sin(x^2 + 1))*sin(x^2 + 1)^2 - 640*x^3*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 + 160*x^3*e^(sin(x^2 + 1))*sin(x^2 + 1) + 120*x*e^(sin(x^2 + 1))*cos(x^2 + 1)^3 - 360*x*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1) - 120*x*e^(sin(x^2 + 1))*cos(x^2 + 1) - sin(x))/sqrt(e^(sin(x^2 + 1)) + cos(x)) - 5/4*(2*x*e^(sin(x^2 + 1))*cos(x^2 + 1) - sin(x))*(16*x^4*e^(sin(x^2 + 1))*cos(x^2 + 1)^4 - 96*x^4*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1)^2 + 48*x^4*e^(sin(x^2 + 1))*sin(x^2 + 1)^2 - 64*x^4*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 + 16*x^4*e^(sin(x^2 + 1))*sin(x^2 + 1) + 48*x^2*e^(sin(x^2 + 1))*cos(x^2 + 1)^3 - 144*x^2*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1) - 48*x^2*e^(sin(x^2 + 1))*cos(x^2 + 1) + 12*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 - 12*e^(sin(x^2 + 1))*sin(x^2 + 1) + cos(x))/(e^(sin(x^2 + 1)) + cos(x))^(3/2) - 5/2*(4*x^2*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 - 4*x^2*e^(sin(x^2 + 1))*sin(x^2 + 1) + 2*e^(sin(x^2 + 1))*cos(x^2 + 1) - cos(x))*(8*x^3*e^(sin(x^2 + 1))*cos(x^2 + 1)^3 - 24*x^3*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1) - 8*x^3*e^(sin(x^2 + 1))*cos(x^2 + 1) + 12*x*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 - 12*x*e^(sin(x^2 + 1))*sin(x^2 + 1) + sin(x))/(e^(sin(x^2 + 1)) + cos(x))^(3/2) + 15/4*(2*x*e^(sin(x^2 + 1))*cos(x^2 + 1) - sin(x))^2*(8*x^3*e^(sin(x^2 + 1))*cos(x^2 + 1)^3 - 24*x^3*e^(sin(x^2 + 1))*sin(x^2 + 1)*cos(x^2 + 1) - 8*x^3*e^(sin(x^2 + 1))*cos(x^2 + 1) + 12*x*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 - 12*x*e^(sin(x^2 + 1))*sin(x^2 + 1) + sin(x))/(e^(sin(x^2 + 1)) + cos(x))^(5/2) + 45/8*(2*x*e^(sin(x^2 + 1))*cos(x^2 + 1) - sin(x))*(4*x^2*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 - 4*x^2*e^(sin(x^2 + 1))*sin(x^2 + 1) + 2*e^(sin(x^2 + 1))*cos(x^2 + 1) - cos(x))^2/(e^(sin(x^2 + 1)) + cos(x))^(5/2) - 75/8*(2*x*e^(sin(x^2 + 1))*cos(x^2 + 1) - sin(x))^3*(4*x^2*e^(sin(x^2 + 1))*cos(x^2 + 1)^2 - 4*x^2*e^(sin(x^2 + 1))*sin(x^2 + 1) + 2*e^(sin(x^2 + 1))*cos(x^2 + 1) - cos(x))/(e^(sin(x^2 + 1)) + cos(x))^(7/2) + 105/32*(2*x*e^(sin(x^2 + 1))*cos(x^2 + 1) - sin(x))^5/(e^(sin(x^2 + 1)) + cos(x))^(9/2) + 24/x^5
}}}
To compute the partial derivatives of
x2 +
17y2 with respect to x and
y, respectively:
{{{id=33|
f = x^2 + 17*y^2
///
}}}
{{{id=53|
f.diff(x)
///
2*x
}}}
{{{id=54|
f.diff(y)
///
34*y
}}}
{{{id=55|
g = x^3+x
///
}}}
{{{id=56|
g.diff()
///
3*x^2 + 1
}}}
{{{id=57|
f.diff()
///
Traceback (most recent call last):
File "
We move on to integrals, both indefinite and definite. To compute
the indefinite integral of x
sin(x2) and
the definite integral, as
x goes from 0 to 1, of
x/(x2 + 1):
{{{id=59|
int = integral(x*sin(x^2), x)
int
///
-1/2*cos(x^2)
}}}
{{{id=40|
diff(int,x)
///
x*sin(x^2)
}}}
{{{id=41|
integral(x/(x^2+1), x, 0, 1)
///
1/2*log(2)
}}}
{{{id=134|
integral(sqrt(x)/sqrt(x+1),x)
///
sqrt(x + 1)/(((x + 1)/x - 1)*sqrt(x)) + 1/2*log(sqrt(x + 1)/sqrt(x) - 1) - 1/2*log(sqrt(x + 1)/sqrt(x) + 1)
}}}
Sage provides standard constructions from linear algebra, e.g., the
characteristic polynomial, echelon form, trace, decomposition, etc.,
of a matrix.
Creation of matrices and matrix multiplication is easy and natural:
{{{id=42|
A = Matrix([[1,2,3],[3,2,1],[1,1,1]])
w = vector([1,1,-4])
w*A
///
(0, 0, 0)
}}}
{{{id=43|
A*w
///
(-9, 1, -2)
}}}
We create the space Mat3 3(Q):
{{{id=44|
M = MatrixSpace(QQ, 3)
M
///
Full MatrixSpace of 3 by 3 dense matrices over Rational Field
}}}
(To specify the space of 3 by 4 matrices, you would use
We create a matrix as an element of
{{{id=45|
A = M(range(9))
A
///
[0 1 2]
[3 4 5]
[6 7 8]
}}}
Next we compute its reduced row echelon form.
{{{id=46|
A.echelon_form()
///
[ 1 0 -1]
[ 0 1 2]
[ 0 0 0]
}}}
To define a new function in Sage, use the
You do not specify the types of any of the input
arguments. You can specify multiple inputs, each of which
may have an optional default value. For example, the function below
defaults to
{{{id=74|
def is_divisible_by(number, divisor=2):
return number % divisor == 0
///
}}}
{{{id=50|
is_divisible_by(6, 3)
///
True
}}}
You can also explicitly specify one or either
of the inputs when calling the function; if you specify
the inputs explicitly, you can give them in any order:
{{{id=76|
is_divisible_by(6)
///
True
}}}
{{{id=52|
is_divisible_by(divisor = 5, number = 21)
///
False
}}}
In Python, blocks of code are not indicated by
curly braces or begin and end blocks as in many
other languages. Instead, blocks of code are indicated
by indentation, which must match up exactly.
For example, the following is a syntax error because
the
Semicolons are not needed at the ends of lines; a line is
in most cases ended by a newline. However, you can put multiple
statements on one line, separated by semicolons:
{{{id=91|
a = 5; b = a + 3; c = b^2; c
///
64
}}}
If you would like a single
line of code to span multiple lines, use a terminating backslash:
{{{id=93|
2 + \
3
///
5
}}}
In Sage, you count by iterating over a range
of integers. For example, the first line below
is exactly like
Often you will want to create a nice table to display
numbers you have computed using Sage. One easy way
to do this is to use string formatting. Below,
we create three columns each of width exactly 6 and
make a table of squares and cubes.
{{{id=58|
for i in range(5):
print '%6s %6s %6s'%(i, i^2, i^3)
///
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
}}}
Here is a more complicated list:
{{{id=104|
v = [1, "hello", 2/3, sin(x^2)]
v
///
[1, 'hello', 2/3, sin(x^2)]
}}}
List indexing is 0-based, as in many programming languages.
{{{id=60|
v[2]
///
2/3
}}}
Use
Tuples are similar to lists, except they are immutable,
meaning once they are created they can't be changed.
{{{id=109|
v = (1,2,3,4); v
///
(1, 2, 3, 4)
}}}
{{{id=110|
type(v)
///
Sage also has its own set type that is (in some cases)
implemented using the built-in
Python set type, but has a little bit of extra Sage-related
functionality. Create a Sage set using
Another important data structure is the dictionary
(or associative array). This works like a list, except
that it can be indexed with almost any object (the indices
must be immutable):
{{{id=67|
d = {'hi':-2, 3/8:pi, e:pi}
///
}}}
{{{id=68|
d[3/8]
///
pi
}}}
While
{{{id=69|
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
///
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
}}}
and if.
{{{id=70|
def test(y):
if y < 0:
print "negative"
elif y == 0:
print "zero"
elif y == 1:
print "one"
else:
print "other"
///
}}}
{{{id=71|
test(1)
///
one
}}}
{{{id=133|
test(-1)
///
negative
}}}
File -> Download to a file Ctrl-C in the console.
Getting Help, Tab Completion
Solving Equations and Variables
solve
function solves equations. To use it, first specify
some variables; then the arguments to solve
are an equation (or a
system of equations), together with the variables for which to solve:
{{{id=34|
solve(x^2 + 3*x + 2, x) # x is very special and needs not to be declared explicitly
///
[x == -2, x == -1]
}}}
{{{id=39|
solve(y^2 + 3*y + 2, y) # for y it doesn't work
///
Traceback (most recent call last):
File "Plotting, Differentiating, and Integrating
}}}
{{{id=136|
p2 = plot(cos(x), (x, 0, 3*pi), color = 'red')
p1 + p2
///
}}}
Linear Algebra
MatrixSpace(QQ,3,4)
. If the number of columns is omitted, it
defaults to the number of rows, so MatrixSpace(QQ,3)
is a
synonym for MatrixSpace(QQ,3,3)
.)
M
.
Programming in Sage
Functions, Indentation, and Counting
def
command and a
colon after the list of variable names. For example:
{{{id=47|
def is_even(n):
return n%2 == 0
///
}}}
{{{id=48|
is_even(9)
///
False
}}}
divisor=2
if divisor
is not specified.
return
statement is not indented the same amount
as the other lines above it.
{{{id=78|
def even(n):
v = []
for i in range(3, n):
if i % 2 == 0:
v.append(i)
return v
///
}}}
{{{id=83|
even(9)
///
[]
}}}
If you fix the indentation, the function works:
{{{id=80|
def even(n):
v = []
for i in range(3, n):
if i % 2 == 0:
v.append(i)
return v
///
}}}
{{{id=84|
even(9)
///
[4, 6, 8]
}}}
for(i=0; i<3; i++)
in C++ or Java:
{{{id=95|
for i in range(3):
print i
///
0
1
2
}}}
The first line below is like for(i=2;i<5;i++)
.
{{{id=97|
for i in range(2,5):
print i
///
2
3
4
}}}
The third argument controls the step, so the following
is like for(i=1;i<6;i+=2)
.
{{{id=99|
for i in range(1, 6, 2):
print i
///
1
3
5
}}}
Lists, Tuples, Sets, Dictionaries
The most basic data structure in Sage is the list,
which is -- as the name suggests -- just a list of arbitrary objects.
For example, the range
command that we used creates a list:
{{{id=102|
range(2,10)
///
[2, 3, 4, 5, 6, 7, 8, 9]
}}}
len(v)
to get the length of v
, use
v.append(obj)
to append a new object to the end of v
,
and use del v[i]
to delete the ith entry of v
:
{{{id=107|
len(v)
///
4
}}}
{{{id=61|
v.append(1.5)
v
///
[1, 'hello', 2/3, sin(x^2), 1.50000000000000]
}}}
{{{id=62|
del v[1]
v
///
[1, 2/3, sin(x^2), 1.50000000000000]
}}}
Set(...)
. For example,
{{{id=115|
X = Set([1,1,1,2/3])
X
///
{1, 2/3}
}}}
{{{id=116|
1 in X
///
True
}}}
{{{id=117|
X.intersection(Set([1]))
///
{1}
}}}
Iterators
Iterators are a recent addition to Python that are particularly useful
in mathematics applications.
We make an iterator over the squares of the nonnegative integers up to $ 10000000$
{{{id=125|
v = (n^2 for n in xrange(10000000))
///
}}}
{{{id=126|
v.next()
///
0
}}}
{{{id=127|
v.next()
///
1
}}}
{{{id=128|
v.next()
///
4
}}}
Control Statements
Exporting Worksheets and Shutting Down the Sage Server