symbolics.rst
changeset 205 d95288e57cfc
parent 187 3b912b3fdcbf
equal deleted inserted replaced
204:65e5e2362bc9 205:d95288e57cfc
     2 -------------------
     2 -------------------
     3 
     3 
     4 This tutorial on using Sage for symbolic calculation is brought to you
     4 This tutorial on using Sage for symbolic calculation is brought to you
     5 by Fossee group.
     5 by Fossee group.
     6 
     6 
       
     7 .. #[Madhu: Sounds more or less like an ad!]
       
     8 
     7 {{{ Part of Notebook with title }}}
     9 {{{ Part of Notebook with title }}}
       
    10 
       
    11 .. #[Madhu: Please make your instructions, instructional. While
       
    12      recording if I have to read this, think what you are actually
       
    13      meaning it will take a lot of time]
     8 
    14 
     9 We would be using simple mathematical functions on the sage notebook
    15 We would be using simple mathematical functions on the sage notebook
    10 for this tutorial.
    16 for this tutorial.
       
    17 
       
    18 .. #[Madhu: What is this line doing here. I don't see much use of it]
    11 
    19 
    12 During the course of the tutorial we will learn
    20 During the course of the tutorial we will learn
    13 
    21 
    14 {{{ Part of Notebook with outline }}}
    22 {{{ Part of Notebook with outline }}}
    15 
    23 
    19 symbolic expressions and functions.
    27 symbolic expressions and functions.
    20 
    28 
    21 .. #[Nishanth]: The formatting is all messed up
    29 .. #[Nishanth]: The formatting is all messed up
    22                 First fix the formatting and compile the rst
    30                 First fix the formatting and compile the rst
    23                 The I shall review
    31                 The I shall review
    24 
    32 .. #[Madhu: Please make the above items full english sentences, not
    25 Using sage we can perform mathematical operations on symbols .
    33      the slides like points. The person recording should be able to
       
    34      read your script as is. It can read something like "we will learn
       
    35      how to define symbolic expressions in Sage, using built-in ..."]
       
    36 
       
    37 Using sage we can perform mathematical operations on symbols.
       
    38 
       
    39 .. #[Madhu: Same mistake with period symbols! Please get the
       
    40      punctuation right. Also you may have to rephrase the above
       
    41      sentence as "We can use Sage to perform sybmolic mathematical
       
    42      operations" or such]
    26 
    43 
    27 On the sage notebook type::
    44 On the sage notebook type::
    28    
    45    
    29     sin(y)
    46     sin(y)
    30 
    47 
    31 It raises a name error saying that y is not defined . But in sage we
    48 It raises a name error saying that y is not defined. But in sage we
    32 can declare y as a symbol using var function. ::
    49 can declare y as a symbol using var function.
    33    
    50 
       
    51 .. #[Madhu: But is not required]
       
    52 ::
    34     var('y')
    53     var('y')
    35    
    54    
    36 Now if you type::
    55 Now if you type::
    37 
    56 
    38     sin(y)
    57     sin(y)
    39 
    58 
    40      sage simply returns the expression .
    59      sage simply returns the expression .
       
    60 
       
    61 .. #[Madhu: Why is this line indented? Also full stop. When will you
       
    62      learn? Yes we can correct you. But corrections are for you to
       
    63      learn. If you don't learn from your mistakes, I don't know what
       
    64      to say]
    41 
    65 
    42 thus now sage treats sin(y) as a symbolic expression . You can use
    66 thus now sage treats sin(y) as a symbolic expression . You can use
    43 this to do a lot of symbolic maths using sage's built-in constants and
    67 this to do a lot of symbolic maths using sage's built-in constants and
    44 expressions .
    68 expressions .
    45 
    69 
    46 Try out ::
    70 .. #[Madhu: "Thus now"? It sounds like Dus and Nou, i.e 10 and 9 in
       
    71      Hindi! Full stop again. "a lot" doesn't mean anything until you
       
    72      quantify it or give examples.]
       
    73 
       
    74 Try out
       
    75 
       
    76 .. #[Madhu: "So let us try" sounds better]
       
    77  ::
    47    
    78    
    48    var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2
    79    var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2
    49  
    80  
    50 Similarly , we can define many algebraic and trigonometric expressions
    81 Similarly , we can define many algebraic and trigonometric expressions
    51 using sage .
    82 using sage .
    52 
    83 
       
    84 .. #[Madhu: comma again. Show some more examples?]
    53 
    85 
    54 
    86 
    55 Sage also provides a few built-in constants which are commonly used in
    87 Sage also provides a few built-in constants which are commonly used in
    56 mathematics .
    88 mathematics .
    57 
    89 
    58 example : pi,e,oo , Function n gives the numerical values of all these
    90 example : pi,e,oo , Function n gives the numerical values of all these
    59     constants.
    91     constants.
    60 
    92 
       
    93 .. #[Madhu: This doesn't sound like scripts. How will I read this
       
    94      while recording. Also if I were recording I would have read your
       
    95      third constant as Oh-Oh i.e. double O. It took me at least 30
       
    96      seconds to figure out it is infinity]
       
    97 
    61 For instance::
    98 For instance::
    62 
    99 
    63    n(e)
   100    n(e)
    64    
   101    
    65    2.71828182845905
   102    2.71828182845905
    66 
   103 
    67 gives numerical value of e.
   104 gives numerical value of e.
    68 
   105 
    69 If you look into the documentation of n by doing ::
   106 If you look into the documentation of n by doing
    70 
   107 
       
   108 .. #[Madhu: "documentation of the function "n"?]
       
   109 
       
   110 ::
    71    n(<Tab>
   111    n(<Tab>
    72 
   112 
    73 You will see what all arguments it can take etc .. It will be very
   113 You will see what all arguments it can take etc .. It will be very
    74 helpful if you look at the documentation of all functions introduced
   114 helpful if you look at the documentation of all functions introduced
    75       
   115 
       
   116 .. #[Madhu: What does etc .. mean in a script?]
    76 
   117 
    77 Also we can define the no of digits we wish to use in the numerical
   118 Also we can define the no of digits we wish to use in the numerical
    78 value . For this we have to pass an argument digits.  Type::
   119 value . For this we have to pass an argument digits.  Type
    79   
   120 
       
   121 .. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to
       
   122      use"?]
       
   123 ::
       
   124 
    80    n(pi, digits = 10)
   125    n(pi, digits = 10)
    81 
   126 
    82 Apart from the constants sage also has a lot of builtin functions like
   127 Apart from the constants sage also has a lot of builtin functions like
    83 sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ...
   128 sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ...
    84 lets try some out on the sage notebook. ::
   129 lets try some out on the sage notebook.
       
   130 
       
   131 .. #[Madhu: Here "a lot" makes sense]
       
   132 ::
    85      
   133      
    86    sin(pi/2)
   134    sin(pi/2)
    87    
   135    
    88    arctan(oo)
   136    arctan(oo)
    89      
   137      
    94 an arbitrary function with desired name in the following way.::
   142 an arbitrary function with desired name in the following way.::
    95 
   143 
    96        var('x') function(<tab> {{{ Just to show the documentation
   144        var('x') function(<tab> {{{ Just to show the documentation
    97        extend this line }}} function('f',x)
   145        extend this line }}} function('f',x)
    98 
   146 
       
   147 .. #[Madhu: What will the person recording show in the documentation
       
   148      without a script for it? Please don't assume recorder can cook up
       
   149      things while recording. It is impractical]
       
   150 
    99 Here f is the name of the function and x is the independent variable .
   151 Here f is the name of the function and x is the independent variable .
   100 Now we can define f(x) to be ::
   152 Now we can define f(x) to be ::
   101 
   153 
   102      f(x) = x/2 + sin(x)
   154      f(x) = x/2 + sin(x)
   103 
   155 
   104 Evaluating this function f for the value x=pi returns pi/2.::
   156 Evaluating this function f for the value x=pi returns pi/2.::
   105 	   
   157 	   
   106 	   f(pi)
   158 	   f(pi)
   107 
   159 
   108 We can also define function that are not continuous but defined
   160 We can also define functions that are not continuous but defined
   109 piecewise.  We will be using a function which is a parabola between 0
   161 piecewise.  We will be using a function which is a parabola between 0
   110 to 1 and a constant from 1 to 2 .  type the following as given on the
   162 to 1 and a constant from 1 to 2 .  type the following as given on the
   111 screen::
   163 screen
       
   164 
       
   165 .. #[Madhu: Instead of "We will be using ..." how about "Let us define
       
   166      a function ..."]
       
   167 ::
   112       
   168       
   113 
   169 
   114       var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the
   170       var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the
   115       documentation extend this line }}}
   171       documentation extend this line }}}
   116       f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f
   172       f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f
   117 
   173 
   118 Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3)
   174 Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3)
   119 
   175 
       
   176 .. #[Madhu: Again this doesn't sound like a script]
       
   177 
   120 for f(3) it raises a value not defined in domain error .
   178 for f(3) it raises a value not defined in domain error .
   121 
   179 
   122 
   180 
   123 Apart from operations on expressions and functions one can also use
   181 Apart from operations on expressions and functions one can also use
   124 them for series .
   182 them for series .
   125 
   183 
       
   184 .. #[Madhu: I am not able to understand this line. "Use them as
       
   185 .. series". Use what as series?]
       
   186 
   126 We first define a function f(n) in the way discussed above.::
   187 We first define a function f(n) in the way discussed above.::
   127 
   188 
   128    var('n') function('f', n)
   189    var('n') function('f', n)
   129 
   190 
       
   191 .. #[Madhu: Shouldn't this be on 2 separate lines?]
   130 
   192 
   131 To sum the function for a range of discrete values of n, we use the
   193 To sum the function for a range of discrete values of n, we use the
   132 sage function sum.
   194 sage function sum.
   133 
   195 
   134  For a convergent series , f(n)=1/n^2 we can say ::
   196 For a convergent series , f(n)=1/n^2 we can say ::
   135    
   197    
   136    var('n') function('f', n)
   198    var('n') function('f', n)
   137 
   199 
   138    f(n) = 1/n^2
   200    f(n) = 1/n^2
   139 
   201 
   140    sum(f(n), n, 1, oo)
   202    sum(f(n), n, 1, oo)
   141 
   203 
   142 For the famous Madhava series :: var('n') function('f', n)
   204 For the famous Madhava series :: var('n') function('f', n)
       
   205 
       
   206 .. #[Madhu: What is this? your double colon says it must be code block
       
   207      but where is the indentation and other things. How will the
       
   208      recorder know about it?]
   143 
   209 
   144     f(n) = (-1)^(n-1)*1/(2*n - 1)
   210     f(n) = (-1)^(n-1)*1/(2*n - 1)
   145 
   211 
   146 This series converges to pi/4. It was used by ancient Indians to
   212 This series converges to pi/4. It was used by ancient Indians to
   147 interpret pi.
   213 interpret pi.
       
   214 
       
   215 .. #[Madhu: I am losing the context. Please add something to bring
       
   216      this thing to the context]
   148 
   217 
   149 For a divergent series, sum would raise a an error 'Sum is
   218 For a divergent series, sum would raise a an error 'Sum is
   150 divergent' :: 
   219 divergent' :: 
   151 	
   220 	
   152 	var('n') 
   221 	var('n') 
   156 
   225 
   157 
   226 
   158 
   227 
   159 We can perform simple calculus operation using sage
   228 We can perform simple calculus operation using sage
   160 
   229 
       
   230 .. #[Madhu: When you switch to irrelevant topics make sure you use
       
   231     some connectors in English like "Moving on let us see how to
       
   232     perform simple calculus operations using Sage" or something like
       
   233     that]
   161 For example lets try an expression first ::
   234 For example lets try an expression first ::
   162 
   235 
   163     diff(x**2+sin(x),x) 2x+cos(x)
   236     diff(x**2+sin(x),x) 2x+cos(x)
   164 
   237 
   165 The diff function differentiates an expression or a function . Its
   238 The diff function differentiates an expression or a function . Its
   166 first argument is expression or function and second argument is the
   239 first argument is expression or function and second argument is the
   167 independent variable .
   240 independent variable .
       
   241 
       
   242 .. #[Madhu: Full stop, Full stop, Full stop]
   168 
   243 
   169 We have already tried an expression now lets try a function ::
   244 We have already tried an expression now lets try a function ::
   170 
   245 
   171    f=exp(x^2)+arcsin(x) diff(f(x),x)
   246    f=exp(x^2)+arcsin(x) diff(f(x),x)
   172 
   247 
   173 To get a higher order differentiation we need to add an extra argument
   248 To get a higher order differentiation we need to add an extra argument
   174 for order ::
   249 for order ::
   175  
   250  
   176    diff(<tab> diff(f(x),x,3)
   251    diff(<tab> diff(f(x),x,3)
   177 
   252 
       
   253 .. #[Madhu: Please try to be more explicit saying third argument]
   178 
   254 
   179 in this case it is 3.
   255 in this case it is 3.
   180 
   256 
   181 
   257 
   182 Just like differentiation of expression you can also integrate them ::
   258 Just like differentiation of expression you can also integrate them ::
   183 
   259 
   184      x = var('x') s = integral(1/(1 + (tan(x))**2),x) s
   260      x = var('x') s = integral(1/(1 + (tan(x))**2),x) s
   185 
   261 
   186 
   262 .. #[Madhu: Two separate lines.]
   187 
   263 
   188 To find factors of an expression use the function factor
   264 To find the factors of an expression use the "factor" function
   189 
   265 
       
   266 .. #[Madhu: See the diff]
       
   267 
       
   268 ::
   190     factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f =
   269     factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f =
   191     factor(y)
   270     factor(y)
   192 
   271 
   193 One can also simplify complicated expression using sage ::
   272 One can also simplify complicated expression using sage ::
   194     f.simplify_full()
   273     f.simplify_full()
   196 This simplifies the expression fully . You can also do simplification
   275 This simplifies the expression fully . You can also do simplification
   197 of just the algebraic part and the trigonometric part ::
   276 of just the algebraic part and the trigonometric part ::
   198 
   277 
   199     f.simplify_exp() f.simplify_trig()
   278     f.simplify_exp() f.simplify_trig()
   200     
   279     
       
   280 .. #[Madhu: Separate lines?]
   201 
   281 
   202 One can also find roots of an equation by using find_root function::
   282 One can also find roots of an equation by using find_root function::
   203 
   283 
   204     phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2)
   284     phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2)
       
   285 
       
   286 .. #[Madhu: Separate lines?]
   205 
   287 
   206 Lets substitute this solution into the equation and see we were
   288 Lets substitute this solution into the equation and see we were
   207 correct ::
   289 correct ::
   208 
   290 
   209      var('phi') f(phi)=cos(phi)-sin(phi)
   291      var('phi') f(phi)=cos(phi)-sin(phi)
   210      root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root)
   292      root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root)
   211 
   293 
       
   294 .. #[Madhu: Separate lines?]
   212 
   295 
   213 as we can see the solution is almost equal to zero .
   296 as we can see the solution is almost equal to zero .
   214 
   297 
       
   298 .. #[Madhu: So what?]
   215 
   299 
   216 We can also define symbolic matrices ::
   300 We can also define symbolic matrices ::
   217 
   301 
   218 
   302 
   219 
   303 
   220    var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A
   304    var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A
   221 
   305 
   222 
   306 .. #[Madhu: Why don't you break the lines?]
   223 Now lets do some of the matrix operations on this matrix ::
   307 
   224 
   308 Now lets do some of the matrix operations on this matrix
   225 
   309 
       
   310 .. #[Madhu: Why don't you break the lines? Also how do you connect
       
   311      this up? Use some transformation keywords in English]
       
   312 ::
   226     A.det() A.inverse()
   313     A.det() A.inverse()
       
   314 
       
   315 .. #[Madhu: Why don't you break the lines?]
   227 
   316 
   228 You can do ::
   317 You can do ::
   229     
   318     
   230     A.<Tab>
   319     A.<Tab>
   231 
   320 
   232 To see what all operations are available
   321 To see what all operations are available
   233 
   322 
       
   323 .. #[Madhu: Sounds very abrupt]
   234 
   324 
   235 {{{ Part of the notebook with summary }}}
   325 {{{ Part of the notebook with summary }}}
   236 
   326 
   237 So in this tutorial we learnt how to
   327 So in this tutorial we learnt how to
   238 
   328 
   245 diff() and integrate() for calculus operations .  
   335 diff() and integrate() for calculus operations .  
   246 Finding roots , factors and simplifying expression using find_root(), 
   336 Finding roots , factors and simplifying expression using find_root(), 
   247 factor() , simplify_full, simplify_exp , simplify_trig .
   337 factor() , simplify_full, simplify_exp , simplify_trig .
   248 Substituting values in expression using substitute function.
   338 Substituting values in expression using substitute function.
   249 And finally creating symbolic matrices and performing operation on them .
   339 And finally creating symbolic matrices and performing operation on them .
       
   340 
       
   341 .. #[Madhu: See what Nishanth is doing. He has written this as
       
   342      points. So easy to read out while recording. You may want to
       
   343      reorganize like that]