Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
SageMath is similar to Mathematica, Matlab, Maple, or other mathematical software programs. It simplifies numerical and algebraic operations. Each worksheet (this page is a worksheet) is broken into cells. The two types of cells are input cells and output cells. You type your calculations into into an input cell and then hit Shift+Enter to evaluate the cell. SageMath provides the answers to your calculations in an output cell. Click on the input cell below and hit Shift+Enter to evaluate the calculations.
Evaluate a cell
Place the cursor in the input cell and hit
Shift+Enter
to tell SageMath to evaluate a cell.
Notice how , , and are left as ratios and not converted to decimals. This is because SageMath leaves results in their exact form and does not convert the results to decimal-form unless you tell it to. To evaluate and get the answer to three significant figures you would use the numerical approximation command n(1/3,digits=3)
.
Get a numerically approximate answer and specify the number of significant figures
Use
n()
to get an approximate answerPlace the equation to evaluate inside the parentheses
Include
digits=5
to get a numerical result to five significant figuresDon't forget to put a comma between your equation and
digits=5
Try it yourself
Evaluate the following to three significant figures
SageMath comes with units built in. The units are created by calling the units
method but I find it is easiest to define units in terms of the standard abbreviations.
The standard SI units are the following:
Length: meter (abbreviated as m)
Time: second (abbreviated as s)
Mass: kilogram (abbreviated as kg)
Force: Newton (abbreviated as N)
The customary U.S. units of measure are:
Length: foot (ft)
Time: second (s)
Mass: slug (slug)
Force: pound (lb)
An easy way to convert units is to define new units in terms of the original units. For instance, if your results are given in centimeters you could add the code cm=m/100
. Then typing in an answer in cm
it would be converted into m
.
Defining units
Some of the units defined in SageMath include:
units.length.meter
units.length.centimeter
units.length.millimeter
units.length.micron
units.length.kilometer
units.length.foot
units.length.inch
units.length.mile
units.length.yard
A complete list of all units can be found in the units.py file.
Try It Yourself
Evaluate the following to four significant figures and convert all answers to standard SI units (m, s, N, kg)
Remember that:
micro
milli
Mega
Unit conversion can be accomplished using the .substitute()
command. You can tell SageMath to substitute one variable in for another. For example, to convert from meters to feet, substitute in m=3.28084*ft
and evaluate it.
Substitute one variable for another
The
.substitute()
command is added to the end of a variableTo replace
ft
with0.3048*m
in the variable calledd
, you would typed.substitute(ft=0.3048*m)
The arguments in the parentheses sets the orignal variable equal to the new value
You can specify several substitutions at once by separating substitutions with commas
To replace
ft
with0.3048*m
andns
withs*10^(-9)
in the variablev
you would typev.substitute(ft=0.3048*m, ns=s*10^(-9))
This isn't just for unit conversion, this works for any two variables in a function
To replace
y
with3*x-5
in functionf
you would typef.substitute(y=3*x-5)
There is a convert feature built in to SageMath but I find substitution more intuitive
Try It Yourself
Calculate the following quantities using the correct number of signficant figures:
Hint: Units of time include:
units.time.second
units.time.minute
units.time.hour
You can define functions in SageMath by giving the function a name and specifying the arguments of the function in parentheses after the function name. For example, to define a function you might type f(x)=3*x^2+2*x-1
. You can also have functions of multiple variables such as h(x,y,z)=3*x*y-z^3
Defining Functions
Function names follow same convention as variables; Must start with a letter, can contain numbers, can't contain spaces (only underscores
_
), and no special charactersFormat is
function_name(variable1,variable2,)= equation
such asg(a,b)=2*a+3*b
Functions can be evaluated at specific points by entering numbers in for the arguments such as
g(1,2)