 Let us get started. We will continue with the rest of the high level stuff we will be doing in Python. The next is the good old matrix based solution of equations. Look at those three equations we have taken up as a sample. So we have solved it also and kept the solution ready. How do we solve such equations using matrices? You know the answer. Like I said, we will spend more time typing the question and this in data n than typing the solution statement which you can see before you. A equal to that which is a representation of the, we are trying to solve A x equal to b for x where A B x are all matrices. So like I said before, we typed the more things for the problem statement than for solving. There you go. If you confirm, you want to check. You have to get A into x. Let us do the dot product of A and x. Where x is our solution, we get 1 minus 2 and some funny number. Let us go back to the equation we started with. It is 1 minus 2. Third one is 0 but what we have is a floating point number 1.1 something to the power minus 10 power minus 16. That is close enough to 0 but it should actually be 0. So we need to know whether there is a round off error or whether there is a very small value there. So Python provides a special function called all close that checks whether, sorry I forgot to store A x but this tells you true. So we know that this is the right solution. That 1.1 something to power 10 power minus 16 is the smallest possible representable value in Python, the floating point numbers and floating point computation is always subject to some minor round off errors. Floating point computation is a large enough subject by itself. So we will not go there but just remember that you need to take care to understand the intricacies of floating point computation. If you are going to do lot of computation in the any computer domain and in any language not just Python. Let us now take up the next one which is the solution of say polynomial equations. We want to solve something like x squared minus 5x plus 6. So let us look at the equation and try to understand what we are trying to do. The solution of equations, roots of polynomials or rather we want to calculate the roots of x squared minus 5x plus 6. Of course, we can manually and mentally factorize this as x minus 3 into x minus 2 and we know the roots are 3 and 2. But we are choosing this as a sample problem to explain the method or because this is the type of problem that one expects to solve using this. We will try a few others and understand this. The first thing to remember in fact about the only thing to remember in solution of polynomial equations is to represent the coefficients as a list with the coefficient of the highest degree first and to remember that if there is any term missing to put a 0 here, then once again solution is simplicity itself that is all. Let us try something little more complex. Let us try a cubic equation. Now this is the polynomial O equal to let us, this is the polynomial that we are representing. Let us try and plot a chart of its behavior to understand where the roots are and then solve it finally. So, those are the two places where we can see it. Actually, there must be a third root. Maybe we will try. There you see. So, there is a root somewhere near 7, two roots very close to 0. If you want to see all the roots lot more clearly, you have to change the limits. That is okay. Let us try and solve this now. So, how to solve this? Check our co-eps are ready. So, minus 7, minus 1.5, minus 7.75, minus 1.52, plus 1.27 Rg3, roots of that cubic polynomial we gave. Let us try one more. I would like you to try this equation. I want to solve this or the polynomial for which I want to find the roots is let us say x power 4. Please try it out. It is a good idea to try and plot it first and then get a feel for where the roots are and then get the roots. That is x power 4 minus 16 x cube minus 30 x square plus 17. Note that one term is missing. So, you have to put a 0 in the co-eps carefully. Write out. It has been pretty easy. The polynomial given was this minus 16, minus 30, the 0 followed by 17. You will notice that for the first time we got two complex roots and two real roots. You will notice the real roots are given with 0 as the multiplication for j. The moment one complex root is present, all are presented in the complex notation. So, the two real roots are somewhere around 0.7, 0.65 and somewhere around 17, which is to be expected because if you ignore everything else, the equation is x power 4 minus 16 x cube, which approximately which has a root in x equal to 16 and for small values of x, you are trying to solve x power 4 equal to, I mean 30 x square equal to 17, which will give you a very good guess that x is near 0.5. Let us continue. These are polynomial equations, but what if you want to solve an equation which is not representable in a polynomial form or which is a transcendental function or of some slightly different nature. That is the next question we are going to ask. Let us say we are trying to solve what? What are the samples we have? We are trying to solve the root of sin z plus cos square z. This is the function for which we are trying to find a root near to 0. So, the first thing to do is to actually define a function which has this behavior, like we did earlier, return. So, we can see, plot it and see. So, this is how the equation looks like, the root near 0. Obviously, sin cos are periodic functions, so it will have many roots separated by 2 pi most likely, but let us try to find the solution of the root solution of this problem that is find the root near as to 0. Now, for that, we cannot just type solve like before. We need to do some little extra work. We need to choose a special function called fsol which is available in the optimized module of psi pi. Psi pi is a library with many sub libraries. Optimization libraries are one component of psi pi library. So, now, we have imported fsol. For fsol, we have to give two arguments. One is the function itself and the other is the initial estimate or the point near which you want a solution. So, we get the answer minus 0.66, it seems like, like okay. Now, if you want to see whether there is a approximate period for it, we can try. Yes, there is a root very close, so on. So, if you look at the chart, we originally drew there are areas where it will cross the line y equal to 0. So, each of that would give us a root. Now, let us take a minute to look at this. Please note that fsol gets two parameters and one of them is a function. We are not giving a value. We are giving a function itself. Why? These sort of equations which we have given have to be solved iteratively. What do you mean by iteratively? fsol has to do something like okay, this is the function g of x or f of x or alpha of x whatever be the name and this is how to evaluate it. So, it has to evaluate it at a few points near the required solution, judge which way to improve and keep on going till the method converges. For that reason, fsol needs to execute a function which evaluates it rather than be given a set of values. So, fsol takes the function itself as a parameter, which brings us to a very interesting point about Python, the language. Functions are what are called first class objects in Python. They can be passed as arguments to other functions. They can be received as return values from other functions. You can store functions in a variable and do things. Let us see the last bit so that we understand that what is happening is nothing special in Python but is extremely difficult to do in almost all other languages. Let us say we have function which is purely for the purpose of illustration. If you give a number a, it will give you a star a plus 1. Now, Python allows you to do something like this. Please note there is no noise, no shouting from the interpreter. In other words, it is a function and so is this. So, what does sq do? Simple, it does the same thing as square plus. It is in fact just another name for square plus. So, functions can be passed around as values. It all of which is combined in a simple statement called functions are first class objects. So, this gives us a lot of power. You see the same thing can be done but you have to play around with function pointers and so on. We keep talking about Python being a very high level language and function being first class object is one of the characteristics of such high level languages. Now, let us move on to the next topic which is of interest in the using Python for computation that is solving ordinary differential equations. We have taken an example as the slide tells you, we are taking an equation. The first derivative of y with respect to time is proportional to y into l minus y where l is some constant. We are saying the rate of spread of an epidemic in a population l at any point is proportional to the product of infected people and the non-infected people. It is a very simplistic model and we have to make some assumptions for values of k and so we are trying to solve such an ordinary differential equation. Now, let us step back and understand what do you mean by solving a problem of this type. Computationally speaking, given this some number of values in a regular maths class if you ask to solve you expect to find a closed form solution y equal to f of t where f is some combination of analytic functions you know. But in a computational sense all we are asking for is I should be able to give a set an array for y for given values of t. In other words for some x predefined values of t I must have the value of y at those places. So, that is all solution in a numerical sense means and that is pretty easy to do. Once again we import very specialized there is a module for ordinary differential equation integration itself called integrate and that we import ODE int. Our first job is to define the first derivative whatever is given. So, you have to model the problem first essentially. So, one look at this should tell you even though the function has both the variables y and t, t is never used. In other words as per our model the rate of spread of the epidemic is independent of time it is just dependent on the size of the populations. Like I said we are not trying to we are trying to see how to solve differential equations if this is not the best epidemic model most probably not. So, solving it once again takes much less time than typing the problem in why such odd choice for t I leave it as an exercise for you to figure out. ODE int takes three parameters the initial value of y at time 0 the function which evaluates dy by dt which we have given the initial value of y at time 0 is given and the range of y's t is over which we want y value. Now we have solved it. So, what is y? It gives us y values that is all like I noted before this is what we mean by solving. So, let us try and plot y against t. So, the plot should give us a visual representation of a number of infected people against time. I made a mistake somewhere because the plot I we expect is supposed to look like this. Now let us retest our steps and see what mistake I made this part is correct. I think I probably 1 0 short in yeah 1 0 less if you figure it out give me a minute what is the problem say 2.5 e power 5 let us do that again let me avoid typing it my way yeah probably had a 0 less and I typed the other one. So, what does the picture says we start slowly you can see in this in this area growth is very slow, but then it growth of infection the number of infected people starts growing very fast and ultimately everybody is infected. So, that is the physical interpretation of the model like I said it was more to do with how to do the ODE solving than how to model epidemics. So, let us move on before moving on maybe it is a good idea for us to try some exercises for some of the problems. We will give you a matrix exercise so that you can try out the what we covered right about here solving linear equation we will give you the equation plus 2 y minus z equal to what we will try and solve a slightly different one we will solve find the roots of this non-linear equation find the first few roots shall we say are the root near pi let us let me get you started with it and then I will leave you to attempt the problem what we are trying to solve is x squared equal to tan x please remember that you have to be a little careful about this particular one because tan x is not a very nice fellow it blows off infinity and minus infinity. So, we will try and plot both of them to get an idea of you can see a few places where near 0 there is one root 0 x equal to 0 is obviously one root that is where both cross and near minus 2 there is one root there are 1.5 there is one root where the two lines cross is where x square is equal to tan x. So, at 0 there is a crossing around 1.5 there is one and around minus 2 there is one. So, you have to use minus 2 and minus 1 plus 1.5 as the starting points for f solve and solve it go ahead attempt these problems we will give you 5 minutes all right that should have been pretty simple let us look at what we have to do first define the function we want to plot solve then we want to find a solution near 2. So, minus 2 sorry and that is it just these two steps get you going to where we need to be like I said if you are not careful you will find wrong solution say for example, you want to find something near 10 you will get this whether a solution is actually there you must plot and check let us move on ok. The next one is trying to do ODE is a little more at a slightly different level compared to what we did just now in the epidemic modeling problem this is the good old ODE of the simple pendulum where this theta by dt square is equal to minus g by L sin theta. Traditionally we solve this by making the approximation for small values of theta sin theta is equal to theta and then we solve it and then arrive at an answer, but here we do not want to do any approximation we want to solve the problem for large oscillations which essentially means we must be able to get for given values of t values of theta and values of theta dot which is we call it omega given that theta is the angular displacement omega is angular velocity and omega dot is angular acceleration and that exactly is what we have defined it as. So, how to solve a second order ordinary differential equation matrices are lot easier to handle so we set it up as a system of two first order equations as you can see in the slide the first equation merely sets up d theta by dt which you are in the other notation we use theta dot equal to w and sorry omega and then d omega by dt equal to minus g L by sin theta. So, instead of one second order equation we have got two first order equations it is two different think of it as two different variables theta and omega having set it up what are the initial conditions at time 0 theta is 0 sorry those are initial conditions you have to get we are assuming we are given initial values as at time 0 theta 0 is 10 degrees and the obviously, because if you pull up pendulum 10 degrees and then leave it at the time angular velocity is 0. So, those are the initial values. So, how to solve that again we use ODE int, but we need a function which defines the variables at different points of time so that function has to return two values now because we are solving for two just remember that normally we ODE int took one and the function returned one value here we are solving a system of differential equation. So, our evaluation function has to return all the values of the variable in this case omega and theta. So, we need initial value under t. So, we say for different values of t this is the value of value of what we return. So, the next step is to set up as usual initial value is in degrees d theta we wanted radiance because radiance is what we work on completely. Finally, with our good old ODE int dependent and the initial conditions and over time you will get this result that is one is omega and one is theta which confirms that for small loss starting values essentially there is a sinusoidal behavior for this over time. And the can also check that when the angular velocity is maximum for what is the displacement is minimum and vice versa all of that you can check in that graph. If you go back couple of slides the important thing is the formulation. Once you formulate as a system of differential equations then you need to set up your function which in the epidemic case we simply had to we said we need a function which gives d y by d t for different values and that is it. Here we have to give theta and omega different values. So, we have to the function has to return a list consisting of those two things and the rest of the work is very simple rinse phase over a relevant value initialize initial as you require and finally just call ODE int. I think by now you will agree with me I have there with the point I have made it is more often that you will type lot more to set up the problem than to solve the problem which is a good thing solving should be routine setting of the problem should take up a little more time which brings us to the last topic in the workshop which is the how to use python modules and how to write our own modules. We have hand waived a little earlier with respect to scripts we will fix that because that is one of the important areas which we have taken a shortcut in order to get certain things going we will fix that what is that remember what we did yesterday I wrote a script called fourth plot dot pi it ran and it showed a nice picture showing overlaid picture. So, let us not so I have a something called four plot dot pi where I have added some things to what I talked about we want to plot this four functions y equal to x y equal to minus x y equal to sin x and y equal to x sin x and then trim the output to the x value y value limit. So, that it comes nicely framed like what we have got and annotate the origin this was the problem statement for which this script was produced and we hand waived a little yesterday when we said you run percentage run minus i we also said this minus i is something we will talk about later we can this is the later part simply run it without minus i sorry, but when we said script and you say run minus run inside we actually have been cheating a little because i python is what understands this percentage run when we say we have written a python script what we would like to do what anybody else would like to do is to do something like python file name then execute it if you remember that is how we executed collat dot pi it ran good the same way we want to execute the various scripting and plotting things we have done. So, let us see whether we can do that for four plot now this is what I meant by we have been cheating a little. So, we did we said yesterday we have written a script we have written a script. So, this is the list of statements we would have typed inside i python to produce this correct you can look at the code and understand first we say x equal to linspace minus 5 pi to plus 5 pi 500 then we say plot x then we say plot minus x then we say plot gx x sin x in green with a line width of 2 plot x comma x star sin x in red with a line width of 3 and then give the legend x minus x sin x sin x and at the origin give a title 4 plot x lim minus 5 pi 5 pi for y lim and x lim completes the picture. So, this is the 4 underscore plot dot pi now let us run it how to run it we noted that we need a show to see. So, it produces output. So, yesterday we said this and we said this is a script, but we make a statement today this is not a complete script because we hand waived on over important portions and that is what we want to cover today when we try to run python 4 plot dot py we get a name error name linspace is not defined. So, linspace occurs in the first line it says it is not defined, but how is it defined inside i python please remember we invoked i python with i python minus pi lab that ensured that many libraries were around. So, when a it could identify linspace is a function available in some library and that library is known to i python to be inside pi lab and that was a magic hand waving we I talked about that was a cheating I am talking about. So, real python scripts you do not should not need i python to run and should not be told to run only from inside i python. So, if you want to make it runnable everywhere we need to make some changes we need to make python understand what these are. So, where is linspace available we understand linspace is available in psi pi. So, the library psi pi we say import from psi pi import from psi pi import everything available in the library. So, that we can look at it this is our i python we know it works when we run from here we already seen that let us see it again yes it works we equally well know it does not work when we try to run from the command line saying python it did not run, but we made a change now we added a line saying from psi pi import star let us see the impact of that particular line now it says plot is not defined, but please note the earlier rmsh is no longer there in other words now it knows what to do when encountering the name linspace, but it does not know what to do when encountering the name plot. So, where is plot defined we make an inspired guess that it is defined in pi lab and then say we run it again, but we also want to see the output. So, we will do a show at the very end let us see it runs and it produces the picture which we always expected, but what exactly happened what happened is how python identifies a function it needs is through the module system and the module system is intended is works in a certain way and that is what we want to focus about now. Please remember without the module system without understanding what to use where the programs which are legally correct from when running from will and or running from inside the high python may not run as pure python scripts when run at the command line which is not what we want. So, for that we will talk about what is this import statements doing now let us make a let us in fact try and do something slightly different let me log out of high python and this time instead of running high python minus pi lab just run high python without pi lab and let us try to use any of the functions we know what is the value of pi is we have used it comfortably it work now when we type pi instead of displaying the value it screams all over the place because pi is not defined. So, we say import psi pi let us say we assume pi is defined in no not defined, but wait a minute is there any other way to refer to it ah it is not enough to say import psi pi dot pi sorry import psi pi you have to refer to whatever is defined inside psi pi with the qualification psi pi dot sorry if you are type psi pi dot pi earlier it would have work let us close it and rerun so that we really understand it is not my typing order that cause things to happen, but pi not defined psi pi dot still not defined now psi pi itself is not defined. So, then I say import psi pi pi still not defined, but psi pi dot pi is defined. So, this is the basis of the module system you import a module and use the symbols in it by qualifying the symbol name with the name of the module. So, psi pi dot pi refers to what we refer to as pi, but in the complete i pi then session we were simply typing pi we did not type psi pi dot pi correct that is the advantage using an interactive environment like i pi then where when we say i pi then minus pi lab it makes all the symbols available in the local space itself the way to do it is to say something like what we did in the script earlier let me close and come back again. So, that you will appreciate the difference i pi then instead of saying import psi pi I am going to say from psi pi import star, star is essentially everything. So, now the symbol pi need not be qualified, but this is a bad very bad very very bad very very very very bad bad idea do not ever do that accepting do not write it in your script this beats the purpose of the module system. The only reason to do it is interactively you are playing around in i pi then you do not want to type psi pi dot psi pi dot every time to reduce the typing work you can do that. Now, so since it is a very very bad idea you will close this and come back with a good way of doing things we start with import psi pi. Now, how do I know what are all the functions available in psi pi very good you can know it by simply consulting the psi pi manual or you can use python's famous introspection to tell you you will see one two page full of this. So, these are all the symbols python exports sorry psi pi exports you can see our friend pi you can see our friend once once like you can see roots all of which you used they are all here you see the tan functions, but what you do not see is optimized which is a module inside psi pi you only see the functions it directly imports not functions I kept carefully saying symbols pi is not a function, but it is a constant it is also exported 0 0s like which we saw earlier. So, this is the list of functions which are our symbols exported by psi pi almost all of them very large majority of them are functions sorry or functions. So, we can find out what something does by the good old question mark. So, did not help us much the description is not very useful, but yes you can look up the DAR find out and then find out all about itself. So, the slide talks about two correct ways of importing modules from psi pi import star is a very bad way accepting when you are typing interactively the good ways in scripts are specifically importing the function star is the bad one. So, you could instead say from psi pi import linspace pi sign from pi lab import plot and show and then the script will work you will do that to the script to convince ourselves that is what we want, but that assumes we know where our functions are from linspace pi and sign or from while plot and show or from pi lab. So, if you do that we do 534 534 plot dot p y again name legend does not define because we forgot to import that. So, legend where is it likely to be defined looking at plot and show versus linspace it appears very probable it is defined in pi lab rather than psi pi nothing scientific about a graphic legend all it looks more in line with what a plot shows. So, annotate same problem you have to import annotate, annotate is again specific to plotting. So, we take the view it is also likely to be found in their title let us look title x lim y lim we better find all of that this time. So, what is the advantage of importing this way advantage you can just write plot disadvantage you have to change this line when you add a new function advantage of writing import psi lab sorry import psi pi is you do not have to change the import line you can use any function, but disadvantage you have to type psi pi dot linspace pi lab dot plot pi lab dot plot every time. You will find people generally use selective imports only if they are using one or two functions psi pi is a huge library as you saw that. So, when you say import all of them are loaded. So, you do not want to use up too much memory. So, you may end up saying we do not want to import too many things we will just import what we want other times you may not really worry about that. So, you may just say import everything. So, this is how to use the module system if you are going to write modules on your own how to do that how to use functions we have written we are talking about write your own functions and so on. So, let us see that someone ask me a question can you show us the collards code once again. So, instead what I will do? So, what will we will do we will try and create a module for this and use it interactively. So, this is the collards code I wrote now instead of if this were to be made into a module how do you do it a module is simply a set of usable functions the example gives you GCD the slide show you GCD as an example. Now, let us see how to run this now before using this we also need to be sure that this is good and useful. So, we need to be able to test it. So, we say for example we will come to that in a minute we will first see how to use this as a module I will say import collards because there is a file called collards dot pi that by definition a module is there at a file defined at a file level. So, now what are all the symbols defined in collards there are some automatic shown and finally you have one collards in the end. So, how do you use the collards sequence we know how to use it we simply say collards 7 why collards is a module name of now. So, the symbol name inside that is collards dot collards this is what very good. So, it works now let us see what happens if we give 5 good if we give 8. So, we will put these as a test in our code and then get back to what we are doing we want to be able to test our code. So, we say if collards 5 collards 7 not equal to this is the 7 sequence. So, you are putting in some test code. So, that we know it is working I will remove the 7 because it is too large and it will be difficult for us to appreciate not for any other reason we know collard 4 is supposed to be 4 to 1 we could have added lot more, but like 8 4 to 1 and so on, but we are trying to establish something. So, we will not worry. So, how do you know the collards we have written works well very good we can what we can do is we can go and say. So, whatever test cases it is working. So, we are happy it is working, but we want to use it. So, what we have to do we have to go to python again this we do not want. We want the test code to run here when we are running it, but when we want to use the functions inside it we do not want to run the test code. The test code is required to ensure that we are confident and we know what how it has been tested, but we definitely do not want it whenever we import it showing something on its own. So, the way to avoid it is the next what we could call an idiom in python and that is simply in a module put all code inside which. So, we have put one more if and that if has a certain standard format underscore underscore two underscore name underscore underscore is main do all this. Now, when a piece of python code is run as a script its name is underscore underscore main underscore underscore. So, we use that otherwise it is it is name is not the same. So, we are using that to take advantage of this sorry I forgot the colon at the end. So, now it works those if statements which test the code work. Now, we say, but if I import it does it work the same way that code is not executed, because now py colatz is not running as a script it knows it how does it know it that is for the operating system and the python interpreter to work together to let the script know, but as programmers all we need to know is there is a symbol called name and its value is main when you run that file in which this code occurs as a top level script the name is main. So, you put all your testing code or whatever else it could be testing code it could be something else we have chosen the example of testing it need not be testing everything else can be here. And we have chosen an example of showing only one function in a module, but as you know sci pi has hundreds of functions you can write any number of functions here your main can contain any amount of text cases for all of them. Now, that is the way to write your own modules where should the modules be that is another question you have to consult your operating system specific parts of python. Typically, if you are on a Linux machine there is a python 2.6 side packages there is a directory depending on the python version and side packages that is one place there is a environment variable called python path which tells where to look for, but one convenient thing to know is the current directory is always searched for. So, we could always add our scripts our own scripts to the current directory as a last result and expected to work and that is what the last slide attempts to explain. If you do this you know where all python itself will try to locate a module. So, let us do that to understand you see a big list user bin user lib 2.6, lib tk lib volt obviously these are all obviously slash usr slash lib is something which is installed by system level not what we do user local lib dist packages ipython has put its ipython extensions and ipython also writes a local directory with a leading dot. So, you do not see it for some of its own convenience. So, the recommended place for you to put your own modules would be here because this would not be overwritten by the operating system if you upgrade or anything usr local lib. I would not know what to do in windows you will have to consult the documentation. So, that brings us to the end of what we wanted to cover.