This is an introduction to the basic functionality of Sage, with an emphasis on seeing how to handle a worksheet and how to get more detailed help. We will point out a little bit of the power that Sage has hiding in it, but this is **not** a full-fledged tutorial. Down at the bottom of the page you will find links to some more resources.

Note that this worksheet is based upon an earlier one by Theron Hitchman. That worksheet, once available here, was designed for the Sage Notebook interface. This one is written with the SageMathCloud worksheet style in mind. Other contributors include: Harald Schilly, Gustav Delius.

\nThis is an introduction to the basic functionality of Sage, with an emphasis on seeing how to handle a worksheet and how to get more detailed help. We will point out a little bit of the power that Sage has hiding in it, but this is **not** a full-fledged tutorial. Down at the bottom of the page you will find links to some more resources.\n

\n\nNote that this worksheet is based upon an earlier one by Theron Hitchman. That worksheet, once available here, was designed for the Sage Notebook interface. This one is written with the SageMathCloud worksheet style in mind. Other contributors include: Harald Schilly, Gustav Delius.\n\n

\n\n

Note that the output is marked by an orange bar on the left near the line numbers, and the inputs are separated by grey horizontal rulings. Evaluating the cell will turn the farthest righthand portion of that ruling to blue. While the computation is going (and your worksheet is waiting for a response from the server) the middle portion of the grey ruling will flash green. Also, the active cell has a light blue background.

Another way to evaluate a cell is to use the "run" button at the top of the worksheet. Depending on the width of your display, this button may or may not have the word "run" on it. If not, just look for the triangle pointing to the right.

Try that version now with the simple computation below.

