|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. Clearly state the objectives of the LO (along with RBT level) |
|
5 |
|
6 .. By the end of this tutorial, you should -- |
|
7 |
|
8 .. #. Know what Sage and Sage notebook are. |
|
9 .. #. Be able to start a Sage shell or notebook |
|
10 .. #. Be able to start using the notebook |
|
11 .. #. Be able to create new worksheets |
|
12 .. #. Know about the menu options available |
|
13 .. #. Know about the cells in the worksheet |
|
14 .. #. Be able to evaluate cells, create and delete cells, navigate them. |
|
15 .. #. Be able to make annotations in the worksheet |
|
16 .. #. Be able to use tab completion. |
|
17 .. #. Be able to use code from other languages in the cells. |
|
18 |
|
19 .. Prerequisites |
|
20 .. ------------- |
|
21 |
|
22 .. None. |
|
23 |
|
24 .. Author : Madhu |
|
25 Internal Reviewer : |
|
26 External Reviewer : |
|
27 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
28 |
|
29 |
|
30 Script |
|
31 ------ |
|
32 |
|
33 Hello friends. Welcome to this spoken tutorial on Getting started with |
|
34 sage and sage notebook. |
|
35 |
|
36 {{{ Show the slide containing the title }}} |
|
37 |
|
38 {{{ Show the slide containing the outline }}} |
|
39 |
|
40 In this tutorial, we will learn what Sage is, what is Sage notebook, |
|
41 how to start and use the sage notebook. In the notebook we will be |
|
42 specifically learning how to execute our code, how to write |
|
43 annotations and other content, typesetting the content and how to use |
|
44 the offline help available. |
|
45 |
|
46 {{{ Show the slide on what is Sage }}} |
|
47 |
|
48 To start with, What is Sage? Sage is a free, open-source mathematical |
|
49 software. Sage can do a lot of math stuff for you including but not |
|
50 limited to algebra, calculus, geometry, cryptography, graph theory |
|
51 among other things. It can also be used as aid in teaching and |
|
52 research in any of the areas that Sage supports. So let us start Sage |
|
53 now |
|
54 |
|
55 {{{ Shift to terminal }}} |
|
56 |
|
57 We are assuming that you have Sage installed on your computer now. If |
|
58 not please visit the page |
|
59 http://sagemath.org/doc/tutorial/introduction.html#installation for |
|
60 the tutorial on how to install Sage. Let us move on now. |
|
61 |
|
62 On the terminal type:: |
|
63 |
|
64 sage |
|
65 |
|
66 This should start a new Sage shell with the prompt sage: which looks |
|
67 like this |
|
68 |
|
69 {{{ Show what is displayed on the terminal }}} |
|
70 |
|
71 So now we can type all the commands that Sage supports here. But Sage |
|
72 comes bundled with a much much much more elegant tool called Sage |
|
73 Notebook? What is Sage Notebook? Sage Notebook provides a web based |
|
74 user interface to use Sage. So once we have a Sage notebook server up |
|
75 and running all we want is a browser to access the Sage |
|
76 functionality. For example there is an official instance of Sage |
|
77 Notebook server running at http://sagenb.org You can visit that page, |
|
78 create an account there and start using Sage! So all you need is just |
|
79 a browser, a modern browser |
|
80 |
|
81 {{{ Intentional *cough* *cough* }}} |
|
82 |
|
83 to use Sage and nothing else! The Sage notebook also provides a |
|
84 convenient way of sharing and publishing our work which is very handy |
|
85 when we use Sage for research or for teaching. |
|
86 |
|
87 However we can also run our own instances of Sage notebook servers on |
|
88 all the computers we have a local installation of Sage. To start the |
|
89 notebook server just type:: |
|
90 |
|
91 notebook() |
|
92 |
|
93 on the Sage prompt. This will start the Sage Notebook server. If we |
|
94 are starting the notebook server for the first time, we are prompted |
|
95 to enter the password for the admin. Type the password and make a note |
|
96 of it. After this Sage automatically starts a browser page for you |
|
97 with the notebook opened. |
|
98 |
|
99 If it doesn't automatically start a browser page check if the Notebook |
|
100 server started and there were no problems. If so open your browser and |
|
101 in the address bar type the URL shown in the instructions upon running |
|
102 the notebook command on the sage prompt. |
|
103 |
|
104 {{{ The notebook() command gives an instruction telling |
|
105 Open your web browser to http://localhost:8000. Point towards it }}} |
|
106 |
|
107 In our case it is http://localhost:{{{ Tell whatever is shown }}} |
|
108 |
|
109 {{{ Show the browser with Sage notebook }}} |
|
110 |
|
111 If you are not logged in yet, it shows the Notebook home page and |
|
112 textboxes to type the username and the password. You can use the |
|
113 username 'admin' and the password you gave while starting the notebook |
|
114 server for the first time. There are also links to recover forgotten |
|
115 password and to create new accounts. |
|
116 |
|
117 {{{ If you are logged in tell that you are logged in, log out and show |
|
118 what is said above for the login page }}} |
|
119 |
|
120 Once we are logged in with the admin account we can see the notebook |
|
121 admin page. A notebook can contain a collection of Sage Notebook |
|
122 worksheets. Worksheets are basically the working area. This is where |
|
123 we enter all the Sage commands on the notebook. |
|
124 |
|
125 The admin page lists all the worksheets created. On the topmost part |
|
126 of this page we have the links to various pages. |
|
127 |
|
128 {{{ Whenever you talk on an individual link point your mouse towards |
|
129 the link. For each of the link go to the page and explain as below }}} |
|
130 |
|
131 The home link takes us to the admin home page. The published link |
|
132 takes us to the page which lists all the published worksheets. The log |
|
133 link has the complete log of all the actions we did on the |
|
134 notebook. We have the settings link where can configure our notebook, |
|
135 the notebook server, we can create and mangage accounts. We have a |
|
136 link to help upon clicking opens a new window with the complete help |
|
137 of Sage. The entire documentation of Sage is supplied with Sage for |
|
138 offline reference and this help link is the way to get into it. Then |
|
139 we can report bugs about Sage by clicking on Report a Problem link and |
|
140 there is a link to sign out of the notebook. |
|
141 |
|
142 We can create a new worksheet by clicking New Worksheet link |
|
143 |
|
144 {{{ Click on the link }}} |
|
145 |
|
146 Sage prompts you for a name for the worksheet. Let us name the |
|
147 worksheet as nbtutorial. Now we have our first worksheet which is |
|
148 empty. |
|
149 |
|
150 A worksheet will contain a collection of cells. Every Sage command |
|
151 must be entered in this cell. Cell is equivalent to the prompt on |
|
152 console. When we create a new worksheet, to start with we will have |
|
153 one empty cell. Let us try out some math here:: |
|
154 |
|
155 2 + 2 |
|
156 57.1 ^ 100 |
|
157 |
|
158 The cap operator is used for exponentiation. If you observed carefully |
|
159 we typed two commands but the output of only last command was |
|
160 displayed. By default each cell displays the result of only the last |
|
161 operation. We have to use print statement to display all the results |
|
162 we want to be displayed. |
|
163 |
|
164 {{{ Demonstrate creating a new cell }}} |
|
165 |
|
166 Now to perform more operations we want more cells. So how do we create |
|
167 a new cell? It is very simple. As we hover our mouse above or below |
|
168 the existing cells we see a blue line, by clicking on this new line we |
|
169 can create a new cell. |
|
170 |
|
171 We have a cell, we have typed some commands in it, but how do we |
|
172 evaluate that cell? Pressing Shift along with Enter evaluates the |
|
173 cell. Alternatively we can also click on the evaluate link to evaluate |
|
174 the cell |
|
175 |
|
176 {{{ Evaluate the cell and demonstrate for both methods separately |
|
177 }}}:: |
|
178 |
|
179 matrix([[1,2], [3,4]])^(-1) |
|
180 |
|
181 After we create many cells, we may want to move between the cells. To |
|
182 move between the cells use Up and Down arrow keys. Also clicking on |
|
183 the cell will let you edit that particular cell. |
|
184 |
|
185 {{{ Move between two cells created }}} |
|
186 |
|
187 To delete a cell, clear the contents of the cell and hit backspace |
|
188 |
|
189 {{{ Clear and demonstrate deleting the cell }}}:: |
|
190 |
|
191 Makes no sense |
|
192 |
|
193 If you want to add annotations in the worksheet itself on the blue |
|
194 line that appears on hovering the mouse around the cell, Hold Shift |
|
195 and click on the line. This creates a What You See Is What You Get |
|
196 cell. |
|
197 |
|
198 {{{ Create a HTML editable cell }}} |
|
199 |
|
200 We can make our text here rich text. We can make it bold, Italics, we |
|
201 can create bulleted and enumerated lists in this area:: |
|
202 |
|
203 This text contains both the **bold** text and also *italicised* |
|
204 text. |
|
205 It also contains bulleted list: |
|
206 * Item 1 |
|
207 * Item 2 |
|
208 It also contains enumerate list: |
|
209 1. Item 1 |
|
210 2. Item 2 |
|
211 |
|
212 In the same cell we can display typeset math using the LaTeX like |
|
213 syntax:: |
|
214 |
|
215 $\int_0^\infty e^{-x} \, dx$ |
|
216 |
|
217 We enclose the math to be typeset within $ and $ or $$ and $$ as in |
|
218 LaTeX. |
|
219 |
|
220 We can also obtain help for a particular Sage command or function |
|
221 within the worksheet itself by using a question mark following the |
|
222 command:: |
|
223 |
|
224 sin? |
|
225 |
|
226 Evaluating this cell gives me the entire help for the sin function |
|
227 inline on the worksheet itself. Similarly we can also look at the |
|
228 source code of each command or function using double question mark:: |
|
229 |
|
230 matrix?? |
|
231 |
|
232 Sage notebook also provides the feature for autocompletion. To |
|
233 autocomplete a command type first few unique characters and hit tab |
|
234 key:: |
|
235 |
|
236 sudo<tab> |
|
237 |
|
238 To see all the commands starting with a specific name type those |
|
239 characters and hit tab:: |
|
240 |
|
241 plo<tab> |
|
242 |
|
243 To list all the methods that are available for a certain variable or |
|
244 a datatype we can use the variable name followed by the dot to access |
|
245 the methods available on it and then hit tab:: |
|
246 |
|
247 s = 'Hello' |
|
248 s.rep<tab> |
|
249 |
|
250 The output produced by each cell can be one of the three states. It |
|
251 can be either the full output, or truncated output or hidden output. |
|
252 The output area will display the error if the Sage code we wrote in |
|
253 the cell did not successfully execute:: |
|
254 |
|
255 a, b = 10 |
|
256 |
|
257 {{{ Show the three states }}} |
|
258 |
|
259 The default output we obtained now is a truncated output. Clicking at |
|
260 the left of the output area when the mouse pointer turns to hand gives |
|
261 us the full output, clicking again makes the output hidden and it |
|
262 cycles. |
|
263 |
|
264 Lastly, Sage supports a variety of languages and each cell on the |
|
265 worksheet can contain code written in a specific language. It is |
|
266 possible to instruct Sage to interpret the code in the language we |
|
267 have written. This can be done by putting percentage sign(%) followed |
|
268 by the name of the language. For example, to interpret the cell as |
|
269 Python code we put:: |
|
270 |
|
271 %python |
|
272 |
|
273 as the first line in the cell. Similarly we have: %sh for shell |
|
274 scripting, %fortran for Fortran, %gap for GAP and so on. Let us see |
|
275 how this works. Say I have an integer. The type of the integer in |
|
276 default Sage mode is |
|
277 {{{ Read the output }}}:: |
|
278 |
|
279 a = 1 |
|
280 type(a) |
|
281 |
|
282 Output: <type 'sage.rings.integer.Integer'> |
|
283 |
|
284 We see that Integers are Sage Integers. Now let us put %python as the |
|
285 first line of the cell and execute the same code snippet:: |
|
286 |
|
287 %python |
|
288 a = 1 |
|
289 type(a) |
|
290 |
|
291 Output: <type 'int'> |
|
292 |
|
293 Now we see that the integer is a Python integer. Why? Because now we |
|
294 instructed Sage to interpret that cell as Python code. |
|
295 |
|
296 This brings us to the end of the tutorial on using Sage. We learnt |
|
297 quite a lot about using the Notebook User Interface of Sage. We are |
|
298 now confident that we can comfortably use the notebook to learn more |
|
299 about Sage in the following tutorials. Let us summarize what we |
|
300 learnt. In this session we learnt |
|
301 |
|
302 * What is Sage |
|
303 * How to start Sage shell |
|
304 * What is Sage notebook |
|
305 * How to start the Sage notebook |
|
306 * How to create accounts and start using the notebook |
|
307 * How to create new worksheets |
|
308 * The menus available on the notebook |
|
309 * About cells in the worksheet |
|
310 * Methods to evaluate the cell, create new cells, delete the cells |
|
311 and navigate around the cells |
|
312 * To make annotations in the worksheet |
|
313 * Tab completions |
|
314 * And embedding code of other scripting languages in the cells |
|
315 |
|
316 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
317 |
|
318 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
319 |
|
320 Hope you have enjoyed and found it useful. |
|
321 Thank you! |
|
322 |
|
323 |