Chapter 10
22 min read
Section 98 of 353

Moments and Center of Mass

Applications of Integration

Learning Objectives

By the end of this section, you will be able to:

  1. Understand the physical meaning of moments and center of mass
  2. Calculate centers of mass for discrete point masses
  3. Use integration to find centers of mass for continuous distributions
  4. Find centroids of planar regions using double integrals
  5. Apply Pappus's theorem to compute volumes and surface areas
  6. Connect center of mass to weighted averages in machine learning

The Big Picture: Balance and Distribution

"Give me a place to stand, and I will move the Earth." — Archimedes

The center of mass is one of the most powerful concepts in physics and engineering. It's the single point where we can consider all the mass of an object to be concentrated for purposes of analyzing motion and balance. But this concept extends far beyond physics — it appears whenever we need to find the "average position" of a distribution.

Why Center of Mass Matters

The center of mass answers a fundamental question: Where is the "average location" of distributed mass? This concept appears in physics (balance points, projectile motion), engineering (structural design), statistics (expected values), and machine learning (attention mechanisms, weighted averages).

In this section, we'll use integration to extend the simple discrete formula for center of mass to continuous distributions — finding the balance point of curves, regions, and solids.


Historical Context: Archimedes and the Lever

The study of center of mass began with Archimedes of Syracuse (287–212 BCE), who discovered the law of the lever. He showed that two masses balance on a lever when:

m1d1=m2d2m_1 \cdot d_1 = m_2 \cdot d_2

where d1d_1 and d2d_2 are the distances from the fulcrum. This principle — that the product of mass and distance (the moment) must balance — is the foundation of all center of mass calculations.

From Discrete to Continuous

For a finite set of point masses, finding the center of mass is simple summation. But what about a continuous object like a rod with varying density? This is where calculus becomes essential — we replace sums with integrals.


Discrete Point Masses

Let's start with the simplest case: a collection of point masses along a line.

One-Dimensional System

Given nn point masses m1,m2,,mnm_1, m_2, \ldots, m_n located at positions x1,x2,,xnx_1, x_2, \ldots, x_n on the x-axis:

Center of Mass (Discrete, 1D)

xˉ=i=1nmixii=1nmi=MyM\bar{x} = \frac{\sum_{i=1}^{n} m_i x_i}{\sum_{i=1}^{n} m_i} = \frac{M_y}{M}
where My=mixiM_y = \sum m_i x_i is the moment about the origin and M=miM = \sum m_i is the total mass

Understanding Moments

The moment of a mass about a point measures the tendency of that mass to cause rotation. For a mass mm at distance dd from a pivot:

Moment=md\text{Moment} = m \cdot d

The center of mass is the point where the net moment is zero — if you placed a fulcrum there, the system would balance perfectly.

Interactive: 1D Center of Mass

Drag the masses and adjust their weights to see how the center of mass responds. Toggle "Show Moments" to see how each mass contributes:

Center of Mass: Point Masses on a Line

Adjust masses and positions to see how the center of mass changes

Mass 1
Mass 2
Mass 3
0123456782.03.01.0x̄ = 3.500

Center of Mass Formula

x̄ = (Σ mᵢ xᵢ) / (Σ mᵢ)
= (2.0×1.0 + 3.0×4.0 + 1.0×7.0) / 6.0
= 21.00 / 6.0= 3.500

Physical Interpretation

The center of mass at x̄ = 3.500 is the balance point. If you placed a fulcrum here, the system would be in perfect equilibrium. The total mass is 6.0 kg.

Connection to Integration

For discrete masses, we sum: x̄ = Σmᵢxᵢ / Σmᵢ. For a continuous distribution with density ρ(x), this becomes an integral: x̄ = ∫x·ρ(x)dx / ∫ρ(x)dx. The integral is the continuous version of the sum!


Continuous Mass in One Dimension

For a continuous object like a rod with density ρ(x)\rho(x) (mass per unit length), we replace sums with integrals. Consider a rod from x=ax = a to x=bx = b:

Setting Up the Integral

A small segment of length dxdx at position xx has mass:

dm=ρ(x)dxdm = \rho(x) \, dx

The total mass and moment become integrals:

Continuous 1D Center of Mass

