physhorizon 0.19.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. physhorizon/__init__.py +18 -0
  2. physhorizon/__main__.py +3 -0
  3. physhorizon/_assets/lean/PhysHorizon/ActionAngle.lean +147 -0
  4. physhorizon/_assets/lean/PhysHorizon/Analytical.lean +237 -0
  5. physhorizon/_assets/lean/PhysHorizon/Approximation.lean +198 -0
  6. physhorizon/_assets/lean/PhysHorizon/Audit.lean +48 -0
  7. physhorizon/_assets/lean/PhysHorizon/Behavior.lean +63 -0
  8. physhorizon/_assets/lean/PhysHorizon/Bodies.lean +287 -0
  9. physhorizon/_assets/lean/PhysHorizon/Bridge.lean +39 -0
  10. physhorizon/_assets/lean/PhysHorizon/Canonical.lean +511 -0
  11. physhorizon/_assets/lean/PhysHorizon/CoarseGraining.lean +242 -0
  12. physhorizon/_assets/lean/PhysHorizon/Composition.lean +113 -0
  13. physhorizon/_assets/lean/PhysHorizon/ConstrainedFlow.lean +192 -0
  14. physhorizon/_assets/lean/PhysHorizon/Continuum.lean +333 -0
  15. physhorizon/_assets/lean/PhysHorizon/Contracts.lean +79 -0
  16. physhorizon/_assets/lean/PhysHorizon/Coordinates.lean +675 -0
  17. physhorizon/_assets/lean/PhysHorizon/Discovery.lean +89 -0
  18. physhorizon/_assets/lean/PhysHorizon/Frames.lean +400 -0
  19. physhorizon/_assets/lean/PhysHorizon/FreeFall.lean +168 -0
  20. physhorizon/_assets/lean/PhysHorizon/Geometry.lean +103 -0
  21. physhorizon/_assets/lean/PhysHorizon/HamiltonJacobi.lean +89 -0
  22. physhorizon/_assets/lean/PhysHorizon/Impacts.lean +134 -0
  23. physhorizon/_assets/lean/PhysHorizon/KeplerTime.lean +273 -0
  24. physhorizon/_assets/lean/PhysHorizon/Lagrangian.lean +285 -0
  25. physhorizon/_assets/lean/PhysHorizon/Mechanics.lean +224 -0
  26. physhorizon/_assets/lean/PhysHorizon/MechanicsInstances.lean +313 -0
  27. physhorizon/_assets/lean/PhysHorizon/Mesh.lean +52 -0
  28. physhorizon/_assets/lean/PhysHorizon/Modeling.lean +81 -0
  29. physhorizon/_assets/lean/PhysHorizon/Newton.lean +129 -0
  30. physhorizon/_assets/lean/PhysHorizon/Nonholonomic.lean +422 -0
  31. physhorizon/_assets/lean/PhysHorizon/Nonlinear.lean +123 -0
  32. physhorizon/_assets/lean/PhysHorizon/OpenSystems.lean +62 -0
  33. physhorizon/_assets/lean/PhysHorizon/Orbits.lean +385 -0
  34. physhorizon/_assets/lean/PhysHorizon/Oscillations.lean +840 -0
  35. physhorizon/_assets/lean/PhysHorizon/PhaseGeometry.lean +222 -0
  36. physhorizon/_assets/lean/PhysHorizon/Poisson.lean +116 -0
  37. physhorizon/_assets/lean/PhysHorizon/Probability.lean +36 -0
  38. physhorizon/_assets/lean/PhysHorizon/Relu.lean +46 -0
  39. physhorizon/_assets/lean/PhysHorizon/RigidDynamics.lean +250 -0
  40. physhorizon/_assets/lean/PhysHorizon/RigidStability.lean +144 -0
  41. physhorizon/_assets/lean/PhysHorizon/Scenario.lean +117 -0
  42. physhorizon/_assets/lean/PhysHorizon/Syntax.lean +49 -0
  43. physhorizon/_assets/lean/PhysHorizon/TimeCoordinates.lean +215 -0
  44. physhorizon/_assets/lean/PhysHorizon/Trajectory.lean +452 -0
  45. physhorizon/_assets/lean/PhysHorizon/Typed.lean +92 -0
  46. physhorizon/_assets/lean/PhysHorizon/Variational.lean +203 -0
  47. physhorizon/_assets/lean/PhysHorizon/WeakComputation.lean +82 -0
  48. physhorizon/_assets/lean/PhysHorizon.lean +48 -0
  49. physhorizon/_assets/lean/lake-manifest.json +117 -0
  50. physhorizon/_assets/lean/lakefile.toml +11 -0
  51. physhorizon/_assets/lean/lean-toolchain +1 -0
  52. physhorizon/_assets/lean/requirements.lock.json +9 -0
  53. physhorizon/_assets/schemas/bundle.schema.json +48 -0
  54. physhorizon/acceptance.py +92 -0
  55. physhorizon/assessment.py +57 -0
  56. physhorizon/bodies.py +349 -0
  57. physhorizon/calculus.py +369 -0
  58. physhorizon/canonical.py +74 -0
  59. physhorizon/cli.py +177 -0
  60. physhorizon/coarse_graining.py +220 -0
  61. physhorizon/composition.py +267 -0
  62. physhorizon/contracts.py +154 -0
  63. physhorizon/discovery.py +336 -0
  64. physhorizon/domains.py +57 -0
  65. physhorizon/engine.py +187 -0
  66. physhorizon/errors.py +16 -0
  67. physhorizon/free_fall.py +321 -0
  68. physhorizon/geometry.py +265 -0
  69. physhorizon/installation.py +151 -0
  70. physhorizon/intervals.py +71 -0
  71. physhorizon/ir.py +237 -0
  72. physhorizon/lean_backend.py +273 -0
  73. physhorizon/lean_composition.py +202 -0
  74. physhorizon/lean_scalar.py +86 -0
  75. physhorizon/learned.py +230 -0
  76. physhorizon/library.py +166 -0
  77. physhorizon/mechanics_advanced_catalog.py +126 -0
  78. physhorizon/mechanics_catalog.py +66 -0
  79. physhorizon/mechanics_extended_catalog.py +284 -0
  80. physhorizon/mesh.py +341 -0
  81. physhorizon/migration.py +28 -0
  82. physhorizon/modeling.py +352 -0
  83. physhorizon/newton.py +181 -0
  84. physhorizon/oscillator.py +217 -0
  85. physhorizon/packages.py +79 -0
  86. physhorizon/plugins.py +126 -0
  87. physhorizon/poisson.py +233 -0
  88. physhorizon/polynomial.py +75 -0
  89. physhorizon/probability.py +200 -0
  90. physhorizon/provenance.py +99 -0
  91. physhorizon/quantities.py +155 -0
  92. physhorizon/residual.py +143 -0
  93. physhorizon/scalar.py +224 -0
  94. physhorizon/scenario.py +166 -0
  95. physhorizon/textbook_models.py +297 -0
  96. physhorizon/theory_catalog.py +165 -0
  97. physhorizon/uniform_gravity.py +168 -0
  98. physhorizon-0.19.0.dist-info/METADATA +167 -0
  99. physhorizon-0.19.0.dist-info/RECORD +104 -0
  100. physhorizon-0.19.0.dist-info/WHEEL +5 -0
  101. physhorizon-0.19.0.dist-info/entry_points.txt +2 -0
  102. physhorizon-0.19.0.dist-info/licenses/LICENSE +202 -0
  103. physhorizon-0.19.0.dist-info/licenses/NOTICE +10 -0
  104. physhorizon-0.19.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,18 @@
