diff options
Diffstat (limited to 'using_sage/script.rst')
-rw-r--r-- | using_sage/script.rst | 290 |
1 files changed, 234 insertions, 56 deletions
diff --git a/using_sage/script.rst b/using_sage/script.rst index e32ac0d..45fe950 100644 --- a/using_sage/script.rst +++ b/using_sage/script.rst @@ -22,58 +22,100 @@ Script ------ -{{{ show the welcome slide }}} +.. L1 -Hello Friends. Welcome to this tutorial on using Sage. +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -{{{ show the slide with outline }}} +.. R1 -In this tutorial we shall quickly look at a few examples of using Sage -for Linear Algebra, Calculus, Graph Theory and Number theory. +Hello Friends and Welcome to the tutorial on 'Using Sage'. -{{{ show the slide with Calculus outline }}} +.. 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. -{{{ show sage notebook }}} +.. 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) + 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='above') + 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='below') + 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. -Let us now see how to differentiate, using Sage. We shall find the -differential of the expression ``exp(sin(x^2))/x`` w.r.t ``x``. 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') @@ -83,56 +125,95 @@ one of the variables. Let us differentiate the expression 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)`` --- -``e^(sin(-x^2 + y))*cos(-x^2 + y)/x``. The ``integrate`` command is -used to obtain the integral of an expression or function. +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) -We get back the correct expression. The minus sign being inside or -outside the ``sin`` function doesn't change much. +.. 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. -Next let us move on to Matrix Algebra. +.. 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 }}} :: - x = solve_right(A, v) + 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 = solve_left(A, v) + x = A.solve_left(v) + x -The left and right here, denote the position of ``A``, relative to x. +.. R17 -#[Puneeth]: any suggestions on what more to add? +The left and right here, denote the position of ``A``, relative to x. Now, let us look at Graph Theory in Sage. @@ -140,85 +221,182 @@ 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 +of lists. We create a simple graph by using the ``Graph()`` function. + +.. L17 :: - G = Graph({0:[1,2,3], 2:[4]}) + G = Graph({0:[1,2,3], 2:[4]}) + +.. R18 + +to view the visualization of the graph, we say -We say +.. L18 :: - G.show() + G.show() -to view the visualization of the graph. +.. R19 Similarly, we can obtain a directed graph using the ``DiGraph`` function. + +.. L19 :: - G = DiGraph({0:[1,2,3], 2:[4]}) + 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 = graphs.CompleteGraph(5) - G.show() + 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 :: - prime_range(100, 200) + is_prime(1999) + +.. R23 -gives primes in the range 100 to 200. +``factor(2001)`` gives the factorized form of 2001. +.. L23 :: - is_prime(1999) + factor(2001) -checks if 1999 is a prime number or not. +.. R24 +The ``Permutations()`` gives the permutations of ``[1, 2, 3, 4]`` + +.. L24 :: - factor(2001) + 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 -gives the factorized form of 2001. +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 postion. + #. 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="below" as :: - C = Permutations([1, 2, 3, 4]) - C.list() + lim(x/sin(x), x=0, dir="left") -gives the permutations of ``[1, 2, 3, 4]`` +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 :: - C = Combinations([1, 2, 3, 4]) - C.list() + A = Matrix([[1, -2, 3], + [2, 3, -1], + [1, 2, 4]]) -gives all the combinations of ``[1, 2, 3, 4]`` - -That brings us to the end of this session showing various features -available in Sage. + b = vector([7, 5, 9]) -.. #[[Anoop: I feel we should add more slides, a possibility is to add - the code which they are required to type in, I also feel we should - add some review problems for them to try out.]] + x = A.solve_right(b) + +To view the ouput type x +:: + + x -{{{ Show summary slide }}} +.. L29 -We have looked at some of the functions available for Linear Algebra, -Calculus, Graph Theory and Number theory. +{{{ Switch to thankyou slide }}} -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +.. R29 -Hope you have enjoyed and found it useful. +Hope you have enjoyed this tutorial and found it useful. Thank you! |