RustedSciThe 0.4.22

Rust framework for symbolic and numerical computing:BVP ( Newton-Raphson frozen/damped/with collocations ), IVP( BDF, Radau, Backward Euler, LSODE, LSODA, RK45, DoPri), nonlinear equations ( Levenberg, Gavin) and more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
// Port based on the Gavin implementation in `PY/levenberg_marquardt.py`.

use nalgebra::{DMatrix, DVector};
use std::error::Error;
use std::time::{Duration, Instant};

/// Owned source error type used by objective and Jacobian callbacks.
pub type GavinLmCallbackError = Box<dyn Error + Send + Sync + 'static>;

/// Input vector categories used by validation errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GavinLmInput {
    SampleLocations,
    Observations,
    InitialParameters,
}

/// Configurable scalar controls validated before solving.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GavinLmOption {
    GradientTolerance,
    ParameterStepTolerance,
    GainRatioThreshold,
    InitialDamping,
    DampingIncreaseFactor,
    DampingDecreaseFactor,
}

/// Numerical stages that can report non-finite or invalid intermediates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GavinLmNumericalStage {
    WeightedLinearization,
    TrialObjective,
    QuadraticDampingUpdate,
    QuadraticDampingScale,
    QuadraticTrialObjective,
    GainRatio,
    DampingParameterUpdate,
    ReducedChiSquare,
    ResidualVarianceEstimate,
    FittedOutputUncertainty,
}

/// Failure categories returned by the Gavin Levenberg-Marquardt solver.
#[derive(Debug, thiserror::Error)]
pub enum GavinLmError {
    /// The model's output count does not match the supplied target vector.
    #[error("model/target length mismatch: expected {expected}, got {actual}")]
    ModelTargetLengthMismatch { expected: usize, actual: usize },
    /// The model output count overflowed for the requested number of samples.
    #[error("model output count overflow for {samples} samples")]
    OutputCountOverflow { samples: usize },
    /// The model declares no parameters.
    #[error("the objective model must declare at least one parameter")]
    NoParameters,
    /// Initial parameter vector has the wrong length.
    #[error("initial parameter length mismatch: expected {expected}, got {actual}")]
    ParameterLengthMismatch { expected: usize, actual: usize },
    /// There are fewer observations than fitted parameters.
    #[error("insufficient observations: need at least {parameters}, got {observations}")]
    InsufficientObservations {
        parameters: usize,
        observations: usize,
    },
    /// No observations were supplied.
    #[error("at least one observation is required")]
    EmptyObservations,
    /// An input vector contains a non-finite value.
    #[error("{input:?} contains a non-finite value at index {index}: {value}")]
    NonFiniteInput {
        input: GavinLmInput,
        index: usize,
        value: f64,
    },
    /// A named scalar solver option is invalid.
    #[error("invalid solver option {option:?}={value}")]
    InvalidOption { option: GavinLmOption, value: f64 },
    /// Required field was not configured on the symbolic fitting builder.
    #[error("symbolic fitting configuration is missing required field '{field}'")]
    InvalidFitConfiguration { field: &'static str },
    /// The iteration budget is zero.
    #[error("max_iterations must be greater than zero")]
    ZeroIterationBudget,
    /// Observation normalization cannot produce finite positive weights.
    #[error("observation vector must have a finite, non-zero squared norm")]
    InvalidObservationNorm,
    /// Explicit observation weights have a different length from residuals.
    #[error("weight length mismatch: expected {expected}, got {actual}")]
    WeightLengthMismatch { expected: usize, actual: usize },
    /// An explicit weight is negative or non-finite.
    #[error("invalid least-squares weight at index {index}: {value}")]
    InvalidWeight { index: usize, value: f64 },
    /// At least one residual must participate in the objective.
    #[error("at least one least-squares weight must be positive")]
    AllWeightsZero,
    /// Symbolic fitting data do not match the sample vector.
    #[error("symbolic residual/observation length mismatch: {expected}, got {actual}")]
    SymbolicDataLengthMismatch { expected: usize, actual: usize },
    /// An objective callback returned an error; its typed source is retained.
    #[error("objective callback failed: {0}")]
    ObjectiveCallback(#[source] GavinLmCallbackError),
    /// Symbolic preparation or binding failed; the original typed error is retained.
    #[error("symbolic fitting preparation failed: {0}")]
    SymbolicPreparation(#[source] GavinLmCallbackError),
    /// A symbolic equation string could not be parsed before frontend preparation.
    #[error("failed to parse symbolic equation {equation_index}: {message}")]
    SymbolicParse {
        equation_index: usize,
        message: String,
    },
    /// Objective output length does not match the input sample count.
    #[error("objective output length mismatch: expected {expected}, got {actual}")]
    ObjectiveOutputLengthMismatch { expected: usize, actual: usize },
    /// An objective callback produced non-finite values.
    #[error("objective callback produced a non-finite value at output index {index}: {value}")]
    NonFiniteObjective { index: usize, value: f64 },
    /// An exact Jacobian callback returned an error; its typed source is retained.
    #[error("analytic Jacobian callback failed: {0}")]
    AnalyticJacobianCallback(#[source] GavinLmCallbackError),
    /// Exact Jacobian dimensions do not match (observations, parameters).
    #[error(
        "analytic Jacobian shape mismatch: expected ({expected_rows}, {expected_cols}), got ({actual_rows}, {actual_cols})"
    )]
    AnalyticJacobianShapeMismatch {
        expected_rows: usize,
        expected_cols: usize,
        actual_rows: usize,
        actual_cols: usize,
    },
    /// An exact Jacobian contains a non-finite element.
    #[error("analytic Jacobian contains a non-finite value at ({row}, {column}): {value}")]
    NonFiniteJacobian {
        row: usize,
        column: usize,
        value: f64,
    },
    /// A model advertised an exact Jacobian but did not return one.
    #[error("objective model declared an analytic Jacobian but supplied none")]
    MissingModelJacobian,
    /// The model rejected a parameter vector before evaluating its callbacks.
    #[error("trial parameter vector is outside the objective domain at iteration {iteration}")]
    InfeasibleTrialPoint { iteration: usize },
    /// The symbolic fitting adapter requires at least one formula and one sample variable.
    #[error(
        "symbolic model dimensions are unsupported: expected one or more residual equations and exactly one sample parameter, got {equations} and {parameters}"
    )]
    UnsupportedSymbolicModelDimensions { equations: usize, parameters: usize },
    /// The symbolic residual count overflowed while multiplying equations by samples.
    #[error("symbolic residual count overflow for {equations} equations and {samples} samples")]
    SymbolicResidualCountOverflow { equations: usize, samples: usize },
    /// The damped normal equations could not be solved.
    #[error("damped normal equations are singular at iteration {iteration}")]
    LinearSolveFailure { iteration: usize },
    /// The final normal matrix is singular and covariance cannot be computed.
    #[error("parameter covariance matrix is singular")]
    SingularCovariance,
    /// The covariance matrix has an invalid diagonal element.
    #[error("covariance diagonal at index {index} is invalid: {value}")]
    InvalidCovarianceDiagonal { index: usize, value: f64 },
    /// A computed intermediate became non-finite.
    #[error("numerical breakdown during {stage:?}")]
    NumericalBreakdown { stage: GavinLmNumericalStage },
}

/// Damping update formula used by Gavin's controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GavinLmUpdatePolicy {
    /// Marquardt diagonal scaling.
    Marquardt,
    /// Quadratic damping update.
    Quadratic,
    /// Nielsen damping update.
    Nielsen,
}

fn validate_analytic_jacobian(
    jacobian: &DMatrix<f64>,
    expected_rows: usize,
    expected_columns: usize,
) -> Result<(), GavinLmError> {
    if jacobian.shape() != (expected_rows, expected_columns) {
        return Err(GavinLmError::AnalyticJacobianShapeMismatch {
            expected_rows,
            expected_cols: expected_columns,
            actual_rows: jacobian.nrows(),
            actual_cols: jacobian.ncols(),
        });
    }
    if let Some((flat_index, value)) = jacobian
        .iter()
        .copied()
        .enumerate()
        .find(|(_, value)| !value.is_finite())
    {
        let row = flat_index % jacobian.nrows().max(1);
        let column = flat_index / jacobian.nrows().max(1);
        return Err(GavinLmError::NonFiniteJacobian { row, column, value });
    }
    Ok(())
}

/// How the most recent instrumented solve left its iteration loop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GavinLmTermination {
    /// The weighted normal-equation gradient met its configured tolerance.
    GradientTolerance,
    /// The relative parameter update met its configured tolerance.
    ParameterStepTolerance,
    /// The configured iteration budget was exhausted.
    MaxIterations,
    /// A trial evaluation produced a non-finite residual.
    NonFiniteTrial,
}

impl GavinLmTermination {
    /// Whether the controller met one of its convergence tests.
    pub fn is_converged(self) -> bool {
        matches!(self, Self::GradientTolerance | Self::ParameterStepTolerance)
    }
}

/// Complete result from one Gavin LM solve.
///
/// Unlike the historical tuple, this carries the controller termination and
/// represents undefined R-squared explicitly instead of fabricating a value.
#[derive(Debug, Clone)]
pub struct GavinLmResult {
    /// Fitted parameter vector in the model's declared order.
    pub parameters: DVector<f64>,
    /// Reduced weighted chi-square for the accepted state.
    pub reduced_chi_square: f64,
    /// Approximate standard errors of fitted parameters.
    pub parameter_standard_errors: DVector<f64>,
    /// Approximate standard errors of model outputs at the sample locations.
    pub fitted_value_standard_errors: DVector<f64>,
    /// Approximate parameter correlation matrix.
    pub parameter_correlation: DMatrix<f64>,
    /// Weighted coefficient of determination; `None` when target variance is 0.
    pub r_squared: Option<f64>,
    /// Final weighted sum of squared residuals at the accepted parameters.
    pub sum_squared_residuals: f64,
    /// Columns: callback count, reduced chi-square, then accepted parameters.
    pub convergence_history: DMatrix<f64>,
    /// Why the controller stopped, whether or not it converged.
    pub termination: GavinLmTermination,
}

impl GavinLmResult {
    fn into_legacy_tuple(
        self,
    ) -> (
        DVector<f64>,
        f64,
        DVector<f64>,
        DVector<f64>,
        DMatrix<f64>,
        f64,
        DMatrix<f64>,
    ) {
        (
            self.parameters,
            self.reduced_chi_square,
            self.parameter_standard_errors,
            self.fitted_value_standard_errors,
            self.parameter_correlation,
            self.r_squared.unwrap_or(0.0),
            self.convergence_history,
        )
    }
}

/// Jacobian implementation used by the most recent instrumented solve.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GavinLmJacobianSource {
    /// Finite differences with periodic Broyden updates.
    FiniteDifference,
    /// User-supplied exact derivative callback.
    Analytic,
}

/// Optional aggregate diagnostics for one Gavin LM solve.
///
/// Counters and timers are updated only when collection is enabled. Stage
/// durations are diagnostic scopes and may overlap (for example, objective
/// evaluation occurs inside Jacobian construction).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GavinLmTelemetry {
    /// Number of outer LM iterations entered.
    pub iterations: usize,
    /// Termination reason for the most recent solve.
    pub termination: Option<GavinLmTermination>,
    /// Derivative source selected for this solve.
    pub jacobian_source: Option<GavinLmJacobianSource>,
    /// Number of objective callback invocations.
    pub objective_evaluations: usize,
    /// Number of finite-difference Jacobian rebuilds.
    pub finite_difference_jacobians: usize,
    /// Number of exact Jacobian callback invocations.
    pub analytic_jacobian_evaluations: usize,
    /// Number of rank-one Broyden Jacobian updates.
    pub broyden_updates: usize,
    /// Number of attempted parameter updates accepted by the rho test.
    pub accepted_steps: usize,
    /// Number of attempted parameter updates rejected by the rho test.
    pub rejected_steps: usize,
    /// Number of damped normal-equation solves attempted.
    pub linear_solves: usize,
    /// Cumulative time spent in objective callbacks.
    pub objective_time: Duration,
    /// Cumulative time spent assembling Jacobian and normal-equation data.
    pub linearization_time: Duration,
    /// Cumulative time spent in exact Jacobian callbacks.
    pub analytic_jacobian_time: Duration,
    /// Cumulative time spent solving the damped normal equations.
    pub linear_solve_time: Duration,
    /// Total elapsed time for the most recent successful solve.
    pub total_time: Duration,
}

/// Fallible numerical model callback consumed by the Gavin LM controller.
///
/// Implementations may provide an exact Jacobian directly. Symbolic models
/// prepared by Nonlinear_systems use SymbolicGavinObjective, which adapts the
/// prepared residual and Jacobian callbacks without repeating differentiation.
pub trait ObjectiveFunction {
    /// Evaluates all model outputs at the supplied sample locations and parameters.
    fn evaluate(
        &self,
        t: &DVector<f64>,
        p: &DVector<f64>,
    ) -> Result<DVector<f64>, GavinLmCallbackError>;
    /// Returns the number of fitted coefficients expected by the callback.
    fn parameter_count(&self) -> usize;

