1
$\begingroup$

While trying to implement an algorithm, I have come across a strange trick of numbers. I am not really into mathematics so I apologize if this will be a dumb question for some of you.

I am doing addition over arrays of numbers. Here is a picture:

enter image description here

Basically, you have three arrays $r_1$, $r_2$, $r_3$, each containing further arrays (like rows of a matrix). Say you want to do $r_1 + r_2$, you simply:

  • take the first sub-array in $r_1$

  • do an element-wise addition of this sub-array with all the sub-arrays in $r_2$

  • take the second sub-array of $r_1$

  • do an element-wise addition of this sub-array with all the sub-arrays in $r_2$

  • take third sub-array of $r_1$
  • and so on... until there are no more sub-arrays of $r_1$ left to add

Now, you can see from the picture that, by changing the order of addition, the result is not the same, while it should be the same! I have double checked the calculations but I don't see any error. Also, by using letters instead of numbers I get in fact that the result should not change, as in this picture:

enter image description here

Can somebody point me out what exactly am I doing wrong? Thanks.

1 Answers 1

2

First of all, the relevant algebraic property is associativity, not commutativity.

As to your question, it looks like you made an arithmetic mistake in your example. Specifically, $11+21=32$, not $33$. This accounts for both the discrepancies you've observed.

  • 0
    Ok, I really feel ashamed now. I did it three times, each time doing the same mistake. Sorry for wasting your time, ad for messing up with the name of the property.2017-02-02