︠409ca605-8043-4409-bb29-f7d4ca29beb8i︠ %html

MTH213/MH2400 - Experimental Mathematics

Lesson 1: Introduction to Sage

 

Introductory Sage Tutorial - Welcome!

Sage and Python. What are they, and what is the difference between them?

Python is a general programming language. It is higher level than C and Java, which means it makes things easier for you, at the price of speed.

Sage extends Python to do mathematics. That includes a Python library with mathematical objects and a preparser. A preparser is a translator that makes you use Sage objects instead of Python ones, like when you type '3'. Sage is used to work on mathematical subjects as varied as

Sage has a graphical user interface that runs in the browser and is called the Sage Notebook, or the Sage Worksheet. You are using the Sage Notebook right now.

This tutorial has the following sections:

Evaluating Sage Commands

(How do I get Sage to do some math?)

The little boxes below are called input cells or code cells. To evaluate the content of an input cell, first click inside the cell so that the cell is active, i.e., has a bright blue border. Then, just below the cell on the left, an "evaluate" link appears. Clicking this link evaluates the cell.

Exercise: Evaluate the following cell

︡d64b1136-6f0b-44e9-a4b0-37a2ed40d0f7︡{"html": "

MTH213/MH2400 - Experimental Mathematics\ufeff

\r\n

Lesson 1: Introduction to Sage

\r\n

 

\r\n

Introductory Sage Tutorial - Welcome!

\r\n

Sage and Python. What are they, and what is the difference between them?

\r\n

Python is a general programming language. It is higher level than C and Java, which means it makes things easier for you, at the price of speed.

\r\n

Sage extends Python to do mathematics. That includes a Python library with mathematical objects and a preparser. A preparser is a translator that makes you use Sage objects instead of Python ones, like when you type '3'. Sage is used to work on mathematical subjects as varied as

\r\n\r\n

Sage has a graphical user interface that runs in the browser and is called the Sage Notebook, or the Sage Worksheet. You are using the Sage Notebook right now.

\r\n

This tutorial has the following sections:

\r\n\r\n

Evaluating Sage Commands

\r\n

(How do I get Sage to do some math?)

\r\n

The little boxes below are called input cells or code cells. To evaluate the content of an input cell, first click inside the cell so that the cell is active, i.e., has a bright blue border. Then, just below the cell on the left, an \"evaluate\" link appears. Clicking this link evaluates the cell.

\r\n

Exercise: Evaluate the following cell

"}︡ ︠a18c90dc-5983-4368-9c3a-820054b67c5a︠ 2+2 ︡5fe8b8fc-5196-4ae5-bafb-c7150e5d8a01︡︡ ︠0e76e881-b428-4fa6-9fb7-f2ee4a8e4244i︠ %html

Sage prints out its response just below the cell. That's the "4" above, so Sage confirms that $2+2$ equals $4$. Note also that Sage has probably automatically made the next cell active after you evaluated your first cell.

You can also evaluate a cell using a keyboard shortcut. The keyboard shortcut is "Shift-Enter": hold down the Shift key while you press the Enter key. You can also edit a cell and evaluate it again.

Exercises:

︡fb085c2f-0328-4a40-9fe0-3d744cb523ff︡{"html": "

Sage prints out its response just below the cell. That's the \"4\" above, so Sage confirms that $2+2$ equals $4$. Note also that Sage has probably automatically made the next cell active after you evaluated your first cell.

\r\n

You can also evaluate a cell using a keyboard shortcut. The keyboard shortcut is \"Shift-Enter\": hold down the Shift key while you press the Enter key. You can also edit a cell and evaluate it again.\ufeff

\r\n

Exercises:

\r\n"}︡ ︠06daea95-807f-4f33-8903-2df37661eb38︠ factor(2010) ︡bc103d6f-b89a-4647-ae32-ea3568f66e80︡︡ ︠89eaa1dd-5ba3-4d84-babc-07bd085e28e3i︠ %html

To create a new input cell, move your cursor over the space above or below another cell.  A blue horizontal line as wide as the browser appears. Click on the line to insert a new cell.  To delete an input cell, just delete all the text inside of it, and then press backspace in the now-empty cell. 

Exercise: Create a few new input cells below, do some arithmetic, and delete one of the input cells.

︡c2844ba7-6ad8-4ce2-984a-239ad26369e9︡{"html": "

To create a new input cell, move your cursor over the space above or below another cell.  A blue horizontal line as wide as the browser appears. Click on the line to insert a new cell.  To delete an input cell, just delete all the text inside of it, and then press backspace in the now-empty cell. 

\r\n

Exercise: Create a few new input cells below, do some arithmetic, and delete one of the input cells.

"}︡ ︠359350ab-cfa0-4b63-90f1-62e48dd89981i︠ %html

Different (evaluated) cells depend upon each other. This can be best explained by an example.

Exercise: There are three input cells below.

︡c0db1b9f-be51-43dc-981d-66fb8c6d84eb︡{"html": "

Different (evaluated) cells depend upon each other. This can be best explained by an example.

\r\n

Exercise: There are three input cells below.

\r\n"}︡ ︠5dee490d-e9f5-47ae-aecd-087c6a4be793︠ a=30 ︡fcff8bae-1ca9-4d76-b00c-0d36cc62e106︡︡ ︠511ba088-cc34-459f-bf39-5300e9360342︠ b=a^2 ︡dc4ee7a3-48ef-4132-9dec-a3a444813a8b︡︡ ︠a3c1a816-7feb-4d19-9d01-5f8a404934b1︠ factor(b) ︡21dd4802-f66b-409b-b728-924d6d562980︡︡ ︠332da490-c0fa-4726-b8bd-a7a8f1c393e8i︠ %html

Clearly, the variable b above is not defined until we define a first. Hence the second input cell depends on the first input cell. Similarly, the third input depends on both the first and the second input cell. That is, unless both the first and the second input cells have been evaluated, the third input cell will also produce an error.

 


Finally, something less trivial, just to whet your appetite: 

batman logo

Let's draw this figure in Sage!

︡1dc52994-2280-4208-ab5b-5181788eb934︡{"html": "

Clearly, the variable b above is not defined until we define a first. Hence the second input cell depends on the first input cell. Similarly, the third input depends on both the first and the second input cell. That is, unless both the first and the second input cells have been evaluated, the third input cell will also produce an error.

\r\n

 

\r\n


\r\n

Finally, something less trivial, just to whet your appetite: 

\r\n

\"batman

\r\n

Let's draw this figure in Sage!

"}︡ ︠0b21ea63-9ee1-4f0e-8f57-c633d1f6bd67︠ var('x y') f1(x,y)=((x/7)^2*sqrt(abs(abs(x)-3)/(abs(x)-3))+(y/3)^2*sqrt(abs(y+(3*sqrt(33))/7)/(y+(3*sqrt(33))/7))-1) f2(x,y)=(abs(x/2)-((3*sqrt(33)-7)/112)*x^2-3+sqrt(1- (abs(abs(x)-2)-1)^2)- y) f3(x,y)=(9*sqrt(abs((abs(x)-1)*(abs(x)-3/4))/((1- abs(x))*(abs(x)-3/4)))-8*abs(x)-y) f4(x,y)=(3*abs(x)+.75*sqrt(abs((abs(x)-3/4)*(abs(x)-1/2))/((3/4- abs(x))*(abs(x)-1/2)))- y) f5(x,y)=(9/4*sqrt(abs((x-1/2)*(x+1/2))/((1/2-x)*(1/2+x)))-y) f6(x,y)=((6*sqrt(10))/ 7+(3/2-abs(x)/2)*sqrt(abs(abs(x)-1)/(abs(x)-1))-(6*sqrt(10))/14*sqrt(4- (abs(x)-1)^2)-y) p1=implicit_plot(f1==0,(-8,8),(-3,3),plot_points=200) p2=implicit_plot(f2==0,(-8,8),(-3,3),plot_points=200) p3=implicit_plot(f3==0,(-8,8),(-3,3),plot_points=200) p4=implicit_plot(f4==0,(-8,8),(-3,3),plot_points=200) p5=implicit_plot(f5==0,(-8,8),(-3,3),plot_points=200) p6=implicit_plot(f6==0,(-8,8),(-3,3),plot_points=200) show(p1+p2+p3+p4+p5+p6) ︡ac77fe64-789c-4733-afdd-02bc8862c93d︡︡ ︠d314624c-3fdf-4cea-8b96-260bbfbe284ci︠ %html

Exercise: fix the plot! (You might need to use a bit of guessing to figure out the way it works)




Objects in Sage

Everything in Sage and in Python is an object. Each object has a type (e.g. number, vector, matrice, set, etc). You can inspect its type by typing

type
︡d38bec0c-6739-4b6c-a4f6-c7558d5293cc︡{"html": "

Exercise: fix the plot! (You might need to use a bit of guessing to figure out the way it works)

\r\n


\ufeff

\r\n

Objects in Sage

\r\n

Everything in Sage and in Python is an object. Each object has a type (e.g. number, vector, matrice, set, etc). You can inspect its type by typing

\r\n
type
"}︡ ︠6afdeb9d-077f-4ee7-8e91-56d54bdba0fc︠ type(4) ︡36c2cf75-4243-41c6-9336-f607fd48fe7f︡︡ ︠3901b841-c924-4911-8d30-23e5b655f29b︠ type([1,2,3]) ︡c798b07e-fe07-4723-aeb3-7c7305f9f846︡︡ ︠9a51715f-7593-44c1-8e2f-35d18993cc1ai︠ %html

Notice that list does not have sage in its name. Lists are basic Python types.

You can build an object in various ways. The most used one is calling the type name as a function with certain parameters.

︡06830963-5b92-4970-a5ca-70ea8100071a︡{"html": "

Notice that list does not have sage in its name. Lists are basic Python types.

\r\n

You can build an object in various ways. The most used one is calling the type name as a function with certain parameters.\ufeff

"}︡ ︠283b4bd7-7b71-4996-9e4a-554ceec569f7︠ v = vector([1,2,3,5]) type(v) ︡60827ce4-2a4c-4263-b393-4e63df5222c7︡︡ ︠3eb559ef-6555-4c8f-a7b7-2003ba0a4bb9i︠ %html

The letter $x$ is treated in a special manner in sage. By default, it is considered as a symbolic variable. This means that the variable can be used in a program just as one does mathematics. Let us define a function $f(x)$ below in terms of the symbolic variable $x$.

︡2f895948-52f5-479d-8b95-86c12bf27692︡{"html": "

The letter $x$ is treated in a special manner in sage. By default, it is considered as a symbolic variable. This means that the variable can be used in a program just as one does mathematics. Let us define a function $f(x)$ below in terms of the symbolic variable $x$.

"}︡ ︠237da6a0-fa92-407e-9aa7-f333f661cb74︠ type(x) ︡224da566-7fdb-45d6-8d3f-2a896796d8f3︡︡ ︠c07346c9-b99b-4a17-b80b-f1c166605f9e︠ f(x)=x^2 ︡ce2dc719-193e-4948-a6f7-9ec3ec75ce11︡︡ ︠de3f01ad-78e1-4172-a9b9-b7b9866ec702i︠ %html

Exercises:

 

Help inside Sage

There are various ways to get help for doing things in Sage.  Here are several common ways to get help as you are working in a Sage worksheet.

Documentation

Sage includes extensive documentation covering thousands of functions, with many examples, tutorials, and other guidance.  To access these, just click the "Help" link at the top right of any worksheet.  Also, the Quick Reference cards are useful.

Tab completion

To see what you can do to an expression, usually you can just give the expression a name, type a period after it, and then press Tab.  You will see a list pop up of all the things you can do to the expression.

Exercise: Just in case you didn't define the function $f(x)$ before, evaluate this cell to (re-)define $f(x)$.

︡e5024f5c-88d3-43e8-830c-1df729b79191︡{"html": "

Exercises:

\r\n\r\n\r\n

 

\r\n

Help inside Sage

\r\n

There are various ways to get help for doing things in Sage.  Here are several common ways to get help as you are working in a Sage worksheet.

\r\n

Documentation

\r\n

Sage includes extensive documentation covering thousands of functions, with many examples, tutorials, and other guidance.  To access these, just click the \"Help\" link at the top right of any worksheet.  Also, the Quick Reference cards are useful.

\r\n

Tab completion

\r\n

To see what you can do to an expression, usually you can just give the expression a name, type a period after it, and then press Tab.  You will see a list pop up of all the things you can do to the expression.

\r\n

Exercise: Just in case you didn't define the function $f(x)$ before, evaluate this cell to (re-)define $f(x)$.

"}︡ ︠dae26a53-0676-4a7e-9e8c-293014368096︠ f(x)=x^2 ︡82a7ef79-b347-42ce-b6c7-9f05496eab90︡︡ ︠2e338c85-9576-48b8-b55d-3f3142f9db46i︠ %html

Exercise: After evaluating the above cell, put your cursor after the period in the cell below and press your Tab key.

Note: To stop viewing the list, first ensure that the cell containing "f." is active and then press the Escape key on the keyboard.

︡37ad6797-222b-404a-a11f-935058b059d6︡{"html": "

Exercise: After evaluating the above cell, put your cursor after the period in the cell below and press your Tab key.

\r\n

Note: To stop viewing the list, first ensure that the cell containing \"f.\" is active and then press the Escape key on the keyboard.

"}︡ ︠f0773b60-d5d2-4a72-9d72-02cb2fd0c94c︠ f. ︡eab8094b-0dc7-4906-8183-f4df021b8a0c︡︡ ︠f4a1e7df-b203-492c-ade0-09577a08a19ci︠ %html

One of the things in that list above was "integrate".  Let's try it.

︡78e2ef69-5430-4abc-88fe-852fff6ebcce︡{"html": "

One of the things in that list above was \"integrate\".  Let's try it.

"}︡ ︠17e621ec-727d-458c-b97f-e1bfdb3fcd30︠ f.integrate(x) ︡4388a4aa-b0f1-4c89-b35c-ea4c6135aa0c︡︡ ︠82945ee2-b670-48ff-8a65-be377f65ff6ei︠ %html

This works with completing commands as well.

Exercise: Press Tab after the "plot" below to see all the commands that start with the letters "plot". Clicking on one of the options will use it for this cell.

︡6661d17f-1cea-4403-9339-8376aadb15a2︡{"html": "

This works with completing commands as well.

\r\n

Exercise: Press Tab after the \"plot\" below to see all the commands that start with the letters \"plot\". Clicking on one of the options will use it for this cell.

"}︡ ︠ba84011b-d99c-4a16-abc9-88d3b10487ae︠ plot ︡a15ad154-ba67-499b-bfcb-34a8430c34a1︡︡ ︠d37074ab-b85d-4368-899d-643039d3f793i︠ %html

Remember, if you decide not to use any of the options, pressing Escape should make the list disappear.

Finding documentation: the question marks

Use a question mark after a function name to display the documentation for the function.  Almost all documentation in Sage has extensive examples that illustrates how to use the function. To see the help in this manner, one can either

For instance, the documentation for the integrate command has examples illustrating that the syntax requires "f.integrate(x)" and not just "f.integrate()". The latter would be ambiguous if $f$ was a function of several variables.

Exercise: Get the documentation for the integrate function by press "Tab".

︡a88744c1-bab9-49ff-adc1-b70501a37214︡{"html": "

Remember, if you decide not to use any of the options, pressing Escape should make the list disappear.

\r\n

Finding documentation: the question marks

\r\n

Use a question mark after a function name to display the documentation for the function.  Almost all documentation in Sage has extensive examples that illustrates how to use the function. To see the help in this manner, one can either

\r\n\r\n

For instance, the documentation for the integrate command has examples illustrating that the syntax requires \"f.integrate(x)\" and not just \"f.integrate()\". The latter would be ambiguous if $f$ was a function of several variables.

\r\n

Exercise: Get the documentation for the integrate function by press \"Tab\".

"}︡ ︠ab5b0473-929c-4a1c-acad-a06dbcc2f2f2︠ f.integrate? ︡a788a120-4432-4c91-818d-3e5b28229cf4︡︡ ︠beeca7ed-fe78-4b38-b8e4-94dfbd45f023i︠ %html

To stop viewing the documentation after pressing Tab, you can press the Escape key, just as you did with the completion of options.

Exercise: Evaluate the expression below to access its documentation.

︡4b466611-cbd9-4200-b020-d7ce1ed1dd04︡{"html": "

To stop viewing the documentation after pressing Tab, you can press the Escape key, just as you did with the completion of options.

\r\n

Exercise: Evaluate the expression below to access its documentation.

"}︡ ︠d58ad98d-b78b-4a0e-bec1-72639111b3a5︠ binomial? ︡e7c21abc-d1e3-4f11-b6f3-2d60d4f11507︡︡ ︠948e6e6f-f09a-44fa-b61f-863ce4db1402i︠ %html

Exercise: Get the documentation for another function!

︡85967cd6-95fb-4dd3-94d1-bf8fa4e45aa9︡{"html": "

Exercise: Get the documentation for another function!

"}︡ ︠44d93b24-2bfc-4dbc-bdaa-c5538548fca9i︠ %html

Finding the source

One can use two question marks after a function name to get both the documentation and the source code for the function.  Again, to see this help, you can either evaluate a cell, or just move your cursor after the question mark and press Tab.

The ability to see the code is one of Sage's great strengths.  You can see all the code to everything!  This means you can see what Sage is doing; if you are curious you can see how the function is implemented; and if you know a better way to implement it, then you can change it!

Exercise: Get the source code for the binomial function below.

︡c4bff61c-e834-42c9-864f-bc28cf33e14b︡{"html": "

Finding the source

\r\n

One can use two question marks after a function name to get both the documentation and the source code for the function.  Again, to see this help, you can either evaluate a cell, or just move your cursor after the question mark and press Tab.

\r\n

The ability to see the code is one of Sage's great strengths.  You can see all the code to everything!  This means you can see what Sage is doing; if you are curious you can see how the function is implemented; and if you know a better way to implement it, then you can change it!

\r\n

Exercise: Get the source code for the binomial function below.

"}︡ ︠66ba1fd6-98e7-45b9-b86c-0e7190f30503︠ binomial?? ︡1c632dbb-0bee-4d09-981f-e3893090d5aa︡︡ ︠d1358e18-ff0d-4258-ad91-536dc9ad442ai︠ %html

Annotating with Sage

Whether one uses Sage in the classroom or in research, it is usually helpful to describe to the reader what is being done, such as in the description you are now reading.   Thanks to the mini-word processor TinyMCE and a TeX rendering engine called jsmath, you can type much more in Sage than just mathematical Sage commands.  This math-aware word processor makes Sage perfect for annotating computations. 

To use the word processor, we create a text cell, as opposed to a input cell. Recall that an input cell contains Sage commands that Sage evaluates. To create a text cell, first move the cursor between two input cells.  Then hold the Shift key and then left click on the thin blue line that appears. Note that to create an input cell, one merely clicks, but one is required to "Shift-Click" to create a text cell.

Exercise: insert a text cell between the input cells below.

︡d3d63b4e-7f00-4d68-85c9-47eac41d2b3d︡{"html": "

Annotating with Sage

\r\n

Whether one uses Sage in the classroom or in research, it is usually helpful to describe to the reader what is being done, such as in the description you are now reading.   Thanks to the mini-word processor TinyMCE and a TeX rendering engine called jsmath, you can type much more in Sage than just mathematical Sage commands.  This math-aware word processor makes Sage perfect for annotating computations. 

\r\n

To use the word processor, we create a text cell, as opposed to a input cell. Recall that an input cell contains Sage commands that Sage evaluates. To create a text cell, first move the cursor between two input cells.  Then hold the Shift key and then left click on the thin blue line that appears. Note that to create an input cell, one merely clicks, but one is required to \"Shift-Click\" to create a text cell.

\r\n

Exercise: insert a text cell between the input cells below.

"}︡ ︠a66e3b1d-08e4-4f69-8f50-3051ab4b9452i︠ %html

TinyMCE makes it easy for format text in many ways.  Try experimenting with the usual bold button, underline button, different text fonts and colors, ordered and unordered lists, centering, and so on.  Some of the shortcut keys you are familiar with from other word processors may also work, depending on your system.

There are two other things you can do which take advantage of the worksheet being on the web.  

To edit a text cell, simply double-click on the text.  Try double-clicking on this text to edit this text cell (and you can see how we typed the mathematics above!).

Exercise: Double click on the cell below (containing the description of $f(x)=x^2$) and edit it and change it.

If $f(x)=x^2$, then $f(9)=81$.

Of course, one can do much more, since Sage can execute arbitrary commands in the Python programming language, as well as output nicely formatted HTML, and so on.  If you have enough programming experience to do things like this, go for it!

︡3c57b225-9e76-4560-8896-cb0641f9fc39︡{"html": "

TinyMCE makes it easy for format text in many ways.  Try experimenting with the usual bold button, underline button, different text fonts and colors, ordered and unordered lists, centering, and so on.  Some of the shortcut keys you are familiar with from other word processors may also work, depending on your system.

\r\n

There are two other things you can do which take advantage of the worksheet being on the web.  

\r\n\r\n

To edit a text cell, simply double-click on the text.  Try double-clicking on this text to edit this text cell (and you can see how we typed the mathematics above!).

\r\n

Exercise: Double click on the cell below (containing the description of $f(x)=x^2$) and edit it and change it.

\r\n\r\n

If $f(x)=x^2$, then $f(9)=81$.

\r\n\r\n

Of course, one can do much more, since Sage can execute arbitrary commands in the Python programming language, as well as output nicely formatted HTML, and so on.  If you have enough programming experience to do things like this, go for it!

"}︡ ︠9bc30576-575f-4c27-bfc4-f5435f6da8f8︠ print "Sage is somewhat really cool!

(It even does HTML.)

" ︡5eb78c1e-a38a-473c-8d15-7062a103675a︡︡ ︠2d40fbb5-308f-4276-98a0-42cc091532fei︠ %html

The Sage Notebook interface

The input and text cells is usually how you interact with the Sage process. You will also need to save your work and to do that you need to understand the other elements of the notebook interface,- the many buttons and links at the very top of the notebook! Let us go over the most important ones.

Saving your work

There are several ways you can save your work:

Note: It is highly recommended to use the "Save and Quit" button before you log out. It is also advised that you save your notebooks to a file and copy them to your personal USB drive.

That's the end of the 1st lesson.  Feel free to create more of your own worksheets!

︡943e5dee-cd91-4590-98bb-afc4518f5a23︡{"html": "

The Sage Notebook interface

\r\n

The input and text cells is usually how you interact with the Sage process. You will also need to save your work and to do that you need to understand the other elements of the notebook interface,- the many buttons and links at the very top of the notebook! Let us go over the most important ones.

\r\n\r\n

Saving your work

\r\n

There are several ways you can save your work:

\r\n\r\n

Note: It is highly recommended to use the \"Save and Quit\" button before you log out. It is also advised that you save your notebooks to a file and copy them to your personal USB drive.

\r\n\r\n

That's the end of the 1st lesson.  Feel free to create more of your own worksheets!

"}︡ ︠f9923a07-62f4-4c01-b113-b0b93839996bi︠ %html

This worksheet is based on a tutorial from MAA Prep Workshop found at http://wiki.sagemath.org/prep/2011/Tutorials.

︡f39f7aa6-4149-45e1-bb26-6a9d6b806d37︡{"html": "

This worksheet is based on a tutorial from MAA Prep Workshop found at http://wiki.sagemath.org/prep/2011/Tutorials.\ufeff

"}︡