Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 3731

Introduction to Sage

Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.

Basic Computations

Let's start with an easy calculation. Below you will see a Sage cell, with 2+2 already entered. Click inside the cell, then hit Shift-Return to evaluate the cell and get the result:

2*(5-3)^2
8

(You can also click on a gray line to get a new Sage cell in which to enter your own calculation. Try it out!)

As the above example shows, Sage can be used as a calculator. But Sage is a very good calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type sin(pi/3) and press [Shift]-[Enter] to see a little of what Sage can do.

sin(pi/3)
1/2*sqrt(3)

That's really good! But if you want to see a numerical approximation to the number sin(π/3)\sin(\pi/3), you can use the Sage function n. You can also use the symbol _ to refer to the last computation that Sage did. So if the last thing you computed was sin(pi/3) above, you can type n(_) to find a numerical approximation. (If you have entered something else since then, just type n(sin(pi/3)) in a new cell.)

n(_)
0.866025403784439

If you want more accuracy, you can add the argument digits inside the n function, like this:

n(sqrt(2), digits=50)
1.4142135623730950488016887242096980785696718753769

Sage is familiar with many functions you know about. A few notes about some special ones:

  • The inverse trig functions are written as arcsin, arctan, etc.

  • The natural logarithm can be written as ln or as log. If you want a logarithm to another base, such as base 10, specify the base with the base argument: log(x, base=10), for example.

  • The natural exponential function, exe^x, can be written as exp(x).

log(64, base=2)
6

Practice Can you find (to at least six decimal places) the angle which makes sin(x)\sin(x) equal to 1/3?

Algebra

Sage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both x and t to be symbolic variables with the statement below:

var("x, t")
(x, t)

Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication must be indicated with a *; it cannot be represented by juxtaposition!)

expand((x + 5)*(2*x - 1))
2*x^2 + 9*x - 5
show(_)
2x2+9x5\displaystyle 2 \, x^{2} + 9 \, x - 5
factor(6*x^3 + 26*x^2 - 20*x)
2*(3*x - 2)*(x + 5)*x
factor(17363829)
3 * 7 * 826849
solve( x^2 + x == 6, x)
[x == -3, x == 2]
p = (x^2 + 3*x + 5)/(x^2 -5*x+6)
p.partial_fraction()
-15/(x - 2) + 23/(x - 3) + 1
show(_)
15x2+23x3+1\displaystyle -\frac{15}{x - 2} + \frac{23}{x - 3} + 1

It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function f(x)=x2+2f(x) = x^2 + 2:

f(x) = x^2 + 2

When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function ff, if you wish:

f(2)
6

There are many other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more.

Basic Calculus

Sage can also carry out some basic symbolic calculus. Here are a few examples:

var("x") derivative(3*exp(x^2)+log(x), x)
x 6*x*e^(x^2) + 1/x
derivative(x^2*t^2, x)
2*t^2*x
integrate(x^3 + x + 1, x)
1/4*x^4 + 1/2*x^2 + x
integrate(x, (x, 0, 1))
1/2
taylor(cos(x), x, 0, 7)
-1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1
show(_)
1720x6+124x412x2+1\displaystyle -\frac{1}{720} \, x^{6} + \frac{1}{24} \, x^{4} - \frac{1}{2} \, x^{2} + 1

Plotting

It is easy to plot a function in Sage. Here is an example of plotting the function f(x)=3x2+2x1f(x) = 3 x^2 + 2 x - 1 on the interval [2,2][-2, 2]:

plot(3*x^2+2*x-1, (x, -4, -1))

Sometimes the graph does not show the sorts of details you would like. Try the following to plot tan(x)\tan(x) on [π,π][-\pi, \pi]:

plot(tan(x), (x, -pi, pi))

The yy values get so big that we cannot see much else. We can add the extra arguments ymin and ymax to the plot function to fix the window of the plot:

plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2)

By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph. We can adjust the behavior at these points using the detect_poles option:

plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2, detect_poles="show")

3D Plotting

We are more interested in plots of functions of several variables. Here's how to plot a surface:

var("x, y") plot3d(sin(x*y), (x, 0, 4), (y, 0, 4))
(x, y)
3D rendering not yet implemented

A useful trick is to plot an implicit surface, where a function is equal to a given value. Here we can plot a sphere, by plotting where x2+y2+z2=1x^2 + y^2 + z^2 = 1:

var("x, y, z") implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour = [1])
(x, y, z)
3D rendering not yet implemented

Sage is also capable of plotting parametric plots in space, using parametric_plot3d:

var("t") parametric_plot3d((3*cos(t), 3*sin(t), t), (t, 0, 6*pi))
t
3D rendering not yet implemented

To combine two plots, just add them:

p1 = plot3d(x^2+y^2, (x, -1.2, 1.2), (y, -1.2, 1.2)); p2 = plot3d(1 - y^2, (x, -1.2, 1.2), (y, -1.2, 1.2), color="green"); p1+p2
3D rendering not yet implemented

Vectors and Matrices

Sage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the vector function:

v1 = vector([1, 2, 3]); v2 = vector([3, -1, 1])
show(v1)
(1,2,3)\displaystyle \left(1,\,2,\,3\right)

Sage understands common operations, and even knows about dot and cross products:

v1 + 2*v2 v1.cross_product(v2) v1.dot_product(v2) v1 * v2
(7, 0, 5) (5, 8, -7) 4 4

Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by QQ. Then we enter the matrix as a list of lists. We define a matrix m

m = matrix(QQ, [[3, 2, 0], [1, -1, 1], [-1, 1, -5]]); show(m)
(320111115)\displaystyle \left(\begin{array}{rrr} 3 & 2 & 0 \\ 1 & -1 & 1 \\ -1 & 1 & -5 \end{array}\right)
%md Sage recognizes many operations we can perform on matrices. act_on_polynomial Here are some you might be familiar with:
m.rref()
[1 0 0] [0 1 0] [0 0 1]
m.eigenvalues()
[-5.305282147755060?, -1.105324652427052?, 3.410606800182113?]

Programming

Sage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not:

def iseven(number): if (number % 2 == 0): return true else: return false
iseven(64.5)
False

If you are familiar with Python, Sage uses the same syntax.

This is text. We can include italic, or bold.

Writing Text

Finally, to write your responses to some Sage problems, you will want to be able to put in some text explanations. The easiest way to do this is using markdown, a simple language for formatted text. To start a markdown cell, put %md as the first text in the cell. Then you can type any text you want, and when you evaluate that cell, it will mostly just turn in to text. Here are a few simple formatting commands you can add, if you wish

  • To type italicized text, surround the words with an asterisk: *italicized*

  • To make text bold, use two asterisks: **bold**

  • Trickier: You can format a polynomial like f(x)=3x3x2+2x+1f(x) = 3 x^3 - x^2 + 2x + 1 like this: $f(x) = 3 x^3 - x^2 + 2x + 1$

(That last example is very tricky. There is an entire language for mathematical typesetting called LaTeX\LaTeX which is being used there. You are not required to use this notation, but you are welcome to try if you wish.)

All of the text you see in this document is really in a markdown cell. If you double click on one of those cells, you can see exactly how I created that text, if you are interested.

(Alternatively, you can also start a cell with %html, and Sage will recognize and render HTML as well.)

Other References

Here are some useful references if you would like to explore Sage further:

f(x) = ((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3)) table([(x, f(x)) for x in [-.59, -.599, -.5999, -.61, -.601, -.6001, -.60001]], header_row = ("x", "f(x)"))
x f(x) +--------------------+-------------------+ -0.590000000000000 -28.4380000000017 -0.599000000000000 -287.639800000051 -0.599900000000000 -2879.63998000200 -0.610000000000000 29.1579999999997 -0.601000000000000 288.359800000013 -0.600100000000000 2880.35998001141 -0.600010000000000 28800.3599988519
f(2)
10/13
[(x, f(x)) for x in [1, 2, 3]]
[(1, 1/2), (2, 10/13), (3, 1)]
plot((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3),(x, -1, 1), ymax=10, ymin=-10)

Introduction to Sage

Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.

Basic Computations

Let's start with an easy calculation. Below you will see a Sage cell, with 2+2 already entered. Click inside the cell, then hit Shift-Return to evaluate the cell and get the result:

2*(5-3)^2
8

(You can also click on a gray line to get a new Sage cell in which to enter your own calculation. Try it out!)

As the above example shows, Sage can be used as a calculator. But Sage is a very good calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type sin(pi/3) and press [Shift]-[Enter] to see a little of what Sage can do.

sin(pi/3)
1/2*sqrt(3)

That's really good! But if you want to see a numerical approximation to the number sin(π/3)\sin(\pi/3), you can use the Sage function n. You can also use the symbol _ to refer to the last computation that Sage did. So if the last thing you computed was sin(pi/3) above, you can type n(_) to find a numerical approximation. (If you have entered something else since then, just type n(sin(pi/3)) in a new cell.)

n(_)
0.866025403784439

If you want more accuracy, you can add the argument digits inside the n function, like this:

n(sqrt(2), digits=50)
1.4142135623730950488016887242096980785696718753769

Sage is familiar with many functions you know about. A few notes about some special ones:

  • The inverse trig functions are written as arcsin, arctan, etc.

  • The natural logarithm can be written as ln or as log. If you want a logarithm to another base, such as base 10, specify the base with the base argument: log(x, base=10), for example.

  • The natural exponential function, exe^x, can be written as exp(x).

log(64, base=2)
6

Practice Can you find (to at least six decimal places) the angle which makes sin(x)\sin(x) equal to 1/3?

Algebra

Sage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both x and t to be symbolic variables with the statement below:

var("x, t")
(x, t)

Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication must be indicated with a *; it cannot be represented by juxtaposition!)

expand((x + 5)*(2*x - 1))
2*x^2 + 9*x - 5
show(_)
2x2+9x5\displaystyle 2 \, x^{2} + 9 \, x - 5
factor(6*x^3 + 26*x^2 - 20*x)
2*(3*x - 2)*(x + 5)*x
factor(17363829)
3 * 7 * 826849
solve( x^2 + x == 6, x)
[x == -3, x == 2]
p = (x^2 + 3*x + 5)/(x^2 -5*x+6)
p.partial_fraction()
-15/(x - 2) + 23/(x - 3) + 1
show(_)
15x2+23x3+1\displaystyle -\frac{15}{x - 2} + \frac{23}{x - 3} + 1

It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function f(x)=x2+2f(x) = x^2 + 2:

f(x) = x^2 + 2

When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function ff, if you wish:

f(2)
6

There are many other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more.

Basic Calculus

Sage can also carry out some basic symbolic calculus. Here are a few examples:

var("x") derivative(3*exp(x^2)+log(x), x)
x 6*x*e^(x^2) + 1/x
derivative(x^2*t^2, x)
2*t^2*x
integrate(x^3 + x + 1, x)
1/4*x^4 + 1/2*x^2 + x
integrate(x, (x, 0, 1))
1/2
taylor(cos(x), x, 0, 7)
-1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1
show(_)
1720x6+124x412x2+1\displaystyle -\frac{1}{720} \, x^{6} + \frac{1}{24} \, x^{4} - \frac{1}{2} \, x^{2} + 1

Plotting

It is easy to plot a function in Sage. Here is an example of plotting the function f(x)=3x2+2x1f(x) = 3 x^2 + 2 x - 1 on the interval [2,2][-2, 2]:

plot(3*x^2+2*x-1, (x, -4, -1))

Sometimes the graph does not show the sorts of details you would like. Try the following to plot tan(x)\tan(x) on [π,π][-\pi, \pi]:

plot(tan(x), (x, -pi, pi))

The yy values get so big that we cannot see much else. We can add the extra arguments ymin and ymax to the plot function to fix the window of the plot:

plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2)

By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph. We can adjust the behavior at these points using the detect_poles option:

plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2, detect_poles="show")

3D Plotting

We are more interested in plots of functions of several variables. Here's how to plot a surface:

var("x, y") plot3d(sin(x*y), (x, 0, 4), (y, 0, 4))
(x, y)
3D rendering not yet implemented

A useful trick is to plot an implicit surface, where a function is equal to a given value. Here we can plot a sphere, by plotting where x2+y2+z2=1x^2 + y^2 + z^2 = 1:

var("x, y, z") implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour = [1])
(x, y, z)
3D rendering not yet implemented

Sage is also capable of plotting parametric plots in space, using parametric_plot3d:

var("t") parametric_plot3d((3*cos(t), 3*sin(t), t), (t, 0, 6*pi))
t
3D rendering not yet implemented

To combine two plots, just add them:

p1 = plot3d(x^2+y^2, (x, -1.2, 1.2), (y, -1.2, 1.2)); p2 = plot3d(1 - y^2, (x, -1.2, 1.2), (y, -1.2, 1.2), color="green"); p1+p2
3D rendering not yet implemented

Vectors and Matrices

Sage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the vector function:

v1 = vector([1, 2, 3]); v2 = vector([3, -1, 1])
show(v1)
(1,2,3)\displaystyle \left(1,\,2,\,3\right)

Sage understands common operations, and even knows about dot and cross products:

v1 + 2*v2 v1.cross_product(v2) v1.dot_product(v2) v1 * v2
(7, 0, 5) (5, 8, -7) 4 4

Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by QQ. Then we enter the matrix as a list of lists. We define a matrix m

m = matrix(QQ, [[3, 2, 0], [1, -1, 1], [-1, 1, -5]]); show(m)
(320111115)\displaystyle \left(\begin{array}{rrr} 3 & 2 & 0 \\ 1 & -1 & 1 \\ -1 & 1 & -5 \end{array}\right)
%md Sage recognizes many operations we can perform on matrices. act_on_polynomial Here are some you might be familiar with:
m.rref()
[1 0 0] [0 1 0] [0 0 1]
m.eigenvalues()
[-5.305282147755060?, -1.105324652427052?, 3.410606800182113?]

Programming

Sage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not:

def iseven(number): if (number % 2 == 0): return true else: return false
iseven(64.5)
False

If you are familiar with Python, Sage uses the same syntax.

This is text. We can include italic, or bold.

Writing Text

Finally, to write your responses to some Sage problems, you will want to be able to put in some text explanations. The easiest way to do this is using markdown, a simple language for formatted text. To start a markdown cell, put %md as the first text in the cell. Then you can type any text you want, and when you evaluate that cell, it will mostly just turn in to text. Here are a few simple formatting commands you can add, if you wish

  • To type italicized text, surround the words with an asterisk: *italicized*

  • To make text bold, use two asterisks: **bold**

  • Trickier: You can format a polynomial like f(x)=3x3x2+2x+1f(x) = 3 x^3 - x^2 + 2x + 1 like this: $f(x) = 3 x^3 - x^2 + 2x + 1$

(That last example is very tricky. There is an entire language for mathematical typesetting called LaTeX\LaTeX which is being used there. You are not required to use this notation, but you are welcome to try if you wish.)

All of the text you see in this document is really in a markdown cell. If you double click on one of those cells, you can see exactly how I created that text, if you are interested.

(Alternatively, you can also start a cell with %html, and Sage will recognize and render HTML as well.)

Other References

Here are some useful references if you would like to explore Sage further:

f(x) = ((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3)) table([(x, f(x)) for x in [-.59, -.599, -.5999, -.61, -.601, -.6001, -.60001]], header_row = ("x", "f(x)"))
x f(x) +--------------------+-------------------+ -0.590000000000000 -28.4380000000017 -0.599000000000000 -287.639800000051 -0.599900000000000 -2879.63998000200 -0.610000000000000 29.1579999999997 -0.601000000000000 288.359800000013 -0.600100000000000 2880.35998001141 -0.600010000000000 28800.3599988519
f(2)
10/13
[(x, f(x)) for x in [1, 2, 3]]
[(1, 1/2), (2, 10/13), (3, 1)]
plot((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3),(x, -1, 1), ymax=10, ymin=-10)