Tutorial: Using the Sage notebook, navigating the help system, first exercises
system:sage


<h1 class="title">Tutorial: Using the Sage notebook, navigating the help system, first exercises</h1>

<!-- escape-backslashes -->
<p>This worksheet is based on William Stein's <a class="reference external" href="http://modular.math.washington.edu/talks/20090701-sage_graphics_tutorial/JPL09___intro_to_sage.sws">JPL09__intro_to_sage.sws</a>
worksheet and the <a class="reference external" href="http://wiki.sagemath.org/days20.5">Sage days 20.5_demo</a>
worksheet and aims to be an interactive introduction to Sage through exercises. You will learn how to use the notebook and call the help.</p>

<h1>Making this help page into a worksheet</h1>
<p>If you are browsing this on a static web page, use the Upload
worksheet button of the notebook, and copy-paste the URL of this page.</p>
<p>Go into the <tt class="docutils literal">File</tt> menu at the top left of this window and click on <tt class="docutils literal">Copy
worksheet</tt>. Then, you can clear all output in the menu <tt class="docutils literal">Action</tt> by clicking
on <tt class="docutils literal">Delete All Output</tt>.</p>


<h1>Entering, Editing and Evaluating Input</h1>
<p>To <em>evaluate code</em> in the Sage Notebook, type the code into an input cell and
press <tt class="docutils literal"><span class="pre">shift-enter</span></tt> or click the <tt class="docutils literal">evaluate</tt> link. Try it now with a simple
expression (e.g.,  $2 + 3$). The first time you evaluate a cell takes longer
than subsequent times since a new Sage process is started:</p>

{{{id=0|
2 + 3
///
5
}}}

<!-- end of output -->

{{{id=1|
# edit here
///
}}}

<!-- end of output -->

{{{id=2|
# edit here
///
}}}

<!-- end of output -->
<p>Create <em>new input cells</em> by clicking blue line that appears between cells when
you move your mouse around. Try it now:</p>

{{{id=3|
1 + 1
///
}}}

{{{id=4|
# edit here
///
}}}

<p>You can <em>go back</em> 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.</p>
<p>You can also <em>edit this text</em> 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.</p>


<h1>Help systems</h1>
<p>There are various ways of getting help in Sage.</p>
<ul class="simple">
<li>navigate through the documentation (there is a link <tt class="docutils literal">Help</tt> at the top right
of the worksheet),</li>
<li><tt class="docutils literal">tab</tt> completion,</li>
<li>contextual help.</li>
</ul>
<p>We detail in what follows the two last methods through examples.</p>


<h1>Completion and contextual documentation</h1>
<p>Start typing something and press the <tt class="docutils literal">tab</tt> key. The interface tries to
complete it with a command name. If there is more than one completion, then they
are all presented to you. Remember that Sage is case sensitive, e.g. it
differentiates lower case from lower case. Hence the <tt class="docutils literal">tab</tt> completion of
<tt class="docutils literal">klein</tt> won't show you the <tt class="docutils literal">KleinFourGroup</tt> command that build the group
$\ZZ/2 \times \ZZ/2$ as a permutation group. Try it on the next cells</p>

{{{id=5|
klein
///
}}}

{{{id=6|
Klein
///
}}}

<p>To see documentation and examples for a command, type a question mark <tt class="docutils literal">?</tt> at
the end of the command name and press the <tt class="docutils literal">tab</tt> key as</p>

{{{id=7|
KleinFourGroup?&lt;tab&gt;
///
}}}

{{{id=8|
# edit here
///
}}}

<p><em>Exercise A:</em> What is the largest prime factor of $600851475143$?</p>

{{{id=9|
factor?&lt;tab&gt;
///
}}}

{{{id=10|
# edit here
///
}}}

<p>In the above exercise we do not store any mathematical data for later use.
This can be done in Sage with the <tt class="docutils literal">=</tt> symbol as in:</p>

{{{id=11|
a = 3
b = 2
print a+b
///
5
}}}

