Getting started with the basics of SageMath
You don’t have to install Sage to try it out! In this article, we will use the notebook interface to showcase some of the basics of Sage so that you can follow along using a public notebook server. These examples can also be run from an interactive session if you have installed Sage. Go to http://www.sagenb.org and sign up for a free account. You can also browse worksheets created and shared by others. The notebook interface should look like this:
Create a new worksheet by clicking on the link called New Worksheet:
Type in a name when prompted, and click Rename. The new worksheet will look like this:
Enter an expression by clicking in an input cell and typing or pasting in an expression:
Click the evaluate link or press Shift-Enter to evaluate the contents of the cell.
A new input cell will automatically open below the results of the calculation. You can also create a new input cell by clicking in the blank space just above an existing input cell.
Using Sage as a powerful calculator
Sage has all the features of a scientific calculator—and more. If you have been trying to perform mathematical calculations with a spreadsheet or the built-in calculator in your operating system, it’s time to upgrade. Sage offers all the built-in functions you would expect. Here are a few examples:
If you have to make a calculation repeatedly, you can define a function and variables to make your life easier. For example, let’s say that you need to calculate the Reynolds number, which is used in fluid mechanics:
You can define a function and variables like this:
Re(velocity, length, kinematic_viscosity) = velocity * length / kinematic_viscosity
v = 0.01
L = 1e-3
nu = 1e-6
Re(v, L, nu)
When you type the code into an input cell and evaluate the cell, your screen will look like this:
Now, you can change the value of one or more variables and re-run the calculation:
Sage can also perform exact calculations with integers and rational numbers. Using the pre-defined constant pi will result in exact values from trigonometric operations. Sage will even utilize complex numbers when needed. Here are some examples:
Much of the difficulty of higher mathematics actually lies in the extensive algebraic manipulations that are required to obtain a result. Sage can save you many hours, and many sheets of paper, by automating some tedious tasks in mathematics. We’ll start with basic calculus. For example, let’s compute the derivative of the following equation:
The following code defines the equation and computes the derivative:
var('x') f(x) = (x^2 - 1) / (x^4 + 1) show(f) show(derivative(f, x))
The results will look like this:
The first line defines a symbolic variable x (Sage automatically assumes that x is always a symbolic variable, but we will define it in each example for clarity). We then defined a function as a quotient of polynomials. Taking the derivative of f(x) would normally require the use of the quotient rule, which can be very tedious to calculate. Sage computes the derivative effortlessly.
Now, we’ll move on to integration, which can be one of the most daunting tasks in calculus. Let’s compute the following indefinite integral symbolically:
The code to compute the integral is very simple:
f(x) = e^x * cos(x) f_int(x) = integrate(f, x) show(f_int)
The result is as follows:
To perform this integration by hand, integration by parts would have to be done twice, which could be quite time consuming. If we want to better understand the function we just defined, we can graph it with the following code:
f(x) = e^x * cos(x) plot(f, (x, -2, 8))
Sage will produce the following plot:
Sage can also compute definite integrals symbolically:
To compute a definite integral, we simply have to tell Sage the limits of integration:
f(x) = sqrt(1 - x^2) f_integral = integrate(f, (x, 0, 1)) show(f_integral)
The result is:
This would have required the use of a substitution if computed by hand.
Have a go hero
There is actually a clever way to evaluate the integral from the previous problem without doing any calculus. If it isn’t immediately apparent, plot the function f(x) from 0 to 1 and see if you recognize it. Note that the aspect ratio of the plot may not be square.
The partial fraction decomposition is another technique that Sage can do a lot faster than you. The solution to the following example covers two full pages in a calculus textbook —assuming that you don’t make any mistakes in the algebra!
f(x) = (3 * x^4 + 4 * x^3 + 16 * x^2 + 20 * x + 9) / ((x + 2) * (x^2 + 3)^2) g(x) = f.partial_fraction(x) show(g)
The result is as follows:
We’ll use partial fractions again when we talk about solving ordinary differential equations symbolically.
Linear algebra is one of the most fundamental tasks in numerical computing. Sage has many facilities for performing linear algebra, both numerical and symbolic. One fundamental operation is solving a system of linear equations:
Although this is a tedious problem to solve by hand, it only requires a few lines of code in Sage:
A = Matrix(QQ, [[0, -1, -1, 1], [1, 1, 1, 1], [2, 4, 1, -2],
[3, 1, -2, 2]])
B = vector([0, 6, -1, 3])
The answer is as follows:
Notice that Sage provided an exact answer with integer values. When we created matrix A, the argument QQ specified that the matrix was to contain rational values. Therefore, the result contains only rational values (which all happen to be integers for this problem).