Sage is a very powerful free open source computer program for mathematical calculations. It is useful in calculus, linear algebra, differential equations, and much more; it does numerical and symbolic calculations, and both 2D and 3D graphics.
Sage, which is a cloud-based program, can be accessed from any computer via a web browser. There are two ways to use Sage: the quick version and the full version.
To use the quick version of Sage, which is good for simple computations and which does not require setting up an account, simply go to the website
To use the full version of Sage, you first need to set up a free account. To set up an account and log in, go to the website
Although you don't need any prior knowledge of computer programming to use Sage, one of the advantages of Sage is that it is based upon the Python programming language, which is the language taught in Bard's Object Orient Programming course (CMSC 141 or CMSC 143).
There are a number of sources on the web for learning for Sage. Some particularly useful ones are the following.
This textbook by Gregory Bard, which is published by the American Mathematical Society, is available in PDF format for free at the author's website. This website has links for the textbook and for other useful Sage resources.
These two videos by Gregory Bard, which use the convenient SageMathCell server, provide a nice introduction to some basic Sage commands.
These quick reference have very convenient summaries of basic Sage commands. The first quick reference has basic Sage commands, and the other quick references are for specific topics.
Each of the following interactive Sage demonstrations can be accessed via its own webpage. Brief instructions are given for each demonstration.
These interactive demonstrations can be used without any knowledge of Sage.
If you find any errors or problems with these demonstrations, please let me know.
Calculus Demonstrations
Linear Algebra Demonstrations
Probability and Statistics Demonstrations
Various basic Sage commands are demonstrated below. Each command, or group of commands, appears in a Sage Cell. To evaluate the commands, click on the Evaluate button below the cell. The result of doing the operation will appear in a new box below the Evaluate button.
If there is an error in the commands in a Sage Cell, a notice of the error will appear in the box below the Evaluate button.
The commands in any of these Sage Cells can be modified as many times as desired, and then evaluated each time.
Sage commands are meant to resemble normal mathematical notation as much as possible. However, as with any computer programming language, it is important to write Sage commands very precisely.
Here are a few basic rules for writing commands in Sage.
The operations of addition, subtraction, multiplication and division are written
Sage follows the usual order of operations.
xxxxxxxxxx
2 + 3*5
Sage works in fractions if that is the input, and in decimals if that is the input.
xxxxxxxxxx
5*(2/3) - 4*(3/7)
Use the following modification to convert an answer in fractions to decimals.
xxxxxxxxxx
n(5*(2/3) - 4*(3/7))
Sage has built in functions that are written just as you would expect, for example
You can define, and evaluate, functions just as you normally would.
xxxxxxxxxx
f(x) = x^2
f(5)
A function can have more than one variable.
Note: The letter
xxxxxxxxxx
x, y, a = var('x, y, a')
g(x, y) = x^2 + y^3
g(a + 1, a^2)
In the command for plotting a function of one variable, the domain of
xxxxxxxxxx
plot(2 * sin(x), (x, -4*pi, 4*pi))
If you want the aspect ratio of the image to be
xxxxxxxxxx
plot(2 * sin(x), (x, -4*pi, 4*pi), aspect_ratio=1)
Plotting Two Functions at Once
xxxxxxxxxx
f(x) = x^3
g(x) = 5*sin(3*x)
P = plot(f, (x, -2, 2), color='red')
Q = plot(g, (x, -2, 2), color='green')
show(P + Q)
When a function has more than one variable, the variables must be defined.
In the command for plotting a function of two variables, the domain of
The image of a function of two variables can be slow to load.
The image of a function of two variables can be rotated by clicking and holding it with the mouse and then moving it.
xxxxxxxxxx
x, y = var('x, y')
plot3d(x^2 - y^2, (x, -2, 2), (y, -2, 2))
A regular derivative is straightforward to compute.
xxxxxxxxxx
diff(x^2*cos(x))
When finding a partial derivative of a function of more than one variable, it is important to state the name of the variable with respect to which the partial derivative is taken.
xxxxxxxxxx
x, y = var('x, y')
diff(x^3*y + 5*x*y^6, y)
When finding the indefinite integral of a function, it is important to state the name of the variable after stating the function.
When you find an indefinite integral, note that Sage does not give the
xxxxxxxxxx
integrate(x^2 + ln(x), x)
The command for finding a definite integral is the same as the command for an indefinite integral, except that the limits of integration are added.
xxxxxxxxxx
integrate(x^2, x, 0, 2)
Use the following modification to find a definite integral with decimal answer
xxxxxxxxxx
integrate(x^2, x, 0, 2).n()
A list is similar to a set, though in a list the elements have the given order.
xxxxxxxxxx
lis = [3, -1, pi, sqrt(2), 5/7]
lis
Single elements of a list can be called using the index for the element.
Note: In a list in Sage, the first element has index
xxxxxxxxxx
lis = [3, -1, pi, sqrt(2), 5/7]
lis[2]
Lists can be generated very similarly to the way sets are defined. Note that the variable used for generating the list exists only inside the definition (similarly to the variable used in defining a set), and therefore does not need to be defined previously.
xxxxxxxxxx
lis = [n^2 for n in [1..10]]
lis
Solving an equation is simple, as long as two issues are noted.
Note: The equals sign in the equation is typed with a double equals.
Note: The unknown being solved for must be specified.
xxxxxxxxxx
solve( x^3 - 2*x + 1 == 0, x )
An equation with more than one unknown can be solved for any one of the unknowns in terms of the others.
xxxxxxxxxx
x, y = var('x, y')
solve( 3*y^2*x - 2*y*x^3 + 4*x^4 == 0, y)
Systems of equations can be solved as follows.
xxxxxxxxxx
x, y = var('x, y')
solve([3*x - 2*y == 6, 5*x + y == 4], x, y)
When using the solve command in Sage, the set of solutions is given as a list containing the variable and solutions. To extract single numerical values of the solutions, use the following command.
Note: As with all lists in Sage, the first element has index
xxxxxxxxxx
sol = solve( x^3 - 2*x + 1 == 0, x, solution_dict=True)
sol[0][x].n()
Sage can handle matrices very easily, though there is one issue to note.
Note: When you define a matrix in Sage, it is important to state what set of numbers is being used: QQ (rational numbers), or RR (real numbers) or CC (complex numbers).
xxxxxxxxxx
A = Matrix(QQ,[[1,-2,0],[4,1,-1],[0,3,-5]])
A
Matrices can be added, subtracted and multiplied using the same method as adding, subtracting and multiplying numbers.
xxxxxxxxxx
A = Matrix(QQ,[[1,-2,0],[4,1,-1],[0,3,-5]])
B = Matrix(QQ,[[2,2,5],[-3,0,3],[1,-2,0]])
A*B
Determinants are easy to compute.
xxxxxxxxxx
A = Matrix(QQ,[[1,-2,0],[4,1,-1],[0,3,-5]])
A.determinant()
Eigenvalues are easy to compute. The letter
xxxxxxxxxx
T = Matrix(CC,[[1,-2,0],[4,1,-1],[0,3,-5]])
T.eigenvalues()
It is simple to make an interactive program in Sage. In the following example, which is the graph of the function
Interactive programs in Sage use a variety of tools such as sliders buttons, selector buttons, input boxes and range sliders, all of which are shown in the following example.
When you use the interact command in Sage, it is crucial to indent the various parts of the program as shown (other than the fact that some lines have been broken in two in order to fit in a small box).
xxxxxxxxxx
def _(
aa = slider(-5, 5, 0.1, default=1, label="\(A\)"),
bb = selector([4, 3, 2, 1, 1/2, 1/3, 1/4],
default=2, nrows=1, label = "\(B\)"),
cc = input_box(default=0, label="\(C\)"),
xinterval=range_slider(-10, 10, 1, default=(-3, 3),
label="\(x\)-interval"),
yinterval=range_slider(-10, 10, 1, default=(-3, 3),
label="\(y\)-interval")
):
x_min, x_max = map(QQ, xinterval)
y_min, y_max = map(QQ, yinterval)
f(x) = aa*sin(bb*x + cc)
P = plot(f, x, xmin = x_min, xmax = x_max,
ymin = y_min, ymax = y_max, color = 'red',
figsize = 10, aspect_ratio=1);
pretty_print(html( r'\[y = A\sin(Bx + C)\]' ));
show(P)
Ethan Bloch
Professor of Mathematics
Bard College
Office Hours: