 In the previous Python video we had our first real look at the usefulness of symbolic Python in Algebra. Now in this video we're going to do the opposite of what we did last time. This time we're going to take an expression and we are going to factor it. Break it apart into its constituent factors and factorizing or factoring is one of the most important tasks in Algebra. It is very easy when we use Sympa. Here you can see my Google Drive. We're already at lecture number five. I've already created this notebook. It's pre-populated because I really don't want to waste your time watching me type. Remember to create a new notebook. I'll go to new, more and then select Google Colab. At the top left you can see I've already named my file. It's lecture five dot IPYNB. My first cell here is a text cell. Let me double click on that so you can see how I generated that. Just as a reminder I'm using a single hashtag symbol in a text cell that is going to tell this notebook what font size to use in a single hashtag is the largest font size. And there we can see it's a five and a little straight up and down line and then factoring. I'm just trying to make it look good. Now another text cell. I'm saying package is used in this notebook and you can see two hashtag symbols and then a space. There should always be a space that just tells this notebook I want the second largest font size to execute. I'm just holding down shift and hitting return or enter. Now here's my first line of code. If I use a hashtag symbol, a pound symbol inside of a code cell, it has a completely different meaning from the meaning inside of a text cell. Here just tells Python, the Python interpreter, ignore everything that follows in that line. Python just ignores that line of code. And what we're using it for is to leave little comments to ourselves or to someone else who's reading our code. And I said to import only the quite functionality from the Sympy package. I do not want to have to use the Sympy word every time. I don't want to say Sympy dot symbols, Sympy dot GCD. I want to use those function names on their own. And the way to do that is to use the from keyword. So from Sympy import init underscore printing comma symbols comma GCD. Only those three functions that's all I'm going to require in this notebook. Let me execute that line of code at the top right. You'll see connecting. It's now connecting to Google servers because Python is going to run on Google side, not on your computer. That line of code is executed. And so let's call that function immediately in it underscore printing. Remember what that function does. It's going to allow mathematical type setting when I execute code. When I execute Sympy code, I do want that output to look like my mathematical textbook. And that is what this function is going to do. I also have a little code comment to myself call the init underscore printing function to output mathematical type setting. I'm going to run that please note when you call a function, when you're actually using it, you do have to have the open closing parentheses. But when you import that function, you don't use the open close parentheses. Now let's remind ourselves of what we did in the previous lecture. We did distributing over an expression. So here's a text cell. Let me double click on that so you can see how I created that. It's normal text. I'm saying distribute. And inside of a set of dollar symbols, I'm using LaTeX. Now LaTeX is a kind of computer code. It allows for mathematical type setting inside of documents. So that's not code that gets executed. It just formats the printing to the screen. And so it's very well with your while to read up a little bit about LaTeX. LaTeX. Let's execute that code and you can see it's nice mathematical type setting, but that's not code. Nothing gets executed there. I'm just writing like I would in a normal document. Now the beauty of the symbolic Python package Sympy is that we can use mathematical symbols of variables. You cannot do that in many other languages. And what we're going to use is the symbols function. We note that it's a function because we have open close parentheses. And remember, we can use the word symbols as is because we've already imported it. There we go right at the top. Now we don't have to say Sympy dot symbols. Now in this instance, I'm using two arguments, the information that I'm passing to the function so that it can do its job. The first one is a positional argument. That is just where you type the value for the argument. In this instance, it's a string. I know that is a string because it's inside of quotation marks. And I'm just putting the symbol X. That is really what I want, the mathematical symbol, the variable X, the X that I see in my textbook comma. Now this argument is a keyword argument. I know that because it has a name, an assignment operator, and a value. Real equals true is telling Python this variable X, this mathematical variable X can be any real number. Now remember, an assignment operator looks at what it's to its right and assigns it to what is to its left. Now on the right, we've already created this mathematical symbol X. And we're going to assign that to a computer variable. Now you can choose that variable name, but it really just makes sense here to use the same symbol as my mathematical symbol. That is a mathematical symbol X, because it goes inside the symbols function. And the symbols function creates mathematical symbols. On the left hand side, that X though is a computer variable, the name that you give to a little space in your computer memory that stores whatever is to the right of the assignment operator. So let's execute this code. Now Python knows whenever I use the computer variable X, it looks inside the computer memory called X, and it sees, oh, there is a mathematical symbol inside of there. It is a mathematical variable, and it can hold any real value. And so now if I want to print this problem to the screen, I would say X. Now times is the star symbol. And then inside of parentheses, my expression X plus three, let's execute that. And you can see it's as we had our problem. It's X times X plus three. Now if we want to execute on that, we actually want to perform the mathematics, we take the problem and we put it inside of a set of parentheses. Can you see it's just the previous line of code inside of yellow parentheses. And remember the color only comes from the dark theme that I'm using here. You can choose those colors. It's just part of the theme. But there I have my problem inside of a set of parentheses. Now it is a single object. And I can now call one of the methods for that object. And it's the expand method. So let's execute that. And you can see we've distributed the X over the expression X plus three. So X times X is X squared. And X times three is three X. So it's X squared plus three X. Now during each of these lectures, I want to introduce something new. Remember in the previous lecture, I said that this was a method. It is a function that we call after creating the object. So I've created the object and then dot the function name expand. This object comes with it built in a bunch of functions that applies to this type of object. One of them is the expand function. And if I use it in this fashion, I call it a method. Now if we look at what we've imported, we never import it expand. It is a function, but we never imported it because we're using it as a method, a method that is built into an object that we already created. I want to show you, though, that the expand function does exist inside of SMPI. So look at this alternative I'm saying from SMPI import expand. And now I'm using the expand function and as an argument inside of the parentheses, I am passing my problem. And if I execute that, I'm also going to get the result X squared plus three X. So here I'm using expand as a method. Yeah, I'm using it in the form of a function that I've imported from the SMPI package. Now, great. I'm assuming that you watch the pencil and paper lecture and you know how to do factoring. So let's see how to do it in SMPI. My problem here is factor X squared plus three X. Again, I can double click on there. You can just see how I did this. It's also LaTeX. I have X and then a little carrot symbol inside of a set of braces. I've got two plus three X. It's just type setting as far as normal text is concerned. So let's print this to the screen. Remember in Python, if I want to power, I use two star symbols. So it's X star star two. That's X squared plus and then three times X. Let's print this to the screen. And there we have we have X squared plus three X. Now I actually want to break that apart into its constituent factors. And the way that I'm going to do that is to create a single object. So I'm passing this problem that I have inside of a set of parentheses so that I can generate one single object. And then I'm using the factor method. And do remember, I can also say from SMPI import factor and then use factor as a function. But yeah, I'm using it as a method because it's already built into that object. And so I'm just going to run this code and you see it's X times X plus three. So what you immediately notice, it is just the reverse factoring is just the reverse of the distributing that we've just done. We took X times X plus three, we did the distribution and we got X squared plus three X. Now if I factor that X out, it's common between these two terms, there's two X's there, there's a single X there, I can take a single X out and I'm left with X plus three. And indeed, that is what we see. There's our beautiful result because we use the init underscore printing function right at the beginning. Our code is also going to output beautifully rendered mathematical notation. Great, let's move on to the next problem. We have factor three X plus nine Y. Now Y, we have not created a mathematical symbol yet. So look at this code, you can look at that on your own. I'm just creating a new mathematical symbol Y and am I signing that to the computer variable Y. Now we can print this problem to the screen. It's three times X. So that's three star X plus nine star Y. Now remember, SMPI is going to follow the order of mathematical operations. So it knows to do multiplication before addition. So I don't have to use any parentheses there if I do not want to. But I can do that. I can put parentheses around three times X and then parentheses around nine times Y. That is all up to me. And now you can see the problem three X plus nine Y. Now there's an X and a Y for the two terms in this expression. There's nothing common between an X and a Y. So I can't take anything out. But there is something that I can take out that's a common divisor of three and nine. And I just want to remind you of this GCD function in SMPI, the GCD function greatest common divisor as arguments I'm passing those two coefficient values three and nine. And let's have a look at that. It says the greatest common divisor is three. And now if I pass my problem inside of a set of parentheses to create a single object, I call the factor method, you'll see that three is taken out as a common factor. And I'm left with X plus three Y. Now the great thing is I can verify that my results are correct. I can confirm that. So let's take the result that we've just had put it inside of parentheses. So we create a single object. So I'm going to say three times X plus three Y. And I want to expand that. Let's see what happens. And lo and behold, I get back the original problem. So now again, I verified, I've confirmed my result. And I can also see that factoring and expansion, they are just opposites of each other. Let's look at the following problem. X to the power of four plus X to the power of three Y. I want to factor that immediately. I see there are four X's there. That's X times X times X times X plus I've got X times X times X. There are three X's that I can take out as a common factor. Let's print this to the screen. It's X to the power of four plus X to the power of three times Y. And there we see, I'm just want to make sure that it looks like the problem that we're dealing with. And let's pass all of that into a set of parentheses. I'm creating a single object. And then I'm using the factor method. And there we see the X to the power of three was taken out as a common factor. And I'm left with X plus Y. Once again, I can confirm or verify that these results are correct. I'm passing that inside of a single set of parentheses, then using the expand method. Let's see. Yes, indeed, if I take X to the power of three, and I multiply that by the expression X plus Y, I do get back to the original problem. X to the power of four plus X cube times Y. Now let's factor this polynomial X squared plus five X plus six. I'm going to print it to the screen. You can have a look at the code. And I see the result X squared plus five X plus six. Do you remember always I have to use the multiplication symbol. I can't just say five X and Python. Again, I'm creating this single object there. And I'm passing that inside of a set of parentheses, creating a single object. And then on that object, I'm calling the factor method. And I see my two factors X plus two times X plus three. Once again, I can verify my result using the expand method. And I'm back to the original. I know my solution is absolutely correct. Now let's factor this. I've got X cube plus X squared minus 14 X minus 24. First of all, I'm going to print it to the screen. That's indeed correct. So I can just copy and paste all of that. And then I'm just going to put that inside of a set of parentheses, creating a single object and then calling the factor method. And look at that. It's X minus four times X plus two plus X times X plus three. Now, isn't that fantastic? You don't have to do any of the hard work. Python's doing all of this for you. Our next problem is factor X squared minus nine. Let's print that to the screen. Again, Python knows to follow the order of mathematical operations. So it's going to do powers before subtraction. You can put little parentheses around everything just to keep it safe. But you really don't have to in this instance. If I print that to the screen, you see it's X squared minus nine. Let's pass all of that inside of a set of parentheses now to create that single object. Then we're going to call the factor method. And if we do that, we see, well, it's a difference of squares there. So I'm going to get X minus three times X plus three. Once again, I can verify my result. If I take X minus three times X plus three and I expand on that, I'm going to get X squared minus nine. Very easy to do. Let's do the next one factor X cube minus Y cube. There I'm printing it to the screen. There I'm using the factor method to do the factorization. And I see it's X minus Y multiplied by X squared plus X Y plus Y squared. Now in the pencil and paper lecture, we saw we cannot factor this expression X squared plus X Y plus Y squared any further. That is as simple as it gets. And I can just verify my result. And I'm back to X cube minus Y cube. So instead of the subtraction, let's do the addition. I'm just printing it to the screen. Let's factor that. And once again, we'll see that second factor there. We cannot factorize it any further. It's in its simplest form. I can verify the result again. And I'm back with X cube plus nine cube. Now look at this. We have a numerator and denominator. Both of them are polynomials. And we want to factor this. We want to simplify this basically. So let's do the numerator on its own. So I've got the numerator there. There we go. I'm passing that inside of a set of parentheses, creating a single object. Then I'm calling the factor method on that object. And we can see it seems to be X minus three times X plus four squared. So there's actually two X plus fours. And then a two X plus one. Let's factor the denominator as well. You can see there's an X minus three and X minus two and an X plus four. And you will know from pencil and paper, there's a lot of stuff that can cancel here. I have an X minus three in the numerator. I have an X minus three in the denominator. Those can cancel. I have two X plus fours in the numerator. I have one X plus four in the denominator. At least one of the ones in the numerator can cancel with the one in the denominator. And then I'm going to be left with two X plus one. And I'm going to be left with X minus two. Now let's print this whole problem to the screen. I've just printed out the whole thing. And now I want to show you there two things that you can do. Here I have the whole problem passed inside of a set of parentheses. You see the yellow open close parentheses. Once again, I'm creating a single object. And then I'm calling the simplify method. So let's have a look at that. And it certainly has simplified it, but it hasn't given me the factors. And so instead of simplifying, let's actually call the factor method. And now you can see, look what we said before, we are left with a single X plus four in the numerator and a two X plus one. And then the denominator I'm left with a single X minus two. Of course, if I take this numerator and I expand it, then I'm going to get two X square plus nine X plus four. And so I hope you've seen just how easy it is to do factoring with Python and symbolic Python, the simplify package.