So we're back to to multivariable calculus and before I begin the new topic I wanted to to add a couple of things to our discussion last Thursday about Formulas for the area in polar coordinates remember we discussed polar coordinates and One of the questions which arises here is one of the questions Which arises here is to find the area of The following kind of picture you have a curve Which is given by the equation r equals f of theta Where r and theta the two polar coordinates, which we introduced last time, right? and in this polar coordinates a natural Bounds for such a Fortune such an area would not be the vertical lines like x equal a x equal b the way You know we used to do it in rectangular coordinates That's the old picture now. It's more natural to to give bounds in terms of the angle theta So it's going to look like a sector like this Between some angle alpha and beta And so we will assume that alpha is less than equal to is less than beta and theta will be between alpha and beta and so we look at this Figure which is bounded by the lines theta equals alpha theta equals beta and and the curve This is the curve And the question is to find the area of this So I just briefly touched on this at the very end of last lecture And I just wanted to tell you how to get this formula which I wrote down Without Without explaining it last time So the idea is again to to break this to break this picture into small into small angles Which would be? Some within some angle delta theta and And say here where you'll have are and And just look at the area of this sector in the disc, right? So we approximate now our curve not by not by horizontal lines or line segments But we will now approximate our curve by a small segment of a circle because that's more natural That's a more natural object in the polar coordinate system Okay, so so the question then becomes what is the area of Of this of this little sector in the polar coordinate system the analogous picture would be like this So we would have some delta x and delta y Right and delta y and so the formula for the area would be just delta x times delta y right and Or more precisely in fact we don't look at it as a delta y But we just look at it as a y coordinate over over graphs So it would be more precisely y times delta x and that's what after applying the procedure of breaking into small pieces Give us the formula for the for the area in terms of an integral like this y dx In other words y delta x the elementary area gives rise to this integrand y dx So we have to approach this problem in exactly the same way But now instead of calculating this area right here We need to calculate the area of this wedge of this little wedge of this little sector And this actually not so difficult to do because see the point is That we know the area of the entire disc So if you have a disc of radius r We know the entire area What's the area of the disc? Pi times r squared right areas pi times r squared and Okay, so now suppose we were given say one quarter of the disc What's the area of the of the of this of this quarter of the of the disc? Well be one quarter of this right so that would be one quarter why one quarter because the angle enclosed here is pi over 2 and So one quarter is pi over 2 divided by 2 pi Which is the like the full angle of the disc so in other words if you want to calculate the area not of the entire disc but of of Part of the disc which is enclosed within the angle of pi over 2 you have to multiply this result pi r squared by the ratio of the angle which is enclosed To the total area of the disc so it would be pi over 2 divided by 2 pi Right so that would be one quarter of pi r squared Which of course we knew in the first place because clearly this is going to be one quarter because there are four pieces Which are exactly the same but let's suppose You are in a pizza restaurant and you are you have a piece like this With angle theta and they didn't cut it in a very uniform way, so you don't know exactly it's not pi over It's not pi over 4 it's not pi over 6 or pi over 3 whatever and you want to calculate what area How much pizza you got and then compared to how much your friends got say maybe they got more So they should they should share maybe to give you back Give you back some So what would be the answer? Well, we should we should argue in exactly the same way as before Before that angle theta was pi over 2 and what we did we took the ratio between pi over 2 and 2 pi And now instead we'll have a ratio of theta to 2 pi. That's That what that's what will represent the piece that we got as Opposed as a sort of the ratio of that piece to the entire disc to the entire pizza And therefore the area would be pi r squared times theta divided by 2 by 2 pi Right, so pi consoles out and you end up with one half r squared times theta That's the area of the sector of angle theta Which is exactly our question except in asking this question. We called it delta theta instead of theta so this area is Is one half our R squared delta theta and that's the analog of this formula y delta x So just like this formula y delta x gives rise to this integral This formula for the elementary area of the of the of the elementary wedge of the angle delta theta will give rise to the integral from alpha to beta one half r squared D theta you see it's exactly the same just kind of replace delta theta by d theta and That's how we get that formula which I wrote down at the end of last lecture So to summarize The key to calculating the areas like this is to be is to break the The figure that you got into small pieces Which will then look like elementary figures like in this case would be an elementary Rectangle, which is where I magnify a blow it up and I show it there Calculate its area and then it gives rise to the formula for the area of a general picture like this Whereas in the polar coordinate system you do exactly the same But you kind of follow the shape of the figure is different because the coordinates are now different So now we split it into little sectors like this where the angle is delta theta I magnified one of those pieces right there. I found the area of that little piece by thinking about pizza and Calculating it right and and then I put all these little pieces together and this gives gives rise to this integral Okay, any questions about this? So that's the way to calculate That's the way to calculate the area in polar coordinates and there is a similar formula also for the arc length in polar coordinates But the formula for the arc length I don't want to spend time on it because we really take the formula which we derived in the in the rectangular coordinates About a week ago, and we just make the substitution where x is equal to our cosine theta and y is equal to our sine theta And then just see what comes out what comes out is some in some formula in terms of r and theta Which you can then use to calculate the arc length So that's the way we do it for the arc length here It's more conceptual because we change the point of view slightly instead of looking at rectangles We look at these sectors It wouldn't be wise to try to break this into rectangles because they don't fit in this picture What fits perfectly in this picture are these little sectors So then we need to calculate the area of the little sector which we've done here Okay, so this Speaking of pizza analogy this were the leftovers from last lecture and now It's not it's not what you say. It's how you say it, you know, so delivery is So I'm working on it now So now we go we move on to our next topic And the next topic is vectors and geometry of space and space here means three-dimensional space Which surrounds us you see up to now in the last three lectures. We talked about objects which are defined on the plane Here I've been drawing curves on the plane and We've discussed various things about those curves like arc length or various areas which are enclosed by those curves But the basic point The most important aspect of what we've done up to now in this class is the fact that we have worked Exclusively with object confined to a plane and by a plane I mean this blackboard or sort of infinite an infinite extension of this blackboard in all directions Right. All our objects have been defined on this plane But now we want to move on as I told you in the very beginning. We are in this class We are going to study not just objects on the plane but also object in space in the three-dimensional space and In the three because we live in a three-dimensional space If you don't think that if you don't count the time So in fact we live in four-dimensional space time, but that's that would be topic for the next calculus class I guess four-dimensional space now will do here in this class We do two-dimensional space the plane and three-dimensional space So three-dimensional space means more interesting because it fits more objects more of now It fits not just curves and there are a lot of interesting curves Which are appear in space which cannot which are not confined in any particular plane But also perhaps more interestingly it contains objects of dimension higher namely two-dimensional objects the surfaces like a sphere for example Or or or this this this waste basket These are all surfaces if you if you ignore the fact that actually they have certain thickness So what we'd like to do ultimately in this class is to understand the geometry of those surfaces as well as curves and to be able to answer various questions about them like finding the areas and Even more even more sophisticated things So what we do what we need to do now is to sort of lay the groundwork for this in other words We have to develop some formalism because how are we going to represent curves? How are we going to represent surfaces in a three-dimensional space? What's the most efficient? so always check for batteries Before you begin so in space we need to we need to develop some technique in order to attack these problems and The very first thing that we should talk about is a coordinate system Because you know and on the plane it sort of goes without saying that we started out with Whenever I start writing drawing a diagram drawing a picture or drawing a curve I draw this coordinate system. We're kind of it's almost like a reflex I say and usually we call them x and y the two coordinates After last week we kind of we're wiser now We realize that this is actually not the only coordinate system that we have available for for the plane For instance the polar coordinates provides us a different coordinate system But this is the basic one and we should start thinking about a similar coordinate system in the three-dimensional space So this is the first step that we have to make coordinate systems Now of course the problem is that even though I'm going to talk about the three-dimensional space I'm still going to draw things on the two-dimensional blackboard, right? So of course I cannot draw this wastebasket on on this on this blackboard what I can draw is really a projection So I kind of we kind of create the illusion that there is some depth in the picture and we try to imagine that The three dimension the third dimension is there somewhere So the the way we know we usually would draw coordinates Would be like this so that of course the point is that they are not on the same blackboard only This one and this one lie on this blackboard and this one is sticking out He's sticking out by what we are looking at kind of slightly from above and And that's why it appears to us this way So now we want to we want to label this coordinates in a certain way in the on the two-dimensional plane we label them x and y and In fact, some of you may have wondered Why we label them x and y and not And not for y and x not in the opposite Not in the opposite order why not like this Of course in a sense it is the notation is arbitrary. We have chosen These letters once and for all so in a way you can say that it's not a meaningful question Because it depends on what we mean by x and y but of course What is important here is not so much which letters we use but the ordering the implicit ordering between x and y X comes first and y comes second because that's how they are ordered in the alphabet, right? So we could use y and z and again We it will be assumed that y goes first and z goes second so there is actually a basic asymmetry between these two coordinates x and y and We don't talk about it's much. We haven't talked about this much when we talked about curves but in fact now is a good time to To discuss this and to realize that there is a basic asymmetry the point is that You could work with a coordinate system like this xy Or you could work with a coordinate system yx and these are not equivalent to each other There is no way to transform one into the other Without removing them from this plane. Of course if I were able to Maybe I should draw it more like this if I were able to pick it from the plane and flip x and y I Would of course then I Will get back to it to this picture x and y that is a more traditional picture we draw right But we cannot flip them within the plane You can argue. Well, what if we just try to move this coordinates like the You know on the clock and kind of move them and make them go through one another But that's not allowed because then you they would have to pass through each other and at that point They will cease to be a coordinate system. They will become parallel to each other So that's not allowed. So in fact you think about you will see that there is no way to transform one into the other There's no way to transform one into the other by by moves confined to this plane and That's a very important point which tells us that actually our plane has two different orientations Choosing one or the other this coordinates this coordinate system or the other coordinate system Gives it orientation and the funny thing is suppose that there was another class behind that blackboard and There were people there like a mirror image of this one the bizarro world if you will and suppose there were kids, you know sitting there and also learning multivariable calculus then This one one of them The the first one would appear to them as the second and conversely I mean not exactly, but if you if you rotate that that's exactly what you'd get so What in other words the orientation from this side? There is orientation from this side when you look at this blackboard from this side And there is orientation of this blackboard from the other side from the bizarro world side and these are two different orientations There's a two different worlds and you can't transform one world into another Within the plane within the confines of the plane if you go into this three-dimensional space You can't do that you can just pick it up and flip them But inside the two-dimensional space you cannot do that at first it appears as a kind of a nuisance And you may think that it's not a very important point but actually And in fact for the plane it's not so important, but in three-dimensional space it becomes more important and that's why we emphasize it In three-dimensional space. We also have different choices of writing coordinates labeling of labeling coordinates and in three dimensions we will label coordinates as x y z and Of course again the most important point is not so much which letters we use But the ordering in which they appear and the order of course will be the natural one one two three Just the way they appear and the alphabet But then the question is how do you assign there are there are now many choices to assign these letters to to this coordinate axis and The question is are we going to get always the same coordinate system in other words are we going to get coordinate systems? Which can be transformed one into another by moves within the three-dimensional space? And so now since I've shown you that in two-dimensional space on the plane That's not true in other words on the plane There are two different coordinate systems, which cannot be transformed into one another you would not be surprised to know that actually Here as well in three-dimensional space. There are also in equivalent coordinate systems But you might be surprised to know that again there are only two choices not more than two So the choices are basically The one which we will always use will be like this X Y Z and the other choice Which we will not use which is not equivalent to this one which is which is a bizarre world coordinate system is this one In other words we switch X and Y just we did before on the plane Just the way we did before on the plane If you try the other choices for example, you put X Y Z like this There you will see that there are many different ways. There are six different ways to label them You will see that That picture can be transformed by simple rotation into one of these two pictures So really there are only two in equivalent ones up to rotation within the three-dimensional space If we could go into the four-dimensional Into four-dimensional space if there were a fourth dimension that we could visualize Just the way we can visualize the third dimension when talking about the plane We would actually be able to pick it up and transform one coordinate system into another in that four-dimensional space But since we don't have for its fourth dimension or more precisely the fourth dimension time is kind of elusive It's not easy to visualize it. So within the three-dimensional space. We cannot do that So we have to agree from the beginning on what is the rule What is the rule for labeling these coordinates because we have to do it now because from now on We have to use the same system. So we are on the same page or on the same blackboard now What is the rule and So the rule is there are different ways to phrase the rule and that the rule is is in the book It's explained by using fingers But I always forget that so I will tell you the rule which I use Which is you know, I grew up in Russia and We use the rule which is called the corkscrew rule And this is not to say that Russians like to drink But anyway, it is called the corkscrew rule and the way it works is like this If you rotate the corkscrew from x to y So here is the corkscrew I Kind of draw the most the basic one if you rotate it from x to y Then this the thing will go in the direction of the of the z of the z axis right in other words if you Well, it's not a good picture because then the bottle would have to be upside down, but you see what I mean so if you think about the corkscrew going from x to y which is natural right going from x to y is like going from the first coordinate to the second in Going from x going in making a rotation as if you were moving the x core x coordinate system to the y Sorry x axis to the y axis then the screw will move in the direction of the z axis So that's the rule which I find easy to remember Even if I don't have a corkscrew It's easy to remember, but you can use whatever rule you want You just have to remember that there is such a rule and to draw this picture in this way Okay, so that's the first point that we have to make about this the second point is we have to develop we have to Develop some tools for representing objects on on this on this in space Okay, so The first I already told you that the objects will be mostly mostly interested in Still looking for battery, but I guess So the objects that we are mostly interested in our curves and and surfaces But in fact there are simpler more elementary objects namely points So before we talk about you know, it's complicated matters like curves and surfaces. Let's talk about points In the case of a plane we know how to represent a point. We just drop the perpendicular lines onto the x and y axis right and Say we get x zero and y zero and we represent the point as x zero y zero like this And we are going to do the same In the three-dimensional space So I draw the same coordinate system and I'm thinking okay if I go from x to y they're going to be z So that's how I should put them Because if I put them in the other way, I would have to move the corkscrew this way and it would go down So z would have to go this way So this is a good orientation This is this is orientation. We've chosen and Our mirror image will have the opposite orientation. So if we have a point We are we also can represent it by with coordinates and now they're going to be three coordinates Obviously because we are in three-dimensional space and the way we find this coordinates is as follows We drop the perpendicular lines line from this point to the plane x and y Now you have to at this point You have to use your imagination and you really have to think of x and y as a plane which is sticking out like this And the point is actually not here. It's not a it's not in the z y plane, but it's here so you've got Like I can't show you because my one of my hands is busy with the microphone But you can you can I'm trying so this is a plane and the point is here So you drop the perpendicular down like this so you get a point on the plane Which is the x y plane now I draw it like this So the point is somewhere in space and that's what that's what that's where it drops So now I get a point in the x y plane But a point in the x y plane we already know how to represent We just drop the perpendicular lines to the x-axis and y-axis. So what we got already is y zero and x zero Coordinates of this of this point the two of the two of the three coordinates. What about the third coordinate? It is tempting to draw a perpendicular like this But that's not true right to give it really a depth to give it the illusion of a three-dimensional picture I have to do it in the following way. I have to connect the origin to this point, which is the Which is the point I got by dropping my original point on the x y plane and I have to Connect To the z-axis by a parallel line. So this one and this one are parallel to each other and that's going to be the z zero coordinate You see the point is that This is perpendicular to this entire plane. So in particular is perpendicular to this and that's why the z-axis is perpendicular to this line So what I'm doing really is I'm dropping perpendicular to each of the three coordinate systems But the way I draw it is slightly complicated because I'm using two-dimensional projection of the picture It would have been much easier if I had a three-dimensional model of this whole thing So then you would see more clearly what are the what the coordinates are, but I think it's fairly self-explanatory here so If we have this picture, then we will say that the point has coordinates x zero y zero and z zero And this is in complete analogy with The situation that we have the picture that we have in two dimensions. So nothing nothing surprising here What else do we need to know? We need to know the distance between two points Right, so for instance, we would like to find find often times Let me use a different chart often times we might might want to find the distance from From this point to the origin Let's call this so we need a formula for this distance and In two dimensions, we know that this distance is computed by a very simple formula namely square root of the Xzx squared plus y squared and now the formula is going to be very similar It's going to be the square root of x squared plus y squared plus z squared Okay so So very similar instead of sum of two squares like this you have the sum of three squares And then you take the square root and that's how you calculate the distance from this point to this point And it's very easy to prove this by using Pythagoras theorem. It's in the book. I'm not going to get into this It's very easy All right, what if we were What if we were to calculate The distance between two different points Actually So you could have a point P and then you have another point P prime and say here you have coordinates x 0 y 0 and Z 0 and here you have coordinates at x 1 y 1 and z 1 For this one so the distance so you see the point is that This you can find this distance you can just parallel transport this interval to the in such a way that the first point Would end up at the origin and if you do that the second point We'll have coordinates which are just the differences between the coordinates of the original two points. So at this point we'll have coordinates x 1 minus x 0 y 1 minus y 0 and z 1 minus z 0 Okay So the distance let's call it. Let's go D Here the distance will be then according to this formula the square root of x 1 minus x 0 squared plus y 1 minus y 0 squared plus z 1 minus z 0 squared just like this Is this clear to everybody? Okay, and and this already gives us a way to describe a very important surface on in space namely the sphere Because the sphere is defined as the set of all points Which are equidistant from a given point? so for example Let me raise something Let's talk about the sphere of radius R With the center at the origin These are all points whose distance all points P whose distance To the origin origin I denote by by this letter zero. This is not to I don't mean the The number zero, but I mean this point. It's really not zero, but it's point all looks like zero So points whose distance to the point all to the origin is equal to r and R is is some fixed number, right? So what does it mean? It means that the square root of x Square plus y squared plus z squared Where x y and z are coordinates of the point P? We'll have to be equal to R Because that's exactly the distance and if we say that the distance is equal to R that's that's the equation we get Now it's it's what it's it's a good idea to Square both parts both side left hand side and the right hand side So then we get the equation x squared plus y squared plus z squared is equal to R squared And that's remarkably similar to the equation of the circle. So compare with the circle Circle for which the equation is x squared plus y squared equals R squared Here now we have a third variable And so we just add the third square to the left hand side, but otherwise equation looks very similar so now we have an equation of the Out of the sphere with with this centered at the origin and again You see remember the rule which I explained to you at the very beginning that if you want to calculate the dimension of an object You have to take the dimension of the ambient space in this case 3 and subtract the number of equations here I've written one equation so the dimension drops by one and We get a two-dimensional object. So we get the sphere which is two-dimensional Two-dimensional object it is a surface Surface is by definition a two-dimensional object Unlike wise we can write the equation for a sphere which is centered not at the origin But at any given point by using this formula So if I say that this is equal to R that would be sphere of Radius R centered at the point P That would be the equation for x1 y1 and z1 with x0 y0 and z0 fixed being the coordinates of the point P alright So we already have some rudimentary knowledge of the three-dimensional space and we know And the equation of one basic basic surface in it So what's the next step? The next step is to introduce objects to to introduce a new tool to introduce objects of a different kind Which you have not used yet in this course. These objects are called vectors and you will see that these objects are very Very useful for Precisely for studying the kind of questions that we'll be interested in like finding areas Representing objects and so on so the next step is to is to talk about vectors. So how many of you know vectors? So maybe we should just go home Okay, but let me just go briefly it'll give you an overview. So I will not dwell on it too much then So what is a vector? Vector is an object which is which is different from numbers Which we have studied so far. So so far we studied different options. We studied we know numbers. We know points for example on the plane Points on the plane are not numbers in they are represented not by a single number by by two numbers Right and in fact, they are not They're more somehow than a two numbers because they are a gemat a point is a geometric object Which is positioned somewhere on the plane or in space? So a point is something which has a position which is a position on the plane or in space and A vector is an object of yet another kind, which is characterized by two properties It has a magnitude or length and it also has a direction So so we usually draw vectors as as as directed line segments So it is convenient to to draw a vector as The line segment like this connecting a point a to a point B And You see when I do that I give it two properties one is a direction Because this arrow points in that direction and a second is a magnitude or the length and the magnitude is the deal Is the distance between the point point a and point B? okay But now the question is how do we how are we going to represent vectors in practice in other words? In practice, we will represent points by their coordinates So we should discuss also how to present vectors by something like coordinates and various operation on vectors What kind of things can we do with vectors and? Here there is a subtle point, which is very important, which is that? Which is that in fact a Vector is not really a picture like this But vector is really what I said Something which which is just determined by its direction and and its magnitude in other words if you take And not if you take a parallel if you take a parallel transport of this if you just move It to another pointed Directed segment like this we say points a prime and B prime And you make sure that the directions are exactly the same in other words that these two lines are parallel And they point in the same direction and the lengths are the same Then actually we will not distinguish between these two vectors You see we will not distinguish between them So the vector is not really a pointed a pointed segment or directed segment It's a whole class of directed segments. A vector is this is one representative of of This vector. This is one possible way to represent our vector as a Directed segment going from the point a but you might as well start at the point a prime and you would represent exactly the same vector So usually we will denote vector By small light and letter like a and we'll put an arrow above it and In the first approximation you can really think of a vector as just an arrow like this Which has a directional magnitude, but you have to realize that many different air arrows represent the same vector Namely all parallel arrows Parallel to each other pointing in the same direction of the same length represent the same vector So then of course the question is how it can be possibly Work with these guys because they are there are so many possible ways to represent them Well, that's where we have to start using our coordinate system So we draw our coordinate system and again, we remember the rule. That's why see Now as I said the same vector a could be written could be represented many different ways, but To to find such a way we have to pick the initial point once we pick the initial point Then it's it's already the vector the the arrow is determined by that If we're drawing that vector a But amongst all the points on the plane once we introduce the coordinate system. There is the origin There is a special point so we might as well We might as well draw a representative from this class from this whole set of possible Arrows Representing this given vector we can draw one of them which starts at the point at the point of the at the origin of this three-dimensional coordinate system. So the result is And I'm trying to draw it parallel to this one in some sense But although in my previous discussion, I kind of I was kind of working on the plane But now I'm working in space So in fact this end point is not necessarily on the plane is not in necessarily part of the blackboard But it could be again hanging somewhere in the middle like on this picture Right So now the vector a gets a much more concrete realization It is a pointed Interval or directed interval the directed segment But now it goes from the point O from the origin to some point P So that that's already much less ambiguous In other words once you introduce a coordinate system, you have a preferred representative for all possible arrows giving you the same vector and Then what we'll do is we will write that a is equal to OP or in this case we'll write that a is equal to AB or a prime B prime In other words different notation in which you use the initial point and the end point and again put put the arrow on top So from this point of view It looks like a vector is essentially just determined by its end point Because we agree to use as the initial point the origin So the only freedom we have is where to put the end point So it looks at first glance it looks like there is no difference between a vector and the point But this is misleading you have to realize that this is misleading and actually there is a lot more to the vector Than there is to a point even though when we represent it in this way The vector will essentially be determined by its end point once you center it or make the initial point to be at the origin And really the main difference is in the meaning of this in the physical meaning physical interpretation of Of a vector as opposed to a point the point is just a point. It's located somewhere and It knows nothing about the rest of this of space But vector really is something else Here we talk about a vector being a pointed interval But in fact a much better way to think about it of a vector It's much better to think that the vector is a transformation of space a vector is a shift of space because What do you need to know if you want to shift something what do you need to know? well, it's it will be much easier to to express this in instead of Instead of a three-dimensional space it will be much easier to explain this on the plane So let's use this analogy So I have this table. I have this chair. Okay, so It's on the plane. So think of it as a kind of representation of a point on the plane where the plane is the floor or the podium really, okay So I want to shift it. So I move it in this way. What do I need to know? To what do I need to say in order to explain how to how to move it? In other words, if I ask one of you to move it I have to explain which direction and how far, right? So and suppose I ask you to shift everything in other words, I can ask some workers to come here and shift this podium I would say I want to be closer to you So I want to move this podium, you know Whatever one yard closer What does it mean? It means that each point of this podium will be moved by one yard in the same direction That's the vector a vector is a rule By which you shift all points and then if you look at the particular point like a point a Then a vector will displace it or shift it to a point B If you look at another point a prime this same vector this same rule will shift it to a point B prime And the point the origin will shift it to this point B So this is a much better way to think of a vector as a as a rule for displacement for sort of parallel Transport of the entire plane or the entire space If you have such a rule then you know where each point goes and that's what each arrow represents and now it becomes much more clear why different arrows as Long as they are parallel and have the same magnitude Correspond to the same vector because they are part of the same rule part of the same shift rule. You see what I mean Yeah Any questions about this this is an important point to to remember to kind of understand better what's The kind of stuff that we're going to do with vectors All right, but that's sort of a physical interpretation But now more more concretely we would like to We would like to work with vectors and try to represent them In much the same way as we represent points say So a point has three coordinates So now we want to to represent vectors also in a very concrete algebraic way What I talked about up to now is kind of geometry. I draw pictures and I explained the geometric mean But now I would like to do some algebra. I want to represent my vectors by by some coordinates or some algebraic objects and It's clear what we need to do We simply need to place the vector the initial point of the vector at the origin and we have to keep track of the endpoint and the endpoint we already learned how to How to Describe we describe it by its three coordinates X zero Y zero and Z zero right So if that's the case then we will write the following we will write that a Is equal to X zero Y zero and Z zero So what happened? We are using exactly the same information as the information provided by the point P But because now the point P is really not the central object, but it's something which is just We just sort of an auxiliary object which we just use to understand the vector because that's the endpoint For the pointed interval which we obtain when we apply our displacement at the origin, right? So the point P sort of has plays a secondary role here Nevertheless the point P has a very nice representation by X zero Y zero and Z zero So we might as well use this information because it uniquely determines our vector Because we agreed that the initial point is at the origin So all we need to know is the endpoint and the endpoint is just this so that's why this information is sufficient To represent a given vector, but when we write it like this We want to distinguish this notation from the notation Which I use for the point P itself for the point P itself. We use notation with the round brackets And we don't want to write it here like this because if we were to write like this We would be saying that this is a point, but it's not a point as I keep trying to explain to you It's not really a point the point in question here namely the point P is really just the endpoint of the vector But the vector has a lot more Carries a lot more information or has a different geometric interpretation than just a point So we have to separate the notation for the vector and the notation for the point and where we do it We use this angle angular brackets instead of the instead of the round brackets And to someone it may appear like we are too pedantic and it may appear like what's the difference? Well, the difference is if we will see now that the vectors actually have a lot of structure We can add two vectors to each other. We can multiply a vector by a scalar and so on a point A point doesn't have such a structure or points don't have such structures. We are not allowed to add two points We're not allowed to multiply a point by a number a point is just a point. It's static It just sits there a vector is a kind of a dynamic thing Which as like I said is a transformation of the space of shifting everything in the same direction and by the same magnitude So if you have two vectors two such shifts, you can actually do them You know one after another and therefore you get a third vector you get the sum of the two vectors and so on So in that sense a vector really is a different object on a point And that's why we really have to distinguish between vectors and points and use a different notation So this is the notation notation number one notation number two we Amongst all the vectors that we have on the plane we introduce the basic ones The basic ones are the vectors which go from the origin In the direction of one of the three axes I mean from the origin in case you want to apply it to the origin But in fact as I said you can you can apply it to any other point as well so this are There are these are the following three vectors one of them One of them goes along this axis and has length one It's uniquely determined by this because remember vector is uniquely determined by direction and the magnitude So amongst all the directions in space once we introduce coordinate system You have three basic directions x y and z so why not use those three vectors? But then you have to say which magnitude and the simplest magnitude would be one So you got yourself three different vectors this one this one And this one and we'll call them I J and K and so the point is that we can also write this vector as X zero times I Plus Y zero times J Plus Z zero times K in other words We can decompose any vector as a combination of multiples of the three basic vectors Which of course immediately begs the question as to what do I mean by? addition by the addition of vectors and what do I mean by multiplication of a vector by scalar, but I'm kind of Slightly jumping ahead because I know that you already know most of this material Let me just remind you Let me just remind you that a simple rule how to add two vectors There's a so-called parallelogram rule. Do you remember the parallelogram rule? Yes, or no, no Oh Someone said yeah, but most people don't remember should I remind you I remember Okay, so the rule is like this if you have two vectors a and b and You want to calculate what is a plus b? Then the way you do it is you apply the two vectors to the same point Again a vector is a rule. It's a displacement rule. So each point knows where it goes Right. So now we have two vectors. So we have two rules. So let's apply Let's apply both of them to the same point and it depends in your in your in the problem You're solving there will be some natural point to which you would want to apply apply them Now to find a plus b what you need to do is you need to Draw a parallel gram which is spanned by these two vectors. In other words, you draw a parallel line to this one Parallel to this vector and at this point you draw a parallel line to this vector So this and this are parallel and this and this are parallel and then these two lines will intersect somewhere And that's your that's the endpoint of the sum So that's the vector a plus b if this is a and this is b Then that's a plus b explanation of this rule Like I said, it's better to think of a vector as a displacement rule So now you've got yourself two displacement rules a and b Each of these rules can be applied to any point whatsoever. Okay, let's apply first. Let's apply first of them a and then apply b Consequently, let's apply them one after the other So first we apply a now we have to say to which point do we apply it? Well, let's apply it to this point There is the end result is that we Get here to this point after this I would like to apply the displacement rule B And I like I said this placement will be can be applied at any point here I drew the result of its application to this point What will be the result of its application to this point? Well, I have to take a parallel line of the same length This will be a directed interval which will represent the same vector, right? So I end up The net result of this two Two displacements this one and this one is that I end up at this point That's why the composition of the two or the sum of the two vectors is this vector So it's a very simple geometric Interpretation if you draw the picture using this parallel gram. This is called parallel gram rule if you draw it using this triangle is called a Triangle rule by the essence the the meaning is the same is this clear Okay We can also multiply we can also multiply vectors by by scalars When I say scalar, I just mean usual numbers real numbers What does it mean? Well here we should think we should think of a vector as a Direction and the magnitude when we multiply by scalar we we Well, it depends on what kind of scalar let me let me rephrase it If you multiply by a positive number if you multiply by a positive number This will be a vector in the same direction and the magnitude Will be c times the magnitude of the original vector So the picture will be something like this. This is the original vector. Let's say you want to take This is a and you want to a two a is going to be like this So this distance will be twice the distance of a Right, and you have the same direction If you want negative two or if you want any negative number You will have to be the opposite direction. So this is for example negative two a so Same direction see less than zero opposite direction opposite direction So you have two operations addition of two vectors and multiplication of a vector by scalar and And Here I combine these two operations. I take my vector i and my I am multiplied by x zero What do I get well according to the rule I have to get something which goes again along the x axis and now has length Which is equal to the to x zero times the length of this guy, but the length of this guy Just like the length of the second guy and the third guy J and K Is equal to one So when I multiply by x zero, I get something which has length x zero So it's just the vector which ends at this at this point And likewise if I take J times y zero I get a vector which ends at this point And if I take the third one K times Z zero I get this one and now it's not difficult to see That if you take the sum of this vector this vector and this vector you will get this one I'll leave it for you to figure this out So that's that's how you get the second the second representation of a vector This is the first one where we just keep track of the coordinates of the endpoint This is a little bit more descriptive because it really emphasizes the fact that it is The displacement is a superposition of three displacements one in the x direction one in the y direction and one in the z direction All right, so what's next? Next we have to Work out some tools for for dealing with vectors and And these two these tools are the so-called dot product and cross product So how many people know what dot product is? Okay, that's better So I'll go quickly over this By the way an important point is that I tried to do all of this in full generality in other words in a three-dimensional space So you see that I'm writing everything in space. I have three coordinates My vector has three components and so on but you can do the same analysis on the plane if you are on the plane if you are on the plane you can also Represent vectors in a very similar way the only thing that will be different is that you will be missing the last coordinate the last component That's all right because now you will have two coordinates x and y you can still talk about vectors on the plane and You can transport this vector to the in such a way that its initial point is the origin and then you will have two Coordinates we have some point P. You'll have two coordinates x zero y zero and so your vector Let's call it again a Sector a would be written as x zero y zero So see just to just two components instead of three So if you do a homework exercise and you are given a vector in this form That's how you know right away that it's a vector on the plane and not in space because you only have two components Or you can also write it as I x zero times I plus Y zero times J where one more time I and J are unit vectors In the x direction and the y direction so same same kind of representation on the plane is in space Now I will talk about dot product and cross product and Dot product also is perfectly well defined and on the plane Formals are very similar, but I will not write them They will be easy to derive by using the formulas in three in the three dimensional space a cross product the next operation that will introduce Only makes sense in in space not on the plane. So we'll only work with it in space Okay, so what's across? What's the dot product first first comes a dot product so you've got two vectors and A dot product is a certain operation Okay, it's a certain operation where you're given two vectors and you produce a number So this is very important. You start with two vectors, but you get not a vector, but you get a number This is different from say taking the sum of two vectors the sum of two vectors is again a vector You start with two vectors you get the vector also a Scalar multiple or multiplication by a number c times a You start with a number and the vector and the end result is a vector So when you think about all these operations addition scalar multiplication Dot product cross product the first thing you should remember is what is the input and what is the output usually has two inputs and one output Like Some addition to inputs both are vectors output also vector right so dot product Likewise is an operation and you know you can think of it as a kind of a black box. It's a black box. There are two there are two inputs And there is one output So there is some rule something happens here So it eats two vectors and spits out a number. Okay, so The two inputs are vector and another vector vector one and vector two and Output as a number So I have to give you the rule how this black box box works And here the important point is that there are two ways to represent this rule Which is actually very nice because then you can compare them and you can derive some useful information about vectors Which will use so what is the rule what is the rule for the dot product? so the rule number rule number one is He is Now I would like to draw to draw them on the plane or in space in space So I well I always draw everything on the plane because that's all I got I got the blackboard and thank goodness because you know if I think I would be much more difficult but Now I have these two vectors a and b And I would like to give you a rule how to produce a number out of them And so there are two rules the rule number one will only use geometric information about them So it will be kind of geometric formula so What I'll use is the magnitude of the two vectors You see this one has a magnitude which again is just a length of this pointed interval And this one also has a line has a length of magnitude sometimes I call the length sometimes magnitude It's the same thing And there is one more piece of geometric information namely the angle between them Now it's a sharp angle, but actually in general it could be up to the angle They could even be opposite to each other, but anyway, there is an angle between them So the dot product first of all the notation for it is if this one If this one is a and this one is B. This is called a dot B Which is why it's called a dot product. All right What's what so what is it? It is the length of the first one Times the length of the second one times the cosine of the angle between them This is a vector. This is a vector. This is a number. This is number This is a number. I take the product of these three numbers. So this is a number as promised I start with two vectors. I get the number. That's the dot product rule number two up to now We have not used Algebraic representation of a vector an algebraic representation of a vector Has two, you know, we have two different ways to to package this information about the vector by using these three numbers X zero Y zero and Z zero which is obtained in this way So let's suppose that a is X one Y one and Z one let me Let me write it on a different board so that I don't have to then rule number two Might as well right there rule number two is that a dot B is X one times X two plus Y one times Y two plus Z one Times it in other words. I take the X coordinates and multiply them I take the Y coordinates and multiply them and I take the Z coordinates and multiply them. It's very easy Just like this multiply multiply multiply at the mouth that's the number I get and It's sort of it looks terrible. It's a miracle the fact that the two definitions two rules are actually equivalent to each other And but it's actually very easy to prove It's very easy to prove that this implies this By using this presentation Actually, maybe it is actually might might might actually be worthwhile worthwhile doing this to give you Because actually the funny thing is in the book they explain it in the opposite direction From rule two to rule one, which is much more difficult So I think it's actually much better to To take the rule the first rule as a definition and that's often the case that the best definition is geometric because it has a nice interpretation and Usually has a much deeper meaning like here It's something which has to do with the structure of the vectors and the position of the vectors The way they are situated on the plane or in space Whereas the second definition is algebraic and it's really a working definition It's something which is very useful in calculation What might not be so useful for conceptual understanding of what we are calculating So usually the right way to go is from the geometric definition to the algebraic definition And that's the way it works here So you see and I would like I think it's a good idea to actually do this to explain this to you because here I will illustrate different rules of of Dot product in regards to addition of and multiple scalar multiplication of vectors So you see here's what I'm going to do. I'm going to rewrite this in the second way. I'm going to write it as x1 I plus x2 y1 j plus Z1 K and Likewise for the second one so far. I've done nothing. I'm just using an equivalent notation But but now it's my it's it will see how that this is actually better because what I'm going to do is I'm going to calculate the dot product By using the second definition Okay, so now it looks like a product of two things and it's tempting to open the brackets The way we would normally open the brackets if we were calculating with numbers Now a priori is not clear that this is This is allowed But in fact you can you can justify this by using by using rule one so the point is that actually We are allowed to open the brackets in the same way as we open the brackets when we do calculations with numbers So when we open the brackets, we are going to end up with Each of these terms on each of the terms in the first some dot one of the terms on the other side, right? So this is going to be x1 I dot X2 I Now in principle, I have to write down how many three times three nine terms Okay, but I'm going to save I'm going to save some time. So I I'm going to to see the following that There will be terms of two different kinds in one of them. They will have matching vectors like I dot I There will be three of them X1 I times X dot X2 I Y1 J Dot Y2 J and Z1 K Dot Z2 K Right, so these are the three Summons that I will get which will involve the same two vectors I I JJ K K and Then I will have Cross terms I will have terms which will involve say I and J or I and K and so on So, let me just write the first one of them X1 I and you will see why I will not need to write the rest of them X1 I say times Y2 J And so on so there will be I mean I'm skipping five more terms, but all of them are going to be cross terms So now I have to calculate each of this. I mean to calculate each of this Terms each of these nine terms separately and for this I will going to use the following rule that if I have X1 I say dot X2 I I can pull out the numbers I can pull out the scalars out I Can pull out the scalars and put them outside of the dot product Right so that's the same as X1 X2 Times the dot product of I and I So what's the dot product of I and I I dot I? Here is my vector I And I want to take dot product with itself Now the rule is that this is going to be equal to the lengths of I times the lengths of I Times the cosine of the angle between them, but the angle between them now is zero Right because I take the same vector twice So this is one this is one and the cosine of zero is one so this is one the dot product of I with I is one So the net result is X1 X2 So this first term gives me X1 X2 likewise the second term Y1 J Dot Y2 J Will give me Y1 Y2 Because I will need to calculate the dot product J dot J, and that's again one because J is a unit vector same calculation and finally Z1 K Dot Z2 K Will be Z1 Z2 So the first three terms will give me exactly the answer of The which appears in rule 2 X1 X2 plus Y1 Y2 plus Z1 Z2 What about the cross terms? Let me calculate the first one of them X1 I dot Y2 J That's X1 Y2 times the dot product of I and J And what's the dot product of I and J? That's zero right and the reason is that now the angle between them is pi over 2 and The cosine of pi over 2 is 0. So when I apply rule 1 I see that I dot J is 0 So all the cross terms Will disappear and I will end up with rule number 2 So you see it's very easy to go from from rule 1 to rule 2 this way And now the main point is that you can actually use you can put these two rules together and This will allow you to find the cosine of the angle between two vectors when you just when you just know once you know the The three components right because you can turn this you can turn this around and You can say that the cosine of the angle can be written as a ratio of the dot product What is this time I Think I have one more minute Okay, well you got the idea we can turn this around and calculate the cosine from knowing the length and dot product Okay, we'll continue on Thursday


