Sage demo

Sage is a free and open source computer algebra system (CAS). It is a viable alternative to Maple, Mathematica, and Magma. It may be a workable alternative to Matlab, but Octave might be a better alternative. Sage is probably not the tool for every mathematical computation: for quick two-dimensional plotting, perhaps Desmos is more ideal, while GeoGebra is an ideal tool for exploring/understanding the algebra-geometry correspondence. The coding style for Sage is based on the well-known language Python, whereas Maple and Mathematica are proprietary languages.

Computing in Sage can be done in three ways:

  • Sage may be freely downloaded and installed for Windows, Mac, Linux, etc.
  • Sage may be freely run in the cloud at CoCalc, formely the SageMathCloud. (For more processing power, paid subscriptions are available.)
  • ... and a thing I call "Sage on webpages". Live scripts can be embedded into webpages (like all of the examples shown later on this webpage) using the Sage Cell Server. While I have developed the rest of this webpage as a general showcase of Sage's capabilities, there is a nice online linear algebra web-textbook which nicely incorporates "Sage on webpages" within the text. Gregory Bard has nice interactive scripts covering system of inequalities, Leontief input-output analysis, the portfolio balancing problem, etc.

Here are some demonstrations of things that Sage can do. Any of the scripts can be run "live" on the web by clicking "Evaluate." Moreover, you can modify a script and click "Evaluate" again, and your code will run! (In some situations, it would be confusing to the user to see code, so it is also possible to "hide" the code. See an example for linearization or for Riemann sums.)

Decimal approximation

Sage can provide a decimal approximation of a number such as \(\sqrt{5}\). Note that .n() is an an alias for numerical_approx().

Expressions and special constants

Sage will simplify expressions involving special constants such as \(\cos(\frac{\pi}{6})\) and \(\arccos(-\frac12)\). In addition, the value of an expression such as \(e^{\pi i}\) can be stored in a variable to be used later (or printed to screen).

Expanding and factoring expressions

Sage can expand the expression \((x-1)^8\) or factor the expression \(x^3+4x^2+8x+32\).

Solving equations

Sage can solve for \(x\) in the equation \(x^2 + 3x = -2\).

Function evaluation

Sage can take the function \(f(x) = x^3\) and use this to evaluate \(f(2)\) and evaluate the difference quotient \( \dfrac{f(2+h)-f(2)}{h} \).

Two-dimensional graphing: plot function

Sage can plot the graph the function \(f(x) = (x-3)^2 + 4\) for \(-30 \leq x \leq 30\) and \(-30 \leq y \leq 30\).

Sliders: Exploring graph transformations

Sage can take a value of \(h\) and \(k\) from the user (with sliders) and plot \(y = (x-h)^2 + k\)

Sliders and text input boxes: Exploring graph transformations

Sage can take a function defined by the user, by default \(f(x) = x^2 + \frac12\sin(18x)\), and apply a vertical shift by \(a\) units. Both the definition of \(f\) and the value of \(a\) may be modified. For instance, with no coding whatsoever, the user may choose to use the function \(f(x) = \cos(x)\) simply by typing in the input box within the green box.

Sliders and text input boxes: Exploring linearization

Sage can take a function defined by the user, by default, \(f(x)=x^2 + \frac{\pi}{2}\sin(3x)\), show its linearization, and provide a visual representation of how good the linear approximation is for a specified \(x\) value. The same example of linearization is available on a stand-alone webpage where the code is intentionally hidden from the student (but still visible in the HTML source).

Two-dimensional graphing: plot equation in two variables

Sage will provide an implicit plot of an equation in \(x\) and \(y\). For example, here we ask Sage to sketch the plot of the equation \((x-3)^2+(y+4)^2=7\), a circle centered at \(3,-4\) of radius \(\sqrt{7}\).

Limits

Sage can take the function \(g(x) = \frac{\sin(x)}{x}\) and use this to find \( \displaystyle \lim_{x \to 0} g(x)\). Sage can also take the function \(f(x) = x^3 + \frac1x\) and use this to find \( \displaystyle \lim_{h \to 0} \dfrac{f(a+h)-f(a)}{h} \) and \( \displaystyle \lim_{h \to 0} \dfrac{f(x+h)-f(x)}{h} \), the derivative via definition.

