Lesson 14 of 15

Tensor Operations

Tensor Operations

Tensors are the natural language of modern physics — from the stress tensor in continuum mechanics to the Riemann curvature tensor in general relativity.

Transformation Law

A rank-2 contravariant tensor transforms as:

Tij=Λ;kiΛ;ljTklT'^{ij} = \Lambda^i_{;k}\, \Lambda^j_{;l}\, T^{kl}

where Λ\Lambda is the transformation matrix (Jacobian). This is just a double matrix multiplication.

The Metric Tensor

The metric tensor gijg_{ij} encodes the geometry of space. It raises and lowers indices:

Ti=gijTj,Ti=gijTjT_i = g_{ij}\, T^j, \qquad T^i = g^{ij}\, T_j

where gijg^{ij} is the inverse metric. In flat 3D Cartesian space gij=δijg_{ij} = \delta_{ij} (identity).

Polar Coordinates (2D)

In polar coordinates (r,θ)(r, \theta), the metric is:

g=(100r2)g = \begin{pmatrix} 1 & 0 \\ 0 & r^2 \end{pmatrix}

The inverse is grr=1g^{rr} = 1, gθθ=1/r2g^{\theta\theta} = 1/r^2.

Christoffel Symbols

The Christoffel symbols encode how basis vectors change across the manifold:

Γ;ijk=12gkl(igjl+jgillgij)\Gamma^k_{;ij} = \frac{1}{2}\, g^{kl}\bigl(\partial_i g_{jl} + \partial_j g_{il} - \partial_l g_{ij}\bigr)

For 2D polar coordinates, the non-zero Christoffel symbols are:

Γ;θθr=r,Γ;rθθ=Γ;θrθ=1r\Gamma^r_{;\theta\theta} = -r, \qquad \Gamma^\theta_{;r\theta} = \Gamma^\theta_{;\theta r} = \frac{1}{r}

All other components vanish. These give rise to the fictitious forces (centripetal, Coriolis) in rotating frames.

Index Raising

Given a covariant tensor TijT_{ij} and the inverse metric gijg^{ij}, we raise the first index:

T;ji=gikTkjT^i_{;j} = g^{ik}\, T_{kj}

Implementation

  • tensor_contract(A, B) — matrix multiply two 2D lists (works for any square size)
  • metric_polar(r) — returns the 2x2 polar metric as a list of lists
  • christoffel_polar_r_theta_theta(r) — returns Γ;θθr=r\Gamma^r_{;\theta\theta} = -r
  • christoffel_polar_theta_r_theta(r) — returns Γ;rθθ=1/r\Gamma^\theta_{;r\theta} = 1/r
  • raise_index(T_lower, g_inv) — computes T;ji=gikTkjT^i_{;j} = g^{ik} T_{kj}
def metric_polar(r):
    return [[1.0, 0.0], [0.0, r**2]]

def christoffel_polar_r_theta_theta(r):
    return -r
Python runtime loading...
Loading...
Click "Run" to execute your code.