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