 Hey guys, welcome back skits own episode 16 topic today is trigonometry Quick preview in this video. We're going to cover approximations for sine and cosine as well as tangent and arc tangent if that interests you Stick around if not, I'll see you next one. It should be a pretty quick video today So trig is an important part of math. Most of math is waste of time, but trig I would say is less so The only couple of functions that are important in trigonometry and that we're going to use in this series are these four and if we need more we'll code them up later, but Sine cosine have their uses all these functions basically relate a vector to its Components and the angle of those components make with the vector. So yeah, that's kind of how this works In my opinion a tan is the most important one arc tangent We'll use that a lot in this series But of course sine and cosine have their widespread uses as well They're just the same function shifted and then tangent is just the ratio of those two together So I think clear winner is a tan, but to each their own Question is though, how are these functions evaluated like if you open up Matlab or you open up Python And you type in sine of point three what's going on under the hood to give you that answer Is there a lookup table like do you do even know does anyone even know? And of course Some people know and there are a lot of ways to do it some of them are good Some of them are bad some of them are fast some of them are slow I would say again in this series It's all about what makes the most sense to me that I can wrap my head around some of the the ways that people have come up with to compute or to estimate These types of things is just close my mind how cool, but also how like convoluted that Implementation is and so no we're not going to be using any fancy schmancy stuff We're not using a kind of hardware Stuff that you could use we're gonna stick to tried and true Taylor series for three out of the four functions in this video for the last one I'll talk about that later But yeah Taylor series is basically a way to approximate a function of around a point you can see here function of x Basically, we're taking the derivative of that function a bunch of times at a different point and Based off the distance of that point from our point of interest. We're gonna compute kind of like the the x the estimated value of the function and So yeah, it works pretty well especially for functions that are periodic and that you can kind of Pull in for example sign of a million pi you can pull that in to the range Plus or minus pi right because it's a periodic function Of course, yeah, there are other faster ways to do this But we're not gonna use those in this video maybe in the future maybe in the lab to come We will implement some of the very cool Approximations for these type of trigonometric functions I'm not gonna belabor this very much here is the Expansion for sine cosine and arc tangent So yeah, again Taylor series is an infinite sum so you're adding up terms So you can see for sine of x the first term of your approximation is just x and if you only wanted one term There's your first term you may have heard the small angle assumption Well, here it is sine of x The first term of their series is just x Similarly small angle assumption for cosine is just one as well as small small angle assumption for our tangent Which is not really the same thing But whatever it would be x So yeah, but anyway, you're not gonna have one term You're gonna add up a bunch of terms the question is how many terms should we add up? Should we add up ten terms a million terms? How many is too many? You know you have to value the Error you're gonna get right obviously the first term is not gonna capture the full You know definition of the function So how many terms do you want to add in before you say hey that's enough? And so I honestly there are a couple good answers to that you could say well I'm always gonna add up ten terms and that's fine And that's actually good because you don't have to constantly be checking anything you say well I'm gonna do ten iterations of this You know whatever you're gonna loop through in your assembly code to get these terms I'm gonna do it ten times and that's it always ten times. That's fine And that's actually very very very accurate because you know You'll see how fast this converges on my next slide But I would say one but maybe even better than always having a fixed number of terms are adding together Maybe you'd want to say well. I'm I'm gonna be okay with My program needs this sine of x to be within point zero one I don't care if it's point zero zero zero zero zero one or point zero zero zero zero one or point zero zero zero zero zero zero one Doesn't matter to me. I just need to be within you know a hundredth In which case you would want to implement a tolerance And so the way that works is again each of these terms gets smaller and smaller and smaller You kind of can see here the denominator is a factorial and The numerator is also raised to the power of a high number and you plug in the number like You know point five This this shrinks and this causes the shrink so This number gets smaller and smaller very quickly So the question is Which term you think would be would be enough and so basically If you had a tolerance goal of point zero zero zero one like in this example for sine of point five Well, if you plug in point five to these terms you kind of can begin to see just how quickly Your approximation is converging. So the fourth term Is already point zero zero zero zero one six And so you're you're already well below your tolerance So the way we're going to basically do this is we're going to say i'm going to keep adding up terms Until the term I have just added up Is below the tolerance and if you implemented that way you will never Be beyond your tolerance from the correct answer So, yeah, that's how we're going to basically terminate our infinite sum Now When it comes to tangent of x I don't like the Taylor series approximation because it has this B in it if you look at Wikipedia, this is b which means there's a Bernoulli number I'm not sure what that even means. It's too hard for me. So I'm not going to do that Instead we're going to stick with some other Perhaps worse Approximation for tangent and that is what I've shown here is the partial fraction expansion for tangent of x and here's what it is That's simply it. You know that it has a little bit of weird stuff going on on this left hand side But again, you can just divide Away and then you could manipulate your input to be Divided by pi and again, there are things you can do to make this make more sense But this is the way you'll find the partial fraction expansion for tangent of x if you were to look it up Honestly, that's it. That's the whole the theory for this video. Um in terms of code We're going to go through the code right now very superficially Um, we have two examples for a sine and cosine two for tangent And then we have one example that covers arc tangent and I did implement in that arc tangent example A polynomial estimate as well because we're going to use arc tangent a lot in this series. And so it's nice to have We tried and true killer series method that we understand and then also Polynomial approximation as well polynomials are easy to understand because again, it's just hey, I'm Here is my estimate for arc tangent or whatever the it, you know, here's my estimate for the polynomial of arc tangent so Evaluate this point on that curve. It's very uh Very simple. So the Examples are all an example 16 in the repository. So let's check out The first one Actually, let's check out the code first Do I have a Yep, so under our library of assembly functions under math under expressions under trig We have a bunch of these functions defined arc tangent cosine the sine as well as tangent I'm going to go through the sign function that kind of gives you the idea for the rest of them So for the sign function, you'll see that it's a function that returns a Double precision floating point number and it takes in two doubles. The first double is the x. So sine of x That's the x and the Second input and x and m one that is the tolerance As I mentioned before And so you can see here the first couple lines of all the functions that we're going to do in this series Are preserving registers so you can see we're going to Use rcx rdx as well as a couple of floating point registers for this Function and so we preserved them in the beginning and we recovered them at the end And so this way our registers aren't going to be clobbered in the function. It makes it a lot easier that way So the next thing that we do is we adjust our input So you'll note that well, you should you should know that sine is a periodic function and It repeats over and over and over again. And so you can very quickly Pull in if someone passes in x equals 10 million You can very quickly pull that in to the small range around zero About which our Taylor series approximation is accurate So that's the idea of this bit. I'm not going to put a code there. You can kind of figure it out It's pretty simple. Um We're basically getting the multiple of two pi that we are away rounding down by that many two pies Um, and then you can see here is get ready label. What this does is it actually prepares the Taylor series Uh approximation and so they go back to the sine of x Taylor series If you look at these two terms The difference between this term and this term is that you basically multiplied by negative x squared and then you've changed the denominator My numerator has just changed by a factor of negative x squared and that's how it continues from term to term and so We're basically pre computing that multiplier. So if you look back, you'll see here x of m10 I've computed that as negative x squared So we can use that as we go from term to term in our expansion then You can see here. We are Computing the denominator that factorial. We're actually using rdx to keep track of the denominator so it would be like a You know, we're going to multiply by terms over and over and over again So that's how you compute factorials and then rcx tracks the polynomial order But anyway long story short We just compute every term and we check every term against the tolerance Important thing to do though is to always make sure you're taking the absolute value because those terms change sign above and below zero so I'll take the absolute value of your term before you can evaluate it against your Tolerance because otherwise all negative numbers are below tolerance, right if the tolerance is positive It's not going to work. So That's how that works Um at the end of the function Well, once we fall out of that function and we evaluate there below where tolerance We pop everything back off the stack and return to the original calling function That's how sign works. That's how cosine works. That's how arc tangent works. It's all the same The only difference would be with our tangent function and in tangent I told you before we're not using the Taylor series approximation. We're using instead a partial fraction expansion But basically it's all the same stuff. We again, we come back from our Potentially large inputs. So if someone passes a negative a trillion We can relimit that input to within a smaller range around zero then We just implement the partial fraction expansion terms and Loop around add them up Check against the tolerance every iteration of a loop and then once we're done We pull everything back off the stack and we leave The last thing I want to show you in the in the code is actually the polynomial estimate for arc tangent and so This is a low order approximation of arc tangent, which is simply this Approximation that you see here. All it is is, you know, pi over four times Z minus z absolute value of z minus one times this random stuff where z is the ratio of y to x And so as you would imagine, this is not an infinite sum We're not comparing anything any tolerance. And so first off, there is no tolerance being passed in. We're only passing in y and x We're not passing any tolerance in this expression. Whereas you know for the regular arc tangent, we did pass in a A yx and a tolerance but uh Yeah, so it's simply a matter of evaluating this expression to give us arc tangent and so It's not As elegant as Taylor series, but it is way way faster and I'll show you how that looks in a second So that's the code Very quick cursory look Now let's take a look at the actual calling examples and so in every Example there is a shell script that we can execute to run the code as well as the code itself. Let me Let me run the code first and we'll see how it what it does So here you can see It's computed and printed out sign of Pi and cosine of pi and you can see It gives values that you would expect the answers to be Now what's going on? How is that working? So how does that look? In all these Uh examples, we have to include obviously the relevant sine and cosine and tangent arc tangent expressions You know trig functions and whatever we need to print out the results to the screen and so you can see all we're doing here is we are For the sine function passing in a number x passing in a tolerance and calling that function and printing it out Similarly, we do the same thing for cosine passing in x passing a tolerance calling cosine printing it out and here are the values and we could quickly change um, you know The number here to another number if you wanted, uh get another answer What's what is sine of 1.14 and cosine here are the answers Before I forget let me change that back otherwise that will be committed Um Example b now this is the same thing just Plotted and so I already covered in a previous video how we're plotting stuff. Let me quickly just run through it again here So we use a lot of stuff for using the heap or using Parameters, this is also from previous videos. Um, we have some things for plotting scatter plots and things um, you can produce the code if you'd like Let me run that code and this code generates the plot that you saw before Oops, that's my face Uh, so yeah sine and cosine here's the plot Done that's that example example c this was tangent and so the tangent example is Same as sine and cosine all we're doing is we are including the tangent Trigametric function Calling that function printing out the answer and so If you look at tangent of point five, it comes out to be supposedly point five four five four five. Is that true? Let's check octave really quick Tangent of point five It is it's not how you quit So that's how that works. Come on Example d This is just plotting tangent. You run this code. You will get the next plot of our Examples, this is the tangent plot actually, you can see Tangent is supposed to go to infinity, right up positive infinity negative infinity, right? That's how it works. So pi over two it goes to I believe that's power two it goes to positive infinity. You can see it based off my sampling. I didn't quite get to infinity. I only got to 16 or something But yeah, if we added more points to this you would this would spike off the chart So yeah, that's how that looks and the last example. I wanted to show oops was example e Now Let me first run that really quick so you can see how it looks This has both the Taylor series approximation for arc tangent as well as the polynomial one and you can see it off the beginning um, it's way faster, so The arc tangent approximation was 81 million cycles for one million arc tangents whereas The faster polynomial version was only 18 million. So it was like four plus times faster to use a polynomial Estimate versus the actual Taylor series Um, but you get the same answers, right? So or close So if you think in arc tangent of point one and point three Using Taylor series you get point three two one seven with the faster version. You're getting point three two one one So not quite exactly the same number, but pretty darn close Um For these special cases you do get the same answer because these are hard coded So obviously arc tangent of zero zero is not a number Similarly the faster polynomial version also has that same precaution built in but yeah for You know all the special cases you can see here We are getting the same answer a positive pi over two negative pi over two zero pi All that stuff we are getting the same answer from one to the other so That's it. That's the entire episode. I hope you guys enjoyed and maybe you became more familiar with how these types of things are evaluated on the computer and how you could write your own Approximations for these if you're not into that just take the code off the suppository. It's all yours public domain Um with that. Thanks for watching. I'll see you in the next video