From 0d83abe45cff3fecdcab1f6104ae6abdbfcad187 Mon Sep 17 00:00:00 2001
From: Tim Richter <tim@cs.uni-potsdam.de>
Date: Sat, 1 Feb 2025 21:42:38 +0100
Subject: [PATCH] add ApproxSolution for use in isValid, cleanup

---
 IVP.agda | 82 +++++++++++++++++++++++++++-----------------------------
 1 file changed, 40 insertions(+), 42 deletions(-)

diff --git a/IVP.agda b/IVP.agda
index 0c16839..bfe63e7 100644
--- a/IVP.agda
+++ b/IVP.agda
@@ -15,9 +15,11 @@ module IVP where
 infix 22 _^_
 infix 15 _*R^sR_
 infix 10 _+R^s_
+infix 22 _/ℝ_
 
 -- We need ℝ ^ s to describe a system of s ODE's as one time dependent function
--- (ℝ → (ℝ ^ s)) that takes the time t : ℝ and gives a vector in ℝ ^ s where each value corresponds to one ODE.
+-- (ℝ → (ℝ ^ s)) that takes the time t : ℝ and gives a vector in ℝ ^ s where each
+-- value corresponds to one ODE.
 
 _^_ : Set → ℕ → Set
 A ^ zero  = ⊤
@@ -27,23 +29,26 @@ postulate
   -- real numbers  with standard operations
   ℝ : Set
   0R : ℝ
-  ℝ+ : Set
+  -- ℝ+ : Set
   _+R_ : ℝ → ℝ → ℝ
   _*R_ : ℝ → ℝ → ℝ
-  _<R_ :  ℝ → ℝ → Set
-  _>R_ :  ℝ → ℝ → Set
+  _<R_ : ℝ → ℝ → Set
+  _>R_ : ℝ → ℝ → Set
+  _/ℝ_ : ℝ → ℝ → ℝ
+
+  -- We need a way to add and multiply natural numbers with real numbers, and divide
+  -- real numbers by natural numbers.
+  -- Converting natural numbers to real numbers and using the operations above is an
+  -- easy way to do so.
 
-  -- We need a way to add and multiply natural numbers with real numbers.
-  -- Converting natural numbers to real numbers and using the operations above is an easy way to do so.
   nToR : ℕ → ℝ
 
-  -- derivative
-  -- this is of course problematic: we postulate that any real function is everywhere differentiable
-  -- we might refine later
+  -- Derivative (This is of course problematic: we postulate that any real function
+  -- is everywhere differentiable; might refine later...)
   D : (ℝ → ℝ) → (ℝ → ℝ)
 
--- We define the derivative of vector valued functions by calculating the derivative for each element of the vector ℝ ^ s
--- to describe the derivative of ODE systems.
+-- We define the derivative of vector valued functions by calculating the derivative
+-- for each element of the vector ℝ ^ s to describe the derivative of ODE systems.
 
 Ds : {s : ℕ} → (ℝ → (ℝ ^ s)) → (ℝ → (ℝ ^ s))
 Ds {zero}  f x = tt
@@ -108,49 +113,42 @@ EulerMethod {s} ivp U k n = U +R^s k *R^sR (IVP.f ivp) (U , (nToR n) *R k)
 
 -- A solution method only approximates for 1 timestep.
 -- We need a method to calculate approximations for all timesteps beginning with t_0 until t_n.
-SolveIVP : {s : ℕ } → (ivp : IVP s) → SolutionMethod ivp → ℕ → ℝ → ℝ ^ s
-SolveIVP {s} ivp m zero k = IVP.η ivp
-SolveIVP {s} ivp m (suc n) k = m (SolveIVP ivp m n k) k n
+SolveIVP : {s : ℕ } → (ivp : IVP s) → SolutionMethod ivp → ℝ → ℕ → ℝ ^ s
+SolveIVP {s} ivp m k zero    = IVP.η ivp
+SolveIVP {s} ivp m k (suc n) = m (SolveIVP ivp m k n) k n
+
+-- Now, for any n ∈ ℕ and t > 0, we can choose step width to be t / n.
+-- SolveIVP {s} ivp m (t / n) n   will then be an approximation for the
+-- solution of the IVP at t (= n * (t / n)):
+
+ApproxSolution : {s : ℕ } → (ivp : IVP s) → SolutionMethod ivp → ℕ → ℝ → ℝ ^ s
+ApproxSolution ivp m n t = SolveIVP ivp m (t /ℝ (nToR n)) n
 
 -- We need to adjust the main condition of IVP to allow approximations with precision prec.
 ApproxMainCond : {s : ℕ} → (ℝ ^ s × ℝ → ℝ ^ s) → (ℝ → ℝ ^ s) → ℝ → ℝ → Set
 ApproxMainCond f u t prec = (distance  (Ds u t) (f (u t , t))) <R prec
 