Total Mass: M=abρ(x)dxM = \int_a^b \rho(x) \, dx
Moment about origin: My=abxρ(x)dxM_y = \int_a^b x \cdot \rho(x) \, dx
xˉ=MyM=abxρ(x)dxabρ(x)dx\bar{x} = \frac{M_y}{M} = \frac{\int_a^b x \cdot \rho(x) \, dx}{\int_a^b \rho(x) \, dx}

Special Case: Uniform Density

If the density is constant (ρ(x)=ρ\rho(x) = \rho), the formulas simplify:

xˉ=ρabxdxρabdx=abxdxba=a+b2\bar{x} = \frac{\rho \int_a^b x \, dx}{\rho \int_a^b dx} = \frac{\int_a^b x \, dx}{b - a} = \frac{a + b}{2}

For a uniform rod, the center of mass is simply the midpoint — exactly what intuition suggests!

Density Cancels for Uniform Objects

When density is uniform, it cancels in the ratio M_y/M. This means the center of mass depends only on the geometry, not the material. For uniform objects, we call this the centroid.


Moments and Center of Mass in 2D

For a two-dimensional region (called a lamina) with density ρ(x,y)\rho(x, y), we need two coordinates for the center of mass.

The Formulas

Center of Mass (2D Lamina)

x-coordinate:
xˉ=MyM=Rxρ(x,y)dARρ(x,y)dA\bar{x} = \frac{M_y}{M} = \frac{\iint_R x \cdot \rho(x,y) \, dA}{\iint_R \rho(x,y) \, dA}
y-coordinate:
yˉ=MxM=Ryρ(x,y)dARρ(x,y)dA\bar{y} = \frac{M_x}{M} = \frac{\iint_R y \cdot \rho(x,y) \, dA}{\iint_R \rho(x,y) \, dA}

Understanding the Moments

  • MyM_y (moment about the y-axis) measures how mass is distributed horizontally. We multiply by xx because we're measuring distance from the y-axis.
  • MxM_x (moment about the x-axis) measures how mass is distributed vertically. We multiply by yy because we're measuring distance from the x-axis.

A Common Point of Confusion

The subscripts may seem backwards: MyM_y gives us xˉ\bar{x}, and MxM_x gives us yˉ\bar{y}. Remember: the subscript tells you which axis you're computing the moment about, and moment about an axis determines the perpendicular coordinate of the centroid.

Interactive: 2D Center of Mass

Explore how the centroid changes for different 2D shapes. For uniform density, the centroid depends only on geometry:

Center of Mass: 2D Planar Regions

Explore how the centroid (center of mass for uniform density) changes with different shapes

Triangle: vertices at (0,0), (a,0), (0,b)

a:4.0
b:3.0
xy0123456780123456(1.33, 1.00)

Area & Mass

Area A = 6.000

Mass M = ρA = 6.000

Centroid

x̄ = 1.333

ȳ = 1.000

Moments

Mx = ȳ·M = 6.000

My = x̄·M = 8.000

Centroid Formulas for Common Shapes

Rectangle (a × b):

x̄ = a/2, ȳ = b/2

Right Triangle:

x̄ = a/3, ȳ = b/3

Semicircle (radius r):

x̄ = r, ȳ = 4r/(3π)

Parabolic segment:

x̄ = 3a/4, ȳ = 3b/5


Centroid Formulas for Regions

For a region RR bounded by curves, with uniform density, we can derive explicit formulas. Consider the region between y=f(x)y = f(x) and y=g(x)y = g(x) from x=ax = a to x=bx = b, where f(x)g(x)f(x) \geq g(x):

Centroid of a Region Between Two Curves

Area: A=ab[f(x)g(x)]dxA = \int_a^b [f(x) - g(x)] \, dx
xˉ=1Aabx[f(x)g(x)]dx\bar{x} = \frac{1}{A} \int_a^b x [f(x) - g(x)] \, dx
yˉ=1Aab12[f(x)2g(x)2]dx\bar{y} = \frac{1}{A} \int_a^b \frac{1}{2}[f(x)^2 - g(x)^2] \, dx

Why the y-formula Has a 1/2

For the y-coordinate, we're averaging the vertical position within each vertical strip. The "center" of a strip from g(x)g(x) to f(x)f(x) is at height f(x)+g(x)2\frac{f(x) + g(x)}{2}. Working out the integral leads to the formula above.

Common Centroids Reference

ShapeCentroid LocationNotes
Rectangle (a × b)(a/2, b/2)Center of the rectangle
Right Triangle(a/3, b/3)1/3 from right-angle vertex
General TriangleAvg of vertices(x₁+x₂+x₃)/3, (y₁+y₂+y₃)/3
Semicircle (radius r)(r, 4r/(3π))≈ 0.424r from flat edge
Quarter Circle(4r/(3π), 4r/(3π))Same distance from each axis
Parabolic Segment(3a/4, 3h/5)y = h(1-(x/a)²) from 0 to a

Interactive: Moments via Integration

Watch how integration builds up the moment by summing infinitely many infinitesimal contributions:

Moments and Center of Mass via Integration

See how integration computes moments by summing x·f(x)·dx contributions

A linear density increasing from left to right

[0.0,3.0]
10
012345x̄ = 1.875f(x) = 2x + 1

Total Mass (Area)

M = ∫0.03.0 f(x) dx
= 12.0000

Moment My

My = ∫0.03.0 x·f(x) dx
= 22.5000

Center of Mass

x̄ = My / M
= 1.8750

How Integration Computes the Center of Mass

Step 1: Divide the region into thin vertical slices of width dx. Each slice at position x has "mass" dm = f(x) dx.

Step 2: The moment of each slice about the y-axis is x · dm = x · f(x) · dx.

Step 3: Sum all moments: My = ∫ x · f(x) dx.

Step 4: Divide by total mass: x̄ = My / M = [∫ x·f(x) dx] / [∫ f(x) dx].


Worked Examples

Example 1: Non-uniform Rod

Problem: A rod of length 2 meters has density ρ(x)=x\rho(x) = x kg/m. Find its center of mass.

Solution:

Step 1: Find total mass:

M=02xdx=[x22]02=2 kgM = \int_0^2 x \, dx = \left[\frac{x^2}{2}\right]_0^2 = 2 \text{ kg}

Step 2: Find moment about origin:

My=02xxdx=02x2dx=[x33]02=83 kg\cdotpmM_y = \int_0^2 x \cdot x \, dx = \int_0^2 x^2 \, dx = \left[\frac{x^3}{3}\right]_0^2 = \frac{8}{3} \text{ kg·m}

Step 3: Compute center of mass:

xˉ=MyM=8/32=431.33 m\bar{x} = \frac{M_y}{M} = \frac{8/3}{2} = \frac{4}{3} \approx 1.33 \text{ m}

Physical interpretation: Since density increases with x, the center of mass is to the right of the midpoint (1 m), at 4/3 m from the left end.

Example 2: Triangular Lamina

Problem: Find the centroid of the triangular region with vertices at (0, 0), (4, 0), and (0, 3).

Solution:

The line connecting (4, 0) and (0, 3) has equation y=3(1x/4)=33x4y = 3(1 - x/4) = 3 - \frac{3x}{4}.

Step 1: Find area:

A=04(33x4)dx=[3x3x28]04=126=6A = \int_0^4 \left(3 - \frac{3x}{4}\right) dx = \left[3x - \frac{3x^2}{8}\right]_0^4 = 12 - 6 = 6

Step 2: Find xˉ\bar{x}:

xˉ=1604x(33x4)dx=1604(3x3x24)dx\bar{x} = \frac{1}{6} \int_0^4 x\left(3 - \frac{3x}{4}\right) dx = \frac{1}{6} \int_0^4 \left(3x - \frac{3x^2}{4}\right) dx
=16[3x22x34]04=16(2416)=86=43= \frac{1}{6} \left[\frac{3x^2}{2} - \frac{x^3}{4}\right]_0^4 = \frac{1}{6}(24 - 16) = \frac{8}{6} = \frac{4}{3}

Step 3: Find yˉ\bar{y}:

yˉ=160412(33x4)2dx=112049(1x4)2dx\bar{y} = \frac{1}{6} \int_0^4 \frac{1}{2}\left(3 - \frac{3x}{4}\right)^2 dx = \frac{1}{12} \int_0^4 9\left(1 - \frac{x}{4}\right)^2 dx
=912[4(1x/4)33]04=3443=1= \frac{9}{12} \cdot \left[-4 \cdot \frac{(1-x/4)^3}{3}\right]_0^4 = \frac{3}{4} \cdot \frac{4}{3} = 1
Centroid=(43,1)\text{Centroid} = \left(\frac{4}{3}, 1\right)

Verification: For any triangle, the centroid is at the average of the vertices: ((0+4+0)/3, (0+0+3)/3) = (4/3, 1). ✓


