1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
.. Objectives
.. ----------
.. By the end of this tutorial you will --
.. 1. Get an idea of the range of things for which Sage can be used.
.. #. Know some of the functions for Calculus
.. #. Get some insight into Graphs in Sage.
.. Prerequisites
.. -------------
.. Getting Started -- Sage
.. Author : Puneeth
Internal Reviewer : Anoop Jacob Thomas<anoop@fossee.in>
External Reviewer :
Language Reviewer : Bhanukiran
Checklist OK? : <06-11-2010, Anand, OK> [2010-10-05]
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello Friends and Welcome to the tutorial on 'Using Sage'.
.. L2
{{{ show the slide with objectives }}}
.. R2
At the end of this tutorial, you will be able to,
1. Learn the range of things for which Sage can be used.
#. Know the functions used for Calculus in Sage.
#. Learn about graph theory and number theory using Sage.
.. L3
{{{ Switch to the pre-requisite slide }}}
.. R3
Before beginning this tutorial,we would suggest you to complete the
tutorial on "Getting started with Sage".
Let us begin with Calculus. We shall be looking at limits,
differentiation, integration, and Taylor polynomial.
.. L4
{{{ open sage notebook }}}
.. R4
We have our Sage notebook running. In case, you don't have it running,
start is using the command, ``sage --notebook``.
.. R5
To find the limit of the function x*sin(1/x), at x=0, we say
.. L5
::
lim(x*sin(1/x), x=0)
.. R6
We get the limit to be 0, as expected.
It is also possible to the limit at a point from one direction. For
example, let us find the limit of 1/x at x=0, when approaching from
the positive side.
.. L6
::
lim(1/x, x=0, dir='right')
.. R7
To find the limit from the negative side, we say,
.. L7
::
lim(1/x, x=0, dir='left')
.. R8
Let us now see how to perform differentiation, using Sage. We shall
find the differential of the expression ``exp(sin(x^2))/x`` w.r.t ``x``.
For this, we shall first define the expression, and then use the ``diff``
function to obtain the differential of the expression.
.. L8
::
var('x')
f = exp(sin(x^2))/x
diff(f, x)
.. R9
We can also obtain the partial differentiation of an expression w.r.t
one of the variables. Let us differentiate the expression
``exp(sin(y - x^2))/x`` w.r.t x and y.
.. L9
::
var('x y')
f = exp(sin(y - x^2))/x
diff(f, x)
diff(f, y)
.. R10
Thus we get our partial differential solution.
Now, let us look at integration. We shall use the expression obtained
from the differentiation that we did before, ``diff(f, y)`` which gave us
the expression ---``e^(sin(-x^2 + y))*cos(-x^2 + y)/x``.
The ``integrate`` command is used to obtain the integral of an
expression or function.
.. L10
::
integrate(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y)
.. R11
As we can see,we get back the correct expression. The minus sign being
inside or outside the ``sin`` function doesn't change much.
Now, let us find the value of the integral between the limits 0 and
pi/2.
.. L11
::
integral(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y, 0, pi/2)
.. R12
Hence we get our solution for the definite integration.
Let us now see how to obtain the Taylor expansion of an expression
using sage. Let us obtain the Taylor expansion of ``(x + 1)^n`` up to
degree 4 about 0.
.. L12
::
var('x n')
taylor((x+1)^n, x, 0, 4)
.. R13
We easlily got the Taylor expansion,using the function ``taylor()``.
This brings us to the end of the features of Sage for Calculus, that
we will be looking at. For more, look at the Calculus quick-ref from
the Sage Wiki.
.. L13
.. L14
{{{ show the equation on the slides }}}
.. R14
Next let us move on to Matrix Algebra.
Let us begin with solving the equation ``Ax = v``, where A is the
matrix ``matrix([[1,2],[3,4]])`` and v is the vector
``vector([1,2])``.
.. R15
To solve the equation, ``Ax = v`` we simply say
.. L15
{{{ Switch back to sage notebook page }}}
::
A = matrix([[1,2],
[3,4]])
v = vector([1,2])
x = A.solve_right(v)
x
.. R16
To solve the equation, ``xA = v`` we simply say
.. L16
::
x = A.solve_left(v)
x
.. R17
The left and right here, denote the position of ``A``, relative to x.
Now, let us look at Graph Theory in Sage.
We shall look at some ways to create graphs and some of the graph
families available in Sage.
The simplest way to define an arbitrary graph is to use a dictionary
of lists. We create a simple graph by using the ``Graph()`` function.
.. L17
::
G = Graph({0:[1,2,3], 2:[4]})
.. R18
to view the visualization of the graph, we say
.. L18
::
G.show()
.. R19
Similarly, we can obtain a directed graph using the ``DiGraph``
function.
.. L19
::
G = DiGraph({0:[1,2,3], 2:[4]})
.. R20
Sage also provides a lot of graph families which can be viewed by
typing ``graph.<tab>``. Let us obtain a complete graph with 5 vertices
and then show the graph.
.. L20
::
G = graphs.CompleteGraph(5)
G.show()
.. R21
Sage provides other functions for Number theory and
Combinatorics. Let's have a glimpse of a few of them.
``prime_range`` gives primes in the range 100 to 200.
.. L21
::
prime_range(100, 200)
.. R22
``is_prime`` checks if 1999 is a prime number or not.
.. L22
::
is_prime(1999)
.. R23
``factor(2001)`` gives the factorized form of 2001.
.. L23
::
factor(2001)
.. R24
The ``Permutations()`` gives the permutations of ``[1, 2, 3, 4]``
.. L24
::
C = Permutations([1, 2, 3, 4])
C.list()
.. R25
And the ``Combinations()`` gives all the combinations of ``[1, 2, 3, 4]``
.. L25
::
C = Combinations([1, 2, 3, 4])
C.list()
.. L26
{{{ Show summary slide }}}
.. R26
This brings us to the end of the tutorial.In this tutorial,
we have learnt to,
1. Use functions for calculus like --
- lim()-- to find out the limit of a function
- diff()-- to find out the differentiation of an expression
- integrate()-- to integrate over an expression
- integral()-- to find out the definite integral of an
expression by specifying the limits
- solve()-- to solve a function, relative to it's position.
#. Create Both a simple graph and a directed graph, using the
functions ``graph`` and ``digraph`` respectively.
#. Use functions for Number theory.For eg:
- primes_range()-- to find out the prime numbers within the
specified range
- factor()-- to find out the factorized form of the number specified
- Permutations(), Combinations()-- to obtain the required permutation
and combinations for the given set of values.
.. L27
{{{Show self assessment questions slide}}}
.. R27
Here are some self assessment questions for you to solve
1. How do you find the limit of the function ``x/sin(x)`` as ``x`` tends
to ``0`` from the negative side.
2. List all the primes between 2009 and 2900
3. Solve the system of linear equations
x-2y+3z = 7
2x+3y-z = 5
x+2y+4z = 9
.. L28
{{{solution of self assessment questions on slide}}}
.. R28
And the answers,
1. To find out the limit of an expression from the negative side,we add
an argument dir="left" as
::
lim(x/sin(x), x=0, dir="left")
2. The prime numbers from 2009 and 2900 can be obtained as,
::
prime_range(2009, 2901)
3. We shall first write the equations in matrix form and then use the
solve() function
::
A = Matrix([[1, -2, 3],
[2, 3, -1],
[1, 2, 4]])
b = vector([7, 5, 9])
x = A.solve_right(b)
To view the ouput type x
::
x
.. L29
{{{ Switch to thankyou slide }}}
.. R29
Hope you have enjoyed this tutorial and found it useful.
Thank you!
|