using-sage/script.rst
changeset 522 d33698326409
parent 521 88a01948450d
child 523 54bdda4aefa5
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. By the end of this tutorial you will --
       
     5 
       
     6 .. 1. Get an idea of the range of things for which Sage can be used. 
       
     7 .. #. Know some of the functions for Calculus
       
     8 .. #. Get some insight into Graphs in Sage. 
       
     9 
       
    10 
       
    11 .. Prerequisites
       
    12 .. -------------
       
    13 
       
    14 .. Getting Started -- Sage  
       
    15      
       
    16 .. Author              : Puneeth 
       
    17    Internal Reviewer   : Anoop Jacob Thomas<anoop@fossee.in>
       
    18    External Reviewer   :
       
    19    Language Reviewer   : Bhanukiran
       
    20    Checklist OK?       : <06-11-2010, Anand, OK> [2010-10-05]
       
    21 
       
    22 Script
       
    23 ------
       
    24 
       
    25 {{{ show the welcome slide }}}
       
    26 
       
    27 Hello Friends. Welcome to this tutorial on using Sage.
       
    28 
       
    29 {{{ show the slide with outline }}} 
       
    30 
       
    31 In this tutorial we shall quickly look at a few examples of using Sage
       
    32 for Linear Algebra, Calculus, Graph Theory and Number theory.
       
    33 
       
    34 {{{ show the slide with Calculus outline }}} 
       
    35 
       
    36 Let us begin with Calculus. We shall be looking at limits,
       
    37 differentiation, integration, and Taylor polynomial.
       
    38 
       
    39 {{{ show sage notebook }}}
       
    40 
       
    41 We have our Sage notebook running. In case, you don't have it running,
       
    42 start is using the command, ``sage --notebook``.
       
    43 
       
    44 To find the limit of the function x*sin(1/x), at x=0, we say
       
    45 ::
       
    46 
       
    47    lim(x*sin(1/x), x=0)
       
    48 
       
    49 We get the limit to be 0, as expected. 
       
    50 
       
    51 It is also possible to the limit at a point from one direction. For
       
    52 example, let us find the limit of 1/x at x=0, when approaching from
       
    53 the positive side.
       
    54 ::
       
    55 
       
    56     lim(1/x, x=0, dir='above')
       
    57 
       
    58 To find the limit from the negative side, we say,
       
    59 ::
       
    60 
       
    61     lim(1/x, x=0, dir='below')   
       
    62 
       
    63 Let us now see how to differentiate, using Sage. We shall find the
       
    64 differential of the expression ``exp(sin(x^2))/x`` w.r.t ``x``. We
       
    65 shall first define the expression, and then use the ``diff`` function
       
    66 to obtain the differential of the expression.
       
    67 ::
       
    68 
       
    69     var('x')
       
    70     f = exp(sin(x^2))/x
       
    71 
       
    72     diff(f, x)
       
    73 
       
    74 We can also obtain the partial differentiation of an expression w.r.t
       
    75 one of the variables. Let us differentiate the expression
       
    76 ``exp(sin(y - x^2))/x`` w.r.t x and y.
       
    77 ::
       
    78 
       
    79     var('x y')
       
    80     f = exp(sin(y - x^2))/x
       
    81 
       
    82     diff(f, x)
       
    83 
       
    84     diff(f, y)
       
    85 
       
    86 Now, let us look at integration. We shall use the expression obtained
       
    87 from the differentiation that we did before, ``diff(f, y)`` ---
       
    88 ``e^(sin(-x^2 + y))*cos(-x^2 + y)/x``. The ``integrate`` command is
       
    89 used to obtain the integral of an expression or function.
       
    90 ::
       
    91 
       
    92     integrate(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y)
       
    93 
       
    94 We get back the correct expression. The minus sign being inside or
       
    95 outside the ``sin`` function doesn't change much. 
       
    96 
       
    97 Now, let us find the value of the integral between the limits 0 and
       
    98 pi/2. 
       
    99 ::
       
   100 
       
   101     integral(e^(sin(-x^2 + y))*cos(-x^2 + y)/x, y, 0, pi/2)
       
   102 
       
   103 Let us now see how to obtain the Taylor expansion of an expression
       
   104 using sage. Let us obtain the Taylor expansion of ``(x + 1)^n`` up to
       
   105 degree 4 about 0.
       
   106 ::
       
   107 
       
   108     var('x n')
       
   109     taylor((x+1)^n, x, 0, 4)
       
   110 
       
   111 This brings us to the end of the features of Sage for Calculus, that
       
   112 we will be looking at. For more, look at the Calculus quick-ref from
       
   113 the Sage Wiki. 
       
   114 
       
   115 Next let us move on to Matrix Algebra. 
       
   116 
       
   117 {{{ show the equation on the slides }}}
       
   118 
       
   119 Let us begin with solving the equation ``Ax = v``, where A is the
       
   120 matrix ``matrix([[1,2],[3,4]])`` and v is the vector
       
   121 ``vector([1,2])``. 
       
   122 
       
   123 To solve the equation, ``Ax = v`` we simply say
       
   124 ::
       
   125 
       
   126     x = solve_right(A, v)
       
   127 
       
   128 To solve the equation, ``xA = v`` we simply say
       
   129 ::
       
   130 
       
   131     x = solve_left(A, v)
       
   132 
       
   133 The left and right here, denote the position of ``A``, relative to x. 
       
   134 
       
   135 #[Puneeth]: any suggestions on what more to add?
       
   136 
       
   137 Now, let us look at Graph Theory in Sage. 
       
   138 
       
   139 We shall look at some ways to create graphs and some of the graph
       
   140 families available in Sage. 
       
   141 
       
   142 The simplest way to define an arbitrary graph is to use a dictionary
       
   143 of lists. We create a simple graph by
       
   144 ::
       
   145 
       
   146   G = Graph({0:[1,2,3], 2:[4]})
       
   147 
       
   148 We say 
       
   149 ::
       
   150 
       
   151   G.show()
       
   152 
       
   153 to view the visualization of the graph. 
       
   154 
       
   155 Similarly, we can obtain a directed graph using the ``DiGraph``
       
   156 function. 
       
   157 ::
       
   158 
       
   159   G = DiGraph({0:[1,2,3], 2:[4]})
       
   160 
       
   161 
       
   162 Sage also provides a lot of graph families which can be viewed by
       
   163 typing ``graph.<tab>``. Let us obtain a complete graph with 5 vertices
       
   164 and then show the graph. 
       
   165 ::
       
   166 
       
   167   G = graphs.CompleteGraph(5)
       
   168 
       
   169   G.show()
       
   170 
       
   171 
       
   172 Sage provides other functions for Number theory and
       
   173 Combinatorics. Let's have a glimpse of a few of them.  
       
   174 
       
   175 
       
   176 ::
       
   177 
       
   178   prime_range(100, 200)
       
   179 
       
   180 gives primes in the range 100 to 200. 
       
   181 
       
   182 ::
       
   183 
       
   184   is_prime(1999) 
       
   185 
       
   186 checks if 1999 is a prime number or not. 
       
   187 
       
   188 ::
       
   189 
       
   190   factor(2001)
       
   191 
       
   192 gives the factorized form of 2001. 
       
   193 
       
   194 ::
       
   195 
       
   196   C = Permutations([1, 2, 3, 4])
       
   197   C.list()
       
   198 
       
   199 gives the permutations of ``[1, 2, 3, 4]``
       
   200 
       
   201 ::
       
   202 
       
   203   C = Combinations([1, 2, 3, 4])
       
   204   C.list()
       
   205 
       
   206 gives all the combinations of ``[1, 2, 3, 4]``
       
   207   
       
   208 That brings us to the end of this session showing various features
       
   209 available in Sage. 
       
   210 
       
   211 .. #[[Anoop: I feel we should add more slides, a possibility is to add
       
   212    the code which they are required to type in, I also feel we should
       
   213    add some review problems for them to try out.]]
       
   214 
       
   215 {{{ Show summary slide }}}
       
   216 
       
   217 We have looked at some of the functions available for Linear Algebra,
       
   218 Calculus, Graph Theory and Number theory.   
       
   219 
       
   220 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   221 
       
   222 Hope you have enjoyed and found it useful.
       
   223 Thank you!
       
   224