 In the previous videos, we explored the use of Python and symbolic Python. We got used to creating mathematical variables and assigning them to computer variables such that we can use Sympi for our algebra problems. Now we're going to do something much more useful. In fact, we're going to start with distributing. How can we distribute a term over an expression or an expression over expression? Distributing is just such a fundamental part of algebra and it is so easy to do with symbolic Python. And here we are in a brand new notebook. You'll see I've already pre-populated it because as I mentioned, we're going to be a bit more serious about using Sympi for our algebraic manipulation. So I've named my notebook Lecture 4, as you can see there, and I've already started right at the top. Let's double-click on that so you can just see that this is a text cell and I'm just using a single hashtag, which means this will be the largest font size and I've called it 4 with a little horizontal bar distributing. I'm just trying to make my title look a little bit better. I do have a subsection. It's called packages used in this notebook. And as you can see, we're doing exactly what we did in the previous lecture. We're going to import these functions and classes. And as you can see, we're going to do exactly what we did in the previous lecture. We're going to use these functions directly. So we're going to say from Sympi import so that we don't have to reference the name Sympi every time that we want to use these functions. Now to get things started, I'm going to click on the connect button and that'll start the runtime on Google servers. Great, so let's run this first code cell. Now the first function that I'm going to use is the init underscore printing function. Remember that's going to print out very nice mathematical notation when I execute code. And so let's get stuck in using Sympi for distributing. And the first thing that we see is expand the following expressions. Now we do notice these are expressions. Look at it, we do have certain terms and they are separated by addition and subtraction. So I've already written these out. Let's double click inside of them. I just want to show you what the code looks like. It is a text cell and you can see the code here. This is LaTeX or LaTeX. There are many pronunciations LaTeX. That is mathematical notation. You see it starts and ends with a dollar symbol and that tells the notebook that it must use LaTeX to represent the mathematical notation. So you can't do any math with it. That is just me using mathematical type setting to print to the screen. We've actually got to do the algebra. So I'm going to create a new code cell and I see I have to use the mathematical symbol a. So I'm going to say a equals symbols because I can just use that function straight away because I've already imported that function. Look at it right up there. So I can use it directly. It is a function, so open, close parentheses and I have to pass the symbol as a string. I'm just going to say a and let's create a new line and I'm just going to say a to print that a to the screen. Let's have a look and there's a beautiful mathematical type-setted version of a. Now we can actually do some mathematics with a. So let's create a new, a brand new code cell. There we go. Now let's try and recreate this mathematical expression that we have up there. There's a times and I'm going to open parentheses. It looks like a to the power of four, which is double star symbols. Remember, that's how we do powers in Python. Then I'm going to say plus two times and I have to put that star symbol four times. It is not like the algebra textbook where you can emit that multiplication sign and then a to the power two plus three and I'm closing my parentheses. Let's have a look at what happens and it actually prints it out that it looks almost exactly like this LaTeX mathematical type setting but this is the init underscore printing function that is allowing this result of the code to look like mathematical type setting. So that's fantastic, but it hasn't done any distributing for us yet. So let's do the following thing. Now I'm going to cheat a little bit. I'm going to copy all of the previous code, control or command C, copy that. And I actually wanted to open a new code cell. Now I'm going to put open close parentheses and inside of those parentheses, I'm going to paste the code that I've just copied. But now it lives inside of a set of parentheses. So it's one single object. Let me just execute this line of code. You'll see that nothing has changed. I've just put it inside of a set of parentheses because what I want to do is apply a method to this object. Remember what we see on the screen there, the result of the execution of our code is an object. An object is put on the screen for us. But I now want to pass that object to a function in the way that we do it. If we use the object first and then apply the function, we call that a method. Remember, I want to use the expand method on that object. Now, if I did not enclose the whole thing in a set of parentheses, this expand will only have applied to the last set of parentheses. So it would only have applied to this part. So I'm putting everything in one set of parentheses just to tell Python, hello, Python, this is one single object. Now please pass this to the expand function. But I'm doing it by creating the object first and then using the function. So I'm going to call that a method. So I'm applying this method to this object. And now let's see what happens now. And now you can see the distribution has happened. A times A to the power of four, the bases are the same. And so one plus four is five and I see A to the power of five. Now I'm distributing A to the next term, which is two A squared and one plus two is three. So I'm going to get two A cubed and then A times three is simply three A. And as we did in the pencil and paper lecture, you can see the result is right there in front of us. We don't have to worry about anything. So I think the use of some pie is great. You can check your work and you can see if you understand what is going on. So let's do the next one. It says A to the power of four. And I want to distribute that into the following expression. A to the power of three plus A to the power a third. So let's see if we can do that one. I'm going to create a new code cell. I can already use the mathematical symbol A because I've assigned this symbol A to the computer variable A. So whenever I call the computer variable A, it's going to print out the mathematical symbol. So we can really do all of this. So once again, let's just print it to the screen. That's going to be A to the power four times. Let's do open parentheses. I'm going to say A to the power three plus A to the power. Now I want that rational. I want a numerator and denominator in my power. So I'm going to do the following. I'm going to say A to the power and I'm going to use the rational. I'm going to create a rational and I want one in the numerator and I want three in the denominator. So let's see what happens. Let's put a three. Let's see what happens. And look at what SMPI has done. I see A to the power four and we see that we have a cube root and we do remember from previous lectures that that cube root of A is the same as A to the power third. That's fine. And then plus A to the power three. Now we do have the commutative property on addition. So it does not matter what order I have these two terms in. It's still exactly the same thing. But now we have not expanded this. We have not distributed this A to the power four into this expression. So let's see if we can do that. So let's create a brand new code cell. Now once again, I'm going to cheat a little bit because I want an object and then I want to expand that object. And so you can see my code day. I'm going to create this object which I want to expand it and the object that I want to expand it is all of this code that we've just already done. I'm going to select all of it, hit command C or control C to copy it. And then I'm going to go inside of that parentheses and I'm going to hold down command and hit V or control V and let's run this code. And look at that. Now we see that distribution has taken place. And so look at the result. We do indeed see that A to the power four times A to the power three, the bases are the same and four plus three is seven. So we see our term A to the power seven there. And now it's A to the power four times A to the power one over three. Now remember we don't multiply the four and the third. It is four plus a third, which is actually 13 over three. It's a little bit small there. Let's increase the size. So hopefully you can see that. You see there? Four plus a third is 13 over three. So we have this cube root of A to the power 13 actually plus A to the power seven. Let's have a look at the next example. Now we have negatives in our as far as our powers are concerned. And remember if I have A to the power negative three, that will be the same as one over A to the power three. Let's create a code cell. Now we do see a B in there. So we just have to set B as a symbol as well. So let's use the function symbols. And inside of quotation marks, I'm going to put B to make that a string. Let's just print B to the screen by calling B now, the computer variable B. And we see a lovely representation of the mathematical constant B. Let's create another code cell and let's execute this. So let's do the following. It's going to be five times. And I'm going to have A to the power. That's negative three. I'm putting the negative three inside of parentheses just to tell some pie. Look, I've got a power symbol and a negative symbol right next to each other. Those are separate things. It's A to the power negative three. I'm going to multiply that by B to the power negative two. So I'm doing the exact same thing times the following expression, which I'm going to put in parentheses as well. That's two times A times B to the power three. And I'm going to have minus three times, that's A to the power two, times B to the power two. And we see the result there. Now look what some pie has done. It's noticed that five A to the power negative three. And I said A to the power negative three is the same as one over A to the power three. So it's actually created that denominator for us. So just written this, I think in a clearer way than to use these negative powers. That was never our aim though. We want to expand this expression. So I'm going to say code. Now once again, I want to create some object and then I want to expand that object. So what do I want? Well, I want all of this. So again, I'm going to cheat a little bit by just selecting everything there. I'm going to hit control C or command C and then I'm just going to paste it right there. And so let's expand that expression. And look at that in the pencil and paper lecture. I do show you an example of this doing it the long way and look how beautiful that is. The expansion has taken place and there's been simplification of our expansion. That's absolutely fantastic. Now look at the following problem. We really have to concentrate here. So let's create a new code cell. First of all, I see X's and Y's. So I've got to create those as symbols. So X comma Y equals symbols and then inside of quotation marks X and Y. I just have to put a space in between those. I actually don't have to put a comma there inside of that string. And the nice thing about the symbols function is that if I have two values inside of my string, I can individually assign them separately to different computer variables X and Y. So I don't actually have to do these on separate lines where I would say X equals symbols X and Y equals symbols Y. No, I can do it all in one go. But look at our problem. It says both X and Y must be non-negative numbers. So there's a second argument and remember arguments of functions are separated by a comma. And then there's a keyword argument. Keyword meaning that argument has a name because you look at this argument right here, this string. It had no name. It's just the argument by itself but a keyword argument has a name. And this keyword arguments name is positive. Positive and I'm gonna set that equal to true. And remember true is an uppercase T. And now I've created these two symbols but somebody knows the values that the variables X and Y can take must be non-negative. They can be zero and any positive number. Now let's create this SQRT. I've already imported that square root function from the Sympy package. I can use it as is. And I'm gonna say X times Y and that's gonna be multiplied by some expression. That is again the square root of X times Y and I'm gonna add to that the square root of Y. Now let's take a look at what Sympy has done. It said if you take the square root of XY right here at the front, look at what it can do. You can write it as a product of the square root of X times the square root of Y. And we do this right now because both X and Y are non-negative numbers. They are positive. They can be zero but let's just ignore the zero case because that's not very interesting. Zero times anything is just zero and the square root of zero is zero. That's not an interesting thing. So let's just imagine that both of the values X and Y are both positive. And so you can break these apart into saying that's the square root of X times the square root of Y. Now inside of my set of parentheses here I again have the square root of XY and Python has broken that out into a product of two square roots. Square root of X times square root of Y plus then the square root of Y. Now let's see what happens when we distribute the square root of XY over our expression. So again I'm gonna copy all of that code because we copy and paste when we write code all the time. You know it saves a lot of time. So let's hit code and I'm going to create an object and then I'm going to expand. I'm gonna use the expand method and so inside of that parentheses let me just paste and let's see what SimPy has done. And now look at that. We see it's the square root of X times Y plus X times Y. And look at the result. It's exactly as if we would do it on pencil and paper because the square root of X times the square root of Y times the square root of X times the square root of Y we have that we have the commutative property or multiplication so that would be the same as the square root of X times the square root of X and that just leaves us with an X. Now these values are always positive so I can just have X. I don't have to think about positive and negative because I said that these values are always positive and the square root of Y times the square root of Y well that's just Y. And then I have the second term square root of X times square root of Y times the square root of Y once again I've got two square roots of Y that just leaves me with the Y and then the single square root of X. This only helped though because I knew that both X and Y were positive values. Now let's do a more complicated one. We see our example right here. Once again X and Y are non-negative. Now we said positive is true so in actual fact do remember that in this instance X and Y cannot be zero according to Python because it's only positive values. In mathematics we would say non-negative so we would actually include zero but as I said zero is not an interesting number to be playing with in this instance so we're actually only dealing with positive values. Now in this problem we again see that X and Y must be equal to a larger than zero. Once again already we have that X and Y are symbols and they are non-negative. Again I'm using positive equals true because the zero doesn't really matter in our instance. So let's do the code, let's do SQRT and that's gonna be X times Y to the power three and I'm going to multiply that by an expression so I'm putting that in parentheses that's the square root SQRT of X to the power five times Y and then I'm going to add to that the following. I'm going to add to that the SQRT the square root of X times Y to the power seven. Let's have a look at that. Now Sympy has noticed that the square root of X times Y cubed now they are both non-negative numbers or let's just call them positive so we can think of them as a product of square roots. So we're gonna have the square root of X times the square root of Y cubed and what it's done with a Y cube it's written it as Y to the power three over two and then multiply by and it's done exactly the same inside of our expression. The square root of X to the power five can be written as X to the power five over two times the square root of Y. So it's the product of these two variables under the square root individually because X and Y can only be positive and it's done that same with this last term as well. Now once again, I'm gonna copy all of this because I actually want to do the distributing. So let's create a new code cell open and close dot expand. What a wonderful method if you think about it and then I'm just gonna paste in my whole expression there and let's see what happens. Look at that beautiful result X cubed Y squared plus X times Y to the power five. Once again, watch the pencil and paper lecture you can see why all of this happens but we just see a beautiful solution right in front of us. Now let's look at this next problem. X equals negative one and let Y equals negative one and show the following that we cannot write the product of these two square roots X times the square root of Y that it's not equal to the square root of X times Y. We just said, well, if we have this product under the square root symbol we can just break them apart into this product of square roots but that only hold if we had non-negative values and in this instance, look, X is now negative one and Y equals negative one. So what we're actually asking, we wanna show that the square root of negative one times the square root of negative one is not equal to multiplying these terms under one square root because negative one times negative one is positive one. So this would be the square root of positive one but hang on a minute. We know we can't take the square root of negative numbers. So these individual square root of negative ones and square root of negative ones that's never going to work. That is not a real number. We cannot take the square root of a negative number. Now we can when we move up to the complex numbers because the square root of negative one is this imaginary unit we call that I. So let's see what happens. I'm gonna call the square root. Let's just first do negative one. So I'm gonna say negative one inside of parentheses times negative one. Now what's gonna happen is that negative one times negative one is positive one. So if we look at the result we're gonna get the solution one. Now remember it's actually positive and negative one because you're thinking of a square root. If I have a square root of a number the solution to that will be such a number that when I square it, I get back to that number. So our number's one and both one squared and negative one squared gives me one. But sometimes only gonna give you the positive value. Now let's do the following. Let's do this sqrt of negative one individually times the sqrt of negative one. Now look at this result. Now we see it's negative one. So certainly even if you think just of the positive result here that the square root of one is one and actual fact is positive negative one. If I take the square root of negative one times the square root of negative one it is no longer plus or minus one. It is only negative one because the square root of negative one is i and i times i is i squared and i squared is defined to be negative one. So clearly be very careful when you consider the values that your variables can actually take. In this instance we've shown if they are both negative we cannot have the situation where we have them together under the square root symbol and we break them apart as this product of square roots that will not hold. So be very careful when you look at these problems what are the possible values that your variable can take. Now let's look at this example now. I've got let x equals negative three and let y equals positive two. Now I have got one negative and one positive number. Now you can switch those around. This might have been positive three and that one negative two doesn't matter. And so let's have a look and show the following. If I now take x times y under the same square root symbol and I square that what do I have if I multiply negative three times two that gives me a negative six. So I'm actually asking about the square root of negative six. So let's have a look at that. Let's say code and I'm going to say the following. I want something squared and what do I want squared? Well, I want the square root of the following. I want negative three times two. And as we did in the pencil paper the answer is negative six. Now think about why this is so because negative three times two that's negative six. I want the square root of a negative number. I know that cannot happen. So what I'm actually thinking about is thinking about six. How do I get from six to negative six? Negative, it's negative one times six. And I can think of that square root of, I can think of the square root of a positive number six but the square root of a negative number is i. So I'm actually thinking about this number negative six. Now what value can I take such that when I square it, I get negative six. Remember square root, think about it. What am I asking if I'm asking about the square root of negative six? I'm asking what value can I square to give me negative six? And we know there's no number that I can square that gives me a negative number. So we have to bring in the imagining unit and that imagining unit is i and i is the square root of negative one. So if I think I take the square root of six times the square root of six, well, that's just gonna give me six but I do have to think about that negative. And i times i or i squared is negative one. And so I'm left with the negative six as my result. What I wanna demonstrate here is be very, very careful whether your variables x and y if you multiply them under a square root symbol or any higher root for that matter, be very careful whether these things are positive and negative. Fortunately for you, you can always come and verify your work in Python. If you use the symbols function and you just set positive to true or you can also set positive to false. Sympy will do the correct thing in those instances and you can always come and check your work. So let's do these couple of examples. We want to expand the following and now we have this expression times a second expression and we do know from the pencil and paper lecture how to do that. Let me show you how easy all of this is of course using some pie. So there's my code and I'm going to have this expression times a following expression. This one is going to be three times x plus four and this one's gonna be two times x minus one. Now I just wanna do the following. Let's create another code cell. I'm gonna say x comma y equals symbols. Now remember we can overwrite things and I'm gonna just have inside of quotation marks an x and a y and now I'm going to say the following. Real equals true. Now so I have another keyword argument. This argument is called real and I'm setting that to true. So now I'm not constraining my variables to be positive or negative. I'm saying they can be any real number. Now do just be concerned about something. If I execute this line of code and I were to go back up and re-execute one of these lines of code with the x's and y's in it, it is now going to think that x and y are real numbers. So it's the order of execution that matters, not the order from top to bottom of your notebook and that can create confusion. Once I've changed this, don't go back and execute something before that line of code because once you run it again, Python's not gonna be sure. Well it'll know where it stands at the moment but you won't be sure where Python stands at the moment. So there we go, let's execute this. Now x and y can be positive or negative numbers. Doesn't matter. Now let's have this printed to the screen and we can see that's exactly our problem. 2x minus one and 3x plus four. Once again we have the commutative property on multiplication. So Python puts it in order according to some algorithm. Now I want to expand this. So open close parentheses. I want to indeed expand this. Open close parentheses and let's do a copy and paste. I'm gonna select all of those, hit the copy. Now you see it stays highlighted. One way to get rid of that is just to click in that cell again. I'm gonna go down inside my parentheses. I'm gonna hit command or control V to paste and let's expand this. Oh and look at that. Is that not a thing of beauty? Look at that polynomial that we see there. 6x squared plus 5x minus four. All our hard work and remembering how to do distribution. Look at it, it's right there in front of you. Let's rapidly do the following. I'm going to take something and I'm going to square it. And so let's look at this. It's three times x plus four. We see three times x plus four. Lovely stuff. Let's copy all of this. I'm selecting it, command or control C. Then I'm just clicking in that cell again just to take that highlighting away. Another code cell. I want open close parentheses dot the expand method and let's plug in the expression right there. I'm gonna paste it right inside the parentheses. And again, look at my beautiful quadratic equation. 9x squared plus 24x plus 16. It's all done for you, isn't that fantastic? Let's do two more. Two more to go for this lecture. I'm going to have one expression times another expression. So let's go to the first one. It's gonna be two times x minus four and the next one is gonna be a polynomial x. Second order polynomial I should say because that first expression is also polynomial. It's just the first order polynomial. So x squared plus x minus one. Now you don't have to do this printed out to the screen every time but I like to do it because it's so easy to make a mistake. Just check your work. Check if this is actually the problem that you are dealing with. It's two x minus four and x squared plus x minus one. Everything is correct. I'm gonna copy all of that. Copy it, click inside the cell to deselect it. Code, open close parentheses dot expand and let's go inside of this set of parentheses. Let's paste our expressions in there and there we go. Once again, look at that. So fantastic, so easy, so quick. It's beautifully rendered as if it's your textbook and we see it's a third order polynomial. Here's our very last example. Let's do a plus b to the power three and so I'm going to have a plus b and do remember you have to think the last time that I used the symbols as far as A and B are concerned, did I set them to be positive? Did I set them to be real? Now by default, if you don't use the keyword argument, which remember sometimes we haven't, it is just real equals true. That's what happens behind the scenes. If you type nothing, that is the default setting. So I'm gonna have a plus b and I'm going to have that to the power three. Let's render that to the screen just to make sure. Indeed, that's what I want and so let's have copy all of this. Now you needn't copy this, this is just a little bit of shortcut, just saves time. Dot expand and we see Pascal's triangle coming into play. There's the coefficients one, three, three, one. We see a decreasing order of the powers of A and an increasing order of the powers of B. Isn't that lovely A cubed plus three A squared B plus three AB squared plus B cubed, isn't that fantastic? So I hope this was a quick enough introduction for you that you've seen the power of using SMPI to do all your distributing. Now I want you to pay particular attention when you're doing distributing and you're working with radicals. As soon as you have square roots, cube roots, et cetera, please pay particular attention. What you can do with pencil and paper on the side is plug in some numbers, plug in values, actual real numbers, make them positives and negatives and make sure that what you're thinking of holds. Now the easiest cases is just to keep all the values positive. Then so many of the laws that we discussed, they just hold and everything works out very well for us. I showed you those edge cases when you do have two negatives or you do have a negative and a positive, you really have to think about those. Fortunately for the majority of the time, we're just gonna deal with these polynomials and we're just going to do distribution with these polynomials and it actually, it's easy and I'm pretty sure you can do it in your head even. But here we have Python, we have symbolic Python and it looks like our textbooks and it's just so easy to do, especially if you use it just to verify your work. Now what I do want you to do, go out and play with us, create your symbols, get some example problems and see how easy it is to use SMPI.