#symbolics.rst#
changeset 418 8a42b4203f6d
equal deleted inserted replaced
417:fc71d5c27ce6 418:8a42b4203f6d
       
     1 Symbolics with Sage
       
     2 -------------------
       
     3 
       
     4 Hello friends and welcome to this tutorial on symbolics with sage.
       
     5 
       
     6 
       
     7 .. #[Madhu: Sounds more or less like an ad!]
       
     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 
       
    18 .. #[Madhu: What is this line doing here. I don't see much use of it]
       
    19 
       
    20 During the course of the tutorial we will learn
       
    21 
       
    22 {{{ Part of Notebook with outline }}}
       
    23 
       
    24 To define symbolic expressions in sage.  Use built-in costants and
       
    25 function. Integration, differentiation using sage. Defining
       
    26 matrices. Defining Symbolic functions. Simplifying and solving
       
    27 symbolic expressions and functions.
       
    28 
       
    29 .. #[Nishanth]: The formatting is all messed up
       
    30                 First fix the formatting and compile the rst
       
    31                 The I shall review
       
    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 
       
    44 On the sage notebook type::
       
    45    
       
    46     sin(y)
       
    47 
       
    48 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.
       
    50 
       
    51 .. #[Madhu: But is not required]
       
    52 ::
       
    53     var('y')
       
    54    
       
    55 Now if you type::
       
    56 
       
    57     sin(y)
       
    58 
       
    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]
       
    65 
       
    66 thus now sage treats sin(y) as a symbolic expression . You can use
       
    67 this to do a lot of symbolic maths using sage's built-in constants and
       
    68 expressions .
       
    69 
       
    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  ::
       
    78    
       
    79    var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2
       
    80  
       
    81 Similarly , we can define many algebraic and trigonometric expressions
       
    82 using sage .
       
    83 
       
    84 .. #[Madhu: comma again. Show some more examples?]
       
    85 
       
    86 
       
    87 Sage also provides a few built-in constants which are commonly used in
       
    88 mathematics .
       
    89 
       
    90 example : pi,e,oo , Function n gives the numerical values of all these
       
    91     constants.
       
    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 
       
    98 For instance::
       
    99 
       
   100    n(e)
       
   101    
       
   102    2.71828182845905
       
   103 
       
   104 gives numerical value of e.
       
   105 
       
   106 If you look into the documentation of n by doing
       
   107 
       
   108 .. #[Madhu: "documentation of the function "n"?]
       
   109 
       
   110 ::
       
   111    n(<Tab>
       
   112 
       
   113 You will see what all arguments it can take etc .. It will be very
       
   114 helpful if you look at the documentation of all functions introduced
       
   115 
       
   116 .. #[Madhu: What does etc .. mean in a script?]
       
   117 
       
   118 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
       
   120 
       
   121 .. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to
       
   122      use"?]
       
   123 ::
       
   124 
       
   125    n(pi, digits = 10)
       
   126 
       
   127 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 ...
       
   129 lets try some out on the sage notebook.
       
   130 
       
   131 .. #[Madhu: Here "a lot" makes sense]
       
   132 ::
       
   133      
       
   134    sin(pi/2)
       
   135    
       
   136    arctan(oo)
       
   137      
       
   138    log(e,e)
       
   139 
       
   140 
       
   141 Given that we have defined variables like x,y etc .. , We can define
       
   142 an arbitrary function with desired name in the following way.::
       
   143 
       
   144        var('x') function(<tab> {{{ Just to show the documentation
       
   145        extend this line }}} function('f',x)
       
   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 
       
   151 Here f is the name of the function and x is the independent variable .
       
   152 Now we can define f(x) to be ::
       
   153 
       
   154      f(x) = x/2 + sin(x)
       
   155 
       
   156 Evaluating this function f for the value x=pi returns pi/2.::
       
   157 	   
       
   158 	   f(pi)
       
   159 
       
   160 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
       
   162 to 1 and a constant from 1 to 2 .  type the following as given on the
       
   163 screen
       
   164 
       
   165 .. #[Madhu: Instead of "We will be using ..." how about "Let us define
       
   166      a function ..."]
       
   167 ::
       
   168       
       
   169 
       
   170       var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the
       
   171       documentation extend this line }}}
       
   172       f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f
       
   173 
       
   174 Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3)
       
   175 
       
   176 .. #[Madhu: Again this doesn't sound like a script]
       
   177 
       
   178 for f(3) it raises a value not defined in domain error .
       
   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 
       
   187 We first define a function f(n) in the way discussed above.::
       
   188 
       
   189    var('n') function('f', n)
       
   190 
       
   191 .. #[Madhu: Shouldn't this be on 2 separate lines?]
       
   192 
       
   193 To sum the function for a range of discrete values of n, we use the
       
   194 sage function sum.
       
   195 
       
   196 For a convergent series , f(n)=1/n^2 we can say ::
       
   197    
       
   198    var('n') function('f', n)
       
   199 
       
   200    f(n) = 1/n^2
       
   201 
       
   202    sum(f(n), n, 1, oo)
       
   203 
       
   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?]
       
   209 
       
   210     f(n) = (-1)^(n-1)*1/(2*n - 1)
       
   211 
       
   212 This series converges to pi/4. It was used by ancient Indians to
       
   213 interpret pi.
       
   214 
       
   215 .. #[Madhu: I am losing the context. Please add something to bring
       
   216      this thing to the context]
       
   217 
       
   218 For a divergent series, sum would raise a an error 'Sum is
       
   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 ::
       
   235 
       
   236     diff(x**2+sin(x),x) 2x+cos(x)
       
   237 
       
   238 The diff function differentiates an expression or a function . Its
       
   239 first argument is expression or function and second argument is the
       
   240 independent variable .
       
   241 
       
   242 .. #[Madhu: Full stop, Full stop, Full stop]
       
   243 
       
   244 We have already tried an expression now lets try a function ::
       
   245 
       
   246    f=exp(x^2)+arcsin(x) diff(f(x),x)
       
   247 
       
   248 To get a higher order differentiation we need to add an extra argument
       
   249 for order ::
       
   250  
       
   251    diff(<tab> diff(f(x),x,3)
       
   252 
       
   253 .. #[Madhu: Please try to be more explicit saying third argument]
       
   254 
       
   255 in this case it is 3.
       
   256 
       
   257 
       
   258 Just like differentiation of expression you can also integrate them ::
       
   259 
       
   260      x = var('x') s = integral(1/(1 + (tan(x))**2),x) s
       
   261 
       
   262 .. #[Madhu: Two separate lines.]
       
   263 
       
   264 To find the factors of an expression use the "factor" function
       
   265 
       
   266 .. #[Madhu: See the diff]
       
   267 
       
   268 ::
       
   269     factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f =
       
   270     factor(y)
       
   271 
       
   272 One can also simplify complicated expression using sage ::
       
   273     f.simplify_full()
       
   274 
       
   275 This simplifies the expression fully . You can also do simplification
       
   276 of just the algebraic part and the trigonometric part ::
       
   277 
       
   278     f.simplify_exp() f.simplify_trig()
       
   279     
       
   280 .. #[Madhu: Separate lines?]
       
   281 
       
   282 One can also find roots of an equation by using find_root function::
       
   283 
       
   284     phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2)
       
   285 
       
   286 .. #[Madhu: Separate lines?]
       
   287 
       
   288 Lets substitute this solution into the equation and see we were
       
   289 correct ::
       
   290 
       
   291      var('phi') f(phi)=cos(phi)-sin(phi)
       
   292      root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root)
       
   293 
       
   294 .. #[Madhu: Separate lines?]
       
   295 
       
   296 as we can see the solution is almost equal to zero .
       
   297 
       
   298 .. #[Madhu: So what?]
       
   299 
       
   300 We can also define symbolic matrices ::
       
   301 
       
   302 
       
   303 
       
   304    var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A
       
   305 
       
   306 .. #[Madhu: Why don't you break the lines?]
       
   307 
       
   308 Now lets do some of the matrix operations on this matrix
       
   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 ::
       
   313     A.det() A.inverse()
       
   314 
       
   315 .. #[Madhu: Why don't you break the lines?]
       
   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 
       
   325 {{{ Part of the notebook with summary }}}
       
   326 
       
   327 So in this tutorial we learnt how to
       
   328 
       
   329 
       
   330 We learnt about defining symbolic expression and functions .  
       
   331 And some built-in constants and functions .  
       
   332 Getting value of built-in constants using n function.  
       
   333 Using Tab to see the documentation.  
       
   334 Also we learnt how to sum a series using sum function.  
       
   335 diff() and integrate() for calculus operations .  
       
   336 Finding roots , factors and simplifying expression using find_root(), 
       
   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]