<p>This can be understood as Sage evaluating the expression to the right of the
<tt class="docutils literal">=</tt> sign and creating the appropriate object, and then associating that
object with a label, given by the right hand side. Multiple assignments can be
done simultaneously:</p>

{{{id=12|
a,b = 2,3
print a,b
///
2 3
}}}

<p>This allows us to swap variables directly:</p>

{{{id=13|
a,b = 2,3
a,b = b,a
print a,b
///
3 2
}}}

<p>Note that when we use the word <em>variable</em> in the computer-science sense we
mean &quot;a label associated to some data stored by Sage&quot;.  Once an object is
created, some <em>methods</em> apply to it. This means <em>functions</em> but instead of
writing <strong>f(my_object)</strong> you write <strong>my_object.f()</strong>.:</p>

{{{id=14|
p = 17
p.is_prime()
///
True
}}}

<p>To know all methods of an object you can still use tab-completion. Write the
name of the object followed by a dot and then press tab.:</p>
<pre class="literal-block">
a.&lt;tab&gt;

</pre>

{{{id=15|
# edit here
///
}}}

{{{id=16|
# edit here
///
}}}

<p><em>Exercise B:</em> Create the <tt class="docutils literal">Permutation</tt> 51324 and assign it to the variable <tt class="docutils literal">p</tt>.</p>

{{{id=17|
Permutation?&lt;tab&gt;
///
}}}

{{{id=18|
# edit here
///
}}}

<p>What is the <tt class="docutils literal">inverse</tt> of <tt class="docutils literal">p</tt> ?</p>

{{{id=19|
p.inv&lt;tab&gt;
///
}}}

{{{id=20|
# edit here
///
}}}

<p>Does <tt class="docutils literal">p</tt> have the <tt class="docutils literal">pattern</tt> 123 ? What about 1234 ? And 312 ? (even if you don't
know what a pattern is, you should be able to find a command that does this).</p>

{{{id=21|
p.pat&lt;tab&gt;
///
}}}

{{{id=22|
# edit here
///
}}}

<h1>Some linear algebra</h1>
<p><em>Exercise C:</em> Use the <tt class="docutils literal">matrix</tt> command to create the following matrix.</p>
<pre class="literal-block">\[
    M = \left(\begin{array}{rrrr}&nbsp;
    10 &amp; 4 &amp; 1 &amp; 1 \\
    4 &amp; 6 &amp; 5 &amp; 1 \\
    1 &amp; 5 &amp; 6 &amp; 4 \\
    1 &amp; 1 &amp; 4 &amp; 10
    \end{array}&nbsp;\right)
\]


</pre>

{{{id=23|
matrix?&lt;tab&gt;
///
}}}

{{{id=24|
# edit here
///
}}}

<p>Then using methods of the matrix:</p>
<ol class="arabic simple">
<li>Find the determinant of the matrix.</li>
<li>Find the echelon form of the matrix.</li>
<li>Find the eigenvalues of the matrix.</li>
<li>Find the kernel of the matrix.</li>
<li>Find the LLL decomposition of the matrix.</li>
</ol>

{{{id=25|
# edit here
///
}}}

{{{id=26|
# edit here
///
}}}

<p>Now you know how to access the different methods of matrices:</p>
<ol class="arabic simple" start="6">
<li>Create the vector $v = (1,-1,-1,1)$.</li>
<li>Compute the products: $M*v$ and $v*M$.</li>
</ol>

{{{id=27|
vector?&lt;tab&gt;
///
}}}

{{{id=28|
# edit here
///
}}}

<p class="first admonition-title">Note</p>
<p class="last">Vectors in Sage are row vectors. A method such as <tt class="docutils literal">eigenspaces</tt> might not
return what you expect, so it is best to specify <tt class="docutils literal">eigenspaces_left</tt> or
<tt class="docutils literal">eigenspaces_right</tt> instead. Same thing for kernel (<tt class="docutils literal">left_kernel</tt> or
<tt class="docutils literal">right_kernel</tt>), and so on.</p>