︡776d3f25-bc21-4cf0-8093-ebe9b0b21b60︡{"html":"

\nNote that the output is marked by an orange bar on the left near the line numbers, and the inputs are separated by grey horizontal rulings. Evaluating the cell will turn the farthest righthand portion of that ruling to blue. While the computation is going (and your worksheet is waiting for a response from the server) the middle portion of the grey ruling will flash green. Also, the active cell has a light blue background.\n

\n\nAnother way to evaluate a cell is to use the \"run\" button at the top of the worksheet. Depending on the width of your display, this button may or may not have the word \"run\" on it. If not, just look for the triangle pointing to the right.\n

\n\nTry that version now with the simple computation below.\n

\n"}︡
︠640cc705-2c35-4751-804b-62f46a23e2bf︠
67-5
︡d68b3a17-da58-4395-ab1d-e249fd5b9747︡
︠aa88d77c-911e-4ea7-9316-8bb11a061c17i︠
%md
To keep things rolling, you will often need to create new cells. At the end of a worksheet this is done automatically: there is always a set of blank lines at the bottom of a *.sagews* file. If you somehow manage to evaluate the last line, the page should refresh with a new blank line below the output.
The other way to make a new cell also works in the middle of a worksheet. Use your mouse or trackpad to hover over one of the thin grey horizontal rulings which separate cells. The ruling should get highlighted in blue. At this point, clicking the highlighted ruling will create a new cell directly below that ruling.
Try it now. Make a new cell between the ones below.
︡2b6c649c-e5b2-4236-b5a4-43e0736d1925︡{"html":"

To keep things rolling, you will often need to create new cells. At the end of a worksheet this is done automatically: there is always a set of blank lines at the bottom of a *.sagews* file. If you somehow manage to evaluate the last line, the page should refresh with a new blank line below the output.

The other way to make a new cell also works in the middle of a worksheet. Use your mouse or trackpad to hover over one of the thin grey horizontal rulings which separate cells. The ruling should get highlighted in blue. At this point, clicking the highlighted ruling will create a new cell directly below that ruling.

\n\nTry it now. Make a new cell between the ones below.

\n"}︡ ︠9c4ca428-6f13-4359-8c45-74abeb7ab930︠ ︡26778828-8bd7-443a-891a-e76c7b86ee7c︡ ︠22707caf-a45b-4edc-adeb-d3f6ce8ad86c︠ print('Do not touch this cell. Make a new one just below it!') ︡54548497-c025-473e-8185-17180a5e7674︡{"stdout":"Do not touch this cell. Make a new one just below it!\n"}︡ ︠8dc9d3db-d6a8-4214-ae0a-6cc8429acfd0i︠ %md It is possible to delete input cells, too. To do so, simply empty the cell and press [Backspace] or [Delete] in the active, empty cell. Try deleting a one or two of the cells just below. Note that pressing [Backspace] at the beginning of a cell will also remove the horizontal bar between cells. This joins the two input cells into a single one. That trick might be useful to you at some point. ︡f459a899-a7e5-4b5a-8e65-44018d266137︡{"html":"It is possible to delete input cells, too. To do so, simply empty the cell and press [Backspace] or [Delete] in the active, empty cell. Try deleting a one or two of the cells just below.

\n\nNote that pressing [Backspace] at the beginning of a cell will also remove the horizontal bar between cells. This joins the two input cells into a single one. That trick might be useful to you at some point.

\n"}︡ ︠47414f7d-e648-49d8-8871-41e21cf62ba8︠ ︡2ced26aa-bddb-4129-acd0-306579f70973︡ ︠5dadf9fc-fe76-4258-88c8-5238c75c5c62︠ "not empty, empty me first to delete me" ︡59eb0273-1436-4132-bfd0-adac8c80f2f2︡ ︠a2a3a8ce-1593-44fc-a33c-7dbc92f6f6e2︠ ︡b1c5fc34-69eb-4f09-9dec-8683b38f0e6a︡ ︠a702b99e-7120-425e-8f20-3a3f69d41de9is︠ %md It is useful to annotate your work by typing text right into the page. To do this, you can use two methods. + HTML: If you open a cell with the "decorator"Note that sage tries to remember what kind of object you are working with, so 4/5 is kept as a rational number in that form: 4/5. But if you give it a decimal expression, Sage will use finite precision arithmetic.

\n\nIf you want to get a numerical (decimal) approximation to a number that is expressed differently, you can use the .n() method. This method has an option to change how many digits you want displayed. Here are some examples, all in a line. [This demonstrates how to get one compute cell to display several computations—separate them by semicolons.]

\n"}︡ ︠d5f515c8-2075-4676-aedf-42e7321e7fa8︠ 4/5; .75; (4/5).n(); pi.n(digits=300) ︡6322b1c0-0091-44c8-b74a-3b90ec920ef8︡ ︠e608b624-5042-4ff7-94c0-c6eb15ef7217i︠ %md Sage can also be used as a *symbolic* calculator. (It is a computer algebra system, after all.) For this to work, you have to tell Sage the names of things it should consider as symbolic variables. After that, you can create an manipulate expressions in a variety of ways. ︡73743e36-b77a-4fb0-8af0-1045aed7e4af︡{"html":"Sage can also be used as a *symbolic* calculator. (It is a computer algebra system, after all.) For this to work, you have to tell Sage the names of things it should consider as symbolic variables. After that, you can create an manipulate expressions in a variety of ways.

An important part of Sage’s capabilities for symbolic expressions is the ability to simplify.

\n"}︡ ︠93c70ef4-e4c3-4ce5-8d61-01526f8cb476︠ show(expr2.simplify_rational()) ︡e9717aab-5e70-4c99-9e6d-f570d3fa76cb︡ ︠0fa6530e-5115-4b64-81d3-40778f35343ai︠ %md Of course, you can evaluate an expression by making a substitution of particular values for a variable, too. ︡f904bf07-29d0-49d8-9028-369d3722e779︡{"html":"Of course, you can evaluate an expression by making a substitution of particular values for a variable, too.

\n"}︡ ︠dacf66d6-91a8-4047-9111-03daa42821b3︠ expr2.subs(x = pi); expr2.subs(x = pi).n() ︡a19fae3e-b395-42f8-9a8e-ae325e2ef521︡ ︠f5a7e54d-0912-41ca-8036-bee01754ad85i︠ %htmlThere are several methods for getting help while using a Sage worksheet. The most obvious one is to click the Help icon in the top left corner of the page. This takes you to the Sage documentation. Almost everything you want to know can be found there in one way or another. I say *almost* because Sage is an open-source project maintained by a community of mathematicians across teh world. Some people are better than others at writing documentation. Also, it is a bit intimidating that "Almost everything you ever need to know" is there! Sage is a large project with a lot of power and functionality. It can be difficult to find the thing you need unless you know how to look for it.

Thankfully, the Sage Developers have incorporated other ways to get help. The first option is called "Tab Completion." You can type part of a command and then hit the [Tab] key to get a menu of possible commands that start that way. Let's try it. In the next cell, type

`A = ma`

then hit the [Tab] key. After you are done looking, press [Escape] to leave the pop-up menu.

︡a93eaeaa-8b3a-4a4c-9293-b3f240ea4c6b︡{"html":"There are several methods for getting help while using a Sage worksheet. The most obvious one is to click the Help icon in the top left corner of the page. This takes you to the Sage documentation. Almost everything you want to know can be found there in one way or another. I say *almost* because Sage is an open-source project maintained by a community of mathematicians across teh world. Some people are better than others at writing documentation. Also, it is a bit intimidating that \"Almost everything you ever need to know\" is there! Sage is a large project with a lot of power and functionality. It can be difficult to find the thing you need unless you know how to look for it.

Thankfully, the Sage Developers have incorporated other ways to get help. The first option is called \"Tab Completion.\" You can type part of a command and then hit the [Tab] key to get a menu of possible commands that start that way. Let's try it. In the next cell, type

\n`A = ma`

then hit the [Tab] key. After you are done looking, press [Escape] to leave the pop-up menu.

"}︡ ︠7bb0339a-603a-4917-99d8-a1197f847767︠ ︡077578ae-ffb0-4707-b3e5-981d742c6a52︡ ︠18755441-931a-4ccc-83ed-a8a4609750bfi︠ %md Notice that there are quite a few commands that start with "ma". We want the one for a matrix. Can you find it? Define a matrix called A by typing the following in the next cell, and then evaluating it.Notice that there are quite a few commands that start with “ma”. We want the one for a matrix. Can you find it?

\n\nDefine a matrix called A by typing the following in the next cell, and then evaluating it.

\n\n`A = matrix(ZZ, [[1,0],[1,1]])`

Nothing seems to happen. Sage made the matrix and put it in memory, but did not print it out. To see it, simply type A in a cell and evaluate.

\n"}︡ ︠ca8d0032-d9b8-41f2-82c3-40002433c8ef︠ ︡26ddb318-a67a-49db-b149-a57e19f5143d︡ ︠1450fcab-848d-45be-b889-72c72a8726c6i︠ %md There is a fundamental number attached to each matrix called its "rank." Sage has a way to compute the rank built in. Like most built-in methods, you can access this function in two ways. First typeThere is a fundamental number attached to each matrix called its “rank.” Sage has a way to compute the rank built in. Like most built-in methods, you can access this function in two ways. First type

\n\n`rank(A)`

and evaluate. Then make a new cell and type

\n\n`A.rank()`

and evaluate. Both times you should get the number 2 as output.

\n"}︡ ︠c75c8d38-aea2-43cd-84fc-602a83841f0f︠ ︡31a2c868-f9db-48b3-95f8-70f302984831︡ ︠e1e8785b-449e-4fb7-a32d-e7b3684ce23ci︠ %md What else can you do with a matrix? Sage thinks like a mathematician! In particular, it attaches "methods" to "objects" depending on their "type." To see all of the methods attached to an object, you can use the "dot-[Tab]" approach. TypeWhat else can you do with a matrix? Sage thinks like a mathematician! In particular, it attaches “methods” to “objects” depending on their “type.”

\n\nTo see all of the methods attached to an object, you can use the “dot-[Tab]” approach. Type `A.` (notice the period!) then press [Tab] to get a list of all the methods attached to the matrix A. Sage knows a lot of linear algebra, so the menu will be very big.

The other ways to get help involve using the question mark key. First, a command name followed by a question mark will evaluate to the documentation for that command.

\n\nI left the matrix command a little mysterious above. To see why I had you type it like that, evaluate this in the next cell.

\n\n`matrix?`

To get more information, you can use the double question mark. When you evaluate the command “matrix??” you will get the Sage source code for how the matrix command works! I don’t use this very often, but if you think you find a bug in Sage, this is the place to start looking. Try it tnow to see what it looks like. Evaluate

\n\n`matrix??`

We have barely scratched the surface on Sage’s capabilities. It is important to see that Sage can be made to deal with functions, it can solve equations, and it can make nice plots. I’ll just put some examples of using Sage to do those things here.

\n\nPlots can be made in a variety of colors, this example uses some standard color names.

\n"}︡ ︠e30db011-ab6d-4392-958f-affc3a1924ea︠ where = [x,-5,5] plot(f,where, color='red') + plot(g, where, color='green') ︡6c4e4d0c-59de-4e80-91aa-e70ee789c413︡ ︠884d9670-4986-4b18-9779-2f3d96510756i︠ %md #### Solving an equation #### ︡5215bdc8-4555-4896-b594-b271cbb63842︡{"html":"I denotes the imaginary unit. Sage can handle complex numbers.

\n\nWait. Complex roots! Really? Let’s zoom in…

\n"}︡ ︠a32d17b6-e6fb-48c6-a1db-c04d12c828d6︠ P = plot(f, [x,-.2,.2], legend_label='$f$') # the default color is blue. And comments can be added after a '#' character. P+= plot(g, [x,-.2,.2], legend_label='$g$', color='red') P.show() ︡b09b0e7b-cd28-41c5-8778-e4d063ea1683︡ ︠6fe2cb3c-443e-4c19-a379-942536ff620bi︠ %md Yes. We see that $f$ is strictly increasing. Therefore, $g'(x)$ is never zero. This is also apparent from the graph, too. Since $g$ is a quadratic polynomial, its two roots must come as a complex conjugate pair. Of course, Sage has many ways of handling finding roots. Here is another one. ︡b84c9943-9341-4614-bd0e-23461bdfdab7︡{"html":"Yes. We see that $f$ is strictly increasing. Therefore, $g'(x)$ is never zero. This is also apparent from the graph, too. Since $g$ is a quadratic polynomial, its two roots must come as a complex conjugate pair.

\n\nOf course, Sage has many ways of handling finding roots. Here is another one.

\n"}︡ ︠83f5fcc5-2342-4036-9325-02b30f51fd07︠ g; g.roots() ︡e6246088-b6d4-4be2-8924-7e7614ad3ef1︡ ︠0f77fc08-340d-498f-aa18-2a2a9003aa79i︠ %md ### What else might you like to know? ### * Sage knows $\LaTeX{}$, so you can use that in Markdown or html cells. I did it with the mathematics above. * Sage is built on Python, a wonderful programming language. You can use any Python code you want to do your mathematics. For example, I used some Python list comprehensions (which look a lot like set-builder notation) to make the following list. ︡2a43abbf-93ee-43fe-9acf-cab838e0805b︡{"html":"- \n
- Sage knows $\\LaTeX{}$, so you can use that in Markdown or html cells. I did it with the mathematics above. \n
- Sage is built on Python, a wonderful programming language. You can use any Python code you want to do your mathematics. For example, I used some Python list comprehensions (which look a lot like set-builder notation) to make the following list. \n

Here is another small bit of Python programming. This time it defines a simple function, and then calls that function to do a computation.

\n"}︡ ︠9612caee-0dcd-4fc3-939c-316ac7ee680d︠ def square_and_add(x,y): z = x^2 + y^2 return z square_and_add(3,4) ︡10c24044-0ab3-4473-85f0-e14723e2cbd3︡ ︠18447ff6-d0ce-46d9-a71f-9f97641c4d3d︠ ︡c14814dd-8241-42ff-872f-e8dd33806f0d︡ ︠7820a191-7c29-45a0-b057-2ff3d91beba7i︠ %md ## Where do I get more instruction? ## There are some good tutorials available. * [From a group at San Diego State University](http://www-rohan.sdsu.edu/~mosulliv/Courses/sdsu-sage-tutorial/index.html) * [The official Sage project Tutorial](http://www.sagemath.org/doc/tutorial) * [The tutorial designed for an MAA PREP Workshop](http://sagemath.org/doc/prep/index.html). * There is a shorter version of [the official tutorial with just highlights](http://www.sagemath.org/doc/tutorial/tour.html?highlight) * There are also "[Sage quick reference documents][quick]" for several subjects on the Sage Wiki. These are useful cheat sheets with frequently used commands, organized by subject. [quick]: http://wiki.sagemath.org/quickref You can also look through relevant documentation. * There is the page for [all of the official Sage documentation](http://www.sagemath.org/doc/index.html). * [The Python Documentation](http://docs.python.org/2/). Note that Sage uses Python 2.x for now. And there a useful Q & A site: * [ask.sagemath.org](http://ask.sagemath.org/questions/) ︡3749a401-750e-4a4b-b47d-8c8ed792ac39︡{"html":"There are some good tutorials available.

\n\n- \n
- \n
- \n
- \n
There is a shorter version of the official tutorial with just highlights

\nThere are also “Sage quick reference documents” for several subjects on the Sage Wiki. These are useful cheat sheets with frequently used commands, organized by subject.

\n

You can also look through relevant documentation.

\n\n- \n
There is the page for all of the official Sage documentation.

\nThe Python Documentation. Note that Sage uses Python 2.x for now.

\n

And there a useful Q & A site:

\n\n- \n
- ask.sagemath.org \n