Pappus's Theorem

Pappus of Alexandria (c. 290–350 CE) discovered a remarkable connection between centroids and volumes/surfaces of revolution.

Pappus's Theorem (First)

If a plane curve is revolved about an external axis in its plane, the surface area of the resulting surface of revolution is:

S=2πrˉLS = 2\pi \bar{r} \cdot L

where rˉ\bar{r} is the distance from the centroid to the axis and LL is the arc length.

Pappus's Theorem (Second)

If a plane region is revolved about an external axis in its plane, the volume of the resulting solid of revolution is:

V=2πrˉAV = 2\pi \bar{r} \cdot A

where rˉ\bar{r} is the distance from the centroid to the axis and AA is the area.

Example: Volume of a Torus

Problem: Find the volume of a torus formed by revolving a circle of radius rr about an axis at distance RR from its center (where R>rR > r).

Solution:

The centroid of the circle is at its center, which is at distance rˉ=R\bar{r} = R from the axis.

The area of the circle is A=πr2A = \pi r^2.

By Pappus's theorem:

V=2πRπr2=2π2Rr2V = 2\pi R \cdot \pi r^2 = 2\pi^2 R r^2

This formula is much simpler than setting up and evaluating the integral directly!


Real-World Applications

Structural Engineering

Application: In structural engineering, the centroid of a beam's cross-section determines how it resists bending. The moment of inertia (second moment of area) about an axis through the centroid is minimum, which is crucial for calculating deflection and stress.

For an I-beam, the cross-section is designed to maximize the moment of inertia while minimizing material — putting mass far from the neutral axis (the centroid line).

Physics: Projectile Motion

Application: When analyzing the motion of an extended object (like a thrown wrench), the center of mass follows a simple parabolic trajectory, even as the object rotates. All the complex rotational motion happens around the center of mass.

Aerospace: Stability

Application: Aircraft stability depends critically on the position of the center of mass relative to the center of lift. If the center of mass is too far forward or back, the aircraft becomes difficult or impossible to control.


Machine Learning Connection: Weighted Averages Everywhere

The center of mass formula is fundamentally a weighted average. This concept appears throughout machine learning.

Expected Value = Continuous Center of Mass

In probability, the expected value of a continuous random variable is:

E[X]=xf(x)dx\mathbb{E}[X] = \int_{-\infty}^{\infty} x \cdot f(x) \, dx

This is exactly the center of mass formula with f(x)f(x) as the density! The expected value is the "center of mass" of a probability distribution.

Attention Mechanisms

In transformer models, attention computes:

output=iαivi\text{output} = \sum_i \alpha_i \cdot v_i

where αi=softmax(scores)i\alpha_i = \text{softmax}(\text{scores})_i are the attention weights. This is a weighted average of value vectors — a center of mass in embedding space! Tokens with higher attention "pull" the output toward their embedding.

L2 Regularization and Moments

The L2 regularization term λwi2\lambda \sum w_i^2 is related to the second moment (moment of inertia) about zero. Keeping weights small is analogous to keeping mass close to the origin.


Python Implementation

Computing Centers of Mass

Center of Mass Calculations
🐍center_of_mass.py
31D Center of Mass Function

For a continuous 1D distribution, the center of mass is x̄ = ∫x·ρ(x)dx / ∫ρ(x)dx. This generalizes the discrete formula Σmᵢxᵢ/Σmᵢ.

18Total Mass Calculation

The total mass is M = ∫ρ(x)dx over the entire object. For uniform density, this equals ρ times the length.

22Moment Calculation

The moment M_y = ∫x·ρ(x)dx is the integral of position times density. This measures the ‘weighted average position’ contribution.

322D Center of Mass

For 2D regions, we need double integrals. The center of mass has two coordinates: x̄ = ∬ xρ dA / M and ȳ = ∬ yρ dA / M.

48Double Integral for Mass

scipy.integrate.dblquad computes the double integral. We integrate ρ(x,y) over the region defined by x bounds and y bounds that depend on x.

77Non-uniform Rod Example

With density ρ(x) = x, more mass is concentrated toward the right, so the center of mass is at x̄ = 4/3, to the right of the midpoint.

90Triangle Centroid

The centroid of a triangle is at (a/3, b/3) from the right-angle vertex. This is the average of the three vertex positions.

101Semicircle Centroid