<h1>Some Plotting</h1>
<p>The <tt class="docutils literal">plot</tt> command allows you to draw plots of functions. Recall that you can
access the documentation by pressing the <tt class="docutils literal">tab</tt> key after writing <tt class="docutils literal">plot?</tt> in
a cell.:</p>

{{{id=29|
plot?&lt;tab&gt;
///
}}}

{{{id=30|
# edit here
///
}}}

<p>Here is a simple example:</p>

{{{id=31|
var('x')   # make sure x is a symbolic variable
plot(sin(x^2), (x,0,10))
///
}}}

<p>Here is a more complicated plot. Try to change every single input to the plot
command in some way, evaluating to see what happens:</p>

{{{id=32|
P = plot(sin(x^2), (x,-2,2), rgbcolor=(0.8,0,0.2), thickness=3, linestyle='--', fill='axis')
show(P, gridlines=True)
///
}}}

<p>Above we used the <tt class="docutils literal">show</tt> command to show a plot after it was created. You can
also use <tt class="docutils literal">P.show</tt> instead:</p>

{{{id=33|
P.show(gridlines=True)
///
}}}

<p>Try putting the cursor right after <tt class="docutils literal">P.show(</tt> and pressing tab to get a list of
the options for how you can change the values of the given inputs.:</p>

{{{id=34|
P.show(
///
}}}

<p>Plotting multiple functions at once is as easy as adding them together:</p>

{{{id=35|
P1 = plot(sin(x), (x,0,2*pi))
P2 = plot(cos(x), (x,0,2*pi), rgbcolor='red')
P1 + P2
///
}}}

<h1>Symbolic Expressions</h1>
<p>Here is an example of a symbolic function:</p>

{{{id=36|
f(x) = x^4 - 8*x^2 - 3*x + 2
f(x)
///
x^4 - 8*x^2 - 3*x + 2
}}}

{{{id=37|
f(-3)
///
20
}}}

<p>This is an example of a function in the <em>mathematical</em> variable $x$. When Sage
starts, it defines the symbol $x$ to be a mathematical variable. If you want
to use other symbols for variables, you must define them first.:</p>

{{{id=38|
x^2
///
x^2
}}}

{{{id=39|
u + v
///
Traceback (click to the left of this block for traceback)
NameError: name 'u' is not defined
}}}

{{{id=40|
var('u v')
u + v
///
u + v
}}}

<p>It is possible, though, to define symbolic functions without first defining
the variables.:</p>

{{{id=41|
f(w) = w^2
f(3)
///
9
}}}

<p><em>Exercise D:</em> Define the symbolic function $f(x) = x \sin(x^2)$. Plot $f$ on the
domain $[-3,3]$ and colour it red. Use the <tt class="docutils literal">find_root</tt> method to numerically
approximate the root of $f$ on the interval $[1,2]$:</p>

{{{id=42|
# edit here
///
}}}

<p>Compute the tangent line to $f$ at $x=1$:</p>

{{{id=43|
# edit here
///
}}}

<p>Plot $f$ and the tangent line to $f$ at $x=1$ in one image:</p>

{{{id=44|
# edit here
///
}}}

<p><em>Exercise E (Advanced):</em> Solve the following equation for $y$</p>
<pre class="literal-block"><span style="font-family: 'Times New Roman';"><span style="white-space: normal;">\[&nbsp;</span></span>y = 1 + x y^2 \]
</pre>
<p>There are two solutions, take the one for which $\lim_{x\to0}y(x) = 1$. (Don't forget to create the variables $x$ and $y$!).:</p>

{{{id=45|
# edit here
///
}}}

<p>Expand $y$ as a truncated Taylor series around $0$ and containing $n=10$
terms.</p>

{{{id=46|
# edit here
///
}}}

<p>Do you recognize the coefficients of the Taylor series expansion? You might
want to use the <a class="reference external" href="http://oeis.org">On-Line Encyclopedia of Integer Sequences</a>,
or better yet, Sage's command <tt class="docutils literal">sloane_find</tt> which queries the encyclopedia:</p>
<pre class="literal-block">
sloane_find?&lt;tab&gt;

</pre>

{{{id=47|
# edit here
///
}}}