(The beginning of this worksheet is based on William Stein's JPL09__intro_to_sage.sws worksheet.)

Sage Days 25.5 - Tutorial

{{{id=4| /// }}}

Entering, Editing, and Evaluating Input

To evaluate code in the Sage Notebook type the code into an input cell and press shift-enter or click the evaluate link.  Try it now with a simple expression (e.g., 2 + 2).   The first time you evaluate cell takes longer than subsequent times since a process starts.

{{{id=2| /// }}} {{{id=7| /// }}}

Create new input cells by clicking on the blue line that appears between cells when you move your mouse around.  Try it now.

{{{id=10| /// }}} {{{id=57| /// }}} {{{id=12| /// }}}

You can go back and edit any cell by clicking in it (or using the keyboard to move up or down).  Go back and change your 2+2 above to 3 + 3 and re-evaluate it.

{{{id=9| /// }}} {{{id=8| /// }}}

You can also edit this text right here by double clicking on it, which will bring up the TinyMCE Javascript text editor.  You can even put embedded mathematics like this $\sin(x) - y^3$ just like with LaTeX.

{{{id=54| /// }}}

$$\int e^x dx = e^x + c$$

{{{id=75| /// }}} {{{id=17| /// }}}

You can also easily make interactive widgets as illustrated below.  Try clicking on the sliders to illustrate multiplication below.   Also, you can try changing the slider ranges to something different by editing the input cell (make sure to also change xmax,ymax).

{{{id=50| @interact def f(n=(1..15), m=(1..15)): print "n * m =", n*m, " =", factor(n*m) P = polygon([(0,0),(0,n),(m,n),(m,0)]) P.show(aspect_ratio=1, gridlines='minor',figsize=[3,3],xmax=14,ymax=14) /// }}} {{{id=146| /// }}} {{{id=74| /// }}}

If you mess everything up, click on Action -> Restart Worksheet at the top of the screen to reset all the variable names and restart everything.  You can also click "Undo" in the upper right to revert the worksheet to a previously saved state.

{{{id=51| /// }}} {{{id=16| /// }}}

Click the Log link at the top of this page to view a log of recent computations!

{{{id=56| /// }}} {{{id=55| /// }}}

How to Get Context-Sensitive Help and Search the Documentation

You find out what functions you can call on an object X by typing X.<tab key>.

{{{id=33| X = 2009 /// }}}

Type X. then press the tab key.

{{{id=28| /// }}} {{{id=40| /// }}}

Once you have selected a function, say factor, type X.factor(<tab key> or X.factor?<tab key> to get help and examples of how to use that function.  Try this now with X.factor.

{{{id=39| /// }}} {{{id=62| /// }}} {{{id=61| /// }}} {{{id=60| /// }}}

To get full-text searchable help and a more extensive tutorial, click the Help link in the upper right of this page, then click on Fast Static Versions of the Documentation.

{{{id=53| /// }}} {{{id=42| /// }}}

If you need live help from a person, "operators are standing by".  Just click on Help above, then click on "Help via Internet Chat (IRC)". This brings you to the Sage chat room where you can often get help.

{{{id=70| /// }}} {{{id=71| /// }}}

Exercises

Exercise A: What is the largest prime factor of 600851475143?

{{{id=46| /// }}} {{{id=24| /// }}}

Exercise B: Create the permutation $$\begin{pmatrix}1 & 2 & 3 & 4 & 5 \\ 5 & 1 & 3 & 2 & 4\end{pmatrix}$$ and assign it to the variable p.

  1. What is the inverse of p ?
  2. Does p have the pattern 123 ? What about 1234 ? And 312 ?
{{{id=144| /// }}} {{{id=31| /// }}}

Exercise C: Use the matrix command to create the following matrix.$$\begin{pmatrix}10 & 4 & 1 & 1 \\4 & 6 & 5 & 1 \\1 & 5 & 6 & 4 \\1 & 1 & 4 & 10\end{pmatrix}$$

  1. Find the determinant of the matrix.
  2. Find the echelon form of the matrix.
  3. Find the eigenvalues of the matrix.
  4. Find the kernel of the matrix.
  5. Find the LLL decomposition of the matrix.
{{{id=49| /// }}} {{{id=130| /// }}}

Exercise D:

Here is an example of a symbolic function.

{{{id=48| f(x) = x^4 - 4*x^3 - 3*x^2 + 10*x + 8 /// }}}
  1. Define the symbolic function $f(x) = x \sin(x^2)$.
  2. plot $f$ on the domain $[-3,3]$ and colour it red.
  3. Use the find_roots method to numerically approximate the root of $f$ on the interval $[1,3]$.
  4. Compute the tangent line to $f$ at $x=1$.
  5. Plot $f$ and the tangent line to $f$ at $x=1$ in one image.
{{{id=100| /// }}} {{{id=94| /// }}}

Exercise E (Advanced):

  1. Solve the following equation for $y$ $$y = 1 + x y^2$$ There are two solutions, take the one for which $y(0) = 1$. (Don't forget to create the variables $x$ and $y$! Do this using the command var('x,y'))
  2. Expand $y$ as a truncated Taylor series around $0$ and containing $n=10$ terms.
  3. Guess the coefficients of the Taylor series expansion (hint: Sage includes a command called sloane_find).
{{{id=93| /// }}} {{{id=135| /// }}} {{{id=85| /// }}}

First Steps Towards Programming

Defining a function

To define a function in Sage, use the def command (and a colon after the variable names!). For example:

{{{id=140| def square(x): return x^2 /// }}}

Notice that body of the function (the line return x^2) is indented. Indentation defines the body of the function!

Conditional statements

Below is an outline of an if statement.

if <conditional_expression>:
<code_block>
else:
<code_block>

Notice how the first and second code blocks of the statement are indented.

{{{id=82| def is_odd(x): if x % 2 == 1: return True else: return False /// }}}

loops: the for statement

Here is an example of a for loop. It computes the sum of the odd integers between 1 and 99 (not including 100).

{{{id=80| s = 0 for i in range(1,100): if is_odd(i): s += i print s /// }}} {{{id=79| /// }}} {{{id=78| /// }}}

Exercises

Exercise F. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

{{{id=77| /// }}} {{{id=76| /// }}}

Exercise G. The Syracuse function is defined on positive integers $n$ as follows:

$$\text{syracuse(n)} = \begin{cases}\frac{n}{2}, & \text{if } n \text{ is even} \\ 3n+1, & \text{if } n \text{ is odd}\end{cases}$$

For example, syracuse(6) = 3 because 6 is even; and syracuse(11) = 34 because 11 is odd.

Implement the Syracuse function.

{{{id=110| /// }}} {{{id=123| /// }}}

Exercise H: If we start with 6 and apply the Syracuse function, then we get 3. If we now apply the Syracuse function to 3, then we get 10. Applying the function to 10 outputs 5. Continuing in this way, we get $$6, 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, 4, 2, 1, \ldots$$ Notice that this sequence has entered the loop $4 \mapsto 2 \mapsto 1 \mapsto 4$. The conjecture is that this always happens.

The $3n+1$ conjecture: For every $n$, the resulting sequence will always reach 1.

Write a function that takes a positive integer and prints the terms in the sequence until it reaches 1. For example, beginning with 6, your function will print

    6
    3
    10
    5
    16
    8
    4
    2
    1

You will find a while loop helpful here. Below is a simple example:

x = 0
while x < 7:
    x = x + 2
    print x

{{{id=122| /// }}} {{{id=143| /// }}} {{{id=142| /// }}}