By symmetry, the x-coordinate is 0. The y-coordinate is 4r/(3π) ≈ 0.424r, above the flat edge.

122 lines without explanation
1import numpy as np
2from scipy import integrate
3import matplotlib.pyplot as plt
4
5def center_of_mass_1d(density_func, a, b):
6    """
7    Compute center of mass for a 1D continuous distribution.
8
9    For a rod with density ρ(x) on [a, b]:
10    x̄ = ∫ x·ρ(x) dx / ∫ ρ(x) dx
11
12    Parameters:
13    -----------
14    density_func : callable - Density function ρ(x)
15    a, b : float - Interval endpoints
16
17    Returns:
18    --------
19    tuple : (center_of_mass, total_mass)
20    """
21    # Total mass: M = ∫ ρ(x) dx
22    mass, _ = integrate.quad(density_func, a, b)
23
24    # Moment about origin: M_y = ∫ x·ρ(x) dx
25    moment_func = lambda x: x * density_func(x)
26    moment, _ = integrate.quad(moment_func, a, b)
27
28    # Center of mass
29    x_bar = moment / mass if mass > 0 else 0
30
31    return x_bar, mass
32
33def center_of_mass_2d(density_func, x_bounds, y_bounds_func):
34    """
35    Compute center of mass for a 2D lamina.
36
37    For a region with density ρ(x, y):
38    x̄ = ∫∫ x·ρ(x,y) dA / M
39    ȳ = ∫∫ y·ρ(x,y) dA / M
40
41    Parameters:
42    -----------
43    density_func : callable - Density function ρ(x, y)
44    x_bounds : tuple - (x_min, x_max)
45    y_bounds_func : callable - Returns (y_min, y_max) for given x
46
47    Returns:
48    --------
49    tuple : (x_bar, y_bar, total_mass)
50    """
51    x_min, x_max = x_bounds
52
53    # Total mass
54    def mass_integrand(y, x):
55        return density_func(x, y)
56
57    mass, _ = integrate.dblquad(
58        mass_integrand,
59        x_min, x_max,
60        lambda x: y_bounds_func(x)[0],
61        lambda x: y_bounds_func(x)[1]
62    )
63
64    # Moment about y-axis: M_y = ∫∫ x·ρ dA
65    def moment_y_integrand(y, x):
66        return x * density_func(x, y)
67
68    M_y, _ = integrate.dblquad(
69        moment_y_integrand,
70        x_min, x_max,
71        lambda x: y_bounds_func(x)[0],
72        lambda x: y_bounds_func(x)[1]
73    )
74
75    # Moment about x-axis: M_x = ∫∫ y·ρ dA
76    def moment_x_integrand(y, x):
77        return y * density_func(x, y)
78
79    M_x, _ = integrate.dblquad(
80        moment_x_integrand,
81        x_min, x_max,
82        lambda x: y_bounds_func(x)[0],
83        lambda x: y_bounds_func(x)[1]
84    )
85
86    x_bar = M_y / mass if mass > 0 else 0
87    y_bar = M_x / mass if mass > 0 else 0
88
89    return x_bar, y_bar, mass
90
91# Example 1: Non-uniform rod with ρ(x) = x on [0, 2]
92print("Example 1: Rod with density ρ(x) = x on [0, 2]")
93print("=" * 50)
94
95rho1 = lambda x: x
96x_bar1, mass1 = center_of_mass_1d(rho1, 0, 2)
97
98print(f"Total mass M = ∫₀² x dx = [x²/2]₀² = 2")
99print(f"Moment M_y = ∫₀² x·x dx = ∫₀² x² dx = [x³/3]₀² = 8/3")
100print(f"Center of mass x̄ = M_y/M = (8/3)/2 = 4/3 ≈ {x_bar1:.4f}")
101print(f"Computed: x̄ = {x_bar1:.6f}, M = {mass1:.6f}")
102
103# Example 2: Triangular region with uniform density
104print("\nExample 2: Right triangle, vertices (0,0), (3,0), (0,2)")
105print("=" * 50)
106
107# Uniform density ρ = 1
108# Triangle: 0 ≤ x ≤ 3, 0 ≤ y ≤ 2(1 - x/3)
109rho2 = lambda x, y: 1.0
110y_bounds2 = lambda x: (0, 2 * (1 - x/3))
111
112x_bar2, y_bar2, mass2 = center_of_mass_2d(rho2, (0, 3), y_bounds2)
113
114print(f"Theoretical centroid: (a/3, b/3) = (1, 2/3)")
115print(f"Computed: x̄ = {x_bar2:.6f}, ȳ = {y_bar2:.6f}")
116print(f"Total mass (area) = {mass2:.6f}")
117
118# Example 3: Semicircle
119print("\nExample 3: Semicircle of radius 2, flat side on x-axis")
120print("=" * 50)
121
122r = 2.0
123rho3 = lambda x, y: 1.0
124y_bounds3 = lambda x: (0, np.sqrt(r**2 - x**2) if abs(x) <= r else 0)
125
126x_bar3, y_bar3, mass3 = center_of_mass_2d(rho3, (-r, r), y_bounds3)
127
128print(f"Theoretical centroid: (0, 4r/(3π)) = (0, {4*r/(3*np.pi):.6f})")
129print(f"Computed: x̄ = {x_bar3:.6f}, ȳ = {y_bar3:.6f}")
130print(f"Total mass (area) = {mass3:.6f} (theory: πr²/2 = {np.pi*r**2/2:.6f})")

