# Commutative vs. Associative

Sometimes a good example can instantly clarify something.

I recall back to my early school days when my math teacher explained that addition and multiplication were Commutative (a+b == b+a) and Associative ( a+(b+c) == (a+b) +c). We students didn't really understand the difference since all of the limited operations we knew were both. We asked for an example that was one but not other other. Our teacher thought and mentioned matrix multiplication. But we didn't really understand matrices and so thought that was a lame example.

Years later, it hit me that string concatenation is an operation that's associative but not commutative.  After all, "A" + ("B" + "C") == "ABC" == ("A"+"B") + "C" (associative).   But "A" + "B" == "AB", while "B"+"A" == "BA", so it's clearly not commutative.

What other common functions can you think of that are either commutative or associative?

Tags

Comments (9)
1. michkap says:

I would have asked for operations that were commutative xor associative. ðŸ™‚

2. Anything involving function composition (or even more generally any semigroup) is associative though not necessarily commutative, eg matrix multiplication.

Color blending is commutative but not associative:

(white blend black) blend black = gray blend black = dark gray.

white blend (black blend black) = white blend black = gray.

3. Wesner Moise says:

Matrix multiplication is associative not commutative.

4. Mich – I was tempted to use "xor" but I thought it may confuse some people. I suppose it’s par for a technical blog. ðŸ™‚

Raymond – great example, thanks! That’s something 5th graders could understand.

Wesner – you’re right. That’s the example our teacher used many years ago; but since we didn’t know much about matrices at the time, we didn’t really appreciate it.

5. Oliver says:

I’ve got an about 8 month old son. Although it seems a little bit too early, I suppose I can show him the difference in quite some time using one of his toys: Some colorful, varying sized buckets that forms sort of a pyramidal pile if they are stacked correctly – and he loves to knock them over every time I do so.

Sometimes – just to be somewhat faster than his attempt to knock it over – I stack one small pile first, than the other and finally put one pile on the other. You can guess that it does not last very long ðŸ˜‰

As it turns out, the technique I use is exactly something like A + (B + C) = (A + B) + C, although more "variables" are involved. And it is easy to see – even for my son – that putting B on A does not turn out to be the same than putting A on B, because the result is rather boring.

So here it is, everything in a game of a 8 month old baby ðŸ™‚

6. Oliver – cute example!

7. Divya says:

Don’t subtraction or division count as operations ? Subtraction is neither commutative nor associative

3-4 != 4-3 and

((3-2)-1) != (3-(2-1))

Division is associative, not commutative

Or am I missing something very basic in this post ?

8. Divya-

You’re right about subtraction. Generally, finding operations that are both Comm + Assoc or finding operations that are neither Comm nor Assoc are easy. It’s more difficult to find intuitive operations that are one or the other.

Note that division is not associative:

((12 div 6) div 2) = ((2) div 2) = 1

(12 div (6 div 2)) = (12 div (3)) = 4

9. lee says:

it doesnt help u at all

Skip to main content