3 May 2010

Introduction to Sage

 

Navigating the help system

There are various ways of getting help in Sage.

Reference Manual

Click the link Help at the top-right of this page. This will bring up documentation for the notebook interface, as well as links to the reference manual.

{{{id=175| /// }}} {{{id=1| /// }}}

Tab completion

Start typing something and press the TAB 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.

{{{id=174| /// }}} {{{id=55| /// }}}

? : documentation and examples

To see documentation and examples for a command, type ? at then end of the command name and press the TAB key.

{{{id=11| /// }}} {{{id=154| /// }}}

Exercises

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

{{{id=46| /// }}} {{{id=44| /// }}}

Exercise B: Create the Permutation 51324 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 ? (even if you don't know what a pattern is, you should be able to find a command that does this).
{{{id=61| /// }}} {{{id=51| /// }}}

Exercise C: Use the matrix command to create the following matrix.

$$M = \left(\begin{array}{rrrr}
10 & 4 & 1 & 1 \\
4 & 6 & 5 & 1 \\
1 & 5 & 6 & 4 \\
1 & 1 & 4 & 10
\end{array}\right)$$

  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.
  6. Create the vector $v = (1,-1,-1,1)$.
  7. Compute the products: $M*v$ and $v*M$.

NOTE: Vectors in Sage are row vectors. A command such as eigenspaces might not return what you expect, so it is best to specify eigenspaces_left or eigenspaces_right instead. Same thing for kernel (left_kernel or right_kernel), and so on.

{{{id=49| /// }}} {{{id=65| /// }}}

Some Plotting

The plot command allows you to draw plots of functions. Type plot(<tab key> for the documentation (or look in the reference manual). Here is a simple example.

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

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

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

Above we used the show command to show a plot after it was created. Try putting the cursor right after P.show( and pressing tab to get a list of the options for how you can change the values of the given inputs.

{{{id=156| /// }}} {{{id=70| /// }}}

Plotting multiple functions at once is as easy as adding them together.

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

Symbolic Expressions

Here is an example of a symbolic function.

{{{id=160| f(x) = x^4 - 8*x^2 - 3*x + 2 /// }}} {{{id=172| f(x) /// x^4 - 8*x^2 - 3*x + 2 }}} {{{id=158| f(-3) /// 20 }}} {{{id=173| /// }}}

Exercises

Exercise D:

  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_root method to numerically approximate the root of $f$ on the interval $[1,2]$.
  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 $\lim_{x\to0}y(x) = 1$. (Don't forget to create the variables $x$ and $y$!)
  2. Expand $y$ as a truncated Taylor series around $0$ and containing $n=10$ terms.
  3. Do you recognize the coefficients of the Taylor series expansion? You might want to use  Sloane's Online Encyclopedia of Integer Sequences, or better yet, Sage's command sloane_find
{{{id=147| /// }}} {{{id=146| /// }}}