 Hello everyone, welcome back to some more Python programming looking at the SimPy module I'm rid of the null and we're gonna be looking at equality Now I'm not talking about race or gender and equal rights and stuff I'm talking about mathematical equality in symbolic computation and in Python and in SimPy So let's go ahead and import everything from SimPy import all that jazz and we're good to go Let's actually create X a little variable that we can work with we know we do that with the symbols function Passing a string for X. Oh Thanks for that autocomplete Oh, that's really funny. All right X equal symbols X there we go Okay, so the point of this tutorial is to talk about equal signs and equality This is going through a little bit more of the online tutorial the online documentation So this this whole series is for people that just don't like to read stuff I just would much rather much rather have it be presented to them But here we go here we freakin go what I want to talk about is this blessed equal sign Typically, we've got an expression. We write we can say this expression. This is gonna be their variable. It can equal the symbol X plus let's say 2 I don't care plus 2 and now we've got our expression working just fine with our X symbol and that works fine for us if I were to try and Set this expression equal to 4 make it an equation, right? Oh Now we're working of course with the variable but this steps away from Symbolic and Symbolism stuff, okay? Symbolism is not the word I want to go for but it doesn't represent things symbolically this expression variable is kind of a wrapper around the actual X plus 2 Expression that we see in mathematics. It's the Python variables representation of these symbols So rather than working with the expression variable. We're gonna work with the core X plus 2 Now if we tried to set this X plus 2 equal to 4 We are we're gonna get something weird. We get a syntax error can't assign to operator Python doesn't know WTF. We're doing so keep in mind here This is a consequence of of SimPy that doesn't extend Python syntax the equals sign and Python is an assignment Operator this is hard coded into the Python language and SimPy isn't gonna change that, you know So how can we assign equality to this expression like X plus 2? How can we make that equal 4? Well, that's not gonna work for us Maybe we can use two equal signs, right? This is this is a kind of a common notion or a common idea But this is used for equality testing in Python, right? We typically like test If a is equal to b, this is what these two equal equal signs in conjunction are used for That's used for equality testing in SimPy. It's used for equality That's not really correct either if I said X plus 1 or our X plus 2 that if that's equal to 4 We get false That's weird instead of treating X plus 2 is equal to 4 Symbolically instead of having this return to us. We just get this Boolean value false Why is that? Now in SimPy, these two equal signs represent exact structural equality testing That means that that like what we saw earlier a is equal to b and it's gonna return It's if a is equal to b just like we see in Python and this is asking if a is equal to b We're always gonna get a Boolean variable. There is a separate object in SimPy That you're probably gonna see a lot of This is what is actually actually used to create symbolic equalities. It's eq capital eq for equality X plus 2 our Expression like we've seen and it takes another argument to say what it's actually equal to let's set it equal to 4 Now what's gonna be returned to us is X plus 2 equals equals 4 Because that's just what we set as this equation now you can think of eq as equality You can think of eq as equation think of it. However, you'd you'd really like but know that okay now at this point in this equation We can set this to a variable or something so we can keep working with it But in this microcosm of the world X plus 2 is equal to 4 This is gonna be returned true now when we use this again It's not gonna return true for us because it's only in this function It's in that microcosm of the world. So we'll see more of this later on but that's how we actually use and create symbolic equalities and There's another kind of caveat about our double equal sign. This is an interesting topic here What if we wanted to tell the difference between the expanded form and mathematics and a factored form of mathematics? Obviously these things are equal Let's let's check it out. We still got our variable X right so By its parentheses X plus 1 and I bring this to the power of 2 Let's say this is equals equals to X the power of 2 Plus 2 times X plus 1 so few mathematicians out there You can easily see that this is the factored form X plus 1 squared and this is the expanded form X squared plus 2 X plus Now if I hit enter We get false Why is that? We know that these two things are equal right at least according to mathematics, right? Did did we like find a bug in the SimPy? Did did we break the world of math? What's going on? X plus 1 squared does equal X squared plus 2 X plus X but Keep in mind that our equals equals represents exact structural equality testing exact This means that the two expressions that are compared if a is equal to b have to be literally the exact thing They're not the same symbolically X plus 1 squared does not equal X squared plus 2 X plus 1 symbolically and I don't know that's a that's a weird kind of word to throw around now Symbolically they can just you can just hey man take over the world symbolically Now one is the power of an addition of two items and the other is an addition of three items X plus 1 squared X plus 2 X Blah blah blah So using the SimPy as a library using this equals equals test is fine for exact symbolic equality But it's it's more useful than representing symbolic equality or even test for mathematical quality You probably care about mathematical equality. So we've seen the alternative to this We can use and set things equal with our EQ function But it doesn't really determine whether or not things truly are equal like this thing X plus 1 squared equals X squared plus 2 X plus 1 what if we really want to know that okay? Yeah, these things are equal the way that we do this pretty much is by testing if this minus this is equal to zero and I mean, I guess this could work for us right now the way that we do this is we actually pass in a Function that's part of SimPy and that we'll learn more and more about later, but it's a Simplify My braces match up my friends these match up No simplify Now we get zero okay, so now this is telling us that yeah, these things are equal but It's interesting. I'll actually show this often in a coming video. This method is not infallible That's at least what the documentation tutorial on lines is in fact it can be theoretically proven It is impossible to determine if two symbolic expressions are Identically identically equal in general, but for most common expressions this works for us So hey That's a really cool thing and I'll show that off in a later tutorial At least not a later tutorial, but an interesting video that it's theoretically proven you can't prove two equal things Truly are equal to each other, so I guess I'll kind of leave off on that Before I go actually there is another method another function Equals and what this one does is it? Test if two expressions are equal to each other by evaluating them at random points So if I had my thing here inside my simplify function Took all this jazz, and I just threw it in here Oh actually it takes it as a Dot operator. Let's say a our new symbol. What that's going to do is will equal our x plus one squared and B can equal the other end of that our x squared plus two times x plus one We could use now simplify A minus B, and we get zero so we're looking at the exact same thing again, and we can also use a dot a A being obviously this expression and dot using a function of that a dot equals Now we'll pass in other it returns true if self is equal to other false if doesn't or not and A equals B, and this is going to equal true for us This is going to return a Boolean value and the way that does that is by a Evaluating these expressions at a random point or random value. So that's the way it works. Okay, that's enough talking That's kind of a bunch of the equality stuff that I wanted to kind of just spew out to you guys I hope you can retain all that eq It's the way we can look at equality or set things equal to each other The double double equal sign is testing for exact equality and you've got other functions like simplify and equals That you can play with if you need to but I mean there they are guys here I'm just handing them to you take them out. I'm take 12 take 9. I mean I got to get rid of them at all All right. Thank you guys for watching. I hope you enjoyed the series Hope you're enjoying the videos if you do like it. Please like the video. Please comment Tell me what you're thinking maybe subscribe to you. I'll see you in the next tutorial