Sliders: exploring the precise definition of a limit

Here is a script, run completely from the web, which allows a student to use sliders to explore what's really happenining the \(\varepsilon\)-\(\delta\) definition of a limit.

Differentiation

Sage will find the derivative of \(f(x) = x^3 + 7^x\) and \(g(x) = \sin(\sin(\sin(\sin(\sin(\sin(5x))))))\).

Integration

Given \(f(x) = x^3 + 10x^6\), Sage can find the indefinite integral \( \displaystyle \int f(x)\,dx\) and the definite integral \( \displaystyle \int_2^4 f(x)\,dx\).

Taylor polynomials

Using a for loop, we can have Sage compute several Taylor polynomials of the function \(f(x) = \sin(x) + \cos(x)\).

Sliders: Taylor polynomials

Sliders can be used to see how similar a Taylor polynomial for \(f(x) = \sin(x) + \cos(x)\) begins to look more like the graph of \(f(x)\) itself.

Partial Differentiation

Sage will find \(\frac{\partial f}{\partial x}\) given \(f(x,y) = x^3y + 7^x\sin(y)\). Sage will also find \( \frac{\partial}{\partial y}( 5x + (xy)^4 - \cos(x+y+xy+x^5y^9) + 7^y)\).

Multiple Integrals and Hessians for functions of several variables

Given \(f(x,y)=x^3-2x\sin(y)\), Sage can find the double integral \( \displaystyle \int_{-\pi/2}^{\pi/3}\int_2^4 f(x,y)\,dx\,dy\) and the Hessian of \(f(x,y)\).

Jacobian for change of variables

Sage can compute the Jacobian for the change of variables \(u = x^3 \sin(y)\) and \(v = 4x^y\) with respect to the variables \(x\) and \(y\).

Contour plot

Sage can visualize a contour plot for \(f(x,y) = y^2 + 1 - x^3 - x\).

Graphing a two-dimensional surface in three-dimensional space

Sage can do three-dimensional implicit plots and graphs of functions which require three dimensions. The sage cell below shows a plot of \( z - 2.5 = 2 \sin(x) + 1.7 \cos(\frac{y}{3})\) for \(-20 \leq x \leq 20\) and \(-20 \leq y \leq 20\) and \(-8 \leq z \leq 8\)

Three-dimensional visualization: sphere

Sage can plot the sphere \(x^2 + y^2 + z^2 = 4\) for \(-3 \leq x \leq 3\) and \(-3 \leq y \leq 3\) and \(-3 \leq z \leq 3\).

Three-dimensional visualization: heart-shaped surface

Sage can plot a heart-shaped surface \((x^2+\frac94y^2+z^2-1)^3 - x^2z^3 - \frac{9}{80}y^2z^3 = 0\) in red.

Three-dimensional visualization: blob shape

Sage can plot the surface defined by \( x^2 + y^2 + z^2 + sin(4x) + sin(4y) + sin(4z) - 1 = 0\) in lavender blush.

Three-dimensional visualization: Klein bottle

Sage can plot a Klein bottle.

Three-dimensional visualization with color gradients

Sage can plot a surface using another function (a coloring function) to provide pretty gradients. In this example, we see all points \((x,y,z)\) satisfying the property that \(x^2+y^2+z^2\) is equal to \(4\), with the color determined by the expression \((\sin 3z)^2\).

Plot a slope field

Sage can plot a slope field for \(\frac{dy}{dx} = \sin(x+y) + \cos(x+y)\).

Plot a vector field

Sage can plot the vector field \({\bf F}(x,y) = \langle y, (\sin x)(\cos x - 2) \rangle \).

Solving differential equations