--- TODO: Better explanation
--- A solution method can be called valid if:
--- a n:ℕ exists for all k:ℝ and all prec:ℝ
--- such that it finds approximations until t_n = n * k by using SolveIVP with a maximum precision prec.
+-- A solution method m (for initial value problem ivp) can be called valid if:
+-- for any t > 0 and any precision prec > 0 there exists n ∈ ℕ
+-- such that at t, ApproxSolution ivp m n  satisfies ApproxMainCond f u t prec
+
 isValid : {s : ℕ} → {ivp : IVP s } → SolutionMethod ivp → Set
-isValid {s} {ivp} m = (k : ℝ) → (prec : ℝ) → Σ ℕ λ n → ApproxMainCond (IVP.f ivp) (SolveIVP ivp m n) ((nToR n) *R k) prec
+isValid {s} {ivp} m = (t : ℝ) → (prec : ℝ) →
+                      Σ ℕ λ n → ApproxMainCond (IVP.f ivp) (ApproxSolution ivp m n) t prec
 
--- Unter welchen Bedingungen an f ist die EulerMethode valid?
+-- isValid is only concerned with (an approximated variant of) the main condition
+-- for Solutions (MainCond). This is justified by the observation that the
+-- initial data condition is automatically satisfied:
 
 -- (A): Proof that for every solution method SolveIVP fulfills the InitialDataCond.
 
--- Adjust InitialDataCond to work with a discretized time: 
+-- Adjust InitialDataCond to work with a discretized time:
 InitialDataCondℕ : {s : ℕ} → ℝ ^ s → (ℕ → ℝ ^ s) → Set
 InitialDataCondℕ η u = (u zero) ≡ η
 
--- The proof only works with changing the argument order of SolveIVP.
-SolveIVPℕ : {s : ℕ } → (ivp : IVP s) → SolutionMethod ivp → ℝ → ℕ → ℝ ^ s
-SolveIVPℕ {s} ivp m k zero = IVP.η ivp
-SolveIVPâ„• {s} ivp m k (suc n) = m (SolveIVPâ„• ivp m k n) k n
-
--- Proof for A:
-SolveIVPℕhasStartCond : {s : ℕ} → (ivp : IVP s) → (m : SolutionMethod ivp) → (k : ℝ) → InitialDataCondℕ (IVP.η ivp) (SolveIVPℕ ivp m k)
-SolveIVPâ„•hasStartCond {s} ivp m n = refl
-
--- Konsequenterweise könnte man jetzt auch MainCond und Solution auf die Zeit als n * k anpassen ????
--- Tim: Ja, wahrscheinlich. Denk' noch ein bisschen drüber nach, ich tue das auch...
-
---postulate
---  Dℕ :{s : ℕ} → (ℕ → ℝ ^ s) → (ℕ → ℝ ^ s)
- -- ∘N :
-
---Dsℕ : {s : ℕ} → (ℕ → (ℝ ^ s)) → (ℕ → (ℝ ^ s))
---Dsâ„• {zero}  f x = tt
---Dsℕ {suc s} f x = (Ds2 (proj₁ ∘N f) x) , (D2 (proj₂ ∘N f) x)
+-- Proof of A:
+SolveIVPhasStartCond : {s : ℕ} → (ivp : IVP s) → (m : SolutionMethod ivp) →
+                        (k : ℝ) → InitialDataCondℕ (IVP.η ivp) (SolveIVP ivp m k)
+SolveIVPhasStartCond {s} ivp m n = refl
 
---MainCondℕ : {s : ℕ} → (ℝ ^ s × ℕ → ℝ ^ s) → (ℕ → ℝ ^ s) → Set
---MainCondℕ f u = (n : ℕ) → Ds2 u n ≡ (f (u n , n))
 
-- 
GitLab