BRAGA

02 Writing02-02 Notes02-02-045

Associative of Addition

Published 16 Apr 20267 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
(a+b)+c=a+(b+c)(a + b) + c = a + (b + c)
(1)

Deep Breakdown

What it tells me (Situation Awareness): The associative property of addition says that the way you group addends does not affect the final sum. Parentheses can be moved freely without changing the result. In computing and DA/DS, this property justifies: summing a list in any order (left-to-right vs. tree-reduction), parallel aggregation (splitting data across nodes and merging sub-totals), and chaining multiple .sum() calls across partitioned datasets without worrying about grouping order.

Flowchart

Yes: association doesn'tmatterThree values: a,b, cOption 1: groupleft → (a+b)+cOption 2: groupright → a+(b+c)Sum a+b first,then add cSum b+c first,then add to aSame finalvalue?(a+b)+c =a+(b+c)confirmed

Real-world use

Where it is used

MapReduce and distributed aggregation (split rows across workers, sum each partition, then merge sub-totals), database query optimizers (they reorder and regroup additions freely), batch gradient computation (split dataset into mini-batches, accumulate partial gradient sums), and multi-level GROUP BY rollups in SQL.

Problem it solves

When summing billions of rows across a distributed cluster (Spark, BigQuery), associativity guarantees that splitting the job into independent chunks and summing the chunk totals gives the same answer as summing everything sequentially. Without associativity, distributed aggregation would be mathematically invalid.

How it solves the problem

(a+b)+c=a+(b+c)(a + b) + c = a + (b + c) means you can freely re-bracket. Workers compute sub-sums s1=a1+a2+⋯+aks_1 = a_1 + a_2 + \cdots + a_k and s2=ak+1+⋯+ans_2 = a_{k+1} + \cdots + a_n, then the coordinator computes s1+s2s_1 + s_2. The final value equals the sequential sum.

Example in a business

Monthly revenue is the sum of daily totals. Weekly sub-totals are computed first, then weekly totals are summed to get monthly totals: (W1+W2)+(W3+W4)=W1+(W2+W3+W4)(W_1 + W_2) + (W_3 + W_4) = W_1 + (W_2 + W_3 + W_4). Associativity guarantees the monthly figure is identical regardless of whether you sum weeks first or days directly.

The formula, piece by piece

Breaking the notation:

  • a,b,ca, b, c: any real numbers.
  • (a+b)(a + b): grouping — add aa and bb first.
  • +c+ c: then add cc to the result.
  • =a+(b+c)= a + (b + c): alternatively, add bb and cc first, then add aa.
  • The parentheses define computation order; the associative property says the order is irrelevant. Formal statement: ∀a,b,c∈R, (a+b)+c=a+(b+c)\forall a, b, c \in \mathbb{R},\ (a + b) + c = a + (b + c).

Key distinction:

  • Associativity is about grouping: (a+b)+c(a + b) + c vs. a+(b+c)a + (b + c).
  • Commutativity is about order: a+ba + b vs. b+ab + a.
  • They are independent properties: a binary operation can have one without the other.

Properties:

  • Generalizes to any number of addends: the sum a1+a2+⋯+ana_1 + a_2 + \cdots + a_n is well-defined regardless of parenthesization.
  • Also holds for vector addition: (u+v)+w=u+(v+w)(\mathbf{u} + \mathbf{v}) + \mathbf{w} = \mathbf{u} + (\mathbf{v} + \mathbf{w}).
  • Also holds for matrix addition: (A+B)+C=A+(B+C)(A + B) + C = A + (B + C).

The formula in words

"It doesn't matter which two numbers you add first. The answer at the end is the same. Grouping is just about computation order, not about the value. This is why you can parallelize a sum across many machines and then add the machine totals — the final number is always the same."

Critical Concepts

  1. Floating-point non-associativity: in IEEE 754 arithmetic, (a+b)+c≠a+(b+c)(a + b) + c \neq a + (b + c) in general due to rounding. This is a real concern in numerical computing — numpy.sum uses pairwise summation specifically to improve floating-point accuracy. Associativity holds exactly only for exact arithmetic.
  2. Distributed aggregation: MapReduce reduce(add, partition) on each worker, then reduce(add, worker_totals) works because of associativity. This is the foundation of Spark's reduceByKey and SQL's SUM in distributed execution.
  3. Gradient accumulation in deep learning: training with gradient accumulation over multiple mini-batches and then applying the sum of gradients is valid because (g1+g2)+g3=g1+(g2+g3)(g_1 + g_2) + g_3 = g_1 + (g_2 + g_3).
  4. Tree vs. sequential summation: computers often use tree-reduction (balanced binary grouping) rather than left-to-right summation for parallelism.

Associativity makes them equivalent in exact arithmetic.

Step-by-step calculation (tiny example)

Task: verify associativity for a=2, b=5, c=3a=2,\ b=5,\ c=3.

Step 1: Left grouping — (a+b)+c(a + b) + c (2+5)+3=7+3=10(2 + 5) + 3 = 7 + 3 = 10.

Step 2: Right grouping — a+(b+c)a + (b + c) 2+(5+3)=2+8=102 + (5 + 3) = 2 + 8 = 10.

Step 3: Confirm equality Both sides equal 10. Grouping did not change the result.

