 Now we've taken pencil and paper and we understand the imaginary unit, complex numbers and complex arithmetic. Now in this video, we're going to look at how to use Python for complex numbers. And we're gonna do that in three steps. We're going to look at the built-in functionality in Python. Then we're going to have a look at the CMath module. And finally, most importantly, we're gonna look at symbolic Python, the Sympi package. So I've already opened up a brand new notebook, a Colab notebook inside of a folder on my Google Drive. Now I've pre-populated this notebook just so that you don't have to watch me type. Now we can see the three sections there, the imaginary unit, IJ and complex arithmetic in Python, the CMath module and complex arithmetic using Sympi. Now I'll make this notebook available so that you can see how I did my type setting. For instance, there we see a title, if I double click on that. And then you can see a single hashtag and a space and that denotes the largest font size and that's very good for a title. Now I'm going to connect to the server. So I'll hit the connect button. I get my little green check mark. So an instance of Python is running on Google servers and that is served up to me here in my browser. So first up, we're going to look at the imaginary unit I and in Python, that is a one and a J, no space in between. So this is built-in functionality inside of Python. We don't have to install anything extra, no extra modules and extra packages. It's just built inside of Python. And there we have it. If I want the imaginary unit I, in Python I would simply type one J. And now I'm going to hold down shift and I'm going to hit return or enter and that line of code is going to be executed. And we see the result right there, one J. Now J is often used in engineering to denote the imaginary unit because I is usually used for current in physics and engineering. So don't get confused between the two whether it's I or J, it doesn't really matter. Now next up, I want to show that I squared equals negative one and that's how we define the imaginary unit. It is a number such that when we square it we get negative one. And so what I'm going to type is one J. There we go, that's code. And then a space and then the star symbol which means multiplying Python, another space and then one J. Now you don't need those spaces but it just looks a little neater. Now I'm going to hold down shift and hit return. What you can also do is just hit that little play button. We see the result minus one plus zero J. Now zero times anything is just zero so we can just ignore that bit at the end and we see negative one. That is the result minus one. I squared or then one J squared. Well that's just equal to negative one just as we would expect. Now please review the videos where I take pencil and paper to show you how these things fit together. In the Python version of these videos let me show you how I constructed this. So you see right there show that I squared equals negative one if I double click on that. This is how I constructed that sentence. First of all this is a text cell and I just typing show that and then inside of these set of dollar symbols I use LaTeX. I to the power two that two goes inside of braces equals negative one. We can see on the right hand side what that's going to look like and I'll just hold down shift and hit return or enter and that line of text is executed. So inside of those dollar symbols as I mentioned that's LaTeX. That's a specific kind of mathematical type setting that you can use inside of a notebook but you can also use that in various other places. Very nice for mathematical type setting. Now if I hover just between any two cells right in the middle I see I can create a new code cell or new text cell and that's what I do. Just as the document grows I'll just add code cell or a text cell, code cell or a text cell and I will remember that I can always come and just in between anything that exists before I can just add a new text cell or new code cell and so I like to keep my documents neat and tidy such that you can see here. If I go back up you see my largest font size there. That was a single hashtag. When I come down here that's my first section. If I double click on that, of course that was a text cell and I've got the two hashtag symbols in a space and that denotes the second largest font size. And by the way, do you see these funny tick marks in front of the one and J? If you do that inside of a text cell that's going to print out a format that looks like code inside of a text cell. So let's execute that by holding down shift and hitting enter return and you can see I'm using a dark theme. So we see the one J here is with a dark background and that just, you know, again it's just a little bit of type setting just makes it look nice and neat to me. Now that we've spent some time just going thinking about how to construct a nice notebook with code cells and text cells, let's carry on with the mathematics. That's what we're here for. So our next question is to use Python to show the complex number three plus four I and there you can see code. Remember what you can do just hover in between click on code and a new code cell will open for you. You can of course just go to the right and delete that code cell. So I created a new code cell and I typed three plus four times one J. We already know one J is just I so that's three plus four times I. Now mathematics we can just put four I and we know there's a multiplication symbol between the four and the I but in Python we have to put that multiplication symbol and as I mentioned the multiplication symbol inside of Python is a star symbol that's shifting eight on my keyboard. So this is gonna be three plus four times I J. Let's execute this. I'll just click on the little play button and there we see the result. Three plus four J. Now that J, remember that's just I so this says three plus four I exactly as I want. Now there is an alternative way to do this. Much of the functionality in Python is locked up in functions. Now functions are key words. They are reserved words for the language and so when I type complex as you can see there Python knows what to do. It's a key word. It can act upon that word and do something. Now with functions you'll see an open parenthesis and a closing parenthesis directly after a function name. There's no spaces between that X and the opening parenthesis. What goes inside of these set of parentheses are the arguments. That's the information that you give to the function so that it can do its job. So complex in this instance I'm parsing two arguments to it. Arguments are always separated by a comma so you see three comma four and that's what we're trying to do here. The complex function creates a complex number and we are parsing the real and the imaginary parts of that complex number. Hence the three comma four. Now just on top of that you'll see another little hashtag symbol. A space and then alternative. Now that hashtag symbol is not the same hashtag symbol as that we see in a text cell. When you see a hashtag symbol inside of a code cell that is an instruction to Python to ignore anything on that line that follows that hashtag symbol. This is what we call a code comment. This is where you can leave comments to yourself or to others that read your code. Python is just going to ignore that whole line. Anything that follows the hashtag symbol on any given line of code is just ignored. So I've written an alternative there. I just want to remind myself that there's an alternative way to create a complex number in Python. So let's execute this. Instead of the play button I'm going to hold down shift and hit return or enter and I see the exact same result. It's three plus four i or in Python speak that would be three plus four j. Show that the real number four is the complex number four plus zero i. And so zero times anything even i is the zero. So this is just four and here we can see really that the real numbers are a subset of the complex numbers. All real numbers are actually complex numbers. They just have a complex part or an imaginary part that is zero. So four plus zero times i j. If I execute that shift and enter shift and return I see four plus zero j and that really is just four. Just to remind you I can also use the complex function to create a complex number. I'm just passing the real and the imaginary part as two arguments. Arguments go inside of the parentheses. Parentheses directly follow the keyword that is a reserved word in Python. So that's Python language and it's going to give me back the complex number four plus zero i and that's exactly what we see. Now let's do some arithmetic. We need to add three plus four i and two plus four i. Now what I like to do is just to take each one of my complex numbers and put them inside of a set of parentheses. So this would almost be like mathematical parentheses. I just want those parentheses around these so that I know which parts because remember I have addition and multiplication going on. I just want to combine them all into this one object, this one complex number. And then I'm using addition and then the second complex number. So that'll be three plus four times one j plus two plus four times one j. And let's execute that and we see the two real parts are going to be added. So that's the three and the two. That gives me the five and the two imaginary parts, the four and the four, four plus four is eight. And so I see eight i on Python speak eight j. The alternative again is just to use the complex function. So complex, there's three, that's my real part for my imaginary part plus complex two, the real part of my second complex number and for the imaginary part. And if I execute this I'm going to get exactly the same result. Now of course I can also do subtraction and we can see there three plus four i minus two plus two i. So I'm going to get three minus two for the real parts. That's just going to be one and then four minus two that is for the imaginary part. So that's going to be plus two i. Let's execute that and see if that's indeed what we get. Yes, we do. That's one plus two i or in Python speak one plus two j. Now of course we can take a scalar and we can multiply that with a complex number. That's just going to give us distribution. So three times two is six and three times three is nine. So that's going to be six plus nine i. And here we have, there's our code over there three times. And now I really have to use that set of parentheses because I want to show that we are going to distribute this scalar three over what is an essence and expression. I have a two and I have a three i and I'm adding those two terms. And so let's execute this and we get six plus nine j as we expected. Now we can also distribute a complex number such as three i. I'm going to distribute that over two plus three i or indeed this is three i times two i plus three i. And this is what I have. Now how would I do this? Now this part here is my three i. That's three times one j and I'm going to multiply that by, and again I put this all in a set of parentheses, two plus three times one j. Let's execute that and three times two is six and that has an i there. Three i times two, that's six i. That's what we can see at the end there, six j. Now three times three is nine but i times i is negative one and that leaves me with a negative nine. So we see negative nine plus six i. Now we can also multiply two complex numbers as you can see there, three plus two i times one minus three i. I'm sure you are used to the code now. Let's execute that and we get nine minus seven j. So you know how to do this with pencil and paper. Please watch those videos in case you can't remember how to do this multiplication. You don't have to really do it with pencil and paper. Of course you just come here in Python and you just do it very, very simple. Now let's use the CMath module. Now that's a module built into Python but you have to import it into the active session. Now you can see the URL there if you wanna read more on the documentation for the CMath or complex math module and the way that I'm going to import it here is just use the full name. So import space CMath. Let's run that. And now everything that's inside of CMath is now being imported into this active session and I can use everything that is inside of CMath in this notebook. So first of we ask to calculate the square root of negative one. Now there is an SQRT function that lives inside of CMath. It does not live inside of base Python so I can't just use it SQRT like I used the function complex. I have to tell Python where to find this function but default it's going to look in base Python. Now base Python has many, many functions but certainly the SQRT function is not inside of base Python. So the way that we do that in Python is we're gonna give the name of our module CMath and then a dot, you see there's no spaces here and then SQRT. We know that it's a function because we see a set of opening and closing parenthesis. And again, we're passing in single argument and the argument is negative one. So this is the square root, the function square root, it knows what to do. You just have to give it something to do its job with and what you give it is the argument and the argument is negative one. So we want the square root of negative one. We just have to remember that this function SQRT lives inside the CMath module and it's because I just did a simple import. I just said import space CMath that I have to use the word CMath dot square root. So let's execute that and we get the result which is I, of course in Python speak that would be one j. Here's an alternative way to do it. I'm gonna say CMath dot SQRT. So that's gonna do the square root of a complex number for me and instead of saying negative one, I'm using the complex function. Now look at that. I'm using a function as an argument to another function. Please note that CMath dot SQRT is a function and you see the yellow open close parentheses and then everything I've highlighted that's the argument for the CMath dot square root function and that argument is actually another function. So there's the complex function. It has its own set of opening closing parentheses, negative one for the real part and zero for the imaginary part. So that is just the number negative one and so I'm gonna pass that to the square root function and the CMath module and again I get the exact same result because negative one and complex negative one comma zero that's just negative one. Now I want to print the complex number to the screen that's one plus square root of three times I. Now remember my real part is one, my imaginary part is square root of three. It's still a real number. The real part and the imaginary part of a complex number they are both real numbers and square root of three is a real number. Now one way I can do it is using SQRT. Now the SQRT function inside of the CMath module can also do proper square roots. Square roots are non-negative numbers so zero and above any non-negative real number. So if I say CMath dot SQRT three that's just gonna calculate the square root of three. So look now how I'm constructing my complex number. I'm saying one plus and then the square root of three using the SQRT function from the CMath module so that I can calculate the square root of three. That is my imaginary part times one j which remember it's just my imaginary unit. Let's execute that and now you can see there is a numerical approximation for the square root of three. 1.732058 so that's not an exact value. The exact value is written right there in LaTeX. Let me just double click on there. See there's LaTeX notation, mathematical type setting. This is not code, well it is LaTeX code but it's inside of a text cell. So there's no execution happening here. It's just rendered to the screen. So the square root of three, that's the exact number. This is a numerical approximation for the square root of three. So that leaves me with 1 plus 1.73 many decimal places i. Now as an alternative let's use the complex function. Now I actually prefer to use the complex function. So there we go complex. There's my first argument. It is one, that's my real part and then my imaginary part is c math dot square root of three. And so if we print that out to the screen we're gonna get exactly the same result. We see our numerical approximation for the square root of three times i which in Python speakers are j. So let's calculate the argument of phase. Now I'm just gonna call it phi or phi that's up to you of one plus the square root of three i. So the argument or this instance we're really talking about the principal argument or the phase then remember that's the angle on the argn plane remember that's the real axis and the imaginary axis on that argn plane. So we've got one on the real axis and square root of three in the imaginary axis. If I were to draw that number one plus square root of three i as a vector that's the angle that we get from the positive real axis towards that vector. And so let's see how to do that. Now again in the c math module there is a function called phase. Now it does not live in base Python so I can't just use it like I use complex. I have to tell Python where did I find this function while it's inside of the c math module. So c math dot phase and I'm passing one single argument to the phase function. I know it's a function because I see open close parentheses no space between that e and the opening parentheses that's a function and I'm passing an argument to it. And what I'm passing as an argument to it is actually my complex number. One plus the square root of three times i. So let's execute that and we see a numerical approximation for this value. That's actually pi over three and we can see 1.04719 et cetera just a numerical approximation. We can also calculate the norm or the length. So the length of that vector as a vector representation of a complex number on the argn plane or then just the norm of the complex number. We're gonna call that lowercase r and again it's of one plus square root of three i and now there's a function abs for absolute. Now that abs function as you can see there it lives inside of base Python. It is a recognized keyword in Python so I can just use it as is abs open close parentheses that is a function in base Python. And what I'm going to pass this time instead of the usual way of creating a complex number I'm just gonna use the complex function. So that's this one comma c math dot square root three. So the real part one imagine part square root of three I'm passing all of this once again I'm passing a function and it's arguments as an argument to a function abs function. So let's execute that and we see the result is two. So if we think of Pythagoras that will just be the length of the hypotenuse thinking of the complex number on the argn plane. Now there's another function in the c math module that's just polar, P-O-L-A-R polar. And now look as argument I'm passing my complex number and I represent my complex number using the complex function. Now what the polar function is going to do it's going to return two things to me it's going to return both the norm and their principal argument and that's what we asked to do. So r and phi and then we see the norm was two and there we see that's a numerical approximation of pi over three that's the argument, the angle between the positive real axis and the vector representation of my complex number. Now if I have those two values now here I have r that's my norm and my principal argument I can get back to that original complex number and there's another function called rect R-E-C-T and that stands for rectangular. You can see there it lives inside of c math. So it's c math dot rect now I'm passing the norm and some numerical approximation for my argument my principal argument. If I hit play now I can see there is very close to what the complex number is. Now there's going to be some rounding errors. So I see 1.000000 and then there's many zeros and a two. Python's really trying to tell you this was one and then look at this 1.732, et cetera. Remember that was my square root of three. So this is really trying to tell me is one plus the square root of three i but we are going to get some rounding errors. That's just the way that a computer language is going to work. That's using the rect function where I pass the norm and the principal argument as the two arguments to the rect function. Now let's think of complex numbers in the four different quadrants of the argn plane. So I'm going to use phase, the phase function because I want to know the argument of this complex number negative one plus one i. Now remember negative one on the real axis, positive on the imaginary axis so that's going to be in the second quadrant. If we use the term quadrants in terms of the argn plane similar to what we would imagine the Cartesian plane to look like. So cmath.phase and now I'm using the complex function again to represent my complex number and that whole function and its arguments is passed as an argument to the phase function. So I'm asking what is the phase of this complex number negative one plus i? Let's execute that and we get 2.35 again a numerical approximation. It is positive though because between zero and pi so in the first and second quadrants we are going to get positive values for our argument. Now let's look at a complex number that'll be in the third quadrant. So that's negative one minus i. Again, I'm going to represent that number using the complex function and I'm passing all of that as an argument to the phase function. Let's execute that. And now we're going to get negative because we are going to now go clockwise from the positive real axis clockwise all the way around to the negative real axis and that's the angle that we're looking at. So negative 2.356 radians. And then in the fourth quadrant let's take a complex number one minus i and if I execute that again I'm going to get that negative angle for the radians. Now the next question there is print a numerical approximation of the number pi and inside of the CMATH module there's a couple of constants and one constant is pi and you can just see it's CMATH dot pi. Now there's no open and closed parentheses because pi is not a function it's just a symbol. So let's execute that and we see again a numerical approximation for the irrational number pi. Now for the last question in the section we asked to calculate e to the power i pi. So we're using some Euler notation there. E, Euler's number to the power i pi. Now how would I do that? Well there is an exp function also inside of CMATH. So CMATH dot exp and I'm passing, there we go I am passing one j, that's the i then times CMATH dot pi. I'm passing that as argument to the exp or exponent function and look at what we get. Negative one. Now we do know e to the power i pi should be negative one. So there's my negative one. Now look at the imaginary part. That is 1.22 et cetera times 10 to the power negative 16. That's this mathematical notation that e minus 16, that means times 10 to the power negative 16. That e there has nothing to do with Euler's number it's just notation four times 10 to the power negative 16. And so what Python is trying to tell you while there's some rounding errors in that is actually just zero. So negative one plus zero i let's just negative one. And indeed we know e to the power i pi should be negative one. Now that we know how to use Python and the CMATH module to do a bit of complex arithmetic let's step it up. We're gonna do some symbolic mathematics and that is what we year for. So complex arithmetic using SMPI that's short for symbolic Python that's the name of a package. Now lots of people have worked on and continue to work on this package. It just is full and full and full of all sorts of symbolic mathematics. That's the mathematics that looks like your textbook. And that's really what we want. Now I'm going to import from SMPI a couple of the functions. Now remember I said import CMATH and then I always had to say CMATH dot then the function. Now here's another way to do an import. So instead of just importing SMPI saying import SMPI and then I'll have to say SMPI dot symbols SMPI dot collect all those functions I'm just going to list all these functions inside of this package that I want to use in this notebook. Now there are many many many many many many functions and other functionalities inside of the SMPI package but this is a very neat way to do things. I'm only importing what I need. And by doing so I also don't have to say SMPI dot symbols SMPI dot collect. I can simply use these names, these reserved keywords remember those other functions I can just use them as if they are part of base Python. It's a very neat way to do your import. So let's import all of that. Now some of these are functions and some of these are just symbols. There's PI for PI look right there. Now this first one in it underscore printing it is a function. So there we go. It's got open and close parentheses. Let's execute that in a code cell. Now what that is going to do it's going to allow this notebook to print out the results of my code so that it looks like a mathematical textbook. It's going to do mathematical type setting in the results and that is very unique here to Python. It is fantastic that you can use Python to look like the mathematics in your textbook and that's what's going to allow this notebook to print that mathematical type setting. So the first thing we want to do here is to print imagine unit I using SMPI. Now I don't have to say SMPI dot and that's an uppercase I because I've already imported it as such. I didn't just say import SMPI I said from SMPI import all of these. Now I can just use it directly and let's execute that and look at that beautiful mathematical type setting that we see. See I now the real mathematical imagining unit not a J it's an I. Now we want to say calculate I squared and that's how we define the imagining unit it is a number such that when we square that number we get negative one. So I'm going to say I star star now remember double star symbols that means to the power I to the power two let's execute that and we get negative one very nicely done. Now here comes the beauty of SMPI I can take computer variables that's what we see on the left hand side then we see an equal symbol now that's not a mathematical equal symbol in computer languages the equal symbol is an assignment operator it looks at what is to its right and then it assigns it to what it's to its left. Now let's see what we're doing on the right we're using the symbols function now let's go up we imported the symbols function there we go it's right here symbols it's a function that lives inside of SMPI but the way that we imported it means we don't have to say SMPI dot symbols we can just use it directly. So symbols open close parentheses it is a function it lives inside of the SMPI package and now here comes two arguments here's my first argument comma there's my second argument now my first argument I see open and close single quotation marks now you can use double or single in Python it really doesn't matter so there's my quotation marks and now I'm putting A space B, space C, space D, space K now I put spaces in between but I could also have used commas it just looks better with the spaces so A, B, C, D and K what I'm trying to create here is mathematical symbols the symbol A, the symbol B, the symbol C the symbol D and the symbol K like you see it in your textbook and so I have to put that inside of a set of quotation marks now comma there's our second argument now this is a very special kind of argument all the arguments that you've seen up till now are just positional arguments this function it's going to expect that you pass certain arguments to it in a certain order that means positional arguments but there's some arguments that have name and you can really put them anywhere they don't need to be in a certain order but you have to use the name of that argument and so this argument's name is real and I'm assigning that a value remember equals is assignment I'm assigning that the value true so I want these symbols A, B, C, D and K to be real numbers now look at that code there isn't that beautiful you can almost read it like just an English sentence now it's going to create these mathematical symbols for me they're all going to be real numbers now we are assigning that so remember what is to its right and that's all those symbols that we've just created now I'm going to assign that to computer variables now a computer variable it's just a little space in your computer's memory and you put something in that little space now I've got these symbols mathematical symbols or variables A, B, C, D, K those are let's call them mathematical variables now I've got to put them inside of a little bit of space in my computer memory now to do that we give that little space a name that name is under your control now that's within reason there's all sorts of stuff you cannot use as a name you can't use any of these keywords that is reserved for the Python language no, no, no, you can't do that so you have to come up with your own now this is very simple why don't we just use the same letter for our computer variable name the name of our little space in memory same name as the symbol so I'm going to say A, B, C, D, K now you have to put commas on the left hand side on the right hand side of the Simon operator I'm creating one, two, three, four, five items so I better have on the left side five items as well and another thing about Python which is very beautiful I could do these one by one I could say A equals symbols, A real equals true next line, B equals symbols, B real equals true but in Python I can do that all in one go so I can do multiple assignments here on the left hand side and after a while you'll get very used to this let's execute that now I have a bunch of mathematical symbols they are all real numbers and they are assigned to these computer variables that have got a similar letter name that's a little space in memory and so the A, little space in memory it contains the mathematical symbol A and this B, the computer variable B contains inside of memory that's the name of that little piece of memory it contains a mathematical symbol called B that holds a real number so let's print this complex number to the screen A plus BI now that looks a lot more like your mathematical textbook so I'm just going to say A plus B times I now if you try to do this in any other computer language it's going to give you an error because it does not know that A is a mathematical symbol it wants it to be some number or some other object such as a whole string something like that but here in symbolic Python it can actually hold a mathematical symbol so let's print this to the screen this is code and look at that beautiful mathematical type setting now that comes from the init underscore printing function remember we called that before and look at that A plus now what somebody's going to do it always puts this I the imagine unit before the imaginary part B so A plus IB instead of A plus BI now that multiplication commutes so that's not really a problem for us let's do another one C plus DI there I have C plus D times I now again remember you have to in the code use this multiplication symbol you can't get away from that you can't just say C plus DI that's going to be an error so let's execute that and then we see C plus ID now when we imported from Sympi one of the functions that we imported was the RE function and we also imported the IM function now the RE function that's going to calculate the real part of a complex number and the IM function it's going to return the imaginary part of a complex number so let's do that I'm saying RE I can use it directly because of the way that I imported this function from the Sympi package it is a function so there's open close parentheses and I'm just going to pass A plus B times I my complex number so let's pass that as argument to the RE function and it returns for us just the A look at that beautiful mathematical type setting for the A the IM is going to return the imaginary part so let's run that code and I get the B back that's the imaginary part of that complex number now let's multiply K this constant K or the scalar K times this complex number A plus BI now that's very easy to do I'm going to say K times there's my times it's the star symbol and then I'm going to put inside of a set of parentheses just to denote it's always a safe thing to do that this is my whole one object this is my complex number A plus B times I now I'm going to multiply that and look at what I get it was actually not performed the mathematical operation was not performed I just see a beautiful type setting K times A plus I B now how do I get Sympi to actually execute that and here we go now remember if I have a code cell and I have a hashtag symbol that means that's a code comment everything in that line after that symbol Python's just going to know that so you can use that to leave a little message to yourself or to someone else who reads your code now you're going to see something special let's look at this code and take it apart look at this first section that I'm highlighting inside of a set of parentheses I am passing that scalar times complex number multiplication K times and then in another set of parentheses A plus B times I now I've enclosed all of that in a set of parentheses so that becomes one unit then follows a dot and then follows a function the expand function how do I know it's a function while it has open close parentheses but hang on a minute have we not been using Python in a way that we use the function name first and then inside of the parentheses we pass the argument yes indeed that's how we use Python but a very special thing inside of Python is this ability to use a function after an object has already been created now everything we create in Python strictly speaking is an object so what we've done here look at this code cell here K times A plus BI that created an object that object was printed to the screen that's what we can see there now there's my object once I've created an object I can actually use this reverse notation I create the object first then I put a full stop so that dot and then the function now what Python understands here is apply this function to this object that already exists it is as if I'm taking this object and passing it as an argument to that function but we do this in this reverse order and that's a very, very useful thing because we just work on stuff we create stuff and then we think what to do with it then we want to apply a function to it and when we do it this way we can refer to this function as a method it is one of the functions that is very specific to this object this object if I create this object the scalar times a complex number that object can be passed to many, many functions it's restricted though to those functions and one of them is the expand function and what we do is then we use it in the form of a method so I create my object first now it's got to be in its own whole set of parentheses so that it is one single unit one single object then the dot then the expand method which remembers now it's just a function so let's do this and now you can see the distribution has taken place k times a is ak we have commutativity and k times ib is gonna be i times bk again, commutativity and that is fantastic so not only can I use it just to print out to the screen the actual math that I want like in your textbook I can actually execute that mathematics in the way that we're gonna do it in some pi as we create the object and then we use a method after creating that object and remember that method is simply a function and this is the expand function so let's add two complex numbers a plus bi and we add to that the other complex number c plus di and what I like to do once again here is just put each one of them in their own set of parentheses it's just a safe thing to do so I've got a plus b times i plus an instead of parentheses c plus d times i let's look at that and now we can see a plus ib plus c plus id now completely correct no problem there but we want to neaten things up a bit now there is a function inside of some pi it's called collect now when I said from some pi import one of the functions that I imported was the collect function and so I can just use it straight away and so there in yellow that's it's open and closing parentheses now this first argument that I'm passing that's just the addition of these two complex numbers as you can see there as we had in the line of code before comma the second argument of this collect function is what do I want to collect upon well I want to collect upon i the imaginary unit so let's execute this code so you can see what happens now look what it's done I have an ib plus an id well I can take the i out as a common factor there's the i as a common factor and now I have b plus d that addition of b and d that is now as far as my solution is concerned that is now the imaginary part and then a plus c there well that's just the real part of my solution now I want to just go back a little bit just so that we're sure about something now this collect I did from Sympy import and then I had a bunch of functions and constants with commas in between that I imported from the Sympy package now this expand you would not have found expand when I did that import why didn't I have to do that because I'm using it in the form of a method and once it's a method Sympy and Python is very clever it knows what type of object this is it knows it's a complex number and it knows that expand is a function that I can use in terms of this object so I did not have to import expand at all these methods they come just as a free gift for all these objects that we create collect though I'm using this as a function and so I had to import that function great now as far as this last addition is concerned our question is just calculate the real and imaginary parts of the last addition now there's my re function and so open to those parentheses there in yellow and we can see we're just passing a plus b times i and add to that c plus d times i I'm passing all of that as an argument I'm using a little code comment there for myself and we can see a plus c that was the real part and let's look at the imaginary part that's the im function exactly the same thing and there's the b plus d in the solution that's going to be our complex part so let's do calculate a plus bi times c plus di I'm multiplying now once again I'm going to put these inside of parentheses keep things neat that is one single complex number then times multiplication that's the star symbol and then my second complex number so let's do this now look at that it's just going to do the type setting to the screen it has not executed that it has not done the mathematics so now how do I get it to do the mathematics I have to do a times c then a times id then ib times c then ib times id remember how to distribute now look at this wonderful way that we can do we're going to use another method so there we go I've got yellow parentheses to create one single object inside of that object I am multiplying my two complex numbers a plus bi and c plus di outside of that set of yellow parentheses creating that single object which is going to be another complex number if I multiply a complex number by complex number I'm going to get a complex number unless I multiply a complex number by its complex conjugate that's a different story and there's my dot or full stop and then I'm using the expand method I know it's a method because it is a function I know it's a function open close parentheses but it's then a method because it's being applied after the object has been created so let's do that and now we can see that multiplication has taken place now once again I can collect everything on that i I can take out i as a common factor and so now admittedly it becomes a bit long but let's just take this apart there's my collect function so inside of my set of yellow parentheses there's my whole two arguments I've got one argument, the second argument remember the second argument is this common factor that I want to take out that's the i my first argument is just the multiplication that we've just done the part that I'm highlighting now is just taken copy and paste from the previous line of code so I want to take that multiplication that's just taken place because of the expand method and I want to take out i as a common factor and now we can see that's beautiful now for the real part I have AC minus BD and for the imaginary part I have AD plus BC exactly as I would expect now we are asked to calculate the complex conjugate of the complex number A plus BI that's how we would write it remember pencil and paper we put this little bar on top A plus BI remember what we do there the complex conjugate that takes this imaginary part B and it multiplies it by negative one and that's where we have negative B that's just A minus BI so we have a function conjugate now if you look back up when I set from SIMPI import conjugate was one of the functions that I wanted to import it is a function because I see open close parentheses I'm passing my complex number A plus B times I and let's take the complex conjugate of that and lo and behold I see A minus BI or A minus IB exactly as I expect now I can also calculate the norm remember if I represent it as a vector there will be the length of that vector on the argn plane but before I had to use it just with numbers and I saw the numerical approximation for those numbers now I can see the symbolic mathematics happening the function that I'm going to use is the ABS that's an uppercase A ABS and again that was one of those functions that I did the import on I set from SIMPI import ABS now I can use ABS all on its own it is a function because we see open and close parentheses and again I'm passing A plus B times I let's pass that to the ABS function and it's exactly as we expect and look at that beautiful mathematical type setting because of the init underscore printing function I see the square root of the square of the real part plus the square of the imaginary part isn't that fantastic next up we need to show that the square of the norm now remember that gives me a square root and if I square square root I'm just getting rid of the square root so show that the square root of the norm of the complex number A plus BI is A plus BI times its complex conjugate so let's do that there I have a set of yellow parentheses so I'm creating one object I'm taking A plus BI I'm multiplying that by its complex conjugate A minus BI and then I'm using the expand method because I want the actual math to take place so let's look at that and indeed I get back the A squared plus B squared as I expect that's the norm squared now let's calculate some arguments remember if I do a vector on the argn plane that's the angle between the positive real axis and counterclockwise on this interval from zero to pi radians that's counterclockwise that's a positive angle but once I get to quadrant number three and four I'm gonna go clockwise going from positive real axis clockwise and I'm gonna express that as negative radians so let's look at one plus one i I can just write that as one plus i I want to look at the principal argument of that and indeed look at this now this is fantastic I do not get back in symbolic Python I do not get back in numerical approximation I get back the exact solution pi over four a quarter pi now that's something very very special now if I want the numerical approximation look at what I'm doing now look at my set of yellow parentheses I'm creating a single object take the arg function and remember I didn't speak about that arg again that was imported directly from Sympi one of those functions that I imported was arg so from Sympi import arg arg that's a lowercase a that's gonna calculate the principal argument for me so I'm creating all of that there's arg open close parentheses one plus i I'm putting all of that in a set of parentheses so that I can use another method now this method is the eval f method evaluate method I'm passing a single argument before we've never passed an argument to a method I'm passing the argument 10 to it now what it's going to do it's gonna give me a numerical approximation let's have a look at that so instead of getting pi over four I get a numerical approximation of pi over four so let's calculate the argument of negative one plus i so that's gonna be in the second quadrant of the argn plane if I do that I get three quarters pi or three pi over four again that's going counterclockwise from the positive real axis so let's do a complex number in the third quadrant negative one minus i let's see what we get well we're gonna get negative three quarters pi three quarters times pi three pi over four again that's gotta be negative because it's going clockwise it's outside of that interval from zero to pi we're now beyond pi going all the way around to two pi there we measure negative angles going clockwise from the positive real axis let's do the fourth quadrant we're going to get one minus i and we get negative pi over four that is absolutely fantastic now remember I can wrap all of that inside of a set of parentheses and then use dot eval f and I can pass an argument to that method just to show you in case I want to use actual constants yeah I have I want the principal argument of three plus four i so three plus four times i I'm using the arg function in some pi and look at what I get I get the arc tan of four over three now that is exactly what I want that is an exact value the arc tan of four over three the four comes from the think of Pythagoras that is going to come from the imaginary part that's the four and the three comes from the real part think of a triangle, a right triangle now I can do the numerical approximation for this to ten significant digits so let's do eval f10 let me just show you remember I'm using those yellow parentheses as you can see there now the color it's not me choosing the color that's just the way that this notebook is rendered with a dark theme and I can see 0.927295218 that's nine decimal places that's ten significant digits great stuff the last thing now we've done it before and we got that numerical approximation let's use simpi e to the power i pi that should give me a negative one now this exp function I imported that I set from simpi import exp so I can just use it directly this is not the same exp function that we have inside of the c-math module this is exp inside of the simpi package so you just have to be very careful there let's execute this and now I get a beautiful value back there is no numerical approximation that is indeed negative one and that is absolutely fantastic so I hope you've enjoyed this introduction to using complex numbers and complex arithmetic inside of Python what I really want you to concentrate on is simpi we're going to use simpi throughout much of this course just to do our work but we will also get back to numerical approximation we cannot always use symbolic math then we're going to introduce another package and that is the numpy or numerical Python package