you know, there is some responsibility, some focus that, you know, some things that we have to do to make sure that our applications are form reality, for example, when we optimize fashion on these new, you know, for example, this machine is a dual core. Dual core seems very, you know, it's like the order of the day today. So, that means, you know, that has an implication on JavaScript as well, right. So, how do you write JavaScript code which can, you know, leverage the modern generation, the new hardware better. You know, fast fluid interfaces are absolutely critical today, right. Nobody wants to look at the applications of, you know, maybe five years ago, right. So, we want, you know, applications that respond to creating JavaScript code which can provide that compelling user experience is very, very critical. There we go. So, you know, ideally, you know, we should be able to delegate IO intensive and CPU intensive tasks to additional cores, probably not IO. IO can be done asynchronously, but at least CPU intensive task and we should be able to leverage additional cores and additional CPUs and so on. So, that's why async is important in the general sense, right. With JavaScript in particular, you know, what are some of the challenges when we are working with async code.  The callback is, you know, it's very easy for us to try and adapt, you know, to code there and, you know, have one set of features that we can handle. That is not possible to do with callback. You would have to have a site that's pretty much invisible callback to have. It just makes the code really messy. And, you know, callback madness is something that's very, you know, very frequently it happens, right.  So, has anybody worked with index DB here, index DB HTML5 spec? Okay. So, you know, if you look at the index DB spec, that's like really, you know, it's like the extreme case of callbacks.  It doesn't make sense to block a UI thread.  If we have time, which I doubt, I'll show you a little bit of, you know, index DB code to iterate through the records in an object store, which is the equivalent of a table.  So, let's look at, you know, a couple of approaches. In fact, this is not a new concept. So, you know, folks who have used CommonJS and 1.5 onwards, you know, these libraries shipped with promises, they might use different names. For example, in jQuery, it's called as a deferred callback. In fact, internally, they use the term promise itself. CommonJS uses promises. So, we'll see what that is. In fact, most of the session is just going to be demos. So, let's straight away go and, you know, take a look at promises. Maybe what we'll do is we'll build a very simple, you know, promises framework and see how we can work with that. So, I'll just create a very, you know, let me just put that into... Maybe I'll call this promises 101, right? So, it's an empty web application. There's nothing in it. So, I'll go, you know, add a... Maybe I'll create a folder where I'll put my scripts there. So, I'll say, add new item. So, I'm going to call this promise.js, all right? So, a promise, you know, it's like a magic trick, right? So, when the magician shows it on stage, it looks like amazing. Wow, how did he do that? When he tells you what actually is the under the covers, it's like, what? That's it, right? So, it's the same thing happens here. When you look at the, you know, how promises work, it's like, it's like no big deal at all. So, basically how... Or let me show you how a promise is used first. So, I have a project here where... Let's see. So, this is basically a page. Hopefully, the internet connection is working. I'll just show you what it does. I have a data card connected here. Maybe it'll work, I don't know, if you're lucky. Great, works. So, basically what it does is it goes to Twitter, gets the, you know, top, the recent 10 tweets which have been tagged with JS2, right? So, these are some of the tweets that are happening right now. JS2 is so pretty awesome. So, great, people are liking it. So, if you look at the code for this, right? That's it. So, what I'm doing here is basically I have a very regular table here. I'm using jQuery templates to do the, you know, basically a client-side templating to... Oh, yes. Thank you. Forgot to do that. So, basically it's a regular table. I just populate that with the tweets. So, the code for getting the tweets is... That's it, right? So, what I'm doing here is there is this object called tweets. I'll show you what that object does. I have a method called get, and then I say then, and I pass a callback here. So, then is basically, you know, a method that encapsulates a continuation, right? If you're familiar with functional programming, a continuation is basically an object which represents a callback, right? So, basically what we're doing here is you're saying tweets.get. Basically, you know, it's a fluent API, right? So, you can say some object dot, some method dot, some method dot, and so on. And you can keep going like that. So, for example, here what I'm saying is I'm calling this API called tweets.get. I'm saying then, you know, run this function. And here, basically, I just get the data and apply the jQuery template and, you know, just goes into that table. Interestingly, you know, it gives you a very clean way of doing error handling as well, right? So, if you have, if something goes wrong, you know, you basically, you pass another, another function, a callback, which basically gets invoked if something goes wrong while invoking that particular tweet. Now, let's suppose that we want to do something more than that, right? So, we want to sequence this. We want to say, go get the top 10 tweets and then you want to do some processing. Maybe I want to store it in a database or I want to do some analysis on it, right? So, basically, how promises work is they compose, right? So, I can say then, and then I can keep going like that.   You're doing extremely asynchronous APIs. For example, when you're dealing with something like index DB or if you're doing Ajax calls as we are doing in this case, then it really kind of helps. So, if you take a look at the tweet.js file here, it's a, you know, that's it. That's all it does. So, what's happening here is basically, all this is the Twitter stuff. The interesting thing is over this, right? So, I create an object called promise here and what I'm returning from get is that promise instance, right? I'm not returning the data. I'm not doing anything because the data is going to come sometime in the future, right? And I'm returning an object which represents the continuation, which represents what is going to happen in the future. And here I basically use jQuery to do the actual call. So, make a JSONP call and the interesting things again are here. In the error and the success callback for Ajax, what I'm doing is I'm saying promise.resolve in case it goes fine, right? Again, this API doesn't care who is listening to or what's going to happen subsequently when the data has come. This is like the, you know, a clear separation of that, right? When I, I'm just dealing with the promise object, the API. The API simply says I'm done. I call reserve, resolve. Whoever is, if you are familiar design patterns, you might be familiar with, you know, this is basically an observer-observable pattern, right? In fact, we'll see that there are commonalities when we talk about RxJs as well, where the observer pattern, you know, plays a big role. So, that's what happens here. And in case of error, it's just a different signal, right? Here we say if something goes wrong, I say error and then the correct callback gets invoked. So, this is the idea behind promises. And now actually it's directed this particular way of, you know, doing asynchronous development is directly supported in JQuery. So, for instance, I can actually do away with this completely. So, this is a promise object that I have implemented. Instead, what I can do is I can simply say return $.ajax. So, I hope I have included the right versions. I have 1.6. So, this is supported from 1.5 and up. So, this actually returns a promise. For backward compatibility reasons, it also returns whatever, you know, you have all the APIs that you are used to having even before 1.5, like error, success, Ajax. I mean, all the regular JavaScript APIs are available. In addition, this is a promise as well. Yeah. Yes. In case of error, okay. This won't get called. No. Because the error actually ends that particular thing, right? So, in that case, it won't get called. So, what you can do is in this case, the error handling happens. And then let's assume that this was successful and then, you know, something went wrong here as a second optional argument. I can pass an error handler for the second asynchronous call and so forth. So, yeah, so I can simply do this and, you know, this code should exactly work as is or perhaps not. The reason is I've used my promise here, right? So, actually what I have to do is I have to create, in fact, let me not try to debug this online. I have a few snippets here that I have kept just for this case. There you go. It's supposed to create a... It's very similar code, right? So, instead of this, what I can do is I can say new jQuery.deferred and then instead of error, they call it reject and resolve is actually exactly the same. So, that's the jQuery version of it. So, you know, the exact thing works. So, basically, you know, just different syntax, right? So, I had used promise in jQuery. You say new jQuery.deferred. That's the only difference. But let's see what this promise actually looks like. So, let's go back to our promises 101. It's very, very straightforward, right? So, if I implement a promise object, so the idea is, or let's say, let's do this function that and yeah, yeah, yeah. Keep reminding me till it's like hammered into my brain. This dot... So, you can simply say this dot resolve or let's call it onResolve or something is null, this dot onError is this and then I can basically go and add stuff to the prototypes, right? I can say prototype.then equals function resolveError. So, those are two callbacks that I, you know, can receive here. So, in here, what I can typically do is I can simply say, you know, probably I'll also add promise.prototype.resolve equals function and promise.prototype.what is that? Error equals function. I guess I'll need the error here. I'll need the value here, right? And here all I need to do is I can simply say if this dot onResolve, I simply say this dot, whoops, this dot onResolve of v, right? And very similar code goes here. So, if the user has passed onError, then I simply say this dot, whoops, this dot onError of e, right? And then all I do is I simply say this dot onResolve equals that, this dot onError equals that. So, this is a very, you know, simplistic implementation of the promise, but you can kind of get an idea of what's really happening, right? So, when I write code like, you know, tweets.get.then or function, something, something, basically when I pass this, you can see what is happening. So, tweets.get is actually returning me a promise object. And in then, basically the internal onResolve gets assigned there. And subsequently when the, you know, when the Ajax response comes back, I call resolve, right? From the, we are the closure, I call resolve on the promise object. So, it's no black magic, right? So, when I call resolve, it sees it is there, it calls this and this particular callback gets invoked, right? It's really, really straightforward. Now, the only thing, additional thing that we do is, you know, to support chaining, right? So, this is basically, I can say then and that's it. I can't change it to the next then, right? So, one additional thing that you can do is, you can actually return another promise from here. So, I can say var new equals promise. So, I'll slightly change the implementation because I need to do something in response to, in response to resolve. So, I'll put that particular if check here. So, if I say, if this dot onError, I mean, sorry, onResolve, then great. Go and call onResolve on that. I guess I'll need the value here. And then, I'll also say promise.resolve, right? And then, I'll do likewise for this. Imagine that I just did all that. You know, I just did of E, if this dot onError, then this dot onError of this and promise.Error. And here I'll simply return promise. So, this is exactly the code that we did in tweets.get, right? It's nothing different. All I have done is, and then I've taken the code there and I've basically extended the idea to the next level. So, inside then, I create yet another promise and return that from then. The worst case that can happen is, you might create one extra promise object which never gets used. So, if you, if you, let's go back to our tweet solution. If you go here. So, I have tweets.then, right? So, then in turn, gives me a promise object. So, just as get return me a promise, then returns another promise. Therefore, that is why I'm able to do .then again. So, in fact, so this is the running code. So, with all the, you know, proper validation and everything. So, basically, you can see the exactly same things. I have resolved error. You know, promise.put it up then. I have onResolve onError. So, in this.resolve, I create my own handler because I need to do a chain invoke of another promise. Right? So, here I make that call to the, to the subsequent, you know, next object in the chain like this. And it keeps going, right? If I say another then, then it just works. Yes. So, that is right down here. So, when I say resolve, it basically calls this.resolve.call. So, in this case, this.resolve refers to my internal function here because that's what I have assigned to, right? This.resolve, I have said this function. That's what this is going to call. When the, so let's say, tweets.getCalls, promise.resolve, this.resolve is going to get called. My internal function is going to get called. Here, I'll just, you know, delegate that to the actual onResolve, which is the callback that the user supplies, and then I just chain it to the next one. If you are, if you, you know, if this seems a little confusing, what we can do is we can just, see, this is the problem with JavaScript. It's already 12.10. So, I'll just very quickly do a debugging here and show you what's going on. So, I'll put f9 here and just refresh the page. So, there we go. We hit that breakpoint. Now, if I say f11, we go into tweets.get. So, that's fine. I'll put a breakpoint here. So, f10, f10, you know, oops, wrong library. I think I'll have to go back to our promise object. So, that, this should be error, and everything else should just work. So, f12, script. I'm sorry if this is small. I don't think I have zoom here. So, what do you know? This is a JavaScript session and a night test field. So, I'll just refresh this. So, I'll say f11. So, I create the promise here. Nothing, you know, major. So, I'll put a breakpoint in the callback from my Ajax call. Sorry? Oh, yeah. Correct. Thank you. And I'll do that.  And now, then it's going to get called right now, right? The callback is going to come sometime in the future, but then it's getting called right now. So, what did I just do? Yeah, I'm returning the promise. You know what? I'm just checking if you guys are paying attention. Looks like you are. At least one gentleman is. All right. So, nice. Remember the breakpoints. So, do we have everything? Yep. So, I'll just refresh the page. Hopefully, we'll get it right this time. So, I'll just come out of there. Now, I will go into then. So, basically, I create this, right? So, I'll put on a breakpoint there. And then let's just hit f5. So, basically, the success is, you know, the Ajax thing came back. We call resolve. Now, we come here. We go inside. So, you can see that this internal function is getting invoked. And we indeed do have an unresolved handler. So, I go there. Now, this is the handler that the user supplied. So, I do my jQuery template thing and, you know, put it on the user interface. And then here, you know, the next change resolve gets invoked. It goes back to resolve. You know, it's like it's a re-entrant call. And then, so now you can see that the next then got invoked after that, right? The change call happened. Obviously, I'm not doing anything there. So, that's basically the idea. So, yeah. So, we're already way past. I wanted to spend 15 minutes on promises and then jump into RxJS. I've taken 30 minutes. So, because RxJS is much more interesting. Let's quickly review what is RxJS. We have shifts with support for the real power of RxJS is in how it allows you to compose asynchronous operations in creative ways. So, we see there's a notion of a combination here which allows you to do this composition, right? I know that that sounds very confusing. But this is the basic fundamental idea behind all these asynchronous sources of information that you want to do. So, that's why I'm basically using RxJS. As we, you know, some of the demos that are not utilizing the source are just to make it easier. And it's a synchronous data from the asynchronous static data source which I used to, you know, build data in my pipeline. But the idea is there are objects which can be observed. And there are other objects which are interested in the things that are observable in RxJS, right? So, are observable in RxJS. So, it's very simple. RxJS has an observable object which is a class of an event source and that can modify stuff to are an impressionist. The idea of observer subscribes to is the observable that I use and impression in getting an image from you to give you an image, right? And whenever the interesting image happens it makes the following an observer and your code runs at that point. So, that's all the, all the PPT I have. So, let's quickly take a look at it.  I wrote this so that I don't forget stuff. So, I have written a little eval console. In fact, that's not what I wanted to do. I have it hosted locally. I'll go to that one. So, basically this is a, let me just hide this toolbar here. Okay.  I can type JavaScript. I can hit control enter and it'll run the JavaScript, right? So, I can, for example, say alert jsfoo, right? I hit control enter, it runs it. But instead of alert, you know, I have a console.log. I have a, you know, convenient function here. If I do, it'll basically print it on the right. You know, if I make errors, then it basically, the errors show up on the bottom right. So, that's what it does. There are some convenience functions here which I'll talk about as we get there. Maybe what I'll do is I'll zoom in a little bit. I know the error window doesn't show, but hopefully I won't make errors. There's one convenient function called clear which will clear the log and all that stuff. So, basically what I've done is, let me show you the source for this page.  So, I've downloaded the js, hosted it in my website and I've included it, rx.js, rx.html.js. So, I'm not using support for jQuery and all. So, basically, you know, this is the root js file that you have to include. And then, depending on what other support you want, there are additional modules that you can include as needed. So, if you want support for jQuery, then there is rx.jQuery.js, rx.dojo.js, and so on and so forth. So, basically, we can, you know, experiment with... You know, in this console window, we'll try to explore the rx framework. So, we talked about observables and observers, right? So, let's see how we can go ahead and create one. So, for example, I can say observer equals rx... Sorry, observable equals rx.observable.create and basically, I pass a callback to it. Now, the idea is this particular function is going to be responsible for raising the events. Whenever, whatever that event is, whatever that interesting event is, this function is responsible for raising that. It accepts one parameter, which is basically the observer, right? Who is the object that's interested in receiving the events that I'm going to raise? So, this is the simplest observable observer example that you're probably ever going to see. I'm simply going to say observer.onNext of, I don't know, 10. And I'm going to say observer.onCompleted and then I'm going to return a function from here. So, I'll tell you what all these pieces are. Let's go ahead and create an observer. So, I'll say varObserver equals, right? In fact, let me do it this way. I'll say observable.subscribe and pass an observer as a parameter here. And I'll say that is my value. And what I'll do here is I'll simply say print... I don't know, print value equals... So, one convenience function I have is... So, I started my career with C++. I love my sprintf. So, I got an sprintf port here. So, I can do something like value equals percentage d, v. So, when I run it, it prints value equals 10. So, what exactly happened here? So, as soon as I subscribed an observer, the whole process got kick-started. The observable got fired and this particular function got invoked and onNext is getting called. So, basically an observer is very simple. It has three methods on it. So, I get these terms... An observer is an object which has three methods on it. OnNext, onCompleted and onError, right? So, actually instead of giving it like this, I could have passed an object here. So, this is the common JavaScript options syntax, right? You can pass all kinds of things. The API will do the correct thing. So, if I pass an object like this, I can say onNext and pass that particular function here. I can say onCompleted. I can pass the handler here. Or I can say onError and I can pass a handler here. So, this is an observer. So, subsequently when we see more complicated examples, you know, just you need to remember that an observer is just this, right? Just three methods on that particular object. So, what's happening here is when I call onNext, basically the onNext callback gets called, right? OnCompleted, onCompleted gets called. So, for example, I can say printCompleted here. Maybe here I'll say printError and, you know, it's all really, really straightforward. Now, what is this additional thing that I'm doing here? So, this is a very interesting pattern in RXJS. One common problem. So, here the idea is the addObserver API will return an object which can be used to dispose or disconnect the connection. That's the idea. So, for example, here the idea is I can do cleanup here. So, I can say printDispose, okay? So, now if I run this, let me put a clear at the top so that we get a... So, when you run this, you say value equals 10, you get completed, and then you get disposed, right? So, who's calling dispose and all that? The RX framework is taking care of doing that, right? So, this particular, you know, observable sequence has completely executed. The onCompleted got fired. Now it can be discarded. Therefore, it calls this particular function to do cleanup. So, you know, it depends. Like, this is a completely useless observable where I don't do anything. But imagine that, you know, you might have allocated some resources here, right? You might have, I don't know, opened a connection to your indexDB. So, this is a good place to close it. So, that's the idea. So, this is the most basic example of, you know, how you can use observable and you can create an observer, and then you can hook them up and do interesting things with that. Now, the RX library ships with a bunch of, you know, useful so I'll just get rid of maybe not all of it. Let's see. But this is how you can do it if you want to create your own observer. So, there is one helper API like this. You can say return of 10. So, if I run this, you know, the exact same thing happens. My observer code doesn't change. Basically, you know, this is just a convenience routine which is exactly what we just did. So, it will simply say observer.onNext and it will pass the value that I pass here, right? So, nothing fancy.  For example, I can say range of 1, 10, right? So, here you can see that in this case what happened was it basically iterated from 1 through 10 and invoked my onNext in response to that, right?  So, you've done, count it from 1 to 10. You know, not very impressive. Hopefully, as we keep going, you'll probably get to appreciate it. Another useful thing that I find that you can do with this is you can say from array and you can pass in an array. For example, I can say, you know, pass a bunch of numbers and if I do that, then it basically trades to that array and invokes your observer. Now, you can do stuff like this, right? So, let's say I have 1, 2, 3, 4, I don't know, 5, 6, 7, 8. Imagine that, you know, I'm interested only in, so this 1 to 10 that I've put, I've hard coded it here, but imagine that this is some event source which is coming from somewhere, okay? So, maybe it's coming from a web service call that you're making, right? You're making an Ajax call to some, you know, some service online that returns the data and now you can compose on top of this. So, for example, I can say, you know, for example, I can say, I don't know, I can say where function of that, we put that there, I'll say return V percentage 3 is 0. So, now you can see that my observer code doesn't, you know, change at all, but you can see that only odd numbers are getting printed now, right? Because basically I have, you know, added a composition on top of the previous observable, right? So, what is getting returned here is, basically you can think of it like a pipeline, right? So, the initial from array gives an observable which iterates through the elements in the array and then there is a where which composes on top of that observable, right? And then it does something extra on top of that and, you know, you can do interesting things like that. For example, you know, you can do, I don't know, so dot select, you can project it into in different ways. I can say return V star V, right? Now, here you can see that I'm getting the square of all the numbers, so maybe I'll get rid of this filter. So, here you can see that, no, basically every number got squared and then, you know, you can, you know, as before I can say where and, you know, do additional operations on top of that and it goes on like that. In fact, let me just open the, sorry, myself I'm kind of new to the library but I'm so excited about it, because I want to talk to you guys about that. I'll just open the, there we go. So, this is the documentation for this, you know, it's very, very detailed documentation available. So, these are some of the combinators. So, the select where that we just explored, these are called as combinators and look at the list of combinators that are available, right? It's like a very, very exhaustive list of operations are there to do all kinds of things. So, we'll look at a couple of slightly non-trivial demos to see how you can creatively combine all this and do interesting things but there's a whole lot of stuff that you can do. Now, so far in this sample, I was just using static sources, right? Either I just created one number 10 or I go through an array and so on. Let's do, you know, something more real world.  So, turns out there is an observable that you can create for that as well. You can save from HTML event. This takes two parameters. The first one is the element that you want to, you know, observe, I'll say document.body and the second parameter is what is the event. So, in this case, I'm interested in mouse move, right? Maybe here, what I'll do is I'll change this slightly. I'll say sprintf %d, %d. I'll say v.x, v.y, close that, close that and that should do it. So, I've run it. Now, when I move the mouse, you can see that it's getting printed on the right, right? So, what is happening here? Basically, the mouse itself has been, you know, abstracted as an observable, right? And now you can compose on top of that and all of your entire list of combinators can be applied here, right? So, there are some interesting combinators that you can apply. For instance, this is too much, right? This is too much of mouse coordinates. Maybe I'm not interested in, you know, getting so much of data. What I'll do is I'll just refresh the page, otherwise that event binding, maybe I can do a dispose, right? But let's do that some other time. So, I'll just refresh that page and I can do some interesting things. So, for example, I can say, I think I forgot the name of that API. Okay, let's try this one first. So, I can say delay of 5,000 milliseconds, okay? So, now I'll just run this. Now, for five seconds as I move the mouse, nothing shows up, right? And then it starts showing up, right? So, that's a very simple, you know, combinator. It might not seem like much, but kind of gives you an idea of the kind of things that you can do, right? Now, I'll just refresh that. There are more interesting things like that. For instance, I can do a sample. So, what I can do is I can say, you know, I don't want it so frequently, I want the coordinates to come only every 100 milliseconds, right? Now, if I run this, now as I move the mouse, you can see that the frequency is much lesser now, right? I don't get every single coordinate. It samples the coordinate every 100 milliseconds, right? And you can obviously give different values there. So, this is basically a concept called a debouncing, if you are in the funny term. But basically what it means is, I'm interested, basically it's a sampling. Sample is a great term, right? I just want to get the information at specified durations. Another thing that you can do, another thing that you can do is, you can throttle the event source. I can say throttle. Let me just make sure I get the thing right. So, you can say, let's try 500 milliseconds. I'll say that. Not quite the effect I was expecting. Right, so, wow. That's very slow. I was expecting it to be milliseconds, but it's not 500 seconds, either. Okay, I don't know what's going on. Sorry? Exactly. So, that's exactly what's happening. It enforces an interval between two mouse moves to be half a second here, right? Maybe if I try this. Good idea. We'll do that as well. Let's put this here. How shall we do this? What prev is date.now. Here I can say that. I can say date.now. Oops. Minus prev. prev equals date. I missed something, no? Oh, yeah, 45 minutes is done. Okay, so we'll just do this one thing and we're done. Yeah. So, some funny business. I don't know. So, that's one thing. I wanted to show you some additional interesting demos which does something more fancy than do this. But you get the idea. Thank you, guys. Hope it was useful.