Machine Learning Connections

Center of Mass in Machine Learning
🐍center_of_mass_ml.py
3The Key Connection

Center of mass IS a weighted average! The formula x̄ = Σmᵢxᵢ/Σmᵢ is identical to the weighted average formula with weights wᵢ = mᵢ/M.

23Normalizing to Weights

When we divide masses by total mass, we get weights that sum to 1. This is exactly what softmax does with attention scores!

35Attention as Center of Mass

Transformer attention computes a weighted sum of value vectors, where weights come from softmax(scores). This is computing the ‘center of mass’ of the values in embedding space.

57Attention Output

The attention output is Σαᵢ·vᵢ. Tokens with higher attention weights ‘pull’ the output toward their embedding, just like heavier masses pull the center of mass.

72L2 Regularization

The L2 penalty λΣwᵢ² is related to the second moment (moment of inertia) about zero. Minimizing it keeps weights ‘close to the origin’ in weight space.

97 lines without explanation
1import numpy as np
2
3def weighted_average_connection():
4    """
5    The center of mass formula IS a weighted average!
6
7    In ML, we use weighted averages constantly:
8    - Expected value: E[X] = ∫ x·f(x) dx
9    - Loss functions: L = (1/n) Σ loss_i
10    - Attention mechanisms: output = Σ αᵢ·vᵢ
11    """
12    print("Center of Mass = Weighted Average")
13    print("=" * 50)
14
15    # Point masses example
16    positions = np.array([1, 3, 5, 7])
17    masses = np.array([2, 1, 3, 2])
18
19    # Method 1: Center of mass formula
20    center_of_mass = np.sum(positions * masses) / np.sum(masses)
21
22    # Method 2: Weighted average (same thing!)
23    weights = masses / np.sum(masses)  # Normalize to sum to 1
24    weighted_avg = np.sum(positions * weights)
25
26    print(f"Positions: {positions}")
27    print(f"Masses: {masses}")
28    print(f"Weights (normalized): {weights}")
29    print(f"Center of mass: {center_of_mass:.4f}")
30    print(f"Weighted average: {weighted_avg:.4f}")
31    print(f"Same result: {np.isclose(center_of_mass, weighted_avg)}")
32
33def attention_as_center_of_mass():
34    """
35    Attention mechanisms compute a 'center of mass' in embedding space!
36
37    output = Σ αᵢ·vᵢ where αᵢ = softmax(scores)
38
39    This is exactly: output = Σ (mass_i / total_mass) · position_i
40    """
41    print("\nAttention = Center of Mass in Embedding Space")
42    print("=" * 50)
43
44    # Simplified example with 4 tokens
45    np.random.seed(42)
46
47    # Value vectors (like 'positions' in embedding space)
48    values = np.array([
49        [1.0, 2.0],  # Token 1
50        [3.0, 1.0],  # Token 2
51        [2.0, 4.0],  # Token 3
52        [0.5, 0.5],  # Token 4
53    ])
54
55    # Attention scores (like 'masses')
56    scores = np.array([0.5, 1.2, 2.0, 0.3])
57
58    # Softmax to get weights (normalize to sum to 1)
59    exp_scores = np.exp(scores - np.max(scores))  # Subtract max for stability
60    attention_weights = exp_scores / np.sum(exp_scores)
61
62    # Attention output = weighted sum = center of mass!
63    attention_output = np.sum(attention_weights[:, np.newaxis] * values, axis=0)
64
65    print(f"Values (positions):\n{values}")
66    print(f"Scores: {scores}")
67    print(f"Attention weights: {attention_weights}")
68    print(f"Attention output (center of mass): {attention_output}")
69
70    # Verify: highest attention weight (Token 3) pulls output toward [2, 4]
71    print(f"\nToken 3 has highest weight ({attention_weights[2]:.3f})")
72    print(f"Output is pulled toward Token 3's value [2, 4]")
73
74def regularization_as_moment():
75    """
76    L2 regularization penalizes the 'moment' of weights!
77
78    L2 penalty = λ Σ wᵢ² = λ · ∫ w² dw (continuous limit)
79
80    This is related to the second moment about zero.
81    Keeping weights small = keeping the 'mass' close to origin.
82    """
83    print("\nRegularization and Moments")
84    print("=" * 50)
85
86    # Example: Two weight configurations
87    weights_spread = np.array([3, -3, 2, -2, 1, -1])
88    weights_compact = np.array([0.5, -0.5, 0.3, -0.3, 0.2, -0.2])
89
90    # Second moment about zero (sum of squares)
91    moment_spread = np.sum(weights_spread ** 2)
92    moment_compact = np.sum(weights_compact ** 2)
93
94    print(f"Spread weights: {weights_spread}")
95    print(f"L2 penalty (second moment): {moment_spread:.4f}")
96    print(f"\nCompact weights: {weights_compact}")
97    print(f"L2 penalty (second moment): {moment_compact:.4f}")
98    print(f"\nL2 regularization encourages compact weights (lower moment)")
99
100weighted_average_connection()
101attention_as_center_of_mass()
102regularization_as_moment()

