What is the commutative property?

We discuss the commutative property, why we use it, and three different possible meanings.

Transcript

[00:00:00] What is the commutative property and why do we use it?

[00:00:07] Hello, my name is Eric Normand, and this is another episode of my podcast. Welcome.

[00:00:14] So the commutative property is another algebraic property, and it's actually two different properties. If you really wanted to get technical, there's two different ways of talking about commutativity.

[00:00:30] The first is the one that you learned in high school algebra, which is that the order of arguments to a function doesn't matter. If you look at something like addition: One plus two is the same as two plus one. And in fact, for all numbers X and Y, X plus Y is equal to Y plus X. Okay. That's the commutative property of addition. You can find ways of implementing your operations often so that they have the commutative property. And you want to do that because it allows for flexibility of the ordering.

[00:01:15] Remember when we talked about the associative property, we were talking about sub solutions and being able to recompose them together. Well, if you're gonna do the sub solutions in parallel, you can't guarantee which one is gonna arrive first, and so you don't want to have to worry about the order. If your combining operation is associative, it lets you break it up into sub solutions. And if it's commutative, it lets you build it up as you get the answers back, regardless of the order. You don't have to maintain the order of those arguments.

[00:02:01] If you wanted to add up the sales of pizzas for the year, you could break it up to do each month separately, so I'll have 12 different calculations going on, and then I'm gonna add them all together. Now the associative property is what lets you do that, but the commutative property tagging along with the associative property, lets you get those month calculations back in any order.

[00:02:35] When you add them up, it doesn't matter what order you add them. Which is just very helpful. It lets you be a little bit more flexible, a little looser about having to maintain that order strictly, which can get hard.

[00:02:50] The next thing is a slight extension to the idea of commutativity. Instead of the arguments being in any order, you could say two operations happen in any order. Now this is just a slight change. So what does it mean? It means that if I've got a UI for a pizza, I can set the size and choose the type of sauce, or I can choose the type of sauce and then set the size and it doesn't matter which order I do it in.

[00:03:42] And that's super useful for a UI because you don't know what the user already knows. You don't want to have to make them do it in a certain order because they might know the sauce already and they're still thinking about the size. You can just present all the options to them, it doesn't matter what order they do them in, and they will get the same pizza at the end.

[00:04:07] Now, this might not be possible for whatever domain you're dealing with, but it's a very useful property for doing these UIs, and it's something we often don't even think about. We think, of course you can set them independently in whatever order. They don't affect each other, right? But we often still put an order on people and we don't guarantee that that's possible. It just happens to be possible the way we happen to have implemented it. So what the algebraic modeling has you do is to explicitly say, yes, I can do these two things in any order or these three things, or what have you.

[00:04:54] I guess there's kind of a third kind of commutative, which is kind of in between. It's kind of a hybrid. So we talked about having the order of arguments can be reversed or changed any order. And we also talked about two or more different operations can happen in any order. But there's this third thing, which is the same operation, but different arguments.

[00:05:26] If your monthly sums come in, in random orders because they're being done in parallel, you could have a running total. Okay. So you take the current total and now this new sum comes in, which is the month of March, add it to the running total and you get the new running total. Now here comes July and then December. They're coming in in random order. You can add them in one at a time. It's the same operation, but with different arguments. You don't have to worry what order they come in. If you get all of them, all 12, you'll have the total for the year. So, my name is Eric Norman. Thanks again for listening. This has been another episode of my podcast, as always, rock on.