Tutorial: using the notebook and navigating the help system (long version)

<span id="tutorial-notebook-and-help-long"></span>


<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>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
}}}

{{{id=1|

///
}}}

{{{id=2|

///
}}}

<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|

///
}}}

<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 details in what follows the two last methods through examples.</p>


<h1>Completion and contextual domcumentation</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
<strong>klein</strong> won't show you the <strong>KleinFourGroup</strong> command that build the group
$\mathbb{Z}/2 \times \mathbb{Z}/2$ as a permutation group. Try it on the next cells</p>

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

{{{id=5|
Klein
///
}}}

<p>To see documentation and examples for a command, type a question mark <strong>?</strong> at
the end of the command name and press the <tt class="docutils literal">tab</tt> key as</p>
<pre class="literal-block">
KleinFourGroup?&lt;tab&gt;

</pre>
{{{id=6|

///
}}}

<p><em>Exercise A:</em> What is the largest prime factor of $600851475143$?</p>
<pre class="literal-block">
factor?&lt;tab&gt;

</pre>
{{{id=7|

///
}}}

<p>In the above exercise we do not affect any mathematical data to variables. The
affectation in Sage is realizer through the <tt class="docutils literal">=</tt> symbol as in:</p>

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

<p>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=9|
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=10|

///
}}}

{{{id=11|

///
}}}

<p><em>Exercise B:</em> Create the <strong>Permutation</strong> 51324 and assign it to the variable <strong>p</strong>.</p>
<pre class="literal-block">
Permutation?&lt;tab&gt;

</pre>
{{{id=12|

///
}}}

<p>What is the <strong>inverse</strong> of <strong>p</strong> ?</p>
<pre class="literal-block">
p.inv&lt;tab&gt;

</pre>
{{{id=13|

///
}}}

<p>Does <strong>p</strong> have the <strong>pattern</strong> 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>
<pre class="literal-block">
p.pat&lt;tab&gt;

</pre>
{{{id=14|

///
}}}

<h1>Some linear algebra</h1>
<p><em>Exercise C:</em> Use the <strong>matrix</strong> command to create the following matrix.</p>
<blockquote>
$M = \left(\begin{array}{rrrr}
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}\right)$</blockquote>
<pre class="literal-block">
matrix?&lt;tab&gt;

</pre>
{{{id=15|

///
}}}

<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=16|

///
}}}

{{{id=17|

///
}}}

<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=18|
vector?&lt;tab&gt;
///
}}}


{{{id=19|

///
}}}

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



<h1>Some Plotting</h1>
<p>The <strong>plot</strong> 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=20|
plot?&lt;tab&gt;
///
}}}


{{{id=21|

///
}}}

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

{{{id=22|
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=23|
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 <strong>show</strong> command to show a plot after it was created. You can
also use <strong>P.show</strong> instead:</p>

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

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

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

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

{{{id=26|
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=27|
f(x) = x^4 - 8*x^2 - 3*x + 2
f(x)
///
x^4 - 8*x^2 - 3*x + 2
}}}

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

<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 <strong>find_root</strong> method to numerically
approximate the root of $f$ on the interval $[1,2]$.:</p>

{{{id=29|

///
}}}

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

{{{id=30|

///
}}}

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

{{{id=31|

///
}}}

<p><em>Exercise E (Advanced):</em>  Solve the following equation for $y$</p>
<blockquote>
$y = 1 + x y^2$</blockquote>
<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=32|

///
}}}

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

{{{id=33|

///
}}}

<p>Do you recognize the coefficients of the Taylor series expansion? You might want
to use, or better yet, Sage's command <strong>sloane_find</strong> which uses the online
encyclopedia of integers:</p>
<pre class="literal-block">
sloane_find?&lt;tab&gt;

</pre>
{{{id=34|

///
}}}

{{{id=35|

///
}}}