Common Mistakes to Avoid

Mistake 1: Confusing moment subscripts

MyM_y gives xˉ\bar{x}, and MxM_x gives yˉ\bar{y}. The subscript indicates which axis you're taking the moment about.

Mistake 2: Forgetting the 1/2 in the y-centroid formula

For the region between two curves, the yˉ\bar{y} formula has a factor of 1/2:

yˉ=1Aab12[f(x)2g(x)2]dx\bar{y} = \frac{1}{A} \int_a^b \frac{1}{2}[f(x)^2 - g(x)^2] \, dx

This comes from averaging the height within each vertical strip.

Mistake 3: Using the wrong axis for Pappus

The axis of revolution must be external to the region — the region cannot cross the axis. Also, measure the distance from the centroid, not from the edge of the region.

Mistake 4: Assuming centroid is at the midpoint

For non-uniform density or non-symmetric shapes, the center of mass is generally not at the geometric center. Always integrate to find the actual center of mass.


Test Your Understanding

Test Your Understanding

1 / 8

What is the center of mass of a uniform rod of length L?


Summary

The center of mass is the "balance point" of a mass distribution — the point where the net moment is zero. Integration extends this concept from discrete point masses to continuous distributions.

Key Formulas

SystemCenter of Mass FormulaNotes
Discrete (1D)x̄ = Σmᵢxᵢ / ΣmᵢSum of (mass × position) / total mass
Continuous (1D)x̄ = ∫x·ρ(x)dx / ∫ρ(x)dxReplace sums with integrals
2D Laminax̄ = ∬x·ρ dA / M, ȳ = ∬y·ρ dA / MDouble integrals for area
Pappus (Volume)V = 2πr̄·ACentroid distance × Area × 2π

Key Takeaways

  1. The moment of a mass about a point is mass × distance. The center of mass is where the net moment is zero.
  2. For continuous distributions, moments become integrals of position × density.
  3. For uniform density, the center of mass is called the centroid and depends only on geometry.
  4. MyM_y (moment about y-axis) gives xˉ\bar{x}, and MxM_x (moment about x-axis) gives yˉ\bar{y}.
  5. Pappus's theorem connects centroids to volumes and surface areas of revolution.
  6. Center of mass = weighted average, which appears throughout ML in expected values, attention, and more.
The Core Insight:
"The center of mass is where you can balance a distribution — the point where the average position of all the mass is located. Integration lets us find this point for any continuous distribution."
Coming Next: In the next section, we'll explore Probability Density Functions — where the center of mass formula becomes the expected value, connecting calculus to probability theory.
Loading comments...