4343
4444from __future__ import annotations
4545
46- import numpy as np
47- from scipy .integrate import solve_ivp
48- from scipy .linalg import expm
49- import matplotlib .pyplot as plt
50- import matplotlib .gridspec as gridspec
46+ import numpy as np # type: ignore
47+ from scipy .integrate import solve_ivp # type: ignore
48+ from scipy .linalg import expm # type: ignore
49+ import matplotlib .pyplot as plt # type: ignore
50+ import matplotlib .gridspec as gridspec # type: ignore
5151from typing import Callable
5252
5353
@@ -163,7 +163,7 @@ def _is_new_point(self, x: np.ndarray) -> bool:
163163 if not self .points :
164164 return True
165165 dists = [np .linalg .norm (x - s ) for s in self .points ]
166- return min (dists ) > self .delta
166+ return bool ( np . min (dists ) > self .delta )
167167
168168 def _add_point (self , x : np .ndarray ):
169169 A = self .jac (x ) # Jacobian at x
@@ -420,13 +420,13 @@ def sa(ax, title, xl="", yl=""):
420420# ============================================================
421421
422422def run_demo (system , system_name , x0_train , x0_test , t_end ,
423- delta_train , state_labels , n_steps = 800 , output_path = None ):
423+ delta_train , state_labels , n_steps = 800 , output_path = None ):
424424 """
425425 Full TPWL workflow:
426- 1. Train on a representative trajectory from x0_train.
427- 2. Simulate TPWL (nearest + Gaussian) from x0_test.
428- 3. Simulate true nonlinear ODE from x0_test for comparison.
429- 4. Plot and report errors.
426+ 1. Train on a representative trajectory from x0_train.
427+ 2. Simulate TPWL (nearest + Gaussian) from x0_test.
428+ 3. Simulate true nonlinear ODE from x0_test for comparison.
429+ 4. Plot and report errors.
430430 """
431431 print (f"\n { '=' * 60 } " )
432432 print (f" { system_name } " )
@@ -438,12 +438,12 @@ def run_demo(system, system_name, x0_train, x0_test, t_end,
438438
439439 # ---- 1. True nonlinear solution (training trajectory) ----
440440 sol_train = solve_ivp (lambda t , x : system .f (x ), t_span ,
441- x0_train , t_eval = t_eval , ** ivp_kw )
441+ x0_train , t_eval = t_eval , ** ivp_kw ) # type: ignore
442442 assert sol_train .success , sol_train .message
443443
444444 # ---- 2. Train TPWL (nearest) ----
445445 model_near = TPWL (system .f , system .jac , delta = delta_train ,
446- weighting = "nearest" )
446+ weighting = "nearest" )
447447 model_near .train (x0_train , t_span , t_eval , ** ivp_kw )
448448 model_near .print_summary ()
449449
@@ -454,7 +454,7 @@ def run_demo(system, system_name, x0_train, x0_test, t_end,
454454
455455 # ---- 4. True nonlinear solution from test initial condition ----
456456 sol_true = solve_ivp (lambda t , x : system .f (x ), t_span ,
457- x0_test , t_eval = t_eval , ** ivp_kw )
457+ x0_test , t_eval = t_eval , ** ivp_kw ) # type: ignore
458458 assert sol_true .success , sol_true .message
459459
460460 # ---- 5. TPWL simulations from test IC ----
@@ -464,8 +464,8 @@ def run_demo(system, system_name, x0_train, x0_test, t_end,
464464 method = "RK45" , rtol = 1e-8 , atol = 1e-10 )
465465
466466 x_true = sol_true .y .T
467- x_near = sol_near .y .T if sol_near .success else np .full_like (x_true , np .nan )
468- x_gauss = sol_gauss .y .T if sol_gauss .success else np .full_like (x_true , np .nan )
467+ x_near = sol_near .y .T if sol_near .success else np .full_like (x_true , np .nan ) # type: ignore
468+ x_gauss = sol_gauss .y .T if sol_gauss .success else np .full_like (x_true , np .nan ) # type: ignore
469469
470470 err_near = np .linalg .norm (x_near - x_true , axis = 1 )
471471 err_gauss = np .linalg .norm (x_gauss - x_true , axis = 1 )
@@ -513,7 +513,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
513513 ivp_kw = dict (method = "RK45" , rtol = 1e-9 , atol = 1e-11 )
514514
515515 sol_true = solve_ivp (lambda t , x : system .f (x ), t_span , x0_test ,
516- t_eval = t_eval , ** ivp_kw )
516+ t_eval = t_eval , ** ivp_kw ) # type: ignore
517517 x_true = sol_true .y .T
518518
519519 results = []
@@ -522,7 +522,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
522522 m .train (x0_train , t_span , t_eval , ** ivp_kw )
523523 sol = m .simulate (x0_test , t_span , t_eval , method = "RK45" ,
524524 rtol = 1e-8 , atol = 1e-10 )
525- x_approx = sol .y .T if sol .success else np .full_like (x_true , np .nan )
525+ x_approx = sol .y .T if sol .success else np .full_like (x_true , np .nan ) # type: ignore
526526 err = np .linalg .norm (x_approx - x_true , axis = 1 ).mean ()
527527 results .append ((delta , len (m .points ), err ))
528528 print (f" delta={ delta :.3f} → { len (m .points ):3d} pts "
@@ -553,7 +553,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
553553
554554 fig .suptitle (f"TPWL: delta sensitivity — { system_name } " ,
555555 color = "#e6edf3" , fontsize = 12 , fontweight = "bold" )
556- plt .tight_layout (rect = [0 , 0 , 1 , 0.95 ])
556+ plt .tight_layout (rect = [0 , 0 , 1 , 0.95 ]) # type: ignore
557557 if output_path :
558558 fig .savefig (output_path , dpi = 150 , bbox_inches = "tight" ,
559559 facecolor = "#0f1117" )
@@ -578,7 +578,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
578578 delta_train = 0.4 ,
579579 state_labels = ["x₁" , "x₂" ],
580580 n_steps = 800 ,
581- output_path = "/mnt/user-data/outputs/ tpwl_vanderpol.png" ,
581+ output_path = "tpwl_vanderpol.png" ,
582582 )
583583
584584 delta_sensitivity (
@@ -589,7 +589,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
589589 t_end = 15.0 ,
590590 deltas = [2.0 , 1.0 , 0.6 , 0.4 , 0.25 , 0.15 , 0.08 ],
591591 state_labels = ["x₁" , "x₂" ],
592- output_path = "/mnt/user-data/outputs/ tpwl_delta_sensitivity.png" ,
592+ output_path = "tpwl_delta_sensitivity.png" ,
593593 )
594594
595595 # ---- Example 2: Lorenz (chaotic) ----
@@ -603,7 +603,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
603603 delta_train = 1.5 ,
604604 state_labels = ["x" , "y" , "z" ],
605605 n_steps = 800 ,
606- output_path = "/mnt/user-data/outputs/ tpwl_lorenz.png" ,
606+ output_path = "tpwl_lorenz.png" ,
607607 )
608608
609609 # ---- Example 3: Duffing oscillator (lightly damped, double-well) ----
@@ -619,7 +619,7 @@ def delta_sensitivity(system, system_name, x0_train, x0_test,
619619 delta_train = 0.35 ,
620620 state_labels = ["x₁ (displacement)" , "x₂ (velocity)" ],
621621 n_steps = 800 ,
622- output_path = "/mnt/user-data/outputs/ tpwl_duffing.png" ,
622+ output_path = "tpwl_duffing.png" ,
623623 )
624624
625- print ("\n All done. Output files written to /mnt/ user-data/outputs/" )
625+ print ("\n All done. Output files written to user-data/outputs/" )
0 commit comments