Skip to main content

Section 1.2 Using SageMath

Preview Activity 1.2.1.

Many of the features of the Python language are optimized for computational efficiency, such as using binary approximations of fractions. This is valuable for applications like data science, but for studying mathematics itself we will find it useful to have another language that more closely models mathematics, namely, SageMath (or “Sage” for short).

(a)

Confirm that 6^2 and 6**2 both represent \(6^2=36\) in SageMath code cells (note the buttons include “(Sage)”, and we prefer to use show() in place of print() when using SageMath.).

(b)

Confirm that 1/10+1/5 equals exactly \(\frac{3}{10}\) in SageMath.

(c)

Run the following code cell to display the algebraic formula \((x+1)^3=x^3+3x^2+3x+1\text{.}\)

Then modify the above cell to find a formula for \((x+1)^4\text{.}\)

Activity 1.2.2.

SageMath is a mathematical software system designed for representing ideas from mathematics as faithfully as possible.

(a)

Several important numbers in mathematics may be used in SageMath with pi for the circular constant \(\pi\text{,}\) e for the natural number \(e\text{,}\) and i for the imaginary unit \(i\text{.}\) (Don't worry if you aren't familiar with these yet.)

Modify the below cell to see what SageMath gives as the value of \(e^{i\pi}+1\) (you might be surprised to see that it's a very simple number!).

(b)

SageMath won't approximate any values unless asked. You can check this by running the following cell.

Modify this cell using sqrt(e) to display \(\sqrt{e}\text{.}\)

(c)

If an approximation is desired, results may be rounded to four decimal places by running code as follows.

Find an approximate value for \(\sqrt{e}\) with five decimal places.

(d)

Use a code cell to decide which is bigger: \(\pi^e\) or \(e^\pi\text{?}\)

Activity 1.2.3.

The term variable means slightly different things in computer science and mathematics. In computer science, variables are used to store and retrieve data within an application. In mathematics, variables are used to represent an arbitrary or unknown value of data within a statement.

These two concepts are related, but not exactly the same. Since SageMath is built upon Python, it provides support for both CS variables and math variables.

(a)

To assign a value to a CS variable, the assignment operator = is used. It's considered best practice to give CS variables semantic, that is, descriptive names such as temperature_in_room, using lowercase letters and underscore _ characters.

Table 1.2.1. Sample pen data
Name Pens kept in backpack
Craig 1
Laura 30
Phillip 18
Sherill 03
Steven 22

For each member of your group, adapt the above code cell to assign a CS variable storing the number of pens (or pencils, or any object you'd like) each person has with them.

(b)

The value stored in a CS variable may be reused later by using the variable name. For example, the following code cell will return the result 74 after the previous cell is run.

Use the below code cell to find the sum of the pens in your group by writing the sum of the variable names you chose. (Later we will learn much more elegant patterns for storing and using lists of data while still using long descriptive variable names as recommended.)

(c)

CS variables can be overwritten. You can go to a previous cell and edit the assignment, but you can also create a new Code cell and reassign the variables value.

Try editing the assignment of Phillip's pens in the Code cell from 1.2.3.a, and try overwriting Laura's pens value by In Code cell below. Re-run both cells.

(d)

Run the Code cell below. Write a sentence explaining why you get a different result even though this cell is exactly the same as a previous cell.

Activity 1.2.4.

We now will explore math variables.

(a)

Unlike CS variables, math variables represent an unknown value or arbitrary value, rather than storing a specific piece of data. These variables are often represented by single letters rather than the long semantic names used for CS variables. For example, SageMath always assumes x is a math variable named \(x\text{.}\)

Before running the below cell, guess what you will see.

(b)

Other math variables are declared using by using the var() tool. For example, the below code cell assigns the CS variables a, b, c to the math variables named \(a,b,c\text{,}\) and then shows one particular expression with the result of adding/multiplying these math variables.

Use the below Code cell to declare \(y\) and \(z\) to be variables, and then show \(y\) raised to the \(z\) power, that is, \(y^z\text{.}\)

(c)

An expression can itself be stored in a CS variable using the assignment operator =; for example, the below cell saves the expression \(x^2-4x+4\) to a CS variable with the name quadratic and then show()s it.

Assign the expression \(-5x^2+4yz\) to a CS variable with the name my_expression, then show() it.

(d)

Math variables and expressions are useful for describing how data should be manipulated before the values of those data are known.

For example, suppose we now want to assume \(x=6,y=0,z=-3\) and compute the corresponding value for the expression \(-5x^2+4yz\) saved as my_expression. You can check that \(-5\times 6^2+4(0)(-3)=-180\text{,}\) either by hand or evaluating -5*6^2+4*0*(-3). But to have SageMath compute this for you immediately, you could run the following cell:

Use a Code cell to substitute the values \(x=1,y=2,z=3\) into the expression \(-5x^2+4yz\) instead, and confirm that the resulting value is equal to \(-5\times 1^2+4(2)(3)\text{.}\)

(e)

SageMath also supports common simplifications of expressions with the full_simplify() tool. For example, show( ((x+1)^3).full_simplify() ) returns the equivalent expression \(x^3+3x^2+3x+1\text{.}\)

Use full_simplify() to find an equivalent expression for \((x+y+z)^2\text{.}\)

Activity 1.2.5.

In addition to storing and manipulating expressions of mathematical variables, SageMath also supports studying equations that set two expressions equal to one another.

(a)

Since the equal sign is already used as the assignment operator =, two equal signs are used to represent the equality operator ==.

When only simple numerical values are present, the equality operator behaves similarly in both Python and SageMath: it evaluates both sides of the equation and returns a Boolean value of True or False. For example, the following code cell verifies that \(3(2)=6\) but \(3^2\not= 6\) since it returns True then False.

Use the equality operator to verify that \(1+2+3+4+5+6=\frac{6\times 7}{2}\text{.}\)

(b)

When math variables or more advanced values are present, the equality operator will return an equation, but won't immediately reveal whether this equation is true or false.

Try show()ing the equation \(x=\sqrt{x^2}\) in the cell below.

(c)

SageMath is able to verify the truth of simple equations by using the bool() tool. For example, the below cell will return False, since \(x\not=\sqrt{x^2}\) when \(-4\) is plugged into \(x\text{.}\)

Use bool() to verify that \(3e^{1-\frac{1}{2}i\pi}\) has the same value as \(-3ei\text{.}\)

(d)

Equations can be manipulated by SageMath similarly to how you manipulate them by hand. For example:

\begin{align*} x+7&=11\\ \scriptsize{-7}&\phantom{=}\scriptsize{-7}\\ x&=4 \end{align*}

This may be verified by running the following Code cell.

Solve the equation \(2x+5=x-7\) by subtracting \(x\) and \(5\) from the equation using a Code cell.

(e)

SageMath can automatically solve many equations for you using the solve() tool. For example, the following code cell reveals the solution \(x=4\) immediately.

Use the solve() tool to solve \(2x+5=x-7\) automatically.

(f)

Use the solve() tool to find both solutions to \(x^2+3x+7=4-x\text{.}\)

Exercises Exercises

1.

Use a SageMath code cell to find the simplified value of \(i^{\ln(e^4)}\text{.}\)

2.

Save the mathematical expression \(\sin(\frac{\pi}{7})\cos(\frac{5\pi}{14})+\sin(\frac{5\pi}{14})\cos(\frac{\pi}{7})\) to a CS variable named fancy_math. Then (since the “trigonometric functions” \(\sin,\cos\) aren't immediately simplified by SageMath) use fancy_math.trig_reduce() to find its exact value.

3.

Are you satisfied with the above calculations? Write a sentence or two explaining why or why not.

4.

Many mathematicians believe (but no one has been able to prove) that the circular constant \(\pi=3.14159\cdots\) is a “normal” number, which means that every finite sequence of digits will appear eventually in its decimal expression. For example, the SageMath code show(round(pi,10)) reveals that \(65\) appears as the 7th and 8th decimal digits of \(\pi\text{:}\) \(3.141592\mathbf{65}36\text{.}\)

Use SageMath to show that \(42\) appears somewhere in the decimal expression of \(\pi\text{.}\)

5.

Use the var() tool to declare the CS variables m and n to represent the math variables \(m\) and \(n\text{.}\) Then have the CS variable quotient_expression represent the mathematical expression \(\frac{m+1}{n^2-3n+2}\text{,}\) and use show() to display quotient_expression with mathematical formatting.

6.

Compute the value of \(\frac{7+1}{(-3)^2-3(-3)+2}\) by substituting \(m=7\) and \(n=-3\) into quotient_expression.

7.

Save the mathematical equation \(x^3+1=(x+1)(x^2-x+1)\) to the CS variable sum_of_cubes. Use the bool() tool to verify that sum_of_cubes represents a valid mathematical identity.

8.

Show how to solve the equation \(17-2x=3x+2\) by hand. Then show how to use SageMath to solve it automatically.