1
+ """PhysHorizon's versioned IR and evaluation interfaces."""
2
+
3
+ __version__ = "0.19.0"
4
+
5
+ from .ir import Artifact, ArtifactRef, Bundle
6
+ from .engine import evaluate, replay
7
+ from .acceptance import policy
8
+ from .migration import revise_bundle, with_acceptance
9
+ from .scenario import Formula, Vocabulary
10
+ from .calculus import ErrorTransformer
11
+
12
+ __all__ = ["Artifact", "ArtifactRef", "Bundle", "evaluate", "replay", "policy", "revise_bundle", "with_acceptance",
13
+ "Formula", "Vocabulary", "ErrorTransformer", "__version__"]
14
+
15
+ # Reuse interfaces are deterministic tools for callers, not an agent runtime.
16
+ from .discovery import ResourceDescriptor, ResourceIndex, installed_index
17
+ from .provenance import StatementLedger
18
+ __all__ += ["ResourceDescriptor", "ResourceIndex", "installed_index", "StatementLedger"]
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ raise SystemExit(main())
@@ -0,0 +1,147 @@
1
+ module
2
+
3
+ public import PhysHorizon.Canonical
4
+ public import Mathlib.Analysis.SpecialFunctions.Integrals.Basic
5
+ public import Mathlib.Analysis.SpecialFunctions.Sqrt
6
+ set_option autoImplicit false
7
+ set_option maxHeartbeats 2000000
8
+ @[expose] public section
9
+ namespace PhysHorizon.ActionAngle
10
+ noncomputable section
11
+ open PhysHorizon.Newton PhysHorizon.Canonical Matrix
12
+
13
+ def oscillator (m w : ℝ) : Observable 1 where
14
+ value z := (z.2 0)^2/(2*m)+(m*w^2/2)*(z.1 0)^2
15
+ dq z := ![m*w^2*z.1 0]
16
+ dp z := ![z.2 0/m]
17
+ derivative z := by
18
+ have hq := (ContinuousLinearMap.proj (0:Fin 1) : Vec 1 →L[ℝ] ℝ).hasFDerivAt.comp z hasFDerivAt_fst
19
+ have hp := (ContinuousLinearMap.proj (0:Fin 1) : Vec 1 →L[ℝ] ℝ).hasFDerivAt.comp z hasFDerivAt_snd
20
+ convert ((hp.pow 2).mul_const (1/(2*m))).add ((hq.pow 2).const_mul (m*w^2/2)) using 1
21
+ · funext y; dsimp; ring
22
+ · apply ContinuousLinearMap.ext
23
+ intro v
24
+ simp [differential_apply,Fin.sum_univ_succ]
25
+ ring
26
+
27
+ /-- The chart is defined on positive action, with a chosen real angle lift.
28
+ It is locally canonical; the periodic angle is not a global real coordinate. -/
29
+ def chart (scale : ℝ) (z : Phase 1) : Phase 1 :=
30
+ (![Real.sqrt (2*z.2 0)/scale*Real.sin (z.1 0)],
31
+ ![Real.sqrt (2*z.2 0)*scale*Real.cos (z.1 0)])
32
+
33
+ def chartDifferential (scale : ℝ) (z : Phase 1) : Phase 1 →L[ℝ] Phase 1 :=
34
+ ContinuousLinearMap.pi (fun _ : Fin 1 =>
35
+ (Real.sqrt (2*z.2 0)/scale*Real.cos (z.1 0)) •
36
+ ((ContinuousLinearMap.proj (0:Fin 1)).comp (ContinuousLinearMap.fst ℝ (Vec 1) (Vec 1)))+
37
+ (Real.sin (z.1 0)/(scale*Real.sqrt (2*z.2 0))) •
38
+ ((ContinuousLinearMap.proj (0:Fin 1)).comp (ContinuousLinearMap.snd ℝ (Vec 1) (Vec 1)))) |>.prod
39
+ (ContinuousLinearMap.pi (fun _ : Fin 1 =>
40
+ (-Real.sqrt (2*z.2 0)*scale*Real.sin (z.1 0)) •
41
+ ((ContinuousLinearMap.proj (0:Fin 1)).comp (ContinuousLinearMap.fst ℝ (Vec 1) (Vec 1)))+
42
+ (scale*Real.cos (z.1 0)/Real.sqrt (2*z.2 0)) •
43
+ ((ContinuousLinearMap.proj (0:Fin 1)).comp (ContinuousLinearMap.snd ℝ (Vec 1) (Vec 1)))))
44
+
45
+ theorem chart_derivative (scale : ℝ) (z : Phase 1) (positive : 0<z.2 0) :
46
+ HasFDerivAt (chart scale) (chartDifferential scale z) z := by
47
+ have ht := (ContinuousLinearMap.proj (0:Fin 1) : Vec 1 →L[ℝ] ℝ).hasFDerivAt.comp z hasFDerivAt_fst
48
+ have hj := (ContinuousLinearMap.proj (0:Fin 1) : Vec 1 →L[ℝ] ℝ).hasFDerivAt.comp z hasFDerivAt_snd
49
+ have hr := (hj.const_mul 2).sqrt (by positivity : (2:ℝ)*z.2 0 ≠ 0)
50
+ have hq := hasFDerivAt_pi.mpr (fun _ : Fin 1 => (hr.mul_const (1/scale)).mul ht.sin)
51
+ have hp := hasFDerivAt_pi.mpr (fun _ : Fin 1 => (hr.const_mul scale).mul ht.cos)
52
+ convert hq.prodMk hp using 1
53
+ · funext y; apply Prod.ext <;> ext i <;> fin_cases i <;> dsimp [chart] <;> ring
54
+ · apply ContinuousLinearMap.ext
55
+ intro v
56
+ apply Prod.ext <;> ext i <;> fin_cases i <;>
57
+ simp only [chartDifferential,ContinuousLinearMap.prod_apply,ContinuousLinearMap.pi_apply,
58
+ add_apply,smul_apply,ContinuousLinearMap.comp_apply,ContinuousLinearMap.proj_apply,
59
+ ContinuousLinearMap.coe_fst,ContinuousLinearMap.coe_snd,smul_eq_mul,Function.comp_apply] <;> ring
60
+
61
+ theorem chart_symplectic (scale : ℝ) (nonzero : scale ≠ 0) (z : Phase 1)
62
+ (positive : 0<z.2 0) (u v : Phase 1) :
63
+ omega (chartDifferential scale z u) (chartDifferential scale z v)=omega u v := by
64
+ have hr : Real.sqrt (2*z.2 0) ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr (by positivity))
65
+ have trig := Real.sin_sq_add_cos_sq (z.1 0)
66
+ dsimp [omega,chartDifferential,dotProduct]
67
+ simp only [Fin.sum_univ_succ,Fin.sum_univ_zero,add_zero]
68
+ simp only [add_apply,smul_apply,ContinuousLinearMap.comp_apply,
69
+ ContinuousLinearMap.proj_apply,ContinuousLinearMap.coe_fst,ContinuousLinearMap.coe_snd,
70
+ smul_eq_mul,Function.comp_apply]
71
+ change (Real.sqrt (2*z.2 0)/scale*Real.cos (z.1 0)*u.1 0+
72
+ Real.sin (z.1 0)/(scale*Real.sqrt (2*z.2 0))*u.2 0)*
73
+ (-Real.sqrt (2*z.2 0)*scale*Real.sin (z.1 0)*v.1 0+
74
+ scale*Real.cos (z.1 0)/Real.sqrt (2*z.2 0)*v.2 0)-
75
+ (-Real.sqrt (2*z.2 0)*scale*Real.sin (z.1 0)*u.1 0+
76
+ scale*Real.cos (z.1 0)/Real.sqrt (2*z.2 0)*u.2 0)*
77
+ (Real.sqrt (2*z.2 0)/scale*Real.cos (z.1 0)*v.1 0+
78
+ Real.sin (z.1 0)/(scale*Real.sqrt (2*z.2 0))*v.2 0)=_
79
+ field_simp [hr,nonzero]
80
+ <;> linear_combination (u.1 0*v.2 0-u.2 0*v.1 0)*Real.sqrt (2*z.2 0)^2*trig
81
+
82
+ theorem energy_action (m w : ℝ) (positiveM : 0<m) (positiveW : 0<w)
83
+ (z : Phase 1) (positiveJ : 0≤z.2 0) :
84
+ (oscillator m w).value (chart (Real.sqrt (m*w)) z)=w*z.2 0 := by
85
+ have hs := Real.sq_sqrt (mul_nonneg positiveM.le positiveW.le)
86
+ have hr := Real.sq_sqrt (mul_nonneg (by norm_num : (0:ℝ)≤2) positiveJ)
87
+ have hn : Real.sqrt (m*w) ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr (mul_pos positiveM positiveW))
88
+ have trig := Real.sin_sq_add_cos_sq (z.1 0)
89
+ change (Real.sqrt (2*z.2 0)*Real.sqrt (m*w)*Real.cos (z.1 0))^2/(2*m)+
90
+ (m*w^2/2)*(Real.sqrt (2*z.2 0)/Real.sqrt (m*w)*Real.sin (z.1 0))^2=w*z.2 0
91
+ simp only [mul_pow,div_pow,hs,hr]
92
+ field_simp [positiveM.ne',positiveW.ne']
93
+ linear_combination z.2 0*trig
94
+
95
+ /-- The actual time-parameterized oscillator generated by constant action. -/
96
+ def path (m w J phase : ℝ) : Curve 1 where
97
+ point t := chart (Real.sqrt (m*w)) (![phase+w*t],![J])
98
+ tangent t :=
99
+ (![Real.sqrt (2*J)/Real.sqrt (m*w)*w*Real.cos (phase+w*t)],
100
+ ![-Real.sqrt (2*J)*Real.sqrt (m*w)*w*Real.sin (phase+w*t)])
101
+ derivative t := by
102
+ apply HasDerivAt.prodMk <;> apply hasDerivAt_pi.mpr <;> intro i <;> fin_cases i
103
+ · convert (((hasDerivAt_id t).const_mul w).const_add phase).sin.const_mul
104
+ (Real.sqrt (2*J)/Real.sqrt (m*w)) using 1
105
+ · rfl
106
+ · simp; ring
107
+ · convert (((hasDerivAt_id t).const_mul w).const_add phase).cos.const_mul
108
+ (Real.sqrt (2*J)*Real.sqrt (m*w)) using 1
109
+ · rfl
110
+ · simp; ring
111
+
112
+ theorem path_hamilton (m w J phase : ℝ) (positiveM : 0<m) (positiveW : 0<w) :
113
+ HamiltonOn (oscillator m w) (path m w J phase) Set.univ := by
114
+ intro t _
115
+ have hn : Real.sqrt (m*w) ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr (mul_pos positiveM positiveW))
116
+ have hs := Real.sq_sqrt (mul_nonneg positiveM.le positiveW.le)
117
+ apply Prod.ext <;> ext i <;> fin_cases i
118
+ · change Real.sqrt (2*J)/Real.sqrt (m*w)*w*Real.cos (phase+w*t)=
119
+ Real.sqrt (2*J)*Real.sqrt (m*w)*Real.cos (phase+w*t)/m
120
+ field_simp [hn,positiveM.ne']
121
+ rw [hs]
122
+ ring
123
+ · change -Real.sqrt (2*J)*Real.sqrt (m*w)*w*Real.sin (phase+w*t)=
124
+ -(m*w^2*(Real.sqrt (2*J)/Real.sqrt (m*w)*Real.sin (phase+w*t)))
125
+ field_simp [hn]
126
+ rw [hs]
127
+ ring
128
+
129
+ /-- The action really equals (2*pi)^(-1) times the p dq contour integral. -/
130
+ theorem contour_action (scale J : ℝ) (nonzero : scale ≠ 0) (positive : 0≤J) :
131
+ (∫ theta in (0:ℝ)..2*Real.pi,
132
+ (chart scale (![theta],![J])).2 0*
133
+ (Real.sqrt (2*J)/scale*Real.cos theta))=2*Real.pi*J := by
134
+ have hr := Real.sq_sqrt (mul_nonneg (by norm_num : (0:ℝ)≤2) positive)
135
+ have hf (theta : ℝ) : (chart scale (![theta],![J])).2 0*
136
+ (Real.sqrt (2*J)/scale*Real.cos theta)=2*J*(Real.cos theta)^2 := by
137
+ change (Real.sqrt (2*J)*scale*Real.cos theta)*
138
+ (Real.sqrt (2*J)/scale*Real.cos theta)=2*J*(Real.cos theta)^2
139
+ field_simp [nonzero]
140
+ linear_combination (Real.cos theta)^2*hr
141
+ simp_rw [hf]
142
+ rw [intervalIntegral.integral_const_mul,integral_cos_sq]
143
+ simp
144
+ <;> ring
145
+
146
+ end
147
+ end PhysHorizon.ActionAngle
@@ -0,0 +1,237 @@
1
+ module
2
+
3
+ public import PhysHorizon.Bodies
4
+ public import Mathlib.MeasureTheory.Integral.IntervalIntegral.IntegrationByParts
5
+ public import Mathlib.Analysis.Calculus.Deriv.Prod
6
+ public import Mathlib.Analysis.Calculus.FDeriv.Pi
7
+
8
+ /-! Natural mechanics in any finite Cartesian dimension, actual partial
9
+ derivatives and exact coordinate constraints. The variational package below
10
+ uses an actual action integral for a quadratic natural Lagrangian. -/
11
+
12
+ @[expose] public section
13
+ namespace PhysHorizon.Analytical
14
+ noncomputable section
15
+ open PhysHorizon.Newton PhysHorizon.Mechanics Matrix Finset
16
+
17
+ def potentialDifferential {n} (g : Vec n) : Vec n →L[ℝ] ℝ :=
18
+ ((dotProductBilin ℝ ℝ) g).toContinuousLinearMap
19
+
20
+ def PotentialGradient {n} (U : Vec n → ℝ) (g : Vec n → Vec n) : Prop :=
21
+ ∀ x, HasFDerivAt U (potentialDifferential (g x)) x
22
+
23
+ def naturalL {n} (m : ℝ) (U : Vec n → ℝ) (x v : Vec n) :=
24
+ (m/2)*∑ i, (v i)^2-U x
25
+ def naturalH {n} (m : ℝ) (U : Vec n → ℝ) (x p : Vec n) :=
26
+ (1/(2*m))*∑ i, (p i)^2+U x
27
+
28
+ theorem lagrangian_velocity_partial {n} (m : ℝ) (U : Vec n → ℝ) (x v : Vec n) (i : Fin n) :
29
+ HasDerivAt (fun z => naturalL m U x (Function.update v i z)) (m*v i) (v i) := by
30
+ classical
31
+ have hf : (fun z => naturalL m U x (Function.update v i z))=
32
+ (fun z => (m/2)*(z^2+∑ j ∈ Finset.univ.erase i, (v j)^2)-U x) := by
33
+ funext z
34
+ unfold naturalL
35
+ congr 2
36
+ rw [← Finset.add_sum_erase _ _ (Finset.mem_univ i)]
37
+ simp only [Function.update_self]
38
+ congr 1
39
+ apply Finset.sum_congr rfl
40
+ intro j hj
41
+ rw [Function.update_of_ne (Finset.ne_of_mem_erase hj)]
42
+ rw [hf]
43
+ convert ((((hasDerivAt_id (v i)).pow 2).add_const (∑ j ∈ Finset.univ.erase i, (v j)^2)).const_mul (m/2)).sub_const (U x) using 1
44
+ · rfl
45
+ · simp; ring
46
+
47
+ theorem lagrangian_position_partial {n} (m : ℝ) (U : Vec n → ℝ) (g : Vec n → Vec n)
48
+ (h : PotentialGradient U g) (x v : Vec n) (i : Fin n) :
49
+ HasDerivAt (fun z => naturalL m U (Function.update x i z) v) (-g x i) (x i) := by
50
+ have hpath := hasFDerivAt_update (𝕜 := ℝ) (E := fun _ : Fin n => ℝ) x (i := i) (x i)
51
+ have houter : HasFDerivAt U (potentialDifferential (g x)) (Function.update x i (x i)) := by
52
+ simpa using h x
53
+ have hh := houter.comp (x i) hpath
54
+ have hd : HasDerivAt (fun z => U (Function.update x i z)) (g x i) (x i) := by
55
+ convert hh.hasDerivAt using 1
56
+ · rfl
57
+ · simp [potentialDifferential,ContinuousLinearMap.comp_apply,Pi.single_apply,
58
+ dotProductBilin,dotProduct,ite_apply,mul_ite]
59
+ have hp (j : Fin n) : (if j=i then ContinuousLinearMap.id ℝ ℝ else 0) (1:ℝ)=
60
+ if j=i then 1 else 0 := by split_ifs <;> simp
61
+ simp_rw [hp]
62
+ simp
63
+ convert hd.const_sub ((m/2)*∑ j, (v j)^2) using 1
64
+ rfl
65
+
66
+ def EulerLagrange {n} (m : ℝ) (g : Vec n → Vec n) (q : Motion n) : Prop :=
67
+ ∀ t i, HasDerivAt (fun s => m*q.velocity s i) (-g (q.position t) i) t
68
+
69
+ def Hamilton {n} (m : ℝ) (g : Vec n → Vec n) (q : Motion n) : Prop :=
70
+ (∀ t i, q.velocity t i=momentum m q t i/m) ∧
71
+ ∀ t i, HasDerivAt (fun s => momentum m q s i) (-g (q.position t) i) t
72
+
73
+ theorem newton_euler_lagrange {n} (m : ℝ) (g : Vec n → Vec n) (q : Motion n) :
74
+ SecondLaw m q (fun t => -g (q.position t)) ↔ EulerLagrange m g q := by
75
+ constructor
76
+ · intro law t i
77
+ exact momentum_derivative m q _ law t i
78
+ · intro h t i
79
+ exact ((q.velocity_derivative t i).const_mul m).unique (h t i)
80
+
81
+ theorem newton_hamilton {n} (m : ℝ) (hm : m ≠ 0) (g : Vec n → Vec n) (q : Motion n) :
82
+ SecondLaw m q (fun t => -g (q.position t)) ↔ Hamilton m g q := by
83
+ constructor
84
+ · intro h
85
+ refine ⟨?_,(newton_euler_lagrange m g q).mp h⟩
86
+ intro t i
87
+ simp [momentum,hm]
88
+ · intro h
89
+ exact (newton_euler_lagrange m g q).mpr h.2
90
+
91
+ theorem legendre_energy {n} (m : ℝ) (hm : m ≠ 0) (U : Vec n → ℝ) (x v : Vec n) :
92
+ (∑ i, (m*v i)*v i)-naturalL m U x v=naturalH m U x (fun i => m*v i) := by
93
+ simp only [naturalL,naturalH]
94
+ simp_rw [mul_pow]
95
+ rw [← Finset.mul_sum]
96
+ have hs : (∑ i, m*v i*v i)=m*∑ i, (v i)^2 := by
97
+ rw [Finset.mul_sum]
98
+ exact Finset.sum_congr rfl (fun i _ => by ring)
99
+ rw [hs]
100
+ field_simp
101
+ ring
102
+
103
+ theorem hamiltonian_position_partial {n} (m : ℝ) (U : Vec n → ℝ) (g : Vec n → Vec n)
104
+ (h : PotentialGradient U g) (x p : Vec n) (i : Fin n) :
105
+ HasDerivAt (fun z => naturalH m U (Function.update x i z) p) (g x i) (x i) := by
106
+ have hd := (lagrangian_position_partial 0 U g h x p i).neg
107
+ have he : (fun z => naturalH m U (Function.update x i z) p)=
108
+ (fun z => -naturalL 0 U (Function.update x i z) p+(1/(2*m))*∑ j, (p j)^2) := by
109
+ funext z
110
+ simp [naturalH,naturalL]
111
+ ring
112
+ rw [he]
113
+ simpa using hd.add_const ((1/(2*m))*∑ j, (p j)^2)
114
+
115
+ theorem hamiltonian_momentum_partial {n} (m : ℝ) (U : Vec n → ℝ) (x p : Vec n) (i : Fin n) :
116
+ HasDerivAt (fun z => naturalH m U x (Function.update p i z)) (p i/m) (p i) := by
117
+ have hd := lagrangian_velocity_partial (1/m) (fun _ => (0:ℝ)) x p i
118
+ have he : (fun z => naturalH m U x (Function.update p i z))=
119
+ (fun z => naturalL (1/m) (fun _ => (0:ℝ)) x (Function.update p i z)+U x) := by
120
+ funext z
121
+ simp only [naturalH,naturalL,sub_zero]
122
+ ring
123
+ rw [he]
124
+ simpa [div_eq_mul_inv,mul_comm] using hd.add_const (U x)
125
+
126
+ theorem potential_chain {n} (U : Vec n → ℝ) (g : Vec n → Vec n)
127
+ (h : PotentialGradient U g) (q : Motion n) (t : ℝ) :
128
+ HasDerivAt (fun s => U (q.position s)) (∑ i, g (q.position t) i*q.velocity t i) t := by
129
+ convert (h (q.position t)).comp_hasDerivAt t (hasDerivAt_pi.mpr (q.position_derivative t)) using 1
130
+ · rfl
131
+ · rfl
132
+
133
+ theorem conservative_energy {n} (m : ℝ) (U : Vec n → ℝ) (g : Vec n → Vec n)
134
+ (hU : PotentialGradient U g) (q : Motion n)
135
+ (h : SecondLaw m q (fun t => -g (q.position t))) (T : ℝ) :
136
+ ∀ t ∈ Set.Icc (0 : ℝ) T, kinetic m q t+U (q.position t)=kinetic m q 0+U (q.position 0) := by
137
+ apply energy_conservation m q _ h
138
+ intro t _
139
+ simpa [power,Finset.sum_neg_distrib] using potential_chain U g hU q t
140
+
141
+ /-- Linear holonomic constraints and their tangent virtual displacements.
142
+ Multiplier reaction annihilates every admissible displacement by theorem. -/
143
+ def Constraint {n r} (A : Matrix (Fin r) (Fin n) ℝ) (b : Vec r) (x : Vec n) : Prop := A*ᵥx=b
144
+ def Tangent {n r} (A : Matrix (Fin r) (Fin n) ℝ) (δ : Vec n) : Prop := A*ᵥδ=0
145
+ def Reaction {n r} (A : Matrix (Fin r) (Fin n) ℝ) (multipliers : Vec r) : Vec n := A.transpose*ᵥmultipliers
146
+
147
+ theorem ideal_reaction {n r} (A : Matrix (Fin r) (Fin n) ℝ) (multipliers : Vec r) (δ : Vec n)
148
+ (h : Tangent A δ) : Reaction A multipliers ⬝ᵥ δ=0 := by
149
+ rw [Reaction,mulVec_transpose,← dotProduct_mulVec]
150
+ simp [show A*ᵥδ=0 from h]
151
+
152
+ theorem dalembert {n r} (m : ℝ) (a F : Vec n) (A : Matrix (Fin r) (Fin n) ℝ) (multipliers : Vec r)
153
+ (h : ∀ i, m*a i=F i+Reaction A multipliers i) (δ : Vec n) (hd : Tangent A δ) :
154
+ (fun i => F i-m*a i) ⬝ᵥ δ=0 := by
155
+ have he : (fun i => F i-m*a i) = -Reaction A multipliers := by
156
+ funext i
157
+ have hi := h i
158
+ dsimp
159
+ linarith
160
+ rw [he,neg_dotProduct,ideal_reaction A multipliers δ hd,neg_zero]
161
+
162
+ theorem cyclic_momentum {n} (m : ℝ) (g : Vec n → Vec n) (q : Motion n)
163
+ (h : EulerLagrange m g q) (i : Fin n) (a b : ℝ)
164
+ (cyclic : ∀ t ∈ Set.Icc a b, g (q.position t) i=0) :
165
+ ∀ t ∈ Set.Icc a b, momentum m q t i=momentum m q a i := by
166
+ apply constant_of_has_deriv_right_zero
167
+ · intro t _
168
+ exact (h t i).continuousAt.continuousWithinAt
169
+ · intro t ht
170
+ simpa [momentum,cyclic t ⟨ht.1,ht.2.le⟩] using (h t i).hasDerivWithinAt (s := Set.Ici t)
171
+
172
+ open MeasureTheory intervalIntegral
173
+
174
+ def quadraticAction (m k : ℝ) (x v : ℝ → ℝ) (a b : ℝ) :=
175
+ ∫ t in a..b, m/2*(v t)^2-k/2*(x t)^2
176
+
177
+ def firstVariation (m k : ℝ) (x v η dη : ℝ → ℝ) (a b : ℝ) :=
178
+ ∫ t in a..b, m*v t*dη t-k*x t*η t
179
+
180
+ theorem action_expansion (m k : ℝ) (x v η dη : ℝ → ℝ) (a b ε : ℝ)
181
+ (hx : Continuous x) (hv : Continuous v) (hη : Continuous η) (hdη : Continuous dη) :
182
+ quadraticAction m k (fun t => x t+ε*η t) (fun t => v t+ε*dη t) a b=
183
+ quadraticAction m k x v a b+ε*firstVariation m k x v η dη a b+
184
+ ε^2*quadraticAction m k η dη a b := by
185
+ have h0 : IntervalIntegrable (fun t => m/2*(v t)^2-k/2*(x t)^2) volume a b :=
186
+ ((continuous_const.mul (hv.pow 2)).sub (continuous_const.mul (hx.pow 2))).intervalIntegrable a b
187
+ have h1 : IntervalIntegrable (fun t => ε*(m*v t*dη t-k*x t*η t)) volume a b :=
188
+ (continuous_const.mul (((continuous_const.mul hv).mul hdη).sub ((continuous_const.mul hx).mul hη))).intervalIntegrable a b
189
+ have h2 : IntervalIntegrable (fun t => ε^2*(m/2*(dη t)^2-k/2*(η t)^2)) volume a b :=
190
+ (continuous_const.mul ((continuous_const.mul (hdη.pow 2)).sub (continuous_const.mul (hη.pow 2)))).intervalIntegrable a b
191
+ unfold quadraticAction firstVariation
192
+ have he : (fun t => m/2*(v t+ε*dη t)^2-k/2*(x t+ε*η t)^2)=
193
+ (fun t => (m/2*(v t)^2-k/2*(x t)^2)+ε*(m*v t*dη t-k*x t*η t)+
194
+ ε^2*(m/2*(dη t)^2-k/2*(η t)^2)) := by funext t; ring
195
+ rw [he,integral_add (h0.add h1) h2,integral_add h0 h1,intervalIntegral.integral_const_mul,intervalIntegral.integral_const_mul]
196
+
197
+ theorem action_derivative (m k : ℝ) (x v η dη : ℝ → ℝ) (a b : ℝ)
198
+ (hx : Continuous x) (hv : Continuous v) (hη : Continuous η) (hdη : Continuous dη) :
199
+ HasDerivAt (fun ε => quadraticAction m k (fun t => x t+ε*η t)
200
+ (fun t => v t+ε*dη t) a b) (firstVariation m k x v η dη a b) 0 := by
201
+ have he : (fun ε => quadraticAction m k (fun t => x t+ε*η t) (fun t => v t+ε*dη t) a b)=
202
+ (fun ε => quadraticAction m k x v a b+ε*firstVariation m k x v η dη a b+
203
+ ε^2*quadraticAction m k η dη a b) := funext (fun ε => action_expansion m k x v η dη a b ε hx hv hη hdη)
204
+ rw [he]
205
+ convert (((hasDerivAt_id (0:ℝ)).mul_const (firstVariation m k x v η dη a b)).const_add (quadraticAction m k x v a b)).add
206
+ (((hasDerivAt_id (0:ℝ)).pow 2).mul_const (quadraticAction m k η dη a b)) using 1
207
+ · rfl
208
+ · norm_num
209
+
210
+ theorem hamilton_stationary (m k : ℝ) (q : Motion 1) (η dη : ℝ → ℝ) (a b : ℝ)
211
+ (hη : ∀ t, HasDerivAt η (dη t) t) (hdη : Continuous dη)
212
+ (ha : Continuous (fun t => q.acceleration t 0))
213
+ (law : ∀ t, m*q.acceleration t 0= -k*q.position t 0)
214
+ (endpoints : η a=0 ∧ η b=0) :
215
+ HasDerivAt (fun ε => quadraticAction m k (fun t => q.position t 0+ε*η t)
216
+ (fun t => q.velocity t 0+ε*dη t) a b) 0 0 := by
217
+ have hx : Continuous (fun t => q.position t 0) := continuous_iff_continuousAt.mpr (fun t => (q.position_derivative t 0).continuousAt)
218
+ have hv : Continuous (fun t => q.velocity t 0) := continuous_iff_continuousAt.mpr (fun t => (q.velocity_derivative t 0).continuousAt)
219
+ have he : Continuous η := continuous_iff_continuousAt.mpr (fun t => (hη t).continuousAt)
220
+ have hib := integral_deriv_mul_eq_sub_of_hasDerivAt
221
+ ((continuous_const.mul hv).continuousOn) he.continuousOn
222
+ (fun t _ => (q.velocity_derivative t 0).const_mul m) (fun t _ => hη t)
223
+ ((continuous_const.mul ha).intervalIntegrable a b) (hdη.intervalIntegrable a b)
224
+ simp only [Pi.mul_apply] at hib
225
+ have hz : firstVariation m k (fun t => q.position t 0) (fun t => q.velocity t 0) η dη a b=0 := by
226
+ unfold firstVariation
227
+ have hh : (fun t => m*q.acceleration t 0*η t+(m*q.velocity t 0)*dη t)=
228
+ (fun t => m*q.velocity t 0*dη t-k*q.position t 0*η t) := by
229
+ funext t
230
+ rw [law t]
231
+ ring
232
+ rw [hh,endpoints.1,endpoints.2] at hib
233
+ simpa using hib
234
+ simpa [hz] using action_derivative m k (fun t => q.position t 0) (fun t => q.velocity t 0) η dη a b hx hv he hdη
235
+
236
+ end
237
+ end PhysHorizon.Analytical
@@ -0,0 +1,198 @@
1
+ module
2
+
3
+ public import PhysHorizon.Modeling
4
+ public import PhysHorizon.Contracts
5
+ public import PhysHorizon.Probability
6
+ public import Mathlib.Analysis.ODE.Gronwall
7
+
8
+ @[expose] public section
9
+
10
+ namespace PhysHorizon.Approximation
11
+
12
+ /-- Monotone affine error transformers, ordered in execution order. -/
13
+ structure Transformer where
14
+ gain : ℝ
15
+ offset : ℝ
16
+ gain_nonneg : 0 ≤ gain
17
+ offset_nonneg : 0 ≤ offset
18
+
19
+ def Transformer.apply (t : Transformer) (e : ℝ) := t.gain * e + t.offset
20
+ def Transformer.identity : Transformer := ⟨1, 0, by norm_num, le_rfl⟩
21
+ def Transformer.then (f g : Transformer) : Transformer :=
22
+ ⟨g.gain*f.gain, g.gain*f.offset+g.offset, mul_nonneg g.gain_nonneg f.gain_nonneg,
23
+ add_nonneg (mul_nonneg g.gain_nonneg f.offset_nonneg) g.offset_nonneg⟩
24
+
25
+ theorem Transformer.then_apply (f g : Transformer) (e : ℝ) :
26
+ (f.then g).apply e = g.apply (f.apply e) := by simp [Transformer.then, Transformer.apply]; ring
27
+
28
+ theorem Transformer.monotone (f : Transformer) : Monotone f.apply := by
29
+ intro a b h
30
+ exact add_le_add (mul_le_mul_of_nonneg_left h f.gain_nonneg) le_rfl
31
+
32
+ theorem Transformer.assoc (f g h : Transformer) : (f.then g).then h = f.then (g.then h) := by
33
+ cases f; cases g; cases h
34
+ simp only [Transformer.then, Transformer.mk.injEq]
35
+ constructor <;> ring
36
+
37
+ theorem Transformer.identity_left (f : Transformer) : Transformer.identity.then f = f := by
38
+ cases f; simp [Transformer.then, Transformer.identity]
39
+
40
+ theorem Transformer.identity_right (f : Transformer) : f.then Transformer.identity = f := by
41
+ cases f; simp [Transformer.then, Transformer.identity]
42
+
43
+ def Bound {X Y : Type*} [PseudoMetricSpace Y] (D : Set X) (f g : X → Y) (ε : ℝ) : Prop :=
44
+ ∀ x ∈ D, dist (f x) (g x) ≤ ε
45
+
46
+ theorem restrict {X Y : Type*} [PseudoMetricSpace Y] {D E : Set X} {f g : X → Y} {ε : ℝ}
47
+ (h : Bound D f g ε) (hED : E ⊆ D) : Bound E f g ε := fun x hx => h x (hED hx)
48
+
49
+ theorem triangle {X Y : Type*} [PseudoMetricSpace Y] {D : Set X} {f g h : X → Y} {ε δ : ℝ}
50
+ (hfg : Bound D f g ε) (hgh : Bound D g h δ) : Bound D f h (ε+δ) := by
51
+ intro x hx
52
+ exact (dist_triangle _ (g x) _).trans (add_le_add (hfg x hx) (hgh x hx))
53
+
54
+ /-- Image coverage is explicit; q and r may themselves differ by delta. -/
55
+ theorem transport {X Y Z : Type*} [PseudoMetricSpace Y] [PseudoMetricSpace Z]
56
+ {D : Set X} {C : Set Y} {f g : X → Y} {q r : Y → Z} {ε L δ : ℝ}
57
+ (h : Bound D f g ε) (hL : 0 ≤ L)
58
+ (coverF : ∀ x ∈ D, f x ∈ C) (coverG : ∀ x ∈ D, g x ∈ C)
59
+ (lip : ∀ a ∈ C, ∀ b ∈ C, dist (q a) (q b) ≤ L*dist a b)
60
+ (approx : Bound C q r δ) : Bound D (q ∘ f) (r ∘ g) (L*ε+δ) := by
61
+ intro x hx
62
+ calc
63
+ dist (q (f x)) (r (g x)) ≤ dist (q (f x)) (q (g x)) + dist (q (g x)) (r (g x)) := dist_triangle _ _ _
64
+ _ ≤ L*dist (f x) (g x) + δ := add_le_add (lip _ (coverF x hx) _ (coverG x hx)) (approx _ (coverG x hx))
65
+ _ ≤ L*ε+δ := add_le_add (mul_le_mul_of_nonneg_left (h x hx) hL) le_rfl
66
+
67
+ theorem cover {X Y : Type*} [PseudoMetricSpace Y] {D : Set X} {cells : List (Set X)}
68
+ {f g : X → Y} {ε : ℝ} (coverage : ∀ x ∈ D, ∃ C ∈ cells, x ∈ C)
69
+ (localBounds : ∀ C ∈ cells, Bound C f g ε) : Bound D f g ε := by
70
+ intro x hx
71
+ obtain ⟨C, hC, hxC⟩ := coverage x hx
72
+ exact localBounds C hC x hxC
73
+
74
+ /-- Proof rules are indexed by their exact domain, maps and error. Output types
75
+ may change at transport nodes; leaf proofs are the only trusted entry points. -/
76
+ inductive Derivation (X : Type) : {Y : Type} → [PseudoMetricSpace Y] →
77
+ (D : Set X) → (f g : X → Y) → (ε : ℝ) → Prop where
78
+ | leaf {Y : Type} [PseudoMetricSpace Y] {D : Set X} {f g : X → Y} {ε : ℝ} :
79
+ Bound D f g ε → Derivation X D f g ε
80
+ | triangle {Y : Type} [PseudoMetricSpace Y] {D : Set X} {f g h : X → Y} {ε δ : ℝ} :
81
+ Derivation X D f g ε → Derivation X D g h δ → Derivation X D f h (ε+δ)
82
+ | restrict {Y : Type} [PseudoMetricSpace Y] {D E : Set X} {f g : X → Y} {ε : ℝ} :
83
+ Derivation X D f g ε → E ⊆ D → Derivation X E f g ε
84
+ | weaken {Y : Type} [PseudoMetricSpace Y] {D : Set X} {f g : X → Y} {ε δ : ℝ} :
85
+ Derivation X D f g ε → ε ≤ δ → Derivation X D f g δ
86
+ | transport {Y Z : Type} [PseudoMetricSpace Y] [PseudoMetricSpace Z]
87
+ {D : Set X} {C : Set Y} {f g : X → Y} {q r : Y → Z} {ε L δ : ℝ} :
88
+ Derivation X D f g ε → 0 ≤ L →
89
+ (∀ x ∈ D, f x ∈ C) → (∀ x ∈ D, g x ∈ C) →
90
+ (∀ a ∈ C, ∀ b ∈ C, dist (q a) (q b) ≤ L*dist a b) →
91
+ Bound C q r δ → Derivation X D (q ∘ f) (r ∘ g) (L*ε+δ)
92
+ | cover {Y : Type} [PseudoMetricSpace Y] {D : Set X} {cells : List (Set X)}
93
+ {f g : X → Y} {ε : ℝ} :
94
+ (∀ x ∈ D, ∃ C ∈ cells, x ∈ C) →
95
+ (∀ C ∈ cells, Derivation X C f g ε) → Derivation X D f g ε
96
+
97
+ theorem Derivation.sound {X Y : Type} [PseudoMetricSpace Y] {D : Set X}
98
+ {f g : X → Y} {ε : ℝ} (d : Derivation X D f g ε) : Bound D f g ε := by
99
+ induction d with
100
+ | leaf h => exact h
101
+ | triangle _ _ ih₁ ih₂ => exact PhysHorizon.Approximation.triangle ih₁ ih₂
102
+ | restrict _ sub ih => exact PhysHorizon.Approximation.restrict ih sub
103
+ | weaken _ le ih => exact fun x hx => (ih x hx).trans le
104
+ | transport _ hL hf hg lip approx ih => exact PhysHorizon.Approximation.transport ih hL hf hg lip approx
105
+ | cover coverage _ ih => exact PhysHorizon.Approximation.cover coverage ih
106
+
107
+ /-- Embed certified maps into the same realization semantics used by model builders. -/
108
+ def mapModel {X Y : Type*} (D : Set X) (f : X → Y) : Modeling.Model X Y :=
109
+ ⟨fun _ => ⟨id, fun _ _ => True⟩, .truth, fun _ => True, fun x => x ∈ D,
110
+ fun _ => True, f⟩
111
+
112
+ theorem mapModel_realizes {X Y : Type*} (D : Set X) (f : X → Y) (x : X) :
113
+ (mapModel D f).realizes x ↔ x ∈ D := by
114
+ simp [Modeling.Model.realizes, mapModel, Scenario.denote]
115
+
116
+ def modelApproximation {X Y : Type*} [PseudoMetricSpace Y] {D : Set X}
117
+ {f g : X → Y} {ε : ℝ} (h : Bound D f g ε) :
118
+ Modeling.Approximation (mapModel D f) (mapModel D g) ε where
119
+ relation x y := x = y
120
+ total x hx := ⟨x, (mapModel_realizes D g x).mpr ((mapModel_realizes D f x).mp hx), rfl⟩
121
+ bound x y hx _ hxy := by
122
+ subst y
123
+ exact h x ((mapModel_realizes D f x).mp hx)
124
+
125
+ /-- Probabilistic transport keeps the shared event space; no independence claim. -/
126
+ theorem probabilistic_transport {Ω Y Z : Type*} [PseudoMetricSpace Y] [PseudoMetricSpace Z]
127
+ {f g : Ω → Y} {q r : Y → Z} {A B : Set Ω} {ε L δ : ℝ}
128
+ (hfg : ∀ x, x ∉ A → dist (f x) (g x) ≤ ε) (hL : 0 ≤ L)
129
+ (lip : ∀ a b, dist (q a) (q b) ≤ L*dist a b)
130
+ (hqr : ∀ x, x ∉ B → dist (q (g x)) (r (g x)) ≤ δ) :
131
+ ∀ x, x ∉ A ∪ B → dist (q (f x)) (r (g x)) ≤ L*ε+δ := by
132
+ intro x hx
133
+ exact (dist_triangle _ (q (g x)) _).trans (add_le_add
134
+ ((lip _ _).trans (mul_le_mul_of_nonneg_left (hfg x (fun ha => hx (Or.inl ha))) hL))
135
+ (hqr x (fun hb => hx (Or.inr hb))))
136
+
137
+ /-- Combine metric transport with an actual measure bound on its failure event. -/
138
+ theorem probabilistic_failure {Ω Y Z : Type*} [MeasurableSpace Ω]
139
+ [PseudoMetricSpace Y] [PseudoMetricSpace Z]
140
+ (μ : MeasureTheory.Measure Ω) [MeasureTheory.IsProbabilityMeasure μ]
141
+ {f g : Ω → Y} {q r : Y → Z} {A B : Set Ω} {ε L δ pA pB : ℝ}
142
+ (hfg : ∀ x, x ∉ A → dist (f x) (g x) ≤ ε) (hL : 0 ≤ L)
143
+ (lip : ∀ a b, dist (q a) (q b) ≤ L*dist a b)
144
+ (hqr : ∀ x, x ∉ B → dist (q (g x)) (r (g x)) ≤ δ)
145
+ (hA : μ.real A ≤ pA) (hB : μ.real B ≤ pB) :
146
+ μ.real {x | L*ε+δ < dist (q (f x)) (r (g x))} ≤ pA+pB := by
147
+ have sub : {x | L*ε+δ < dist (q (f x)) (r (g x))} ⊆ A ∪ B := by
148
+ intro x hx
149
+ by_contra hn
150
+ exact (not_lt_of_ge (probabilistic_transport hfg hL lip hqr x hn)) hx
151
+ exact (MeasureTheory.measureReal_mono sub).trans
152
+ (PhysHorizon.Probability.union_failure μ A B pA pB hA hB)
153
+
154
+ /-- General nonlinear real observables: certify their derivative on an interval. -/
155
+ theorem derivative_transport {q : ℝ → ℝ} {a b L : ℝ}
156
+ (hq : ∀ x ∈ Set.Icc a b, DifferentiableAt ℝ q x)
157
+ (hd : ∀ x ∈ Set.Icc a b, ‖deriv q x‖ ≤ L) :
158
+ ∀ x ∈ Set.Icc a b, ∀ y ∈ Set.Icc a b, |q x-q y| ≤ L*|x-y| := by
159
+ intro x hx y hy
160
+ simpa only [Real.norm_eq_abs] using
161
+ (convex_Icc a b).norm_image_sub_le_of_norm_deriv_le hq hd hy hx
162
+
163
+ /-- Horner recursion proves arbitrary finite polynomial sensitivity. -/
164
+ theorem horner_lipschitz (a x y u v L M : ℝ)
165
+ (_hL : 0 ≤ L) (hy : |y| ≤ M) (hu : |u| ≤ L)
166
+ (hv : |u-v| ≤ L*|x-y|) :
167
+ |(a+x*u)-(a+y*v)| ≤ (L+M*L)*|x-y| := by
168
+ have he : (a+x*u)-(a+y*v) = (x-y)*u+y*(u-v) := by ring
169
+ rw [he]
170
+ calc
171
+ |(x-y)*u+y*(u-v)| ≤ |(x-y)*u|+|y*(u-v)| := abs_add_le _ _
172
+ _ = |x-y| * |u| + |y| * |u-v| := by rw [abs_mul, abs_mul]
173
+ _ ≤ |x-y| * L+M*(L * |x-y|) := add_le_add
174
+ (mul_le_mul_of_nonneg_left hu (abs_nonneg _))
175
+ (mul_le_mul hy hv (abs_nonneg _) (le_trans (abs_nonneg _) hy))
176
+ _ = (L+M*L) * |x-y| := by ring
177
+
178
+ /-- One-step stability plus local error yields a global accumulated bound. -/
179
+ theorem recurrence {e b L δ : ℕ → ℝ} (he : e 0 ≤ b 0)
180
+ (hL : ∀ n, 0 ≤ L n) (step : ∀ n, e (n+1) ≤ L n * e n + δ n)
181
+ (budget : ∀ n, b (n+1) = L n * b n + δ n) : ∀ n, e n ≤ b n := by
182
+ intro n
183
+ induction n with
184
+ | zero => exact he
185
+ | succ n ih =>
186
+ rw [budget n]
187
+ exact (step n).trans (add_le_add (mul_le_mul_of_nonneg_left ih (hL n)) le_rfl)
188
+
189
+ /-- Vector-valued dynamical error, with all continuity/derivative obligations exposed. -/
190
+ theorem dynamical_bound {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]
191
+ {f df : ℝ → E} {δ K ε a b : ℝ}
192
+ (hf : ContinuousOn f (Set.Icc a b))
193
+ (hd : ∀ x ∈ Set.Ico a b, HasDerivWithinAt f (df x) (Set.Ici x) x)
194
+ (initial : ‖f a‖ ≤ δ) (residual : ∀ x ∈ Set.Ico a b, ‖df x‖ ≤ K*‖f x‖+ε) :
195
+ ∀ x ∈ Set.Icc a b, ‖f x‖ ≤ gronwallBound δ K ε (x-a) :=
196
+ norm_le_gronwallBound_of_norm_deriv_right_le hf hd initial residual
197
+
198
+ end PhysHorizon.Approximation
@@ -0,0 +1,48 @@
1
+ module
2
+
3
+ public import Lean
4
+ import Lean.Util.CollectAxioms
5
+
6
+ /-! Exact theorem-type equality, explicit kernel rechecking and an axiom allowlist.
7
+ The certificate is emitted only after these checks. -/
8
+
9
+ public meta section
10
+
11
+ open Lean Meta Elab Command
12
+
13
+ elab "physhorizon_audit " theoremName:ident " expected " expectedName:ident " binding " binding:str : command => do
14
+ let name ← liftCoreM <| realizeGlobalConstNoOverloadWithInfo theoremName
15
+ let expected ← liftCoreM <| realizeGlobalConstNoOverloadWithInfo expectedName
16
+ let result ← liftTermElabM do
17
+ let info ← getConstInfo name
18
+ let .thmInfo theoremInfo := info | throwError "PhysHorizon requires a theorem declaration"
19
+ let expectedInfo ← getConstInfo expected
20
+ let expectedExpr := mkConst expected (expectedInfo.levelParams.map Level.param)
21
+ unless ← isProp expectedExpr do
22
+ throwError "Expected statement must be a closed proposition"
23
+ let env ← getEnv
24
+ let lctx ← getLCtx
25
+ match Kernel.isDefEq env lctx theoremInfo.type expectedExpr with
26
+ | .ok true => pure ()
27
+ | _ => throwError "PhysHorizon theorem type does not match the expected statement"
28
+ match Kernel.check env lctx theoremInfo.value with
29
+ | .error error => throwError "PhysHorizon kernel rejected proof: {error.toMessageData (← getOptions)}"
30
+ | .ok inferred =>
31
+ match Kernel.isDefEq env lctx inferred theoremInfo.type with
32
+ | .ok true => pure ()
33
+ | _ => throwError "PhysHorizon proof has an unexpected kernel type"
34
+ let axioms ← Lean.collectAxioms name
35
+ let allowed := #[``propext, ``Classical.choice, ``Quot.sound]
36
+ for axiomName in axioms do
37
+ unless allowed.contains axiomName do
38
+ throwError "PhysHorizon forbidden axiom: {axiomName}"
39
+ let statement ← ppExpr (← withTransparency .all <| whnf expectedExpr)
40
+ pure <| Json.mkObj [
41
+ ("format", toJson "physhorizon.lean.audit/0.1"),
42
+ ("theorem", toJson name.toString),
43
+ ("expected_declaration", toJson expected.toString),
44
+ ("statement", toJson statement.pretty),
45
+ ("axioms", toJson (axioms.map Name.toString)),
46
+ ("binding_digest", toJson binding.getString),
47
+ ("kernel_checked", toJson true)]
48
+ logInfo ("PHYSHORIZON_CERTIFICATE " ++ result.compress)