PhysLib FLRW coordinates
DefinitionPhyslibFLRWCoordinatesgeneral-relativityphyslib-exact-spacetimes
The FLRW metric and the coordinate-time scale factor associated with a physical cosmological model.
Definition code
import Definitions.Def_PhyslibCoordinateGeometry
import Definitions.Def_PhyslibPhysicalCosmology
import Lean.Elab.Command
import Lean.Exception
import Mathlib.Algebra.BigOperators.Fin
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Algebra.BigOperators.Ring.Finset
import Mathlib.Algebra.Field.Rat
import Mathlib.Algebra.Field.TransferInstance
import Mathlib.Algebra.Lie.Classical
import Mathlib.Algebra.Order.Ring.InjSurj
import Mathlib.Algebra.Order.Ring.Rat
import Mathlib.Analysis.Calculus.ContDiff.Deriv
import Mathlib.Analysis.Calculus.ContDiff.WithLp
import Mathlib.Analysis.Calculus.Deriv.Add
import Mathlib.Analysis.Calculus.Deriv.Basic
import Mathlib.Analysis.Calculus.Deriv.Comp
import Mathlib.Analysis.Calculus.Deriv.Inv
import Mathlib.Analysis.Calculus.Deriv.Mul
import Mathlib.Analysis.Calculus.Deriv.Pow
import Mathlib.Analysis.Calculus.Deriv.Prod
import Mathlib.Analysis.Calculus.FDeriv.Symmetric
import Mathlib.Analysis.Distribution.TemperateGrowth
import Mathlib.Analysis.InnerProductSpace.Calculus
import Mathlib.Analysis.InnerProductSpace.PiL2
import Mathlib.Analysis.ODE.ExistUnique
import Mathlib.Analysis.SpecialFunctions.Pow.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.NNReal
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Deriv
import Mathlib.Analysis.SpecialFunctions.Trigonometric.DerivHyp
import Mathlib.Data.Matrix.Basic
import Mathlib.Data.Real.Basic
import Mathlib.Data.Set.Basic
import Mathlib.Geometry.Manifold.ContMDiff.Atlas
import Mathlib.Geometry.Manifold.Instances.Real
import Mathlib.Geometry.Manifold.IsManifold.Basic
import Mathlib.Geometry.Manifold.MFDeriv.NormedSpace
import Mathlib.LinearAlgebra.Matrix.Notation
import Mathlib.LinearAlgebra.Matrix.Symmetric
import Mathlib.Logic.Equiv.Fin.Basic
import Mathlib.MeasureTheory.Measure.Haar.InnerProductSpace
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.FinCases
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.LinearCombination
import Mathlib.Tactic.NoncommRing
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Positivity
import Mathlib.Tactic.Ring
import Mathlib.Topology.Algebra.Module.Spaces.ContinuousLinearMap
section
/-
Copyright (c) 2026 Alex Vergara Gil. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alex Vergara Gil
-/
/-!
# FLRW spacetime metric and inverse
Comoving spherical FLRW spacetimes `FLRWSpacetime` with diagonal metric and inverse, their
symmetry and two-sided invertibility, and the induced `MetricTensor 4` and `CoordinateMetric 4`.
## i. Overview
The homogeneous, isotropic Friedmann–Lemaître–Robertson–Walker (FLRW) spacetime in comoving
spherical coordinates `x = (t, r, θ, φ)` is parameterized by a scale factor `a(t)` and normalized
spatial curvature `k`, carrying the diagonal line element:
`g = diag(-1, a(t)² / (1 - k r²), a(t)² r², a(t)² r² sin²θ)`.
This module establishes the symmetry and two-sided invertibility of the inverse `metricInv` on the
regular chart domain, and constructs the resulting `MetricTensor 4` and `CoordinateMetric 4`.
## ii. Key results
- `FLRWSpacetime` : comoving FLRW spacetime parameterized by scale factor `a` and curvature `k`.
- `FLRWSpacetime.metric`, `FLRWSpacetime.metricInv` : diagonal FLRW metric and inverse in
comoving coordinates `(t, r, θ, φ)`.
- `FLRWSpacetime.metric_symm`, `FLRWSpacetime.metric_mul_inv`, `FLRWSpacetime.metricInv_mul` :
symmetry and two-sided inverse identities on the regular coordinate domain.
- `FLRWSpacetime.metricTensor`, `FLRWSpacetime.toCoordinateMetric` : `MetricTensor 4` at a regular
point and `CoordinateMetric 4` on the regular chart.
## iii. Table of contents
- A. FLRW spacetime structure, metric, and CoordinateMetric
## iv. References
* R. M. Wald, *General Relativity* (1984), Ch. 5 (Sec. 5.1-5.2). [ref: wald_1984_general_relativity]
* C. W. Misner, K. S. Thorne, and J. A. Wheeler, *Gravitation* (1973), Ch. 27.
[ref: mtw_1973_gravitation]
* S. M. Carroll, *Lecture Notes on General Relativity* (1997), Ch. 8
(https://arxiv.org/pdf/gr-qc/9712019#page=201). [ref: Carroll1997]
-/
open GeneralRelativity
namespace Cosmology
namespace FLRW
/-!
## A. FLRW spacetime structure, metric, and CoordinateMetric
In comoving coordinates `(t, r, θ, φ)`, the spatially homogeneous and isotropic FLRW line element
is diagonal, invertible whenever the scale factor `a(t)`, radius `r`, `1 - k r²`, and `sin θ` are
non-zero.
-/
/-- Comoving Friedmann–Lemaître–Robertson–Walker (FLRW) spacetime parameterized by a scale
factor `a(t)` and spatial curvature `k`. -/
structure FLRWSpacetime where
/-- Scale factor `a(t)` as a function of comoving coordinate time. -/
a : ℝ → ℝ
/-- Normalized spatial curvature parameter `k`. -/
k : ℝ
namespace FLRWSpacetime
/-- A regular chart point of an FLRW metric: the scale factor, `r`, `1 - k r²`, and `sin θ` are
all nonzero, so the metric and its inverse are both defined. -/
structure RegularPoint (S : FLRWSpacetime) where
/-- Comoving coordinate point `(t, r, θ, φ)`. -/
x : Fin 4 → ℝ
/-- The scale factor does not vanish at the time. -/
ha : S.a (x 0) ≠ 0
/-- The radial coordinate is nonzero. -/
hr : x 1 ≠ 0
/-- The spatial curvature factor `1 - k r²` is nonzero. -/
hkr : 1 - S.k * (x 1) ^ 2 ≠ 0
/-- The polar angle is away from the axis. -/
hsin : Real.sin (x 2) ≠ 0
/-- The FLRW metric in comoving coordinates `(t, r, θ, φ)` with scale factor `S.a(t)` and spatial
curvature parameter `S.k`. -/
noncomputable def metric (S : FLRWSpacetime) (x : Fin 4 → ℝ) : Tensor2 4 :=
Matrix.of fun i j =>
if i = j then
if i = 0 then -1
else if i = 1 then (S.a (x 0)) ^ 2 / (1 - S.k * (x 1) ^ 2)
else if i = 2 then (S.a (x 0)) ^ 2 * (x 1) ^ 2
else (S.a (x 0)) ^ 2 * (x 1) ^ 2 * Real.sin (x 2) ^ 2
else 0
/-- The inverse of the FLRW metric in `(t, r, θ, φ)` coordinates. -/
noncomputable def metricInv (S : FLRWSpacetime) (x : Fin 4 → ℝ) : Tensor2 4 :=
Matrix.of fun i j =>
if i = j then
if i = 0 then -1
else if i = 1 then (1 - S.k * (x 1) ^ 2) / (S.a (x 0)) ^ 2
else if i = 2 then ((S.a (x 0)) ^ 2 * (x 1) ^ 2)⁻¹
else ((S.a (x 0)) ^ 2 * (x 1) ^ 2 * Real.sin (x 2) ^ 2)⁻¹
else 0
/-- Symmetry of `FLRWSpacetime.metric`. -/
lemma metric_symm (S : FLRWSpacetime) (x : Fin 4 → ℝ) : IsSymm (S.metric x) := by
ext i j
fin_cases i <;> fin_cases j <;> simp [Matrix.transpose_apply, metric]
/-- `S.metricInv x * S.metric x = 1` on the regular coordinate domain. -/
lemma metricInv_mul (S : FLRWSpacetime) (x : Fin 4 → ℝ) (ha : S.a (x 0) ≠ 0)
(hr : x 1 ≠ 0) (hkr : 1 - S.k * (x 1) ^ 2 ≠ 0) (hsin : Real.sin (x 2) ≠ 0) :
S.metricInv x * S.metric x = 1 := by
ext i j
fin_cases i <;> fin_cases j <;>
simp [metric, metricInv, Matrix.mul_apply]
· field_simp [ha, hkr]
· field_simp [ha, hr]
· field_simp [ha, hr, hsin]
/-- `S.metric x * S.metricInv x = 1` on the regular coordinate domain. -/
lemma metric_mul_inv (S : FLRWSpacetime) (x : Fin 4 → ℝ) (ha : S.a (x 0) ≠ 0)
(hr : x 1 ≠ 0) (hkr : 1 - S.k * (x 1) ^ 2 ≠ 0) (hsin : Real.sin (x 2) ≠ 0) :
S.metric x * S.metricInv x = 1 :=
mul_eq_one_of_isSymm_of_mul_eq_one (metric_symm S x) (metricInv_mul S x ha hr hkr hsin)
/-- The FLRW metric as a `CoordinateMetric 4` on its regular comoving chart where
`a(t) ≠ 0`, `r ≠ 0`, `1 - k r² ≠ 0`, `sin θ ≠ 0`. -/
noncomputable def toCoordinateMetric (S : FLRWSpacetime) : CoordinateMetric 4 where
g := S.metric
ginv := S.metricInv
domain :=
{x | S.a (x 0) ≠ 0 ∧ x 1 ≠ 0 ∧ 1 - S.k * (x 1) ^ 2 ≠ 0 ∧ Real.sin (x 2) ≠ 0}
symm := S.metric_symm
inv_left x hx := S.metric_mul_inv x hx.1 hx.2.1 hx.2.2.1 hx.2.2.2
inv_right x hx := S.metricInv_mul x hx.1 hx.2.1 hx.2.2.1 hx.2.2.2
end FLRWSpacetime
end FLRW
end Cosmology
end
section
/-
Copyright (c) 2026 Alex Vergara Gil. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alex Vergara Gil
-/
/-!
# FLRW Christoffel symbols and curvature
Closed-form Levi-Civita Christoffel symbols, partial-derivative tables, and metric-level curvature
of the FLRW coordinate metric.
## i. Overview
For an FLRW spacetime in comoving coordinates `(t, r, θ, φ)`, this module derives the Levi-Civita
connection directly from the metric components:
1. Defines closed-form tables `FLRWSpacetime.christoffel` and `FLRWSpacetime.christoffelDeriv`.
2. Proves all 64 Christoffel symbols and all 256 partial derivatives match the general coordinate
expressions (`christoffel_eq`, `eventually_christoffel_eq`, `pd_christoffel_eq_deriv`).
3. Identifies the coordinate Riemann tensor with `riemannFromData` (`riemann_eq_data_raw`).
4. Contracts it to the Ricci tensor, Ricci scalar, and Einstein tensor of the coordinate metric.
## ii. Key results
- `FLRWSpacetime.christoffel`, `FLRWSpacetime.christoffelDeriv` : closed-form Christoffel and
partial-derivative tables.
- `FLRWSpacetime.christoffel_eq`, `FLRWSpacetime.eventually_christoffel_eq`,
`FLRWSpacetime.pd_christoffel_eq_deriv` : coordinate-metric connection identities.
- `FLRWSpacetime.riemann_eq_data_raw` : coordinate Riemann tensor from the Christoffel data.
- `FLRWSpacetime.riemann_eq_data`, `FLRWSpacetime.ricciTensor`, `FLRWSpacetime.ricciScalar`,
`FLRWSpacetime.einsteinTensor` : metric-level curvature of the FLRW coordinate metric.
## iii. Table of contents
- A. Christoffel symbols and derivative tables
- B. Metric-level Ricci, scalar, and Einstein tensors
## iv. References
* R. M. Wald, *General Relativity* (1984), Ch. 5 (Sec. 5.1-5.2). [ref: wald_1984_general_relativity]
* C. W. Misner, K. S. Thorne, and J. A. Wheeler, *Gravitation* (1973), Ch. 27.
[ref: mtw_1973_gravitation]
* S. M. Carroll, *Lecture Notes on General Relativity* (1997), Ch. 8
(https://arxiv.org/pdf/gr-qc/9712019#page=201). [ref: Carroll1997]
-/
open GeneralRelativity
namespace Cosmology
namespace FLRW
open GeneralRelativity.CoordinateCurvature
namespace FLRWSpacetime
/-!
## A. Christoffel symbols and derivative tables
Evaluating the coordinate Christoffel symbols and their partial derivatives isolates the
Hubble-rate terms `a' / a`, spatial curvature factor `1 - k r²`, and angular factors across all
256 index combinations.
-/
-- Factored out separately from `CoordinateCurvature.christoffel` so symbolic time derivatives
-- of `S.a(t)` remain isolated from metric-inverse index sums.
/-- Closed-form Levi-Civita Christoffel symbols `Γ^m_{ij}` of an FLRW spacetime in comoving
coordinates `(t, r, θ, φ)`. -/
noncomputable def christoffel (S : FLRWSpacetime) (x : Fin 4 → ℝ) (m i j : Fin 4) : ℝ :=
if m = 0 then
(if i = 1 ∧ j = 1 then S.a (x 0) * deriv S.a (x 0) / (1 - S.k * (x 1) ^ 2)
else if i = 2 ∧ j = 2 then S.a (x 0) * deriv S.a (x 0) * (x 1) ^ 2
else if i = 3 ∧ j = 3 then S.a (x 0) * deriv S.a (x 0) * (x 1) ^ 2 * Real.sin (x 2) ^ 2
else 0)
else if m = 1 then
(if (i = 0 ∧ j = 1) ∨ (i = 1 ∧ j = 0) then deriv S.a (x 0) / S.a (x 0)
else if i = 1 ∧ j = 1 then S.k * x 1 / (1 - S.k * (x 1) ^ 2)
else if i = 2 ∧ j = 2 then -(x 1 * (1 - S.k * (x 1) ^ 2))
else if i = 3 ∧ j = 3 then -(x 1 * (1 - S.k * (x 1) ^ 2) * Real.sin (x 2) ^ 2)
else 0)
else if m = 2 then
(if (i = 0 ∧ j = 2) ∨ (i = 2 ∧ j = 0) then deriv S.a (x 0) / S.a (x 0)
else if (i = 1 ∧ j = 2) ∨ (i = 2 ∧ j = 1) then 1 / x 1
else if i = 3 ∧ j = 3 then -(Real.sin (x 2) * Real.cos (x 2))
else 0)
else
(if (i = 0 ∧ j = 3) ∨ (i = 3 ∧ j = 0) then deriv S.a (x 0) / S.a (x 0)
else if (i = 1 ∧ j = 3) ∨ (i = 3 ∧ j = 1) then 1 / x 1
else if (i = 2 ∧ j = 3) ∨ (i = 3 ∧ j = 2) then Real.cos (x 2) / Real.sin (x 2)
else 0)
-- Decouples scalar derivative evaluation from `CoordinateCurvature.riemann` assembly.
/-- Complete partial-derivative table `∂_μ Γ^m_{ij}` of the FLRW Christoffel symbols across all
256 coordinate and connection index tuples. -/
noncomputable def christoffelDeriv (S : FLRWSpacetime) (μ : Fin 4) (x : Fin 4 → ℝ)
(m i j : Fin 4) : ℝ :=
if μ = 0 then
(if m = 0 ∧ i = 1 ∧ j = 1 then
((deriv S.a (x 0)) ^ 2 + S.a (x 0) * deriv (deriv S.a) (x 0)) / (1 - S.k * (x 1) ^ 2)
else if m = 0 ∧ i = 2 ∧ j = 2 then
((deriv S.a (x 0)) ^ 2 + S.a (x 0) * deriv (deriv S.a) (x 0)) * (x 1) ^ 2
else if m = 0 ∧ i = 3 ∧ j = 3 then
((deriv S.a (x 0)) ^ 2 + S.a (x 0) * deriv (deriv S.a) (x 0)) * (x 1) ^ 2
* Real.sin (x 2) ^ 2
else if (m = 1 ∧ ((i = 0 ∧ j = 1) ∨ (i = 1 ∧ j = 0)))
∨ (m = 2 ∧ ((i = 0 ∧ j = 2) ∨ (i = 2 ∧ j = 0)))
∨ (m = 3 ∧ ((i = 0 ∧ j = 3) ∨ (i = 3 ∧ j = 0))) then
(deriv (deriv S.a) (x 0) * S.a (x 0) - (deriv S.a (x 0)) ^ 2) / (S.a (x 0)) ^ 2
else 0)
else if μ = 1 then
(if m = 0 ∧ i = 1 ∧ j = 1 then
2 * S.k * x 1 * S.a (x 0) * deriv S.a (x 0) / (1 - S.k * (x 1) ^ 2) ^ 2
else if m = 0 ∧ i = 2 ∧ j = 2 then
2 * x 1 * S.a (x 0) * deriv S.a (x 0)
else if m = 0 ∧ i = 3 ∧ j = 3 then
2 * x 1 * S.a (x 0) * deriv S.a (x 0) * Real.sin (x 2) ^ 2
else if m = 1 ∧ i = 1 ∧ j = 1 then
S.k * (1 + S.k * (x 1) ^ 2) / (1 - S.k * (x 1) ^ 2) ^ 2
else if m = 1 ∧ i = 2 ∧ j = 2 then
-(1 - 3 * S.k * (x 1) ^ 2)
else if m = 1 ∧ i = 3 ∧ j = 3 then
-(1 - 3 * S.k * (x 1) ^ 2) * Real.sin (x 2) ^ 2
else if (m = 2 ∧ ((i = 1 ∧ j = 2) ∨ (i = 2 ∧ j = 1)))
∨ (m = 3 ∧ ((i = 1 ∧ j = 3) ∨ (i = 3 ∧ j = 1))) then
-1 / (x 1) ^ 2
else 0)
else if μ = 2 then
(if m = 0 ∧ i = 3 ∧ j = 3 then
S.a (x 0) * deriv S.a (x 0) * (x 1) ^ 2 * (2 * Real.sin (x 2) * Real.cos (x 2))
else if m = 1 ∧ i = 3 ∧ j = 3 then
-(x 1 * (1 - S.k * (x 1) ^ 2) * (2 * Real.sin (x 2) * Real.cos (x 2)))
else if m = 2 ∧ i = 3 ∧ j = 3 then
Real.sin (x 2) ^ 2 - Real.cos (x 2) ^ 2
else if m = 3 ∧ ((i = 2 ∧ j = 3) ∨ (i = 3 ∧ j = 2)) then
-1 / Real.sin (x 2) ^ 2
else 0)
else 0
/-!
## B. Metric-level Ricci, scalar, and Einstein tensors
Contracting the closed-form FLRW Riemann tensor yields the diagonal Ricci components, scalar
curvature `R = 6 (a''/a + (a'/a)² + k/a²)`, and Einstein tensor directly from the metric.
-/
end FLRWSpacetime
end FLRW
end Cosmology
end
section
/-
Copyright (c) 2026 Alex Vergara Gil. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alex Vergara Gil
-/
/-!
# Deriving the Friedmann equations from Einstein's field equations
Comoving perfect-fluid stress-energy in an FLRW chart, and derivation of `Physlib.Cosmology.FLRW`'s
first- and second-order Friedmann equations for an arbitrary speed of light and Newton's
gravitational constant from the FLRW Einstein field equations.
## i. Overview
Equating the FLRW Einstein tensor with the stress-energy tensor of a comoving perfect fluid —
energy density `ρ`, pressure `p`, and four-velocity `u = ∂_t` in comoving coordinates — gives the
first and second Friedmann equations and their equivalence with the Einstein field equations.
Whereas `Physlib.Cosmology.FLRW` postulates the two Friedmann equations as ordinary differential
equations for a cosmological model with scale factor `a(t)`, this module derives them directly from
Einstein's field equations `G_{ij} + Λ g_{ij} = (8 π G / c⁴) T_{ij}` for a homogeneous, isotropic
metric filled with a comoving perfect fluid, with `G` the gravitational constant and `c` the speed
of light. Reparametrizing cosmic time `t` by the length-dimensioned coordinate `τ = c t` gives
`flrwScaleFactorOfReal` and `FLRWSpacetime.ofCosmology` and yields
`Cosmology.FLRW.FriedmannSolution` directly from the Einstein equations.
## ii. Key results
- `FLRWSpacetime.einsteinTensor_00_iff_friedmann`,
`FLRWSpacetime.einsteinTensor_spatial_iff_friedmann`,
`FLRWSpacetime.einsteinTensor_iff_friedmann` : equivalence of the comoving perfect-fluid
Einstein equations with the two Friedmann equations.
- `flrwScaleFactorOfReal`, `FLRWSpacetime.ofCosmology` : scale factor in coordinate time `τ = c t`
and corresponding FLRW spacetime geometry.
- `hasDerivAt_flrwScaleFactorOfReal`, `eventually_differentiableAt_flrwScaleFactorOfReal`,
`deriv_flrwScaleFactorOfReal_eq`, `hasDerivAt_deriv_flrwScaleFactorOfReal` : first- and
second-derivative relations between cosmic-time derivatives and `τ = c t` derivatives.
- `einsteinTensor_ofCosmology_iff_friedmann`, `friedmannSolution_of_einsteinTensor` :
equivalence between the FLRW Einstein field equations and `FirstOrderFriedmann` /
`SecondOrderFriedmann`, constructing a `Cosmology.FLRW.FriedmannSolution`.
## iii. Table of contents
- A. Comoving perfect fluid and Einstein–Friedmann equivalence
- B. Real-coordinate scale factor and Friedmann derivation
## iv. References
* R. M. Wald, *General Relativity* (1984), Ch. 5 (Sec. 5.1-5.2). [ref: wald_1984_general_relativity]
* C. W. Misner, K. S. Thorne, and J. A. Wheeler, *Gravitation* (1973), Ch. 27.
[ref: mtw_1973_gravitation]
* S. M. Carroll, *Lecture Notes on General Relativity* (1997), Ch. 8
(https://arxiv.org/pdf/gr-qc/9712019#page=201). [ref: Carroll1997]
-/
open GeneralRelativity
namespace Cosmology
namespace FLRW
open GeneralRelativity.CoordinateCurvature
/-!
## A. Comoving perfect fluid and Einstein–Friedmann equivalence
Equating the FLRW Einstein tensor with the comoving perfect-fluid stress-energy tensor directly
recovers the first and second Friedmann equations for an arbitrary speed of light and Newton's
gravitational constant.
-/
namespace FLRWSpacetime
end FLRWSpacetime
/-!
## B. Real-coordinate scale factor and Friedmann derivation
Rewriting a cosmological model's scale factor `a(t)` as a function of the length-dimensioned time
coordinate `τ = c t` relates the FLRW Einstein tensor directly to the first- and second-order
Friedmann equations in `Physlib.Cosmology.FLRW`.
-/
/-- The scale factor `τ ↦ a(τ / c)` of a cosmological model as a function of coordinate time
`τ = c t`. -/
noncomputable def flrwScaleFactorOfReal (c : SpeedOfLight) (U : Cosmology.FLRW) : ℝ → ℝ :=
fun τ => U.a ⟨τ / (c : ℝ)⟩
/-- Construct an FLRW spacetime metric from a `Cosmology.FLRW` model and speed of light `c`. -/
noncomputable def FLRWSpacetime.ofCosmology (c : SpeedOfLight) (U : Cosmology.FLRW) :
FLRWSpacetime where
a := flrwScaleFactorOfReal c U
k := U.geometry.curvature
end FLRW
end Cosmology
end
Source