    /// Returns the number of scalar outputs for the requested sample count.
    /// Ordinary fitting models produce one output per sample; vector-valued
    /// symbolic systems may produce several residual equations per sample.
    fn output_count(&self, sample_count: usize) -> Option<usize> {
        Some(sample_count)
    }

    /// Validates a trial point before residual or Jacobian evaluation.
    fn validate_trial(&self, p: &DVector<f64>) -> bool {
        p.iter().all(|value| value.is_finite())
    }

    /// Supplies an exact model-output Jacobian when the model owns one.
    ///
    /// Implementations without an exact derivative return None; the solver
    /// then uses its configured closure or the FD/Broyden strategy.
    fn analytic_jacobian(
        &self,
        _t: &DVector<f64>,
        _p: &DVector<f64>,
    ) -> Option<Result<DMatrix<f64>, GavinLmCallbackError>> {
        None
    }

    /// Reports whether this model always provides an analytic Jacobian.
    fn has_analytic_jacobian(&self) -> bool {
        false
    }
}

/// Residual/Jacobian adapter for a prepared scalar symbolic model over samples.
///
/// For each data point and equation the evaluator emits `f_j(x_i, p) - y_ij`,
/// and the exact Jacobian contains `df_j(x_i, p)/dp`. Outputs use equation-major
/// layout, matching `sym_fitting`. The adapter borrows the prepared binding and
/// data, so neither symbolic preparation nor data copies happen inside solve.
pub struct SymbolicGavinObjective<'bound, 'prepared> {
    problem: &'bound crate::numerical::Nonlinear_systems::least_squares::BoundSymbolicLeastSquaresProblem<'prepared>,
    observations: &'bound [f64],
}

impl<'bound, 'prepared> SymbolicGavinObjective<'bound, 'prepared> {
    /// Adapts a prepared symbolic model with one sample parameter to Gavin's API.
    pub fn new(
        problem: &'bound crate::numerical::Nonlinear_systems::least_squares::BoundSymbolicLeastSquaresProblem<'prepared>,
        observations: &'bound [f64],
    ) -> Result<Self, GavinLmError> {
        let equations = problem.residual_count();
        let parameters = problem.equation_parameter_count();
        if equations == 0 || parameters != 1 {
            return Err(GavinLmError::UnsupportedSymbolicModelDimensions {
                equations,
                parameters,
            });
        }
        Ok(Self {
            problem,
            observations,
        })
    }

    /// Exposes one-time symbolic preparation telemetry separately from the
    /// Gavin solve telemetry collected by `LevenbergMarquardt`.
    pub fn preparation_report(
        &self,
    ) -> &crate::numerical::Nonlinear_systems::symbolic::SymbolicPreparationReport {
        self.problem.preparation_report()
    }

    /// Returns the ExprLegacy/AtomNative backend selected during preparation.
    pub fn backend_kind(
        &self,
    ) -> crate::numerical::Nonlinear_systems::symbolic::SymbolicBackendKind {
        self.problem.backend_kind()
    }
}

impl ObjectiveFunction for SymbolicGavinObjective<'_, '_> {
    fn evaluate(
        &self,
        t: &DVector<f64>,
        p: &DVector<f64>,
    ) -> Result<DVector<f64>, GavinLmCallbackError> {
        let expected = t
            .len()
            .checked_mul(self.problem.residual_count())
            .ok_or_else(|| {
                Box::new(GavinLmError::SymbolicResidualCountOverflow {
                    equations: self.problem.residual_count(),
                    samples: t.len(),
                }) as GavinLmCallbackError
            })?;
        if expected != self.observations.len() {
            return Err(Box::new(GavinLmError::SymbolicDataLengthMismatch {
                expected,
                actual: self.observations.len(),
            }));
        }
        let equation_count = self.problem.residual_count();
        let mut output = DVector::zeros(expected);
        let mut parameter_value = DVector::zeros(1);
        let mut point_output = DVector::zeros(equation_count);
        for (index, sample) in t.iter().copied().enumerate() {
            parameter_value[0] = sample;
            self.problem
                .residual_into_with_parameter_values(p, &parameter_value, &mut point_output)
                .map_err(|error| Box::new(error) as GavinLmCallbackError)?;
            for equation in 0..equation_count {
                let output_index = equation * t.len() + index;
                output[output_index] = point_output[equation] - self.observations[output_index];
            }
        }
        Ok(output)
    }

    fn parameter_count(&self) -> usize {
        self.problem.parameter_count()
    }

    fn output_count(&self, sample_count: usize) -> Option<usize> {
        sample_count.checked_mul(self.problem.residual_count())
    }

    fn validate_trial(&self, p: &DVector<f64>) -> bool {
        self.problem.validate_trial(p)
    }

    fn analytic_jacobian(
        &self,
        t: &DVector<f64>,
        p: &DVector<f64>,
    ) -> Option<Result<DMatrix<f64>, GavinLmCallbackError>> {
        let equation_count = self.problem.residual_count();
        let Some(output_rows) = t.len().checked_mul(equation_count) else {
            return Some(Err(Box::new(GavinLmError::SymbolicResidualCountOverflow {
                equations: equation_count,
                samples: t.len(),
            })));
        };
        let mut output = DMatrix::zeros(output_rows, p.len());
        let mut parameter_value = DVector::zeros(1);
        let mut point_jacobian = DMatrix::zeros(equation_count, p.len());
        for (sample_index, sample) in t.iter().copied().enumerate() {
            parameter_value[0] = sample;
            if let Err(error) = self.problem.jacobian_into_with_parameter_values(
                p,
                &parameter_value,
                &mut point_jacobian,
            ) {
                return Some(Err(Box::new(error)));
            }
            for equation in 0..equation_count {
                let output_row = equation * t.len() + sample_index;
                output
                    .row_mut(output_row)
                    .copy_from(&point_jacobian.row(equation));
            }
        }
        Some(Ok(output))
    }

    fn has_analytic_jacobian(&self) -> bool {
        true
    }
}

/// Gavin's damped, finite-difference/Broyden nonlinear least-squares fitter.
///
/// The controller follows the reference implementation in
/// `PY/levenberg_marquardt.py`. The default configuration is silent and does
/// not collect telemetry; callers can opt in per solver instance.
pub struct LevenbergMarquardt<F: ObjectiveFunction> {
    /// Iterations performed by the most recent solve.
    pub iteration: usize,
    /// Objective callback evaluations performed by the most recent solve.
    pub func_calls: usize,
    /// User-provided numerical model.
    pub objective_fn: F,
    /// Termination of the latest solve, available even when telemetry is off.
    pub last_termination: Option<GavinLmTermination>,
    /// Infinity-norm threshold for the weighted normal-equation gradient.
    pub eps_grad: f64,
    /// Relative parameter-step convergence threshold.
    pub eps_coeff: f64,
    /// Minimum gain ratio required to accept a trial step (Gavin epsilon_4).
    pub eps_chi: f64,
    /// Initial damping parameter.
    pub lambda_0: f64,
    /// Multiplier applied after a rejected Marquardt step.
    pub lambda_up_fac: f64,
    /// Divisor applied after an accepted Marquardt step.
    pub lambda_dn_fac: f64,
    /// Damping update formula.
    pub update_policy: GavinLmUpdatePolicy,
    /// Maximum number of controller iterations (1000 by default).
    pub max_iterations: usize,
    /// Enables log-facade messages for this solver instance; disabled by default.
    logging_enabled: bool,
    /// Presence enables counters and timing; `None` keeps the hot path minimal.
    telemetry: Option<GavinLmTelemetry>,
    /// Optional exact model-output derivative, shaped (samples, parameters).
    analytic_jacobian: Option<
        Box<dyn Fn(&DVector<f64>, &DVector<f64>) -> Result<DMatrix<f64>, GavinLmCallbackError>>,
    >,
}

impl<F: ObjectiveFunction> LevenbergMarquardt<F> {
    pub fn new(objective_fn: F) -> Self {
        let epsilon_1 = 1e-3;
        let epsilon_2 = 1e-3;
        let epsilon_4 = 1e-1;

        Self {
            iteration: 0,
            func_calls: 0,
            objective_fn,
            last_termination: None,
            eps_grad: epsilon_1,
            eps_coeff: epsilon_2,
            eps_chi: epsilon_4,
            lambda_0: 1e-2,
            lambda_up_fac: 11.0,
            lambda_dn_fac: 9.0,
            update_policy: GavinLmUpdatePolicy::Marquardt,
            max_iterations: 1000,
            logging_enabled: false,
            telemetry: None,
            analytic_jacobian: None,
        }
    }

    /// Enables solver diagnostics through the crate's configured `log` logger.
    pub fn with_logging(mut self, enabled: bool) -> Self {
        self.logging_enabled = enabled;
        self
    }

    /// Enables or disables aggregate telemetry collection.
    pub fn with_telemetry(mut self, enabled: bool) -> Self {
        self.telemetry = enabled.then(GavinLmTelemetry::default);
        self
    }

    /// Sets the iteration budget without changing the selected damping policy.
    pub fn with_max_iterations(mut self, max_iterations: usize) -> Self {
        self.max_iterations = max_iterations;
        self
    }

    /// Sets the controller tolerances.
    pub fn with_tolerances(mut self, gradient: f64, parameter_step: f64, gain_ratio: f64) -> Self {
        self.eps_grad = gradient;
        self.eps_coeff = parameter_step;
        self.eps_chi = gain_ratio;
        self
    }

    /// Sets the three damping scale factors.
    pub fn with_damping_factors(mut self, initial: f64, increase: f64, decrease: f64) -> Self {
        self.lambda_0 = initial;
        self.lambda_up_fac = increase;
        self.lambda_dn_fac = decrease;
        self
    }

    /// Selects Gavin's damping update policy.
    pub fn with_update_policy(mut self, policy: GavinLmUpdatePolicy) -> Self {
        self.update_policy = policy;
        self
    }

    /// Selects an exact Jacobian callback for model outputs `f(t, p)`.
    ///
    /// The callback must return `df/dp` with shape `(t.len(), p.len())`.
    /// It is evaluated at each accepted parameter state. Rejected trials reuse
    /// the Jacobian at the unchanged accepted state. The callback is never
    /// invoked from symbolic construction code by this numerical solver.
    pub fn with_analytic_jacobian<J>(mut self, jacobian: J) -> Self
    where
        J: Fn(&DVector<f64>, &DVector<f64>) -> DMatrix<f64> + 'static,
    {
        self.analytic_jacobian = Some(Box::new(move |t, p| Ok(jacobian(t, p))));
        self
    }

    /// Selects a fallible exact Jacobian callback, preserving callback errors.
    ///
    /// This is the adapter point for prepared evaluators whose runtime can
    /// report typed failures, including symbolic Lambdify/AOT evaluators.
    pub fn with_fallible_analytic_jacobian<J, E>(mut self, jacobian: J) -> Self
    where
        J: Fn(&DVector<f64>, &DVector<f64>) -> Result<DMatrix<f64>, E> + 'static,
        E: Error + Send + Sync + 'static,
    {
        self.analytic_jacobian = Some(Box::new(move |t, p| {
            jacobian(t, p).map_err(|error| Box::new(error) as GavinLmCallbackError)
        }));
        self
    }

    /// Restores the default finite-difference/Broyden Jacobian route.
    pub fn with_finite_difference_jacobian(mut self) -> Self {
        self.analytic_jacobian = None;
        self
    }

    /// Returns diagnostics for the most recent solve when collection is enabled.
    pub fn telemetry(&self) -> Option<&GavinLmTelemetry> {
        self.telemetry.as_ref()
    }

    fn evaluate_objective(
        &mut self,
        t: &DVector<f64>,
        p: &DVector<f64>,
        expected_outputs: usize,
    ) -> Result<DVector<f64>, GavinLmError> {
        if !self.objective_fn.validate_trial(p) {
            return Err(GavinLmError::InfeasibleTrialPoint {
                iteration: self.iteration,
            });
        }
        let started = self.telemetry.as_ref().map(|_| Instant::now());
        let result = self.objective_fn.evaluate(t, p);
        self.func_calls += 1;
        if let (Some(stats), Some(started)) = (self.telemetry.as_mut(), started) {
            stats.objective_evaluations += 1;
            stats.objective_time += started.elapsed();
        }
        let value = result.map_err(GavinLmError::ObjectiveCallback)?;
        if value.len() != expected_outputs {
            return Err(GavinLmError::ObjectiveOutputLengthMismatch {
                expected: expected_outputs,
                actual: value.len(),
            });
        }
        if let Some((index, value)) = value
            .iter()
            .copied()
            .enumerate()
            .find(|(_, value)| !value.is_finite())
        {
            return Err(GavinLmError::NonFiniteObjective { index, value });
        }
        Ok(value)
    }
    fn lm_fd_j(
        &mut self,
        t: &DVector<f64>,
        p: &DVector<f64>,
        y: &DVector<f64>,
        dp: &DVector<f64>,
    ) -> Result<DMatrix<f64>, GavinLmError> {
        let m = y.len();
        let n = p.len();
        let mut j = DMatrix::zeros(m, n);
        let mut del = DVector::zeros(n);

        // A negative fractional increment selects a backward difference; a
        // positive one selects a centered difference; zero freezes a column.
        for j_idx in 0..n {
            del[j_idx] = dp[j_idx] * (1.0 + p[j_idx].abs());
            if del[j_idx] == 0.0 {
                continue;
            }

            let mut plus = p.clone();
            plus[j_idx] += del[j_idx];
            let plus_valid = self.objective_fn.validate_trial(&plus);
            let mut minus = p.clone();
            minus[j_idx] -= del[j_idx];
            let minus_valid = self.objective_fn.validate_trial(&minus);

            // Keep finite differences inside declared domains. Prefer Gavin's
            // configured direction; use the opposite one-sided difference if
            // a log/positive constraint excludes that perturbation.
            if dp[j_idx] < 0.0 {
                if plus_valid {
                    let y_plus = self.evaluate_objective(t, &plus, y.len())?;
                    for i in 0..m {
                        j[(i, j_idx)] = (y_plus[i] - y[i]) / del[j_idx];
                    }
                } else if minus_valid {
                    let y_minus = self.evaluate_objective(t, &minus, y.len())?;
                    for i in 0..m {
                        j[(i, j_idx)] = (y[i] - y_minus[i]) / del[j_idx];
                    }
                }
            } else if plus_valid && minus_valid {
                let y_plus = self.evaluate_objective(t, &plus, y.len())?;
                let y_minus = self.evaluate_objective(t, &minus, y.len())?;
                for i in 0..m {
                    j[(i, j_idx)] = (y_plus[i] - y_minus[i]) / (2.0 * del[j_idx]);
                }
            } else if plus_valid {
                let y_plus = self.evaluate_objective(t, &plus, y.len())?;
                for i in 0..m {
                    j[(i, j_idx)] = (y_plus[i] - y[i]) / del[j_idx];
                }
            } else if minus_valid {
                let y_minus = self.evaluate_objective(t, &minus, y.len())?;
                for i in 0..m {
                    j[(i, j_idx)] = (y[i] - y_minus[i]) / del[j_idx];
                }
            }
        }
        Ok(j)
    }

    fn lm_broyden_j(
        &self,
        p_old: &DVector<f64>,
        y_old: &DVector<f64>,
        j: &DMatrix<f64>,
        p: &DVector<f64>,
        y: &DVector<f64>,
    ) -> DMatrix<f64> {
        // Rank-one Broyden update enforces the secant condition along the last
        // accepted parameter displacement. The caller must avoid a zero step,
        // for which the denominator below is undefined.
        let h = p - p_old;
        let dy = y - y_old;
        let denominator = h.dot(&h);
        if denominator == 0.0 || !denominator.is_finite() {
            return j.clone();
        }
        let jh = j * &h;
        let a = (&dy - &jh) * h.transpose();
        j + a / denominator
    }

    fn lm_matx(
        &mut self,
        t: &DVector<f64>,
        p_old: &DVector<f64>,
        y_old: &DVector<f64>,
        dx2: f64,
        j: &DMatrix<f64>,
        p: &DVector<f64>,
        y_dat: &DVector<f64>,
        weight: &DVector<f64>,
        dp: &DVector<f64>,
    ) -> Result<(DMatrix<f64>, DVector<f64>, f64, DVector<f64>, DMatrix<f64>), GavinLmError> {
        self.lm_matx_with_output(t, p_old, y_old, dx2, j, p, y_dat, weight, dp, None)
    }

    fn lm_matx_with_output(
        &mut self,
        t: &DVector<f64>,
        p_old: &DVector<f64>,
        y_old: &DVector<f64>,
        dx2: f64,
        j: &DMatrix<f64>,
        p: &DVector<f64>,
        y_dat: &DVector<f64>,
        weight: &DVector<f64>,
        dp: &DVector<f64>,
        cached_output: Option<&DVector<f64>>,
    ) -> Result<(DMatrix<f64>, DVector<f64>, f64, DVector<f64>, DMatrix<f64>), GavinLmError> {
        let linearization_started = self.telemetry.as_ref().map(|_| Instant::now());
        let npar = p.len();
        let y_hat = match cached_output {
            Some(output) => output.clone(),
            None => self.evaluate_objective(t, p, y_dat.len())?,
        };
        if y_hat.len() != y_dat.len() {
            return Err(GavinLmError::ObjectiveOutputLengthMismatch {
                expected: y_dat.len(),
                actual: y_hat.len(),
            });
        }
        if let Some((index, value)) = y_hat
            .iter()
            .copied()
            .enumerate()
            .find(|(_, value)| !value.is_finite())
        {
            return Err(GavinLmError::NonFiniteObjective { index, value });
        }

        let displacement_sq = (p - p_old).norm_squared();
        let j_new = if let Some(analytic_jacobian) = &self.analytic_jacobian {
            let started = self.telemetry.as_ref().map(|_| Instant::now());
            let exact_result = analytic_jacobian(t, p);
            if let (Some(stats), Some(started)) = (self.telemetry.as_mut(), started) {
                stats.analytic_jacobian_evaluations += 1;
                stats.analytic_jacobian_time += started.elapsed();
            }
            let exact = exact_result.map_err(GavinLmError::AnalyticJacobianCallback)?;
            validate_analytic_jacobian(&exact, y_dat.len(), npar)?;
            exact
        } else if self.objective_fn.has_analytic_jacobian() {
            let started = self.telemetry.as_ref().map(|_| Instant::now());
            let exact_result = self.objective_fn.analytic_jacobian(t, p);
            if let (Some(stats), Some(started)) = (self.telemetry.as_mut(), started) {
                stats.analytic_jacobian_evaluations += 1;
                stats.analytic_jacobian_time += started.elapsed();
            }
            let exact = exact_result
                .ok_or(GavinLmError::MissingModelJacobian)?
                .map_err(GavinLmError::AnalyticJacobianCallback)?;
            validate_analytic_jacobian(&exact, y_dat.len(), npar)?;
            exact
        } else if self.iteration % (2 * npar) == 0
            || dx2 > 0.0
            || displacement_sq == 0.0
            || !displacement_sq.is_finite()
        {
            if let Some(stats) = &mut self.telemetry {
                stats.finite_difference_jacobians += 1;
            }
            self.lm_fd_j(t, p, &y_hat, dp)?
        } else {
            if let Some(stats) = &mut self.telemetry {
                stats.broyden_updates += 1;
            }
            self.lm_broyden_j(p_old, y_old, j, p, &y_hat)
        };

        let delta_y = y_dat - &y_hat;
        let weighted_delta = delta_y.component_mul(weight);
        let chi_sq = weighted_delta.dot(&delta_y);

        let mut jtw_j = DMatrix::zeros(npar, npar);
        let mut jtw_dy = DVector::zeros(npar);

        for i in 0..npar {
            for j_idx in 0..npar {
                let mut sum = 0.0;
                for k in 0..y_dat.len() {
                    sum += j_new[(k, i)] * j_new[(k, j_idx)] * weight[k];
                }
                jtw_j[(i, j_idx)] = sum;
            }

            let mut sum = 0.0;
            for k in 0..y_dat.len() {
                sum += j_new[(k, i)] * weight[k] * delta_y[k];
            }
            jtw_dy[i] = sum;
        }

        if !chi_sq.is_finite()
            || jtw_j.iter().any(|value| !value.is_finite())
            || jtw_dy.iter().any(|value| !value.is_finite())
        {
            return Err(GavinLmError::NumericalBreakdown {
                stage: GavinLmNumericalStage::WeightedLinearization,
            });
        }

        if let (Some(stats), Some(started)) = (self.telemetry.as_mut(), linearization_started) {
            stats.linearization_time += started.elapsed();
        }
        Ok((jtw_j, jtw_dy, chi_sq, y_hat, j_new))
    }

    /// Fits model predictions to observations using Gavin's reference
    /// normalization `w_i = 1 / ||y||^2`.
    pub fn lm(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
        y_dat: &DVector<f64>,
    ) -> Result<
        (
            DVector<f64>,
            f64,
            DVector<f64>,
            DVector<f64>,
            DMatrix<f64>,
            f64,
            DMatrix<f64>,
        ),
        GavinLmError,
    > {
        self.lm_impl(p, t, y_dat, None)
            .map(GavinLmResult::into_legacy_tuple)
    }

    /// Fits predictions and observations, returning the explicit controller status.
    pub fn lm_detailed(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
        y_dat: &DVector<f64>,
    ) -> Result<GavinLmResult, GavinLmError> {
        self.lm_impl(p, t, y_dat, None)
    }

    /// Solves a least-squares problem with caller-supplied nonnegative weights.
    /// The objective is `sum_i weights[i] * (f_i(p) - y_i)^2`.
    pub fn lm_with_weights(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
        y_dat: &DVector<f64>,
        weights: &DVector<f64>,
    ) -> Result<
        (
            DVector<f64>,
            f64,
            DVector<f64>,
            DVector<f64>,
            DMatrix<f64>,
            f64,
            DMatrix<f64>,
        ),
        GavinLmError,
    > {
        self.lm_impl(p, t, y_dat, Some(weights))
            .map(GavinLmResult::into_legacy_tuple)
    }

    /// Weighted fitting with explicit termination and undefined-statistic states.
    pub fn lm_with_weights_detailed(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
        y_dat: &DVector<f64>,
        weights: &DVector<f64>,
    ) -> Result<GavinLmResult, GavinLmError> {
        self.lm_impl(p, t, y_dat, Some(weights))
    }

    /// Minimizes residuals directly with unit weights and no observation-norm
    /// normalization. The objective is `sum_i r_i(p)^2`.
    pub fn lm_residuals(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
    ) -> Result<
        (
            DVector<f64>,
            f64,
            DVector<f64>,
            DVector<f64>,
            DMatrix<f64>,
            f64,
            DMatrix<f64>,
        ),
        GavinLmError,
    > {
        let output_count = self
            .objective_fn
            .output_count(t.len())
            .ok_or(GavinLmError::OutputCountOverflow { samples: t.len() })?;
        let zero_target = DVector::zeros(output_count);
        let unit_weights = DVector::from_element(output_count, 1.0);
        self.lm_impl(p, t, &zero_target, Some(&unit_weights))
            .map(GavinLmResult::into_legacy_tuple)
    }

    /// Direct residual solve with the complete typed result.
    pub fn lm_residuals_detailed(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
    ) -> Result<GavinLmResult, GavinLmError> {
        let output_count = self
            .objective_fn
            .output_count(t.len())
            .ok_or(GavinLmError::OutputCountOverflow { samples: t.len() })?;
        self.lm_impl(
            p,
            t,
            &DVector::zeros(output_count),
            Some(&DVector::from_element(output_count, 1.0)),
        )
    }

    /// Direct residual solve with explicit nonnegative observation weights.
    pub fn lm_residuals_with_weights_detailed(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
        weights: &DVector<f64>,
    ) -> Result<GavinLmResult, GavinLmError> {
        let output_count = self
            .objective_fn
            .output_count(t.len())
            .ok_or(GavinLmError::OutputCountOverflow { samples: t.len() })?;
        self.lm_impl(p, t, &DVector::zeros(output_count), Some(weights))
    }

    /// Minimizes direct residuals using explicit nonnegative weights.
    pub fn lm_residuals_with_weights(
        &mut self,
        p: DVector<f64>,
        t: &DVector<f64>,
        weights: &DVector<f64>,
    ) -> Result<
        (
            DVector<f64>,
            f64,
            DVector<f64>,
            DVector<f64>,
            DMatrix<f64>,
            f64,
            DMatrix<f64>,
        ),
        GavinLmError,
    > {
        let output_count = self
            .objective_fn
            .output_count(t.len())
            .ok_or(GavinLmError::OutputCountOverflow { samples: t.len() })?;
        let zero_target = DVector::zeros(output_count);
        self.lm_impl(p, t, &zero_target, Some(weights))
            .map(GavinLmResult::into_legacy_tuple)
    }

