# Associative Swap

The `Associative Property`

of numbers says that we can re-group two `addition`

or `multiplication`

terms so that one is evaluated before the other, without changing the value of the expression.

The formulation of this property is the same for addition and multiplication:

- Addition
`(a + b) + c = a + (b + c)`

- Multiplication
`(a * b) * c = a * (b * c)`

Note

Interestingly, the application of the associative property of numbers to a binary expression tree is a common tree operation called a "node rotation".

### Transformations¶

#### Addition¶

```
(a + b) + c = a + (b + c)
(y) + + (x)
/ \ / \
/ \ / \
(x) + c -> a + (y)
/ \ / \
/ \ / \
a b b c
```

#### Multiplication¶

```
(a * b) * c = a * (b * c)
(x) * * (y)
/ \ / \
/ \ / \
(y) * c <- a * (x)
/ \ / \
/ \ / \
a b b c
```

### Examples¶

Info

All the examples shown below are drawn from the mathy test suite that verifies the expected input/output combinations for rule transformations.

Input | Output | Valid |
---|---|---|

1f + 98i + 3f + 14t | 1f + (98i + 3f) + 14t | ✔ |

8x^2 * (9b) * 7 | (8x^2 * 9b) * 7 | ✔ |

2 + 1 + 6 | 2 + (1 + 6) | ✔ |

2 + (1 + 6) | 2 + 1 + 6 | ✔ |

(2 + x) + 9 | 2 + (x + 9) | ✔ |

2x | --- | --- |

2 | --- | --- |