Sage can solve the differential equation \(y'(x) + y(x) - 1 = 0\).
The same differential equation is written with different variables as \(x'(t)+x(t)-1=0\), and Sage can handle different roles for variables.

Solving separable differential equations

Sage can solve separable differential equations such as \( \frac{dy}{dx}\sin(y) = \cos(x)\).

Solving second-order differential equations

Sage can solve second order differential equations such as \(y'' - y = x\).

Solving differential equations with multiple variables

Sage can solve differential equations involving more variables. For example, let's solve \(x^2 \frac{dy}{dx} = a + b x^n + cx^2y^2\) for \(y\) with independent variable \(x\).

Solving a linear system of first-order ordinary differential equations

Sage can solve a system of differential equations such as \( x_1' = -99x_1 + 306x_2 \) and \(x_2' =-51x_1 + 156x_2\).

Solving an (affine linear) system of first-order ordinary differential equations

Sage can solve a system of differential equations such as \( x_1'(t) + x_2(t) - 1 = 0\) and \(x_2'(t) - x_1(t) + 1 = 0\).

Vector geometry

Given vectors \({\bf u}=3{\bf i} + 4{\bf j} + 5{\bf k}\) and \({\bf v} = {\bf i} -3{\bf k}\), Sage will compute the dot product \({\bf u} \cdot {\bf v}\), the cross product \({\bf u} \times {\bf v}\), and the linear combination \(10{\bf u} + 3{\bf v}\).

Matrix algebra/operations: powers of a matrix, transpose, linear combination, identity

This example specifies a \(3 \times 3\) matrix \(A\) and asks Sage to compute \(A^5\). This example also specifies a \(2 \times 3\) matrix \(B\), defines \(C\) to be \(B^TB\), then asks Sage to compute \(2A+3C+4000I\), where \(I\) is the \(3 \times 3\) identity matrix.

Solving a system of linear equations

Sage can solve a system of linear equations such as \(y+3x=-2\) and \(x-2y = 5\). Of course, Sage can also solve the same system by producing the rref of the appropriate augmented matrix.

Linear algebra: construct matrix for Gaussian elimination practice

This example constructs a \(3 \times 4\) matrix of rank \(2\) whose reduced row-echelon form has only integer values, followed by a LaTeX version.

Linear algebra: Gaussian elimination

This example constructs a \(5 \times 9\) matrix of rank \(3\) whose reduced row-echelon form has only integer values, followed by a LaTeX version.

Linear algebra: Compute kernel, null space

This example constructs a \(7 \times 9\) matrix of rank \(5\) and finds a basis for its kernel. We need to use right_kernel for what is generally consider the null space (or kernel) of a matrix.

Linear algera: construct random diagonalizable matrix

This example constructs a \(4 \times 4\) diagonalizable matrix with eigenvalues \(5\), and \(3\) with multiplicity \(1\), and eigenvalue \(-6\) with multiplicity \(2\), displaying the matrix in sage's standard output, followed by a LaTeX version.

Linear algera: computations on a diagonalizable matrix

This example constructs a \(4 \times 4\) diagonalizable matrix \(A\) with eigenvalues \(2\), and \(3\) with multiplicity \(1\), and eigenvalue \(-6\) with multiplicity \(2\). Then, the characteristic polynomial is computed and displayed both in native Sage form, and in LaTeX output. Then, we display the factored version of the characteristic polynomial.

Singular Value Decomposition

Sage can compute SVD for a matrix.

LU Decomposition

Sage can compute LU for a matrix.

Discrete Markov Chains

Sage can find the steady state distribution for a discrete Markov chain.

Continuous Markov Chains

Sage can find the steady state probabilities for a continuous Markov chain.

Markov Chain visualization

Sage can visualize the states of Markov chains.

Visualizing a graph

Sage can provide a visual representation of the complete graph \(K_5\) on \(5\) vertices. The same visual representation can be exported as TikZ code.

Graph properties

Sage can construct a random graph \(G\) on \(n=9\) vertices, with each edge inserted independently with probability \(p=.3\). Sage can present many computations related to the graph \(G\), such as its adjacency matrix, Tutte polynomial, matching polynomial, chromatic polynomial, chromatic number (even finding a proper coloring for you), and a maximum independent set. Since the graph \(G\) was constructed randomly, running this script again will produce different results each time.

Powered by Jekyll, theme by Scott Emmons under Creative Commons Attribution. All of the code for this website can be viewed at GitHub.