Intro to Sage on Jupyter

To evaluate the below equation. Put your cursor into the cell and press Shift+Enter.

In [1]:
2 + 3 + 6^2 + sin(2)
Out[1]:
sin(2) + 41

Try again.

In [2]:
r = 2 + 3 + 6^2 + sin(2)

This seems to do nothing, but it has assigned this value to the variable $r$. We can see it by printing $r$.

In [3]:
r
Out[3]:
sin(2) + 41

But maybe we want a decimal expression for it. The variable $r$ is now a real number. There are functions we can apply on real numbers. Many of them are written in the form $\sin(r)$ others are written in the form $r$.show(). To get a decimal approximation of a real we use the .n() function.

In [4]:
r.n()
Out[4]:
41.9092974268257

Often I like to see the results when I define a variable. The semicolon allows multiple commands at once.

In [5]:
n = factorial(7); n
Out[5]:
5040

To write a nice comment like this, open a new evaluation cell change its type to markdown. This tells sage to interpret it as markdown. When you are finished, type "Shift+Enter" to leave. To edit it, double click on it. You can use html and you can use $\LaTeX{}$ to prettify your math: $\phi(x) = \frac{1}{\ln{x}}$.

Sage has all sorts of build in functions.

In [6]:
2^6
Out[6]:
64
In [7]:
factor(60)
Out[7]:
2^2 * 3 * 5
In [8]:
prime_factors(60)
Out[8]:
[2, 3, 5]
In [9]:
type(n)
Out[9]:
<type 'sage.rings.integer.Integer'>
In [10]:
n.binary()
Out[10]:
'1001110110000'

See what else you can do with an integer. Go up to the above cell and erase upto the n. then press 'Tab'. YOu will get a list of possible functions. If you try to use one like n.binary without the () you will get an error. Some of them, like n.binomial need an argument: n.binomial(2) gives you n choose 2.

In [11]:
n.prime_factors()
Out[11]:
[2, 3, 5, 7]
In [12]:
pf = n.prime_factors(); pf
Out[12]:
[2, 3, 5, 7]
In [13]:
pf.index(5)
Out[13]:
2
In [14]:
pf.append('a'); pf
Out[14]:
[2, 3, 5, 7, 'a']
In [15]:
pf.count(3)
Out[15]:
1
In [16]:
pf.append(3); pf
Out[16]:
[2, 3, 5, 7, 'a', 3]
In [17]:
pf.count(3)
Out[17]:
2
In [18]:
pf.index(3)
Out[18]:
1
In [19]:
pf[2]
Out[19]:
5