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