Workshop: Getting Started with Sage Robert A. Beezer University of Puget Sound Kansas City Regional Mathematics Technology Expo October 6, 2012
Account Creation, Public Servers

1. Need an OpenID account (GMail, Yahoo!, etc.)
2. Passwords/Security is OK, but not super-robust.
3. "New Worksheet" link/button in upper-left.
4. Retitle to anything you want.

Making a Plot

1. Click inside a cell - border changes, "evaluate" button appears
2. Type: ` plot(x^3, (x, -2, 2)) `
3. Hit "evaluate"
4. Want a new cell? Hover until you see a blue bar and click on it.
5. Congratulations: You can now use Sage from any computer with a connection and browser!

Algebra and Calculus Expressions versus Functions

Sage has variables + symbolic expressions AND functions. They look the same, but are different.

The variable `x` is predefined, everything else must be declared.

var('a b') expand((2*a+b)^4) expression = 91125*a^9 + 346275*a^8*b - 95985*a^7*b^2 - 1849527*a^6*b^3 - 1535976*a^5*b^4 + 2893680*a^4*b^5 + 4382720*a^3*b^6 - 456960*a^2*b^7 - 3311616*a*b^8 - 1404928*b^9 factor(expression)
Equation Solving slns = solve(2*x^2 + 9*x - 35 == 0, x) slns slns

We want the right hand side.

slns.rhs()

An alternative:

solve(2*x^2 + 9*x - 35 == 0, x, solution_dict=True)

How would we know about this alternative? Immediate help on `solve()`. (Question-mark, then the TAB key.)

solve?
Functions and Calculus

Notice how a function prints differently than an expression.

f(x) = e^-x - x f

Evaluation (exact, approximate), graphing, differentiation, integration. Note object-oriented syntax.

f(4) f(4).n(digits=15) f.plot(f, -2, 2) f.diff() f.integral(x)

Question: How would we learn about these methods?
Answer: "Tab-completion" on `f`. f-dot-tab

f.
Linear Algebra

Suppose you know nothing about how to do linear algebra in Sage. (Maybe this is not a difficult supposition!)

If you can make a matrix and a vector, then you are off and running. Here they are, with entries from the field of rational numbers.

A = matrix(QQ, [[1,2,3], [4,5,6]]) A v = vector(QQ, [7, 8, 9])

Then tab-completion and ?-tab-help will get you up to speed quickly.

A. v.

And the Sage Quick Reference sheets ("quickrefs") are very handy.

Getting Help

There is lots of documentation. The "Reference Manual" is about 4000 pages as a PDF and not organized for a beginner. The "Tutorial" has some fairly advanced mathematics in it. The "PREP Tutorials" are new and a great way to get started.

• Help link in upper-right corner gives a pop-up window
• Tutorial, Thematic Tutorials, Constructions, Reference Manual
• Highly recommended: MAA PREP Workshop Tutorials

Moving Worksheets Around

• File drop-down box - Save worksheet to a file... makes a `*.sws` file
• Upload link/button - provide URL or Browse to local file to pull in a worksheet
• Publish on a server
• Share on a server
• Warning: accounts do not span multiple servers
• Students can share to a single-purpose class account to submit work

Upload Now: `http://buzzard.ups.edu/kcexpo.sws`

An Exercise

Try the following exercise, with minimal guidance.

Theorem (from Algebraic Graph Theory):
The number of walks of length \(k\), between vertices \(u\) and \(v\) of a graph, is the \((u,v)\) entry of \(A^k\), where \(A\) is the adjacency matrix of the graph.

Question: How many walks are there of length \(7\) between two vertices of the complete bipartite graph \(K_{6,9}\) that lie in different halves of the bipartition?

1. Build the complete bipartite graph \(K_{6,9}\), whose edges join every vertex of a \(6\)-set to every vertex of a \(9\)-set. Save this graph as the variable `G`. Plot a picture of the graph. (Hint: try out `]]>`)

2. Construct and save the adjacency matrix of the graph as the variable `A`.

3. Construct and save the seventh power of the adjacency matrix of the graph as the variable `B`.

4. Output the entry of `B` that corresponds to two vertices from different halves of the bipartition. (Hint: use your picture to decide which vertices to use. Matrix indexing starts counting from `0` and brackets, (`[, ]`), are used to index into a matrix.)

Did you get \(6^3\,9^3 = 157\,464\) walks of length \(7\)? How about walks of length \(8\) between the same two vertices?

Extra credit: write a one-line statement to get the answer.

Extra, extra credit: write a Python function that accepts a graph \(G\), and values of \(k\), \(u\), \(v\) as input and returns the number of walks.