Code examples

Beginner example (Python)

python
# Associative property: (a + b) + c = a + (b + c)
a, b, c = 2, 5, 3
left_group = (a + b) + c
right_group = a + (b + c)
print(f"(a + b) + c = ({a} + {b}) + {c} = {a+b} + {c} = {left_group}")
print(f"a + (b + c) = {a} + ({b} + {c}) = {a} + {b+c} = {right_group}")
print(f"Equal: {left_group == right_group}")

# Distributed sum: partition a list and sum parts, then combine
data = [10, 20, 30, 40, 50, 60, 70, 80]
partition_1 = data[:4]   # [10, 20, 30, 40]
partition_2 = data[4:]   # [50, 60, 70, 80]
sub_total_1 = sum(partition_1)
sub_total_2 = sum(partition_2)
distributed_sum = sub_total_1 + sub_total_2   # (sum1 + sum2) = associativity
sequential_sum = sum(data)   # left-to-right sum
print(f"\nDistributed sum: {sub_total_1} + {sub_total_2} = {distributed_sum}")
print(f"Sequential sum: {sequential_sum}")
print(f"Equal: {distributed_sum == sequential_sum}")

Beginner example (R)

r
# Associative property: (a + b) + c = a + (b + c)
a <- 2; b <- 5; c <- 3
left_group <- (a + b) + c
right_group <- a + (b + c)
cat(sprintf("(a+b)+c = (%d+%d)+%d = %d+%d = %d\n", a, b, c, a+b, c, left_group))
cat(sprintf("a+(b+c) = %d+(%d+%d) = %d+%d = %d\n", a, b, c, a, b+c, right_group))
cat("Equal:", left_group == right_group, "\n")

# Distributed sum
data_vec <- c(10, 20, 30, 40, 50, 60, 70, 80)
part1 <- data_vec[1:4]
part2 <- data_vec[5:8]
distributed <- sum(part1) + sum(part2)
sequential <- sum(data_vec)
cat(sprintf("\nDistributed: %d + %d = %d\n", sum(part1), sum(part2), distributed))
cat(sprintf("Sequential: %d\n", sequential))
cat("Equal:", distributed == sequential, "\n")

Professional example (Python)

python
import numpy as np
import pandas as pd   # Scenario: demonstrate associativity in multi-level revenue rollup
# (simulating distributed / partitioned aggregation)
np.random.seed(42)
n_days = 365
dates = pd.date_range("2025-01-01", periods=n_days, freq="D")
revenue = np.random.uniform(1_000, 10_000, n_days).round(2)
df = pd.DataFrame({"date": dates, "revenue": revenue})
df["week"] = df["date"].dt.isocalendar().week.astype(int)
df["month"] = df["date"].dt.month
df["quarter"] = df["date"].dt.quarter   # Strategy 1: sequential sum of all days
total_sequential = df["revenue"].sum()   # Strategy 2: weekly sub-totals → monthly rollup → quarterly rollup → annual
weekly = df.groupby("week")["revenue"].sum()
monthly = df.groupby("month")["revenue"].sum()
quarterly = df.groupby("quarter")["revenue"].sum()
total_via_weeks = weekly.sum()
total_via_months = monthly.sum()
total_via_quarters = quarterly.sum()
print("Associativity: Multi-Level Revenue Rollup")
print("=" * 44)
print(f" Sequential (365 days): {total_sequential:>12,.2f}")
print(f" Via weekly subtotals: {total_via_weeks:>12,.2f}")
print(f" Via monthly subtotals: {total_via_months:>12,.2f}")
print(f" Via quarterly subtotals:{total_via_quarters:>12,.2f}")
print()
tol = 1e-6
all_equal = ( abs(total_sequential - total_via_weeks) < tol and abs(total_sequential - total_via_months) < tol and abs(total_sequential - total_via_quarters) < tol )
print(f" All strategies agree: {all_equal}")
print(f" (Max deviation: {max(abs(total_sequential - v) for v in [total_via_weeks, total_via_months, total_via_quarters]):.2e})")
print()
print(" Quarterly breakdown:")
for q, s in quarterly.items():
    print(f" Q{q}: {s:>10,.2f}")

Professional example (R)

r
# Associativity of addition: sum in any grouping gives same total
set.seed(42)
df <- data.frame( date = seq(as.Date("2024-01-01"), by = "day", length.out = 90), revenue = runif(90, 50, 200)
)
total_sequential <- sum(df$revenue)
df$week <- cut(df$date, breaks = "week")
df$month <- format(df$date, "%Y-%m")
total_via_weeks <- sum(aggregate(revenue ~ week, data = df, sum)$revenue)
total_via_months <- sum(aggregate(revenue ~ month, data = df, sum)$revenue)
quarterly <- aggregate(revenue ~ quarter, data = transform(df, quarter = paste0("Q", (as.numeric(format(df$date, "%m")) - 1) %/% 3 + 1)), sum)
cat(" All strategies agree:", abs(total_sequential - total_via_weeks) < 1e-9, abs(total_sequential - total_via_months) < 1e-9, "\n")
cat(" Quarterly breakdown:\n")
print(quarterly)

Source: 1300 p. 15

#foundations#number-sets-and-logic#basic-identities

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close