    fn lm_impl(
        &mut self,
        mut p: DVector<f64>,
        t: &DVector<f64>,
        y_dat: &DVector<f64>,
        explicit_weights: Option<&DVector<f64>>,
    ) -> Result<GavinLmResult, GavinLmError> {
        // Reset per-run counters while preserving the user's diagnostics policy.
        self.iteration = 0;
        self.func_calls = 0;
        self.last_termination = None;
        if let Some(stats) = &mut self.telemetry {
            *stats = GavinLmTelemetry::default();
            stats.jacobian_source = Some(
                if self.analytic_jacobian.is_some() || self.objective_fn.has_analytic_jacobian() {
                    GavinLmJacobianSource::Analytic
                } else {
                    GavinLmJacobianSource::FiniteDifference
                },
            );
        }
        let solve_started = self.telemetry.as_ref().map(|_| Instant::now());

        let expected_outputs = self
            .objective_fn
            .output_count(t.len())
            .ok_or(GavinLmError::OutputCountOverflow { samples: t.len() })?;
        if expected_outputs != y_dat.len() {
            return Err(GavinLmError::ModelTargetLengthMismatch {
                expected: expected_outputs,
                actual: y_dat.len(),
            });
        }

        let expected_parameters = self.objective_fn.parameter_count();
        if expected_parameters == 0 {
            return Err(GavinLmError::NoParameters);
        }
        if p.len() != expected_parameters {
            return Err(GavinLmError::ParameterLengthMismatch {
                expected: expected_parameters,
                actual: p.len(),
            });
        }

        if y_dat.is_empty() {
            return Err(GavinLmError::EmptyObservations);
        }
        if y_dat.len() < p.len() {
            return Err(GavinLmError::InsufficientObservations {
                parameters: p.len(),
                observations: y_dat.len(),
            });
        }

        for (input, values) in [
            (GavinLmInput::SampleLocations, t),
            (GavinLmInput::Observations, y_dat),
            (GavinLmInput::InitialParameters, &p),
        ] {
            if let Some((index, value)) = values
                .iter()
                .copied()
                .enumerate()
                .find(|(_, value)| !value.is_finite())
            {
                return Err(GavinLmError::NonFiniteInput {
                    input,
                    index,
                    value,
                });
            }
        }
        for (option, value, minimum, inclusive) in [
            (GavinLmOption::GradientTolerance, self.eps_grad, 0.0, false),
            (
                GavinLmOption::ParameterStepTolerance,
                self.eps_coeff,
                0.0,
                false,
            ),
            (GavinLmOption::GainRatioThreshold, self.eps_chi, 0.0, true),
            (GavinLmOption::InitialDamping, self.lambda_0, 0.0, false),
            (
                GavinLmOption::DampingIncreaseFactor,
                self.lambda_up_fac,
                1.0,
                false,
            ),
            (
                GavinLmOption::DampingDecreaseFactor,
                self.lambda_dn_fac,
                1.0,
                false,
            ),
        ] {
            if !value.is_finite()
                || if inclusive {
                    value < minimum
                } else {
                    value <= minimum
                }
            {
                return Err(GavinLmError::InvalidOption { option, value });
            }
        }

        let npar = p.len();
        let npnt = y_dat.len();
        let mut p_old = DVector::zeros(npar);
        let mut y_old = DVector::zeros(npnt);
        let j = DMatrix::zeros(npnt, npar);
        let dof = (npnt - npar + 1) as f64;

        let explicit_weighting = explicit_weights.is_some();
        let weight = if let Some(weights) = explicit_weights {
            if weights.len() != npnt {
                return Err(GavinLmError::WeightLengthMismatch {
                    expected: npnt,
                    actual: weights.len(),
                });
            }
            if let Some((index, value)) = weights
                .iter()
                .copied()
                .enumerate()
                .find(|(_, value)| !value.is_finite() || *value < 0.0)
            {
                return Err(GavinLmError::InvalidWeight { index, value });
            }
            if weights.iter().all(|weight| *weight == 0.0) {
                return Err(GavinLmError::AllWeightsZero);
            }
            weights.clone()
        } else {
            let y_dat_norm_sq = y_dat.dot(y_dat);
            if !y_dat_norm_sq.is_finite() || y_dat_norm_sq <= 0.0 {
                return Err(GavinLmError::InvalidObservationNorm);
            }
            // Preserve the Python fitting API's historical normalization.
            DVector::from_element(npnt, 1.0 / y_dat_norm_sq)
        };
        let dp = DVector::from_element(npar, -0.001);
        // These are Gavin's reference bounds, not user-configurable box bounds.
        // As in that reference, an exactly-zero initial coefficient is fixed at
        // zero by this rule; callers needing such a coefficient should start
        // from a small nonzero value.
        let p_min = -100.0 * p.map(|x| x.abs());
        let p_max = 100.0 * p.map(|x| x.abs());

        if self.max_iterations == 0 {
            return Err(GavinLmError::ZeroIterationBudget);
        }
        let max_iter = self.max_iterations;
        let eps_grad = self.eps_grad;
        let eps_coeff = self.eps_coeff;
        let eps_chi = self.eps_chi;
        let lambda_0 = self.lambda_0;
        let lambda_up_fac = self.lambda_up_fac;
        let lambda_dn_fac = self.lambda_dn_fac;
        let update_policy = self.update_policy;

        let idx: Vec<usize> = (0..npar).collect();
        let (mut jtw_j, mut jtw_dy, mut x2_val, mut y_hat, mut j_mat) =
            self.lm_matx(t, &p_old, &y_old, 1.0, &j, &p, y_dat, &weight, &dp)?;

        if jtw_dy.amax() < eps_grad {
            if self.logging_enabled {
                log::info!("Gavin LM: initial guess is already close to stationary");
            }
        }

        let mut lambda = if update_policy == GavinLmUpdatePolicy::Marquardt {
            lambda_0
        } else {
            lambda_0 * jtw_j.diagonal().max()
        };

        let mut nu = 2.0;
        // Gavin's reference keeps the previous accepted objective separately:
        // on rejection, X2 is restored to this value; on acceptance it advances
        // immediately before the new linearization is assembled.
        let mut x2_old = x2_val;
        // The history layout mirrors Gavin's reference: callback count,
        // reduced chi-square, followed by the accepted parameter vector.
        let mut cvg_hst = DMatrix::zeros(max_iter, npar + 2);

        let mut termination = GavinLmTermination::MaxIterations;
        while self.iteration < max_iter {
            self.iteration += 1;
            if let Some(stats) = &mut self.telemetry {
                stats.iterations = self.iteration;
            }

            let linear_solve_started = self.telemetry.as_ref().map(|_| Instant::now());
            if let Some(stats) = &mut self.telemetry {
                stats.linear_solves += 1;
            }
            // Solve the damped normal equations for the proposed coefficient
            // correction. Marquardt scales each diagonal by its Hessian entry;
            // the quadratic and Nielsen variants add lambda*I instead.
            let mut h = if update_policy == GavinLmUpdatePolicy::Marquardt {
                let mut jtw_j_reg = jtw_j.clone();
                for i in 0..npar {
                    jtw_j_reg[(i, i)] += lambda * jtw_j[(i, i)];
                }
                match jtw_j_reg.lu().solve(&jtw_dy) {
                    Some(solution) => solution,
                    None => {
                        return Err(GavinLmError::LinearSolveFailure {
                            iteration: self.iteration,
                        });
                    }
                }
            } else {
                let mut jtw_j_reg = jtw_j.clone();
                for i in 0..npar {
                    jtw_j_reg[(i, i)] += lambda;
                }
                match jtw_j_reg.lu().solve(&jtw_dy) {
                    Some(solution) => solution,
                    None => {
                        return Err(GavinLmError::LinearSolveFailure {
                            iteration: self.iteration,
                        });
                    }
                }
            };
            if let (Some(stats), Some(started)) = (self.telemetry.as_mut(), linear_solve_started) {
                stats.linear_solve_time += started.elapsed();
            }

            let mut p_try = p.clone();
            for i in &idx {
                p_try[*i] += h[*i];
            }

            for i in 0..npar {
                p_try[i] = p_try[i].max(p_min[i]).min(p_max[i]);
            }

            if !self.objective_fn.validate_trial(&p_try) {
                if let Some(stats) = &mut self.telemetry {
                    stats.rejected_steps += 1;
                }
                x2_val = x2_old;
                lambda = match update_policy {
                    GavinLmUpdatePolicy::Marquardt => (lambda * lambda_up_fac).min(1e7),
                    GavinLmUpdatePolicy::Quadratic => (lambda * lambda_up_fac).min(1e7),
                    GavinLmUpdatePolicy::Nielsen => {
                        let updated = lambda * nu;
                        nu *= 2.0;
                        updated
                    }
                };
                if !lambda.is_finite() || lambda <= 0.0 {
                    return Err(GavinLmError::NumericalBreakdown {
                        stage: GavinLmNumericalStage::DampingParameterUpdate,
                    });
                }
                cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
                cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;
                for i in 0..npar {
                    cvg_hst[(self.iteration - 1, i + 2)] = p[i];
                }
                continue;
            }

            let mut y_hat_try = match self.evaluate_objective(t, &p_try, npnt) {
                Ok(output) => output,
                Err(GavinLmError::NonFiniteObjective { .. }) => {
                    termination = GavinLmTermination::NonFiniteTrial;
                    cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
                    cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;
                    for i in 0..npar {
                        cvg_hst[(self.iteration - 1, i + 2)] = p[i];
                    }
                    if self.logging_enabled {
                        log::warn!(
                            "Gavin LM: non-finite trial output; keeping last accepted parameters"
                        );
                    }
                    break;
                }
                Err(error) => return Err(error),
            };
            let mut delta_y_try = y_dat - &y_hat_try;

            if !delta_y_try.iter().all(|&x| x.is_finite()) {
                termination = GavinLmTermination::NonFiniteTrial;
                cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
                cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;
                for i in 0..npar {
                    cvg_hst[(self.iteration - 1, i + 2)] = p[i];
                }
                if self.logging_enabled {
                    log::warn!(
                        "Gavin LM: non-finite trial residual; keeping last accepted parameters"
                    );
                }
                break;
            }

            let mut x2_try = delta_y_try.component_mul(&weight).dot(&delta_y_try);
            if !x2_try.is_finite() {
                return Err(GavinLmError::NumericalBreakdown {
                    stage: GavinLmNumericalStage::TrialObjective,
                });
            }

            let mut alpha = 1.0;
            if update_policy == GavinLmUpdatePolicy::Quadratic {
                let numerator = jtw_dy.dot(&h);
                let denominator = (x2_try - x2_val) / 2.0 + 2.0 * jtw_dy.dot(&h);
                if denominator == 0.0 || !denominator.is_finite() {
                    return Err(GavinLmError::NumericalBreakdown {
                        stage: GavinLmNumericalStage::QuadraticDampingUpdate,
                    });
                }
                alpha = numerator / denominator;
                if !alpha.is_finite() {
                    return Err(GavinLmError::NumericalBreakdown {
                        stage: GavinLmNumericalStage::QuadraticDampingScale,
                    });
                }
                h *= alpha;

                for i in &idx {
                    p_try[*i] = p[*i] + h[*i];
                }

                for i in 0..npar {
                    p_try[i] = p_try[i].max(p_min[i]).min(p_max[i]);
                }

                if !self.objective_fn.validate_trial(&p_try) {
                    if let Some(stats) = &mut self.telemetry {
                        stats.rejected_steps += 1;
                    }
                    x2_val = x2_old;
                    lambda = (lambda * lambda_up_fac).min(1e7);
                    if !lambda.is_finite() || lambda <= 0.0 {
                        return Err(GavinLmError::NumericalBreakdown {
                            stage: GavinLmNumericalStage::DampingParameterUpdate,
                        });
                    }
                    cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
                    cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;
                    for i in 0..npar {
                        cvg_hst[(self.iteration - 1, i + 2)] = p[i];
                    }
                    continue;
                }

                y_hat_try = match self.evaluate_objective(t, &p_try, npnt) {
                    Ok(output) => output,
                    Err(GavinLmError::NonFiniteObjective { .. }) => {
                        termination = GavinLmTermination::NonFiniteTrial;
                        cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
                        cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;
                        for i in 0..npar {
                            cvg_hst[(self.iteration - 1, i + 2)] = p[i];
                        }
                        if self.logging_enabled {
                            log::warn!(
                                "Gavin LM: non-finite quadratic trial output; keeping last accepted parameters"
                            );
                        }
                        break;
                    }
                    Err(error) => return Err(error),
                };
                delta_y_try = y_dat - &y_hat_try;
                if !delta_y_try.iter().all(|value| value.is_finite()) {
                    termination = GavinLmTermination::NonFiniteTrial;
                    cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
                    cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;
                    for i in 0..npar {
                        cvg_hst[(self.iteration - 1, i + 2)] = p[i];
                    }
                    if self.logging_enabled {
                        log::warn!(
                            "Gavin LM: non-finite quadratic trial residual; keeping last accepted parameters"
                        );
                    }
                    break;
                }
                x2_try = delta_y_try.component_mul(&weight).dot(&delta_y_try);
                if !x2_try.is_finite() {
                    return Err(GavinLmError::NumericalBreakdown {
                        stage: GavinLmNumericalStage::QuadraticTrialObjective,
                    });
                }
            }

            // Gain ratio compares the predicted decrease of the local model
            // with the decrease actually observed at the trial parameters.
            let rho_num = h.dot(&(lambda * &h + &jtw_dy));
            let rho_den = x2_val - x2_try;
            let rho = if rho_den == 0.0 {
                0.0
            } else {
                rho_num / rho_den
            };
            if !rho.is_finite() {
                return Err(GavinLmError::NumericalBreakdown {
                    stage: GavinLmNumericalStage::GainRatio,
                });
            }

            if rho > eps_chi {
                if let Some(stats) = &mut self.telemetry {
                    stats.accepted_steps += 1;
                }
                let dx2 = x2_val - x2_old;
                x2_old = x2_val;
                p_old = p.clone();
                y_old = y_hat.clone();
                p = p_try;

                let (new_jtw_j, new_jtw_dy, new_x2, new_y_hat, new_j) = self.lm_matx_with_output(
                    t,
                    &p_old,
                    &y_old,
                    dx2,
                    &j_mat,
                    &p,
                    y_dat,
                    &weight,
                    &dp,
                    Some(&y_hat_try),
                )?;

                jtw_j = new_jtw_j;
                jtw_dy = new_jtw_dy;
                x2_val = new_x2;
                y_hat = new_y_hat;
                j_mat = new_j;

                if update_policy == GavinLmUpdatePolicy::Marquardt {
                    lambda = (lambda / lambda_dn_fac).max(1e-7);
                } else if update_policy == GavinLmUpdatePolicy::Quadratic {
                    lambda = (lambda / (1.0 + alpha)).max(1e-7);
                } else {
                    lambda =
                        lambda * (1.0 / 3.0 as f64).max((1.0 - (2.0 * rho - 1.0).powi(3)) as f64);
                    nu = 2.0;
                }
            } else {
                if let Some(stats) = &mut self.telemetry {
                    stats.rejected_steps += 1;
                }
                x2_val = x2_old;

                if self.analytic_jacobian.is_none() && self.iteration % (2 * npar) == 0 {
                    let (new_jtw_j, new_jtw_dy, new_x2, new_y_hat, new_j) =
                        self.lm_matx(t, &p_old, &y_old, -1.0, &j_mat, &p, y_dat, &weight, &dp)?;
                    jtw_j = new_jtw_j;
                    jtw_dy = new_jtw_dy;
                    x2_val = new_x2;
                    y_hat = new_y_hat;
                    j_mat = new_j;
                }

                if update_policy == GavinLmUpdatePolicy::Marquardt {
                    lambda = (lambda * lambda_up_fac).min(1e7);
                } else if update_policy == GavinLmUpdatePolicy::Quadratic {
                    lambda = lambda + ((x2_try - x2_val) / 2.0 / alpha).abs();
                } else {
                    lambda = lambda * nu;
                    nu = 2.0 * nu;
                }
            }
            if !lambda.is_finite() || lambda <= 0.0 {
                return Err(GavinLmError::NumericalBreakdown {
                    stage: GavinLmNumericalStage::DampingParameterUpdate,
                });
            }

            cvg_hst[(self.iteration - 1, 0)] = self.func_calls as f64;
            cvg_hst[(self.iteration - 1, 1)] = x2_val / dof;

            for i in 0..npar {
                cvg_hst[(self.iteration - 1, i + 2)] = p[i];
            }

            if jtw_dy.amax() < eps_grad && self.iteration > 2 {
                termination = GavinLmTermination::GradientTolerance;
                if self.logging_enabled {
                    log::info!(
                        "Gavin LM converged: weighted normal-equation gradient tolerance reached"
                    );
                }
                break;
            }

            let mut max_rel_change = 0.0;
            for i in 0..npar {
                let rel_change = h[i].abs() / (p[i].abs() + 1e-12);
                if rel_change > max_rel_change {
                    max_rel_change = rel_change;
                }
            }

            if max_rel_change < eps_coeff && self.iteration > 2 {
                termination = GavinLmTermination::ParameterStepTolerance;
                if self.logging_enabled {
                    log::info!("Gavin LM converged: relative parameter-step tolerance reached");
                }
                break;
            }

            if self.iteration == max_iter {
                if self.logging_enabled {
                    log::warn!(
                        "Gavin LM reached its iteration limit without satisfying a convergence test"
                    );
                }
            }
        }

        self.last_termination = Some(termination);
        if let Some(stats) = &mut self.telemetry {
            stats.termination = Some(termination);
        }
        let accepted_residual = y_dat - &y_hat;
        let accepted_x2 = accepted_residual
            .component_mul(&weight)
            .dot(&accepted_residual);
        if !accepted_x2.is_finite() {
            return Err(GavinLmError::NumericalBreakdown {
                stage: GavinLmNumericalStage::TrialObjective,
            });
        }

        // The default weights are constructed as a uniform vector. Detect that
        // policy from its source rather than recomputing variance: roundoff in
        // a reduction can make identical floating-point weights appear to
        // have a tiny nonzero variance. Explicit user weights remain absolute.
        // Unlike the Python script, scale from the accepted fit rather than
        // the last rejected trial's local delta_y.
        let final_weight = if !explicit_weighting {
            let residual_norm_sq = accepted_residual.dot(&accepted_residual);
            if !residual_norm_sq.is_finite() || residual_norm_sq <= 0.0 {
                return Err(GavinLmError::NumericalBreakdown {
                    stage: GavinLmNumericalStage::ResidualVarianceEstimate,
                });
            }
            let weight_val = dof / residual_norm_sq;
            DVector::from_element(npnt, weight_val)
        } else {
            weight.clone()
        };

        let red_x2 = accepted_x2 / dof;
        if !red_x2.is_finite() {
            return Err(GavinLmError::NumericalBreakdown {
                stage: GavinLmNumericalStage::ReducedChiSquare,
            });
        }

        // Rebuild the final linearization at the accepted parameter vector so
        // uncertainty estimates do not depend on a stale Broyden approximation.
        let (final_jtw_j, _final_jtw_dy, _final_x2, _final_y_hat, final_j) = self
            .lm_matx_with_output(
                t,
                &p_old,
                &y_old,
                -1.0,
                &j_mat,
                &p,
                y_dat,
                &final_weight,
                &dp,
                Some(&y_hat),
            )?;

        let covar_p = match final_jtw_j.try_inverse() {
            Some(inv) => inv,
            None => return Err(GavinLmError::SingularCovariance),
        };

        // The inverse weighted normal matrix approximates parameter covariance.
        let mut sigma_p = DVector::zeros(npar);
        for i in 0..npar {
            let variance = covar_p[(i, i)];
            if !variance.is_finite() || variance < 0.0 {
                return Err(GavinLmError::InvalidCovarianceDiagonal {
                    index: i,
                    value: variance,
                });
            }
            sigma_p[i] = variance.sqrt();
        }

        let mut sigma_y = DVector::zeros(npnt);
        for i in 0..npnt {
            let mut sum = 0.0;
            for j in 0..npar {
                for k in 0..npar {
                    sum += final_j[(i, j)] * covar_p[(j, k)] * final_j[(i, k)];
                }
            }
            if !sum.is_finite() || sum < 0.0 {
                return Err(GavinLmError::NumericalBreakdown {
                    stage: GavinLmNumericalStage::FittedOutputUncertainty,
                });
            }
            sigma_y[i] = sum.sqrt();
        }

        let mut corr_p = DMatrix::zeros(npar, npar);
        for i in 0..npar {
            for j in 0..npar {
                let scale = sigma_p[i] * sigma_p[j];
                if !scale.is_finite() || scale == 0.0 {
                    return Err(GavinLmError::InvalidCovarianceDiagonal {
                        index: if sigma_p[i] == 0.0 { i } else { j },
                        value: if sigma_p[i] == 0.0 {
                            covar_p[(i, i)]
                        } else {
                            covar_p[(j, j)]
                        },
                    });
                }
                corr_p[(i, j)] = covar_p[(i, j)] / scale;
            }
        }

        let total_weight = weight.sum();
        let r_squared = if total_weight > 0.0 && total_weight.is_finite() {
            let weighted_mean = y_dat.dot(&weight) / total_weight;
            let weighted_total = y_dat
                .iter()
                .zip(weight.iter())
                .map(|(target, weight)| weight * (target - weighted_mean).powi(2))
                .sum::<f64>();
            if weighted_total > 0.0 && weighted_total.is_finite() {
                let residual = y_dat - &y_hat;
                Some(1.0 - residual.component_mul(&weight).dot(&residual) / weighted_total)
            } else {
                None
            }
        } else {
            None
        };

        let cvg_hst_trimmed = cvg_hst.rows(0, self.iteration).into_owned();

        if self.logging_enabled {
            for i in 0..npar {
                let relative_error = if p[i] == 0.0 {
                    f64::INFINITY
                } else {
                    sigma_p[i] / p[i].abs() * 100.0
                };
                log::info!(
                    "Gavin LM parameter p{}={:.6e}, relative standard error={:.3}%",
                    i + 1,
                    p[i],
                    relative_error
                );
            }
        }
        if let (Some(stats), Some(started)) = (self.telemetry.as_mut(), solve_started) {
            stats.total_time = started.elapsed();
            stats.termination = Some(termination);
        }
        Ok(GavinLmResult {
            parameters: p,
            reduced_chi_square: red_x2,
            parameter_standard_errors: sigma_p,
            fitted_value_standard_errors: sigma_y,
            parameter_correlation: corr_p,
            r_squared,
            sum_squared_residuals: accepted_x2,
            convergence_history: cvg_hst_trimmed,
            termination,
        })
    }
}

// Example implementations of ObjectiveFunction trait
pub struct ExponentialSinusoidalModel;

impl ObjectiveFunction for ExponentialSinusoidalModel {
    fn evaluate(
        &self,
        t: &DVector<f64>,
        p: &DVector<f64>,
    ) -> Result<DVector<f64>, GavinLmCallbackError> {
        let mut y_hat = DVector::zeros(t.len());
        for i in 0..t.len() {
            y_hat[i] = p[0] * (-t[i] / p[1]).exp() + p[2] * (t[i] / p[3]).sin();
        }
        Ok(y_hat)
    }

    fn parameter_count(&self) -> usize {
        4
    }
}

pub struct PolynomialModel {
    degree: usize,
}

impl PolynomialModel {
    pub fn new(degree: usize) -> Self {
        Self { degree }
    }
}

impl ObjectiveFunction for PolynomialModel {
    fn evaluate(
        &self,
        t: &DVector<f64>,
        p: &DVector<f64>,
    ) -> Result<DVector<f64>, GavinLmCallbackError> {
        let mut y_hat = DVector::zeros(t.len());
        for i in 0..t.len() {
            let mut sum = 0.0;
            for j in 0..=self.degree {
                sum += p[j] * t[i].powi(j as i32);
            }
            y_hat[i] = sum;
        }
        Ok(y_hat)
    }

    fn parameter_count(&self) -> usize {
        self.degree + 1
    }
}

pub struct GaussianModel;

impl ObjectiveFunction for GaussianModel {
    fn evaluate(
        &self,
        t: &DVector<f64>,
        p: &DVector<f64>,
    ) -> Result<DVector<f64>, GavinLmCallbackError> {
        let mut y_hat = DVector::zeros(t.len());
        for i in 0..t.len() {
            // p[0] = amplitude, p[1] = mean, p[2] = std_dev
            let exponent = -0.5 * ((t[i] - p[1]) / p[2]).powi(2);
            y_hat[i] = p[0] * exponent.exp();
        }
        Ok(y_hat)
    }

    fn parameter_count(&self) -> usize {
        3
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use nalgebra::{DVector, dvector};

    struct SquareModel;

    impl ObjectiveFunction for SquareModel {
        fn evaluate(
            &self,
            t: &DVector<f64>,
            p: &DVector<f64>,
        ) -> Result<DVector<f64>, GavinLmCallbackError> {
            Ok(DVector::from_element(t.len(), p[0] * p[0]))
        }

        fn parameter_count(&self) -> usize {
            1
        }
    }

    #[test]
    fn quadratic_update_uses_scaled_step_and_reevaluated_trial_for_acceptance() {
        let mut lm = LevenbergMarquardt::new(SquareModel)
            .with_tolerances(1e-3, 1e-3, 0.6)
            .with_update_policy(GavinLmUpdatePolicy::Quadratic);

        let t = dvector![0.0, 1.0, 2.0];
        let y_dat = dvector![4.0, 4.0, 4.0];
        let (_, _, _, _, _, _, history) = lm
            .lm(dvector![1.0], &t, &y_dat)
            .expect("Quadratic update should finish with a valid covariance");

        // The Python quadratic step scales h by alpha and recomputes X2_try
        // before forming rho. For this fixture rho is below 0.6 on iteration
        // one, then exceeds it after damping rises and the next trial is accepted.
        assert_eq!(history[(0, 2)], 1.0);
        assert!(history[(1, 2)] > 1.0);
    }

    #[test]
    fn nielsen_update_recovers_linear_fit_with_explicit_termination() {
        let t = dvector![0.0, 1.0, 2.0, 3.0, 4.0];
        let y = dvector![1.0, 3.0, 5.0, 7.0, 9.0];
        let mut lm = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_update_policy(GavinLmUpdatePolicy::Nielsen);
        let result = lm
            .lm_detailed(dvector![0.7, 0.8], &t, &y)
            .expect("Nielsen policy should produce a fit result");

        assert!(result.termination.is_converged());
        assert!((result.parameters[0] - 1.0).abs() < 1e-3);
        assert!((result.parameters[1] - 2.0).abs() < 1e-3);
        assert_eq!(lm.last_termination, Some(result.termination));
    }

    fn linear_jacobian(
        t: DVector<f64>,
    ) -> impl Fn(&DVector<f64>, &DVector<f64>) -> DMatrix<f64> + Clone + 'static {
        move |_, _| DMatrix::from_fn(t.len(), 2, |row, col| if col == 0 { 1.0 } else { t[row] })
    }

    #[test]
    fn covariance_statistics_match_independent_unweighted_linear_oracle() {
        let t = dvector![-2.0, -1.0, 0.0, 1.0, 2.0];
        let noise = dvector![1.0, -2.0, 2.0, -2.0, 1.0];
        let observations = DVector::from_iterator(
            t.len(),
            t.iter()
                .zip(noise.iter())
                .map(|(x, residual)| 4.0 + 3.0 * x + residual),
        );
        let mut solver = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_analytic_jacobian(linear_jacobian(t.clone()));
        let result = solver
            .lm_detailed(dvector![3.5, 2.5], &t, &observations)
            .expect("full-rank linear fit should produce statistics");

        // The residual is orthogonal to both columns of [1, x], so the exact
        // least-squares solution is (4, 3), SSE=14, and dof=n-p+1=4.
        assert_relative_eq!(result.parameters[0], 4.0, epsilon = 1e-8);
        assert_relative_eq!(result.parameters[1], 3.0, epsilon = 1e-8);
        assert_relative_eq!(
            result.sum_squared_residuals,
            14.0 / observations.dot(&observations),
            epsilon = 1e-12
        );
        let dof = 4.0;
        assert_relative_eq!(
            result.reduced_chi_square,
            14.0 / (observations.dot(&observations) * dof),
            epsilon = 1e-12
        );

        // Cov(p) = (SSE/dof) * inverse(X'X), X'X=diag(5,10).
        assert_relative_eq!(
            result.parameter_standard_errors[0],
            (14.0 / (dof * 5.0_f64)).sqrt(),
            epsilon = 1e-10
        );
        assert_relative_eq!(
            result.parameter_standard_errors[1],
            (14.0 / (dof * 10.0_f64)).sqrt(),
            epsilon = 1e-10
        );
        for (index, x) in t.iter().enumerate() {
            assert_relative_eq!(
                result.fitted_value_standard_errors[index],
                (14.0 / (dof * 5.0) + 14.0 * x * x / (dof * 10.0)).sqrt(),
                epsilon = 1e-10
            );
        }
        assert_relative_eq!(result.parameter_correlation[(0, 0)], 1.0, epsilon = 1e-12);
        assert_relative_eq!(result.parameter_correlation[(1, 1)], 1.0, epsilon = 1e-12);
        assert_relative_eq!(result.parameter_correlation[(0, 1)], 0.0, epsilon = 1e-12);
        assert_relative_eq!(result.parameter_correlation[(1, 0)], 0.0, epsilon = 1e-12);
        assert_relative_eq!(result.r_squared.unwrap(), 45.0 / 52.0, epsilon = 1e-12);
    }

    #[test]
    fn covariance_statistics_match_independent_explicit_weight_oracle() {
        let t = dvector![-2.0, -1.0, 0.0, 1.0, 2.0];
        let weights = dvector![1.0, 2.0, 3.0, 2.0, 1.0];
        let residual = [1.0, -1.0, 2.0 / 3.0, -1.0, 1.0];
        let observations = DVector::from_iterator(
            t.len(),
            t.iter()
                .zip(residual.iter())
                .map(|(x, residual)| 4.0 + 3.0 * x + residual),
        );
        let mut solver = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_analytic_jacobian(linear_jacobian(t.clone()));
        let result = solver
            .lm_with_weights_detailed(dvector![3.5, 2.5], &t, &observations, &weights)
            .expect("weighted full-rank fit should produce statistics");

        // Weighted residuals are orthogonal to [1, x]. The weighted Gram
        // matrix is diag(9,12), and weighted SSE is 22/3.
        assert_relative_eq!(result.parameters[0], 4.0, epsilon = 1e-8);
        assert_relative_eq!(result.parameters[1], 3.0, epsilon = 1e-8);
        assert_relative_eq!(result.sum_squared_residuals, 22.0 / 3.0, epsilon = 1e-10);
        assert_relative_eq!(result.reduced_chi_square, 11.0 / 6.0, epsilon = 1e-10);
        assert_relative_eq!(
            result.parameter_standard_errors[0],
            1.0 / 3.0,
            epsilon = 1e-10
        );
        assert_relative_eq!(
            result.parameter_standard_errors[1],
            1.0 / 12.0_f64.sqrt(),
            epsilon = 1e-10
        );
        for (index, x) in t.iter().enumerate() {
            assert_relative_eq!(
                result.fitted_value_standard_errors[index],
                (1.0 / 9.0 + x * x / 12.0).sqrt(),
                epsilon = 1e-10
            );
        }
        assert_relative_eq!(result.parameter_correlation[(0, 1)], 0.0, epsilon = 1e-12);
        assert_relative_eq!(result.r_squared.unwrap(), 162.0 / 173.0, epsilon = 1e-12);
    }

    #[test]
    fn rank_deficient_final_jacobian_returns_typed_covariance_error() {
        struct DuplicateColumns;

        impl ObjectiveFunction for DuplicateColumns {
            fn evaluate(
                &self,
                t: &DVector<f64>,
                p: &DVector<f64>,
            ) -> Result<DVector<f64>, GavinLmCallbackError> {
                Ok(DVector::from_element(t.len(), p[0] + p[1]))
            }

            fn parameter_count(&self) -> usize {
                2
            }
        }

        let t = dvector![0.0, 1.0, 2.0, 3.0];
        let target = DVector::from_element(t.len(), 2.0);
        let mut solver = LevenbergMarquardt::new(DuplicateColumns)
            .with_analytic_jacobian(|t: &DVector<f64>, _| DMatrix::from_element(t.len(), 2, 1.0));
        let error = solver
            .lm_detailed(dvector![0.8, 0.8], &t, &target)
            .expect_err("rank-deficient covariance must not be reported as valid");
        assert!(matches!(error, GavinLmError::SingularCovariance));
    }

    #[test]
    fn covariance_after_final_rejection_uses_last_accepted_state() {
        let t = DVector::from_iterator(201, (0..201).map(|index| index as f64 * 0.1));
        let target = ExponentialSinusoidalModel
            .evaluate(&t, &dvector![2.0, 0.5, 1.0, 2.0])
            .unwrap();
        let mut solver = LevenbergMarquardt::new(ExponentialSinusoidalModel).with_telemetry(true);
        let result = solver
            .lm_detailed(dvector![1.94, 0.55, 1.0, 1.38], &t, &target)
            .expect("final rejected trial must preserve valid accepted-state statistics");
        let telemetry = solver.telemetry().expect("telemetry enabled");

        assert_eq!(
            result.termination,
            GavinLmTermination::ParameterStepTolerance
        );
        assert_eq!(telemetry.accepted_steps, 1);
        assert_eq!(telemetry.rejected_steps, 7);
        assert_eq!(telemetry.finite_difference_jacobians, 3);

        // Independent NumPy oracle: covariance was formed from the last
        // accepted parameter vector after seven terminal rejected trials.
        assert_relative_eq!(result.parameters[0], 1.2351243514830532, epsilon = 1e-8);
        assert_relative_eq!(result.parameters[1], 1.4209449041658679, epsilon = 1e-8);
        assert_relative_eq!(result.parameters[2], -0.26519741339092007, epsilon = 1e-8);
        assert_relative_eq!(result.parameters[3], 1.3885070236157069, epsilon = 1e-8);
        assert_relative_eq!(
            result.sum_squared_residuals,
            0.7176299191991665,
            epsilon = 1e-8
        );
        assert_relative_eq!(
            result.reduced_chi_square,
            0.7176299191991665 / 198.0,
            epsilon = 1e-10
        );
        assert!(
            (result.parameter_standard_errors[0] - 0.31658562805197854).abs() < 1e-7,
            "parameters={:?}, sse={}, stderr={:?}, corr={:?}",
            result.parameters,
            result.sum_squared_residuals,
            result.parameter_standard_errors,
            result.parameter_correlation,
        );
        assert_relative_eq!(
            result.parameter_standard_errors[1],
            0.55481506077878007,
            epsilon = 1e-7
        );
        assert_relative_eq!(
            result.parameter_standard_errors[2],
            0.06792518979293888,
            epsilon = 1e-7
        );
        assert_relative_eq!(
            result.parameter_standard_errors[3],
            0.041544397391543399,
            epsilon = 1e-7
        );
        assert_relative_eq!(
            result.parameter_correlation[(0, 1)],
            -0.6552129211875944,
            epsilon = 1e-7
        );
        assert_relative_eq!(
            result.r_squared.unwrap(),
            0.20262730362715942,
            epsilon = 1e-7
        );
    }

    #[test]
    fn telemetry_is_opt_in_and_matches_objective_call_counter() {
        let t = dvector![0.0, 1.0, 2.0, 3.0];
        let p_true = dvector![1.0, 2.0];
        let model = LevenbergMarquardt::new(PolynomialModel::new(1));
        let y = model.objective_fn.evaluate(&t, &p_true).unwrap();

        let mut silent = LevenbergMarquardt::new(PolynomialModel::new(1));
        assert!(silent.telemetry().is_none());
        silent
            .lm(dvector![0.8, 1.8], &t, &y)
            .expect("default silent solve should complete");
        assert!(silent.telemetry().is_none());

        let mut measured = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_telemetry(true)
            .with_logging(false);
        measured
            .lm(dvector![0.8, 1.8], &t, &y)
            .expect("instrumented solve should complete");
        let stats = measured.telemetry().expect("telemetry was enabled");
        assert_eq!(stats.objective_evaluations, measured.func_calls);
        assert_eq!(stats.iterations, measured.iteration);
        assert!(stats.finite_difference_jacobians > 0);
        assert!(stats.linear_solves > 0);
        assert_eq!(
            stats.accepted_steps + stats.rejected_steps,
            stats.iterations
        );
        assert!(stats.termination.is_some());
    }

    #[test]
    fn analytic_jacobian_path_matches_fd_fit_without_fd_or_broyden_work() {
        let t = dvector![0.0, 1.0, 2.0, 3.0, 4.0];
        let p_true = dvector![1.0, 2.0];
        let y = PolynomialModel::new(1).evaluate(&t, &p_true).unwrap();
        let jacobian_t = t.clone();
        let mut exact = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_telemetry(true)
            .with_analytic_jacobian(move |_, _| {
                DMatrix::from_fn(jacobian_t.len(), 2, |row, col| {
                    if col == 0 { 1.0 } else { jacobian_t[row] }
                })
            });
        let (exact_p, exact_red_chi, ..) = exact
            .lm(dvector![0.8, 1.8], &t, &y)
            .expect("exact-Jacobian fit should complete");

        let mut finite_difference = LevenbergMarquardt::new(PolynomialModel::new(1));
        let (fd_p, fd_red_chi, ..) = finite_difference
            .lm(dvector![0.8, 1.8], &t, &y)
            .expect("finite-difference fit should complete");

        for index in 0..2 {
            assert_relative_eq!(exact_p[index], fd_p[index], epsilon = 1e-8);
        }
        assert_relative_eq!(exact_red_chi, fd_red_chi, epsilon = 1e-12);
        let telemetry = exact.telemetry().expect("telemetry enabled");
        assert_eq!(
            telemetry.jacobian_source,
            Some(GavinLmJacobianSource::Analytic)
        );
        assert!(telemetry.analytic_jacobian_evaluations > 0);
        assert_eq!(telemetry.finite_difference_jacobians, 0);
        assert_eq!(telemetry.broyden_updates, 0);
    }

    #[test]
    fn prepared_symbolic_objective_uses_shared_residual_and_jacobian() {
        use crate::numerical::Nonlinear_systems::least_squares::PreparedSymbolicLeastSquaresProblem;
        use crate::numerical::Nonlinear_systems::symbolic::SymbolicProblemOptions;

        let options = SymbolicProblemOptions::new()
            .with_variables(vec!["a".to_string(), "b".to_string()])
            .with_equation_parameters(vec!["x".to_string()])
            .with_equation_parameter_values(dvector![0.0])
            .with_lambdify_backend();
        let prepared =
            PreparedSymbolicLeastSquaresProblem::from_strings(vec!["a + b*x".to_string()], options)
                .expect("symbolic model should prepare once");
        let bound = prepared
            .bind_initial_with_guess(dvector![0.8, 1.8])
            .expect("symbolic model should bind");
        let observations = [1.0, 3.0, 5.0, 7.0, 9.0];
        let model = SymbolicGavinObjective::new(&bound, &observations)
            .expect("scalar formula with one sample input should adapt");
        let t = dvector![0.0, 1.0, 2.0, 3.0, 4.0];
        let mut solver = LevenbergMarquardt::new(model).with_telemetry(true);
        let (parameters, reduced_chi_square, ..) = solver
            .lm_residuals(dvector![0.8, 1.8], &t)
            .expect("Gavin controller should use the prepared symbolic model");

        assert_relative_eq!(parameters[0], 1.0, epsilon = 1e-7);
        assert_relative_eq!(parameters[1], 2.0, epsilon = 1e-7);
        assert!(reduced_chi_square < 1e-12);
        let telemetry = solver.telemetry().expect("telemetry enabled");
        assert_eq!(
            telemetry.jacobian_source,
            Some(GavinLmJacobianSource::Analytic)
        );
        assert!(telemetry.analytic_jacobian_evaluations > 0);
        assert_eq!(telemetry.finite_difference_jacobians, 0);
        assert_eq!(telemetry.broyden_updates, 0);
    }

    #[test]
    fn symbolic_callback_error_keeps_nonlinear_solve_error_as_source() {
        use crate::numerical::Nonlinear_systems::least_squares::PreparedSymbolicLeastSquaresProblem;
        use crate::numerical::Nonlinear_systems::symbolic::SymbolicProblemOptions;

        let prepared = PreparedSymbolicLeastSquaresProblem::from_strings(
            vec!["ln(a) + x".to_string()],
            SymbolicProblemOptions::new()
                .with_variables(vec!["a".to_string()])
                .with_equation_parameters(vec!["x".to_string()])
                .with_equation_parameter_values(dvector![0.0])
                .with_lambdify_backend(),
        )
        .expect("symbolic model should prepare");
        let bound = prepared
            .bind_initial_with_guess(dvector![1.0])
            .expect("symbolic model should bind");
        let observations = [1.0];
        let model = SymbolicGavinObjective::new(&bound, &observations).unwrap();
        let mut solver = LevenbergMarquardt::new(model);
        let error = solver
            .lm_residuals(dvector![-1.0], &dvector![0.0])
            .expect_err("invalid logarithm domain must surface as callback error");
        let callback_error =
            std::error::Error::source(&error).expect("typed callback wrapper retains its source");
        assert!(
            callback_error
                .downcast_ref::<crate::numerical::Nonlinear_systems::error::SolveError>()
                .is_some()
        );
    }

    #[test]
    fn symbolic_positive_domain_is_checked_before_callback_evaluation() {
        use crate::numerical::Nonlinear_systems::least_squares::PreparedSymbolicLeastSquaresProblem;
        use crate::numerical::Nonlinear_systems::symbolic::SymbolicProblemOptions;

        let mut prepared = PreparedSymbolicLeastSquaresProblem::from_strings(
            vec!["ln(a) + x".to_string()],
            SymbolicProblemOptions::new()
                .with_variables(vec!["a".to_string()])
                .with_equation_parameters(vec!["x".to_string()])
                .with_equation_parameter_values(dvector![0.0])
                .with_lambdify_backend(),
        )
        .expect("symbolic model should prepare");
        prepared
            .set_positive(&["a"])
            .expect("declared positivity constraint should resolve");
        let bound = prepared
            .bind_initial_with_guess(dvector![1.0])
            .expect("feasible model should bind");
        let observations = [0.0];
        let objective = SymbolicGavinObjective::new(&bound, &observations).unwrap();
        let mut solver = LevenbergMarquardt::new(objective).with_telemetry(true);
        let error = solver
            .lm_residuals(dvector![-1.0], &dvector![0.0])
            .expect_err("infeasible trial must be rejected before symbolic evaluation");

        assert!(matches!(error, GavinLmError::InfeasibleTrialPoint { .. }));
        assert_eq!(
            solver
                .telemetry()
                .expect("telemetry enabled")
                .objective_evaluations,
            0
        );
    }

    #[test]
    fn prepared_symbolic_gavin_fit_matches_sym_fitting_reference() {
        use crate::numerical::Nonlinear_systems::least_squares::PreparedSymbolicLeastSquaresProblem;
        use crate::numerical::Nonlinear_systems::symbolic::SymbolicProblemOptions;
        use crate::numerical::optimization::sym_fitting::Fitting;

        let x_values = vec![-2.0, -1.0, 0.0, 1.0, 2.0, 3.0];
        let y_values = x_values.iter().map(|x| 1.25 + 2.75 * x).collect::<Vec<_>>();
        let fitting = Fitting::new()
            .with_data(x_values.clone(), y_values.clone())
            .with_equation_str("a + b*x".to_string())
            .with_arg("x".to_string())
            .with_unknowns(vec!["a".to_string(), "b".to_string()])
            .with_initial_guess(vec![1.0, 2.0])
            .build();
        let reference = fitting
            .map_of_solutions
            .expect("sym_fitting builder should produce the reference fit");

        let options = SymbolicProblemOptions::new()
            .with_variables(vec!["a".to_string(), "b".to_string()])
            .with_equation_parameters(vec!["x".to_string()])
            .with_equation_parameter_values(dvector![0.0])
            .with_lambdify_backend();
        let prepared =
            PreparedSymbolicLeastSquaresProblem::from_strings(vec!["a + b*x".to_string()], options)
                .expect("shared symbolic preparation should succeed");
        let bound = prepared
            .bind_initial_with_guess(dvector![1.0, 2.0])
            .expect("prepared symbolic problem should bind once");
        let objective = SymbolicGavinObjective::new(&bound, &y_values)
            .expect("prepared scalar model should adapt to sampled residuals");
        let mut gavin = LevenbergMarquardt::new(objective);
        let (parameters, ..) = gavin
            .lm_residuals(dvector![1.0, 2.0], &DVector::from_vec(x_values))
            .expect("Gavin LM should solve the same symbolic fitting problem");

        assert_relative_eq!(parameters[0], reference["a"], epsilon = 1e-7);
        assert_relative_eq!(parameters[1], reference["b"], epsilon = 1e-7);
    }

    #[test]
    fn analytic_jacobian_rejects_wrong_shape() {
        let t = dvector![0.0, 1.0, 2.0];
        let y = dvector![1.0, 3.0, 5.0];
        let mut solver = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_analytic_jacobian(|_, _| DMatrix::zeros(1, 1));
        let error = solver.lm(dvector![0.8, 1.8], &t, &y).unwrap_err();
        assert!(matches!(
            error,
            GavinLmError::AnalyticJacobianShapeMismatch { .. }
        ));
    }

    #[test]
    fn fallible_analytic_jacobian_preserves_callback_failure_context() {
        let t = dvector![0.0, 1.0, 2.0];
        let y = dvector![1.0, 3.0, 5.0];
        let mut solver = LevenbergMarquardt::new(PolynomialModel::new(1))
            .with_fallible_analytic_jacobian(|_, _| {
                Err::<DMatrix<f64>, _>(std::io::Error::other("prepared evaluator failed"))
            });
        let error = solver.lm(dvector![0.8, 1.8], &t, &y).unwrap_err();
        assert!(matches!(&error, GavinLmError::AnalyticJacobianCallback(_)));
        assert!(std::error::Error::source(&error).is_some());
    }

    #[test]
    fn rejects_non_finite_inputs_and_invalid_solver_options() {
        let t = dvector![0.0, 1.0, 2.0];
        let y = dvector![1.0, 2.0, 3.0];
        let mut lm = LevenbergMarquardt::new(PolynomialModel::new(1));
        assert!(matches!(
            lm.lm(dvector![f64::NAN, 1.0], &t, &y),
            Err(GavinLmError::NonFiniteInput { .. })
        ));

        lm.eps_grad = f64::NAN;
        assert!(matches!(
            lm.lm(dvector![1.0, 1.0], &t, &y),
            Err(GavinLmError::InvalidOption {
                option: GavinLmOption::GradientTolerance,
                ..
            })
        ));
    }

    #[test]
    fn test_exponential_sinusoidal_model() {
        let model = ExponentialSinusoidalModel;
        let mut lm = LevenbergMarquardt::new(model);

        let t = DVector::from_vec((0..10).map(|i| i as f64 * 0.1).collect());
        let p_true = dvector![2.0, 0.5, 1.0, 2.0];
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        let p_initial = dvector![1.5, 0.4, 0.8, 1.3];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((_p_fitted, red_x2, _sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                assert!(red_x2 < 1e-4);
            }
            Err(e) => panic!("LM fitting failed: {}", e),
        }
    }

    #[test]
    fn challenging_fits_match_gavin_python_reference() {
        // Golden values are emitted by optimization/gavin_reference's Python
        // example using the same model, samples, data, and default controller.
        let exact_parameters = dvector![
            1.998465202538,
            0.500449967249,
            0.999312764688,
            1.999967485164
        ];
        let noisy_parameters = dvector![
            1.998760613259,
            0.501073397130,
            0.999124294596,
            1.999999693341
        ];
        let nonuniform_parameters = dvector![
            1.999498343880,
            0.500247781856,
            0.999639175801,
            1.999979622694
        ];
        let broad_t = DVector::from_iterator(201, (0..201).map(|i| i as f64 * 0.1));
        let noisy_observations = {
            let clean = ExponentialSinusoidalModel
                .evaluate(&broad_t, &dvector![2.0, 0.5, 1.0, 2.0])
                .unwrap();
            DVector::from_iterator(
                broad_t.len(),
                clean.iter().enumerate().map(|(index, value)| {
                    let t = broad_t[index];
                    value + 0.001 * (1.7 * t).sin() + 0.0005 * (2.3 * t).cos()
                }),
            )
        };
        let nonuniform_t = DVector::from_iterator(
            81,
            (0..81).map(|index| 20.0 * (index as f64 / 80.0).powf(1.25)),
        );

        let cases = [
            (
                "broad_exact",
                broad_t.clone(),
                ExponentialSinusoidalModel
                    .evaluate(&broad_t, &dvector![2.0, 0.5, 1.0, 2.0])
                    .unwrap(),
                exact_parameters,
                2.153794225532e-9,
            ),
            (
                "broad_noisy",
                broad_t,
                noisy_observations,
                noisy_parameters,
                7.950234822553e-9,
            ),
            (
                "nonuniform_exact",
                nonuniform_t.clone(),
                ExponentialSinusoidalModel
                    .evaluate(&nonuniform_t, &dvector![2.0, 0.5, 1.0, 2.0])
                    .unwrap(),
                nonuniform_parameters,
                1.253274629481e-9,
            ),
        ];

        for (name, t, observations, python_parameters, python_reduced_chi_square) in cases {
            let mut solver = LevenbergMarquardt::new(ExponentialSinusoidalModel);
            let result = solver
                .lm_detailed(dvector![1.9, 0.55, 0.9, 1.9], &t, &observations)
                .unwrap_or_else(|error| panic!("{name}: Gavin fit failed: {error}"));

            assert_eq!(
                result.termination,
                GavinLmTermination::GradientTolerance,
                "{name}: Python reference termination"
            );
            assert_eq!(result.convergence_history.nrows(), 5, "{name}: iterations");
            assert_eq!(solver.func_calls, 10, "{name}: cached objective calls");
            for (actual, expected) in result.parameters.iter().zip(python_parameters.iter()) {
                assert_relative_eq!(actual, expected, epsilon = 5e-10, max_relative = 2e-7);
            }
            assert_relative_eq!(
                result.reduced_chi_square,
                python_reduced_chi_square,
                epsilon = 1e-12,
                max_relative = 5e-2
            );
        }
    }

    #[test]
    fn test_polynomial_model() {
        let model = PolynomialModel::new(2); // Quadratic
        let mut lm = LevenbergMarquardt::new(model);

        let t = DVector::from_vec((0..10).map(|i| i as f64).collect());
        let p_true = dvector![1.0, 2.0, 0.5]; // 1 + 2x + 0.5x^2
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        let p_initial = dvector![0.8, 1.8, 0.4];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((_p_fitted, red_x2, _sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                assert!(red_x2 < 1e-10);
            }
            Err(e) => panic!("Polynomial LM fitting failed: {}", e),
        }
    }

    #[test]
    fn test_gaussian_model() {
        let model = GaussianModel;
        let mut lm = LevenbergMarquardt::new(model);

        let t = DVector::from_vec((-50..50).map(|i| i as f64 * 0.1).collect());
        let p_true = dvector![2.0, 0.0, 1.0]; // amplitude=2, mean=0, std=1
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        let p_initial = dvector![1.8, 0.1, 0.9];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((_p_fitted, red_x2, _sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                assert!(red_x2 < 1e-4);
            }
            Err(e) => panic!("Gaussian LM fitting failed: {}", e),
        }
    }

    #[test]
    fn test_custom_closure_model() {
        // Example of how to use a closure as an objective function
        struct ClosureModel<F>
        where
            F: Fn(&DVector<f64>, &DVector<f64>) -> DVector<f64>,
        {
            func: F,
            param_count: usize,
        }

        impl<F> ObjectiveFunction for ClosureModel<F>
        where
            F: Fn(&DVector<f64>, &DVector<f64>) -> DVector<f64>,
        {
            fn evaluate(
                &self,
                t: &DVector<f64>,
                p: &DVector<f64>,
            ) -> Result<DVector<f64>, GavinLmCallbackError> {
                Ok((self.func)(t, p))
            }

            fn parameter_count(&self) -> usize {
                self.param_count
            }
        }

        // Custom exponential decay model
        let custom_func = |t: &DVector<f64>, p: &DVector<f64>| -> DVector<f64> {
            let mut y = DVector::zeros(t.len());
            for i in 0..t.len() {
                y[i] = p[0] * (-p[1] * t[i]).exp();
            }
            y
        };

        let model = ClosureModel {
            func: custom_func,
            param_count: 2,
        };

        let mut lm = LevenbergMarquardt::new(model);

        let t = DVector::from_vec((0..20).map(|i| i as f64 * 0.1).collect());
        let p_true = dvector![3.0, 0.5];
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        let p_initial = dvector![2.5, 0.4];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((_p_fitted, red_x2, _sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                assert!(red_x2 < 1e-5);
            }
            Err(e) => panic!("Custom model LM fitting failed: {}", e),
        }
    }
}

#[cfg(test)]
mod additional_tests {
    use super::*;
    use approx::assert_relative_eq;
    use nalgebra::{DVector, dvector};

    // Test for noisy data fitting
    #[test]
    fn test_exponential_decay_with_noise() {
        struct ExponentialDecay;

        impl ObjectiveFunction for ExponentialDecay {
            fn evaluate(
                &self,
                t: &DVector<f64>,
                p: &DVector<f64>,
            ) -> Result<DVector<f64>, GavinLmCallbackError> {
                let mut y = DVector::zeros(t.len());
                for i in 0..t.len() {
                    y[i] = p[0] * (-p[1] * t[i]).exp();
                }
                Ok(y)
            }

            fn parameter_count(&self) -> usize {
                2
            }
        }

        let model = ExponentialDecay;
        let mut lm = LevenbergMarquardt::new(model);

        // Generate synthetic noisy data
        let t = DVector::from_vec((0..20).map(|i| i as f64 * 0.2).collect());
        let p_true = dvector![5.0, 0.8];
        let y_clean = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        // Add small amount of noise
        let noise = dvector![
            0.01, -0.02, 0.015, -0.01, 0.008, -0.012, 0.005, 0.018, -0.007, 0.011, 0.003, -0.009,
            0.014, -0.006, 0.002, 0.016, -0.004, 0.013, -0.008, 0.001
        ];
        let y_noisy = y_clean + noise;

        let p_initial = dvector![4.0, 0.6];

        match lm.lm(p_initial, &t, &y_noisy) {
            Ok((p_fitted, red_x2, sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                // Should recover true parameters within reasonable tolerance
                assert_relative_eq!(p_fitted[0], p_true[0], epsilon = 0.1);
                assert_relative_eq!(p_fitted[1], p_true[1], epsilon = 0.1);
                assert!(red_x2 > 0.0);
                assert!(sigma_p[0] > 0.0 && sigma_p[1] > 0.0);
            }
            Err(e) => panic!("Noisy data fitting failed: {}", e),
        }
    }

    // Test for multi-parameter complex model
    #[test]
    fn test_complex_multi_parameter_model() {
        struct ComplexModel;

        impl ObjectiveFunction for ComplexModel {
            fn evaluate(
                &self,
                t: &DVector<f64>,
                p: &DVector<f64>,
            ) -> Result<DVector<f64>, GavinLmCallbackError> {
                let mut y = DVector::zeros(t.len());
                for i in 0..t.len() {
                    // y = p[0] * exp(-p[1]*t) + p[2] * sin(p[3]*t + p[4]) + p[5]
                    y[i] = p[0] * (-p[1] * t[i]).exp() + p[2] * (p[3] * t[i] + p[4]).sin() + p[5];
                }
                Ok(y)
            }

            fn parameter_count(&self) -> usize {
                6
            }
        }

        let model = ComplexModel;
        let mut lm = LevenbergMarquardt::new(model);

        let t = DVector::from_vec((0..5000).map(|i| i as f64 * 0.001).collect());
        let p_true = dvector![3.0, 0.5, 2.0, 1.2, 0.3, 1.0];
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        let p_initial = dvector![2.5, 0.4, 1.8, 1.0, 0.2, 0.8];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((p_fitted, red_x2, _sigma_p, _sigma_y, corr_p, _r_sq, cvg_hst)) => {
                // Check convergence
                assert!(red_x2 < 1e-3);
                assert!(cvg_hst.nrows() > 0);
                assert!(lm.func_calls > 0);

                // Check parameter recovery
                for i in 0..6 {
                    assert_relative_eq!(p_fitted[i], p_true[i], epsilon = 1e-2);
                }

                // Check correlation matrix properties
                assert_eq!(corr_p.nrows(), 6);
                assert_eq!(corr_p.ncols(), 6);
                for i in 0..6 {
                    assert_relative_eq!(corr_p[(i, i)], 1.0, epsilon = 1e-3);
                }
            }
            Err(e) => panic!("Complex model fitting failed: {}", e),
        }
    }

    // Test error handling for mismatched dimensions
    #[test]
    fn test_dimension_mismatch_error() {
        let model = ExponentialSinusoidalModel;
        let mut lm = LevenbergMarquardt::new(model);

        let t = DVector::from_vec(vec![1.0, 2.0, 3.0]);
        let y_dat = DVector::from_vec(vec![1.0, 2.0]); // Different length
        let p_initial = dvector![1.0, 1.0, 1.0, 1.0];

        let error = lm.lm(p_initial, &t, &y_dat).unwrap_err();
        assert!(matches!(
            error,
            GavinLmError::ModelTargetLengthMismatch { .. }
        ));
    }

    #[test]
    fn prepared_multi_equation_gavin_fit_matches_sym_fitting_reference() {
        use crate::numerical::Nonlinear_systems::least_squares::PreparedSymbolicLeastSquaresProblem;
        use crate::numerical::Nonlinear_systems::symbolic::SymbolicProblemOptions;
        use crate::numerical::optimization::sym_fitting::Fitting;

        let x_values = vec![-1.0, 0.0, 1.0, 2.0, 3.0];
        let first = x_values.iter().map(|x| 1.5 + 2.0 * x).collect::<Vec<_>>();
        let second = x_values
            .iter()
            .map(|x| -0.25 + 0.75 * x * x)
            .collect::<Vec<_>>();
        let mut observations = first;
        observations.extend(second);
        let equations = vec!["a + b*x".to_string(), "c + d*x^2".to_string()];
        let unknowns = vec!["a", "b", "c", "d"];
        let reference = Fitting::new()
            .with_data(x_values.clone(), observations.clone())
            .with_equations_str(equations.clone())
            .with_arg("x".to_string())
            .with_unknowns(unknowns.iter().map(|name| (*name).to_string()).collect())
            .with_initial_guess(vec![1.0, 1.0, -0.1, 1.0])
            .build()
            .map_of_solutions
            .expect("sym_fitting should solve the matched multi-equation task");

        let prepared = PreparedSymbolicLeastSquaresProblem::from_strings(
            equations,
            SymbolicProblemOptions::new()
                .with_variables(unknowns.iter().map(|name| (*name).to_string()).collect())
                .with_equation_parameters(vec!["x".to_string()])
                .with_equation_parameter_values(dvector![0.0])
                .with_lambdify_backend(),
        )
        .expect("shared symbolic preparation should succeed");
        let bound = prepared
            .bind_initial_with_guess(dvector![1.0, 1.0, -0.1, 1.0])
            .expect("prepared symbolic problem should bind");
        let objective = SymbolicGavinObjective::new(&bound, &observations)
            .expect("multi-equation symbolic model should adapt");
        let mut gavin = LevenbergMarquardt::new(objective);
        let (parameters, ..) = gavin
            .lm_residuals(dvector![1.0, 1.0, -0.1, 1.0], &DVector::from_vec(x_values))
            .expect("Gavin LM should solve the matched multi-equation task");

        for (index, name) in unknowns.iter().enumerate() {
            assert_relative_eq!(parameters[index], reference[*name], epsilon = 1e-6);
        }
    }

    // Test convergence behavior with poor initial guess
    #[test]
    fn test_poor_initial_guess_convergence() {
        let model = GaussianModel;
        let mut lm = LevenbergMarquardt::new(model);
        let t = DVector::from_vec((-300..300).map(|i| i as f64 * 0.02).collect());
        let p_true = dvector![5.0, 2.0, 1.5];
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        // Very poor initial guess
        let p_initial = dvector![4.0, 3.0, 2.1];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((p_fitted, red_x2, _sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                // Should still converge, though may take more iterations
                assert!(red_x2 < 1e-3);

                // Parameters should be recovered
                assert_relative_eq!(p_fitted[0], p_true[0], epsilon = 1e-2);
                assert_relative_eq!(p_fitted[1], p_true[1], epsilon = 1e-2);
                assert_relative_eq!(p_fitted[2], p_true[2], epsilon = 1e-2);
            }
            Err(error) => panic!("difficult-but-identifiable Gaussian fit failed: {error}"),
        }
    }

    // Test Jacobian computation methods
    #[test]
    fn test_jacobian_computation() {
        let model = PolynomialModel::new(2);
        let mut lm = LevenbergMarquardt::new(model);

        let t = dvector![0.0, 1.0, 2.0, 3.0];
        let p = dvector![1.0, 2.0, 0.5];
        let y = lm.objective_fn.evaluate(&t, &p).unwrap();
        let dp = dvector![-0.001, -0.001, -0.001];

        // Test finite difference Jacobian
        let j_fd = lm
            .lm_fd_j(&t, &p, &y, &dp)
            .expect("finite-difference Jacobian should be evaluable");

        // Check Jacobian dimensions
        assert_eq!(j_fd.nrows(), t.len());
        assert_eq!(j_fd.ncols(), p.len());

        // For polynomial y = p[0] + p[1]*t + p[2]*t^2
        // dy/dp[0] = 1, dy/dp[1] = t, dy/dp[2] = t^2
        for i in 0..t.len() {
            assert_relative_eq!(j_fd[(i, 0)], 1.0, epsilon = 1e-6);
            assert_relative_eq!(j_fd[(i, 1)], t[i], epsilon = 1e-6);
            assert_relative_eq!(j_fd[(i, 2)], t[i] * t[i], epsilon = 1e-6);
        }
    }

    // Test Broyden update
    #[test]
    fn test_broyden_jacobian_update() {
        let model = PolynomialModel::new(1); // Linear model
        let lm = LevenbergMarquardt::new(model);

        let p_old = dvector![1.0, 2.0];
        let p_new = dvector![1.1, 2.1];
        let y_old = dvector![3.0, 5.0, 7.0];
        let y_new = dvector![3.2, 5.2, 7.2];

        // Initial Jacobian for linear model y = p[0] + p[1]*t
        let j_old = nalgebra::dmatrix![
            1.0, 0.0;
            1.0, 1.0;
            1.0, 2.0;
        ];

        let j_broyden = lm.lm_broyden_j(&p_old, &y_old, &j_old, &p_new, &y_new);

        // Check dimensions
        assert_eq!(j_broyden.nrows(), j_old.nrows());
        assert_eq!(j_broyden.ncols(), j_old.ncols());

        // For a linear model, Broyden update should preserve the Jacobian structure
        assert!(j_broyden.norm() > 0.0);
    }

    #[test]
    fn broyden_zero_step_preserves_finite_jacobian() {
        let solver = LevenbergMarquardt::new(PolynomialModel::new(1));
        let p = dvector![1.0, 2.0];
        let y = dvector![3.0, 5.0, 7.0];
        let jacobian = nalgebra::dmatrix![
            1.0, 0.0;
            1.0, 1.0;
            1.0, 2.0;
        ];

        let updated = solver.lm_broyden_j(&p, &y, &jacobian, &p, &y);
        assert_eq!(updated, jacobian);
        assert!(updated.iter().all(|value| value.is_finite()));
    }

    // Test with singular/ill-conditioned cases
    #[test]
    fn test_ill_conditioned_problem() {
        struct IllConditionedModel;

        impl ObjectiveFunction for IllConditionedModel {
            fn evaluate(
                &self,
                t: &DVector<f64>,
                p: &DVector<f64>,
            ) -> Result<DVector<f64>, GavinLmCallbackError> {
                let mut y = DVector::zeros(t.len());
                for i in 0..t.len() {
                    // Highly correlated parameters: y = p[0] + p[1] + (p[0] - p[1]) * t
                    y[i] = p[0] + p[1] + (p[0] - p[1]) * t[i];
                }
                Ok(y)
            }

            fn parameter_count(&self) -> usize {
                2
            }
        }

        let model = IllConditionedModel;
        let mut lm = LevenbergMarquardt::new(model);

        let t = dvector![0.0, 1.0];
        let p_true = dvector![2.0, 3.0];
        let y_true = lm.objective_fn.evaluate(&t, &p_true).unwrap();

        let p_initial = dvector![1.0, 1.0];

        match lm.lm(p_initial, &t, &y_true) {
            Ok((p_fitted, red_x2, _sigma_p, _sigma_y, _corr_p, _r_sq, _cvg_hst)) => {
                // Should converge but with high parameter uncertainties
                assert!(red_x2 < 1e-10);

                // Check that we get a valid solution (may not be unique)
                let y_fitted = lm.objective_fn.evaluate(&t, &p_fitted).unwrap();
                for i in 0..y_true.len() {
                    assert_relative_eq!(y_fitted[i], y_true[i], epsilon = 1e-5);
                }
            }
            Err(error) => panic!("identifiable ill-conditioned fit failed: {error}"),
        }
    }
}