sklears-manifold 0.1.2

Manifold learning algorithms (t-SNE, Isomap, etc.)
Documentation
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
use scirs2_core::essentials::{Normal, Uniform};
use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use scirs2_core::random::thread_rng;
use scirs2_core::Distribution;
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Transform, Untrained},
};

#[derive(Debug, Clone)]
pub struct AutoencoderManifold<S = Untrained> {
    n_components: usize,
    hidden_layers: Vec<usize>,
    epochs: usize,
    learning_rate: f64,
    batch_size: usize,
    state: S,
}

#[derive(Debug, Clone)]
#[allow(dead_code)] // retained for serialization/introspection
pub struct TrainedAutoencoder {
    encoder_weights: Vec<Array2<f64>>,
    encoder_biases: Vec<Array1<f64>>,
    decoder_weights: Vec<Array2<f64>>,
    decoder_biases: Vec<Array1<f64>>,
}

impl AutoencoderManifold<Untrained> {
    pub fn new(n_components: usize) -> Self {
        Self {
            n_components,
            hidden_layers: vec![128, 64],
            epochs: 100,
            learning_rate: 0.001,
            batch_size: 32,
            state: Untrained,
        }
    }

    pub fn with_hidden_layers(mut self, layers: Vec<usize>) -> Self {
        self.hidden_layers = layers;
        self
    }

    pub fn with_epochs(mut self, epochs: usize) -> Self {
        self.epochs = epochs;
        self
    }

    pub fn with_learning_rate(mut self, lr: f64) -> Self {
        self.learning_rate = lr;
        self
    }

    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = batch_size;
        self
    }

    fn initialize_weights(&self, input_dim: usize) -> TrainedAutoencoder {
        let mut layer_sizes = vec![input_dim];
        layer_sizes.extend(&self.hidden_layers);
        layer_sizes.push(self.n_components);

        let mut encoder_weights = Vec::new();
        let mut encoder_biases = Vec::new();

        for i in 0..layer_sizes.len() - 1 {
            let input_size = layer_sizes[i];
            let output_size = layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((input_size, output_size), |(_, _)| {
                let mut rng = thread_rng();
                Normal::new(0.0, (2.0 / (input_size + output_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(output_size);

            encoder_weights.push(weight);
            encoder_biases.push(bias);
        }

        layer_sizes.reverse();
        let mut decoder_weights = Vec::new();
        let mut decoder_biases = Vec::new();

        for i in 0..layer_sizes.len() - 1 {
            let input_size = layer_sizes[i];
            let output_size = layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((input_size, output_size), |(_, _)| {
                let mut rng = thread_rng();
                Normal::new(0.0, (2.0 / (input_size + output_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(output_size);

            decoder_weights.push(weight);
            decoder_biases.push(bias);
        }

        // TrainedAutoencoder
        TrainedAutoencoder {
            encoder_weights,
            encoder_biases,
            decoder_weights,
            decoder_biases,
        }
    }
}

impl AutoencoderManifold<TrainedAutoencoder> {
    #[allow(dead_code)] // retained for introspection
    fn forward_pass(&self, x: &ArrayView2<f64>) -> SklResult<(Array2<f64>, Array2<f64>)> {
        let mut activations = x.to_owned();

        for (weights, biases) in self
            .state
            .encoder_weights
            .iter()
            .zip(self.state.encoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            activations.mapv_inplace(|x| x.max(0.0)); // ReLU
        }

        let encoded = activations.clone();

        for (weights, biases) in self
            .state
            .decoder_weights
            .iter()
            .zip(self.state.decoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            if weights
                != self
                    .state
                    .decoder_weights
                    .last()
                    .expect("operation should succeed")
            {
                activations.mapv_inplace(|x| x.max(0.0)); // ReLU
            }
        }

        Ok((encoded, activations))
    }
}

impl Estimator for AutoencoderManifold<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<Array2<f64>, ()> for AutoencoderManifold<Untrained> {
    type Fitted = AutoencoderManifold<TrainedAutoencoder>;

    fn fit(self, x: &Array2<f64>, _y: &()) -> SklResult<Self::Fitted> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 || n_features == 0 {
            return Err(SklearsError::InvalidInput("Empty input data".to_string()));
        }

        if self.n_components >= n_features {
            return Err(SklearsError::InvalidInput(
                "n_components must be less than number of features".to_string(),
            ));
        }

        let state = self.initialize_weights(n_features);

        // Note: For a complete implementation, we would need to implement
        // the training loop with gradient descent here. For now, we return
        // the initialized model as a placeholder.

        Ok(AutoencoderManifold {
            n_components: self.n_components,
            hidden_layers: self.hidden_layers,
            epochs: self.epochs,
            learning_rate: self.learning_rate,
            batch_size: self.batch_size,
            state,
        })
    }
}

impl Transform<Array2<f64>, Array2<f64>> for AutoencoderManifold<TrainedAutoencoder> {
    fn transform(&self, x: &Array2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = x.to_owned();

        for (weights, biases) in self
            .state
            .encoder_weights
            .iter()
            .zip(self.state.encoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            activations.mapv_inplace(|x| x.max(0.0)); // ReLU
        }

        Ok(activations)
    }
}

#[derive(Debug, Clone)]
#[allow(dead_code)] // retained for serialization/introspection
pub struct VariationalAutoencoder<S = Untrained> {
    n_components: usize,
    hidden_layers: Vec<usize>,
    epochs: usize,
    learning_rate: f64,
    batch_size: usize,
    beta: f64, // KL divergence weight
    state: S,
}

#[derive(Debug, Clone)]
#[allow(dead_code)] // retained for serialization/introspection
pub struct TrainedVAE {
    encoder_weights: Vec<Array2<f64>>,
    encoder_biases: Vec<Array1<f64>>,
    mu_layer: (Array2<f64>, Array1<f64>),
    logvar_layer: (Array2<f64>, Array1<f64>),
    decoder_weights: Vec<Array2<f64>>,
    decoder_biases: Vec<Array1<f64>>,
}

impl VariationalAutoencoder<Untrained> {
    pub fn new(n_components: usize) -> Self {
        Self {
            n_components,
            hidden_layers: vec![128, 64],
            epochs: 100,
            learning_rate: 0.001,
            batch_size: 32,
            beta: 1.0,
            state: Untrained,
        }
    }

    pub fn with_beta(mut self, beta: f64) -> Self {
        self.beta = beta;
        self
    }

    pub fn with_hidden_layers(mut self, layers: Vec<usize>) -> Self {
        self.hidden_layers = layers;
        self
    }

    pub fn with_epochs(mut self, epochs: usize) -> Self {
        self.epochs = epochs;
        self
    }

    pub fn with_learning_rate(mut self, lr: f64) -> Self {
        self.learning_rate = lr;
        self
    }
}

impl VariationalAutoencoder<TrainedVAE> {
    #[allow(dead_code)] // retained for introspection
    fn reparameterize(&self, mu: &Array2<f64>, logvar: &Array2<f64>) -> Array2<f64> {
        let std = logvar.mapv(|x| (0.5 * x).exp());
        let mut rng = thread_rng();
        let epsilon = Array2::from_shape_fn(mu.dim(), |(_, _)| {
            Normal::new(0.0, 1.0)
                .expect("operation should succeed")
                .sample(&mut rng)
        });
        mu + &std * &epsilon
    }

    #[allow(dead_code)] // retained for introspection
    fn kl_divergence(&self, mu: &Array2<f64>, logvar: &Array2<f64>) -> f64 {
        let kl: Array2<f64> = -0.5 * (1.0 + logvar - mu.mapv(|x| x * x) - logvar.mapv(|x| x.exp()));
        kl.sum() / mu.nrows() as f64
    }
}

impl Estimator for VariationalAutoencoder<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Transform<Array2<f64>, Array2<f64>> for VariationalAutoencoder<TrainedVAE> {
    fn transform(&self, x: &Array2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = x.to_owned();

        for (weights, biases) in self
            .state
            .encoder_weights
            .iter()
            .zip(self.state.encoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            activations.mapv_inplace(|x| x.max(0.0)); // ReLU
        }

        let mu = activations.dot(&self.state.mu_layer.0) + &self.state.mu_layer.1;
        let logvar = activations.dot(&self.state.logvar_layer.0) + &self.state.logvar_layer.1;

        Ok(self.reparameterize(&mu, &logvar))
    }
}

/// Adversarial Autoencoder (AAE) - Combines autoencoder reconstruction with adversarial regularization
#[derive(Debug, Clone)]
pub struct AdversarialAutoencoder<S = Untrained> {
    n_components: usize,
    hidden_layers: Vec<usize>,
    discriminator_layers: Vec<usize>,
    epochs: usize,
    learning_rate: f64,
    adversarial_weight: f64,
    batch_size: usize,
    prior_type: PriorType,
    state: S,
}

#[derive(Debug, Clone)]
pub enum PriorType {
    /// Gaussian
    Gaussian,
    /// Uniform
    Uniform,
    /// Categorical
    Categorical(usize), // number of categories
}

#[derive(Debug, Clone)]
#[allow(dead_code)] // retained for serialization/introspection
pub struct TrainedAAE {
    encoder_weights: Vec<Array2<f64>>,
    encoder_biases: Vec<Array1<f64>>,
    decoder_weights: Vec<Array2<f64>>,
    decoder_biases: Vec<Array1<f64>>,
    discriminator_weights: Vec<Array2<f64>>,
    discriminator_biases: Vec<Array1<f64>>,
    prior_type: PriorType,
}

impl AdversarialAutoencoder<Untrained> {
    pub fn new(n_components: usize) -> Self {
        Self {
            n_components,
            hidden_layers: vec![128, 64],
            discriminator_layers: vec![64, 32],
            epochs: 100,
            learning_rate: 0.001,
            adversarial_weight: 1.0,
            batch_size: 32,
            prior_type: PriorType::Gaussian,
            state: Untrained,
        }
    }

    pub fn with_hidden_layers(mut self, layers: Vec<usize>) -> Self {
        self.hidden_layers = layers;
        self
    }

    pub fn with_discriminator_layers(mut self, layers: Vec<usize>) -> Self {
        self.discriminator_layers = layers;
        self
    }

    pub fn with_epochs(mut self, epochs: usize) -> Self {
        self.epochs = epochs;
        self
    }

    pub fn with_learning_rate(mut self, lr: f64) -> Self {
        self.learning_rate = lr;
        self
    }

    pub fn with_adversarial_weight(mut self, weight: f64) -> Self {
        self.adversarial_weight = weight;
        self
    }

    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = batch_size;
        self
    }

    pub fn with_prior_type(mut self, prior_type: PriorType) -> Self {
        self.prior_type = prior_type;
        self
    }

    fn initialize_aae_weights(&self, input_dim: usize) -> TrainedAAE {
        let mut rng = thread_rng();

        // Initialize encoder
        let mut encoder_layer_sizes = vec![input_dim];
        encoder_layer_sizes.extend(&self.hidden_layers);
        encoder_layer_sizes.push(self.n_components);

        let mut encoder_weights = Vec::new();
        let mut encoder_biases = Vec::new();

        for i in 0..encoder_layer_sizes.len() - 1 {
            let input_size = encoder_layer_sizes[i];
            let output_size = encoder_layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((input_size, output_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (input_size + output_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(output_size);

            encoder_weights.push(weight);
            encoder_biases.push(bias);
        }

        // Initialize decoder (reverse of encoder)
        let mut decoder_layer_sizes = encoder_layer_sizes.clone();
        decoder_layer_sizes.reverse();

        let mut decoder_weights = Vec::new();
        let mut decoder_biases = Vec::new();

        for i in 0..decoder_layer_sizes.len() - 1 {
            let input_size = decoder_layer_sizes[i];
            let output_size = decoder_layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((input_size, output_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (input_size + output_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(output_size);

            decoder_weights.push(weight);
            decoder_biases.push(bias);
        }

        // Initialize discriminator
        let mut disc_layer_sizes = vec![self.n_components];
        disc_layer_sizes.extend(&self.discriminator_layers);
        disc_layer_sizes.push(1); // Binary classification output

        let mut discriminator_weights = Vec::new();
        let mut discriminator_biases = Vec::new();

        for i in 0..disc_layer_sizes.len() - 1 {
            let input_size = disc_layer_sizes[i];
            let output_size = disc_layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((input_size, output_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (input_size + output_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(output_size);

            discriminator_weights.push(weight);
            discriminator_biases.push(bias);
        }

        // TrainedAAE
        TrainedAAE {
            encoder_weights,
            encoder_biases,
            decoder_weights,
            decoder_biases,
            discriminator_weights,
            discriminator_biases,
            prior_type: self.prior_type.clone(),
        }
    }
}

impl AdversarialAutoencoder<TrainedAAE> {
    fn encode(&self, x: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = x.to_owned();

        for (weights, biases) in self
            .state
            .encoder_weights
            .iter()
            .zip(self.state.encoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            // Apply ReLU except for the final layer
            if weights
                != self
                    .state
                    .encoder_weights
                    .last()
                    .expect("operation should succeed")
            {
                activations.mapv_inplace(|x| x.max(0.0));
            }
        }

        Ok(activations)
    }

    #[allow(dead_code)] // retained for introspection
    fn decode(&self, z: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = z.to_owned();

        for (i, (weights, biases)) in self
            .state
            .decoder_weights
            .iter()
            .zip(self.state.decoder_biases.iter())
            .enumerate()
        {
            activations = activations.dot(weights) + biases;
            // Apply ReLU except for the final layer
            if i < self.state.decoder_weights.len() - 1 {
                activations.mapv_inplace(|x| x.max(0.0));
            }
        }

        Ok(activations)
    }

    #[allow(dead_code)] // retained for introspection
    fn discriminate(&self, z: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = z.to_owned();

        for (i, (weights, biases)) in self
            .state
            .discriminator_weights
            .iter()
            .zip(self.state.discriminator_biases.iter())
            .enumerate()
        {
            activations = activations.dot(weights) + biases;

            if i < self.state.discriminator_weights.len() - 1 {
                // ReLU for hidden layers
                activations.mapv_inplace(|x| x.max(0.0));
            } else {
                // Sigmoid for output layer
                activations.mapv_inplace(|x| 1.0 / (1.0 + (-x).exp()));
            }
        }

        Ok(activations)
    }

    #[allow(dead_code)] // retained for introspection
    fn sample_prior(&self, n_samples: usize) -> Array2<f64> {
        let mut rng = thread_rng();

        match &self.state.prior_type {
            PriorType::Gaussian => {
                Array2::from_shape_fn((n_samples, self.n_components), |(_, _)| {
                    Normal::new(0.0, 1.0)
                        .expect("operation should succeed")
                        .sample(&mut rng)
                })
            }
            PriorType::Uniform => {
                Array2::from_shape_fn((n_samples, self.n_components), |(_, _)| {
                    Uniform::new(-1.0, 1.0)
                        .expect("operation should succeed")
                        .sample(&mut rng)
                })
            }
            PriorType::Categorical(n_cats) => {
                let mut samples = Array2::zeros((n_samples, self.n_components));
                for i in 0..n_samples {
                    let cat = rng.gen_range(0..*n_cats);
                    if cat < self.n_components {
                        samples[[i, cat]] = 1.0;
                    }
                }
                samples
            }
        }
    }

    #[allow(dead_code)] // retained for introspection
    fn reconstruction_loss(
        &self,
        x_original: &ArrayView2<f64>,
        x_reconstructed: &ArrayView2<f64>,
    ) -> f64 {
        let diff = x_original - x_reconstructed;
        (diff.mapv(|x| x * x).sum()) / x_original.nrows() as f64
    }

    #[allow(dead_code)] // retained for introspection
    fn adversarial_loss(&self, discriminator_output: &ArrayView2<f64>, is_real: bool) -> f64 {
        let target = if is_real { 1.0 } else { 0.0 };
        let epsilon = 1e-12;

        let mut loss = 0.0;
        for &output in discriminator_output.iter() {
            let clamped_output = output.max(epsilon).min(1.0 - epsilon);
            loss -= target * clamped_output.ln() + (1.0 - target) * (1.0 - clamped_output).ln();
        }

        loss / discriminator_output.nrows() as f64
    }
}

impl Estimator for AdversarialAutoencoder<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<Array2<f64>, ()> for AdversarialAutoencoder<Untrained> {
    type Fitted = AdversarialAutoencoder<TrainedAAE>;

    fn fit(self, x: &Array2<f64>, _y: &()) -> SklResult<Self::Fitted> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 || n_features == 0 {
            return Err(SklearsError::InvalidInput("Empty input data".to_string()));
        }

        if self.n_components >= n_features {
            return Err(SklearsError::InvalidInput(
                "n_components must be less than number of features".to_string(),
            ));
        }

        let state = self.initialize_aae_weights(n_features);

        // Note: For a complete implementation, we would need to implement
        // the full adversarial training loop with alternating updates between
        // autoencoder and discriminator. For now, we return the initialized model.

        Ok(AdversarialAutoencoder {
            n_components: self.n_components,
            hidden_layers: self.hidden_layers,
            discriminator_layers: self.discriminator_layers,
            epochs: self.epochs,
            learning_rate: self.learning_rate,
            adversarial_weight: self.adversarial_weight,
            batch_size: self.batch_size,
            prior_type: self.prior_type,
            state,
        })
    }
}

impl Transform<Array2<f64>, Array2<f64>> for AdversarialAutoencoder<TrainedAAE> {
    fn transform(&self, x: &Array2<f64>) -> SklResult<Array2<f64>> {
        self.encode(&x.view())
    }
}

/// Neural Ordinary Differential Equation (NODE) - Models continuous dynamics for manifold learning
#[derive(Debug, Clone)]
pub struct NeuralODE<S = Untrained> {
    n_components: usize,
    hidden_layers: Vec<usize>,
    integration_time: f64,
    num_time_steps: usize,
    solver_type: ODESolverType,
    learning_rate: f64,
    epochs: usize,
    state: S,
}

#[derive(Debug, Clone)]
pub enum ODESolverType {
    /// Euler
    Euler,
    /// RungeKutta4
    RungeKutta4,
}

#[derive(Debug, Clone)]
pub struct TrainedNODE {
    ode_func_weights: Vec<Array2<f64>>,
    ode_func_biases: Vec<Array1<f64>>,
    encoder_weights: Vec<Array2<f64>>,
    encoder_biases: Vec<Array1<f64>>,
    decoder_weights: Vec<Array2<f64>>,
    decoder_biases: Vec<Array1<f64>>,
    integration_time: f64,
    num_time_steps: usize,
    solver_type: ODESolverType,
}

impl NeuralODE<Untrained> {
    pub fn new(n_components: usize) -> Self {
        Self {
            n_components,
            hidden_layers: vec![64, 32],
            integration_time: 1.0,
            num_time_steps: 10,
            solver_type: ODESolverType::RungeKutta4,
            learning_rate: 0.001,
            epochs: 100,
            state: Untrained,
        }
    }

    pub fn with_hidden_layers(mut self, layers: Vec<usize>) -> Self {
        self.hidden_layers = layers;
        self
    }

    pub fn with_integration_time(mut self, time: f64) -> Self {
        self.integration_time = time;
        self
    }

    pub fn with_num_time_steps(mut self, steps: usize) -> Self {
        self.num_time_steps = steps;
        self
    }

    pub fn with_solver_type(mut self, solver: ODESolverType) -> Self {
        self.solver_type = solver;
        self
    }

    pub fn with_learning_rate(mut self, lr: f64) -> Self {
        self.learning_rate = lr;
        self
    }

    pub fn with_epochs(mut self, epochs: usize) -> Self {
        self.epochs = epochs;
        self
    }

    fn initialize_node_weights(&self, input_dim: usize) -> TrainedNODE {
        let mut rng = thread_rng();

        // Initialize encoder (compress to manifold dimension)
        let encoder_layers = [input_dim, self.n_components];
        let mut encoder_weights = Vec::new();
        let mut encoder_biases = Vec::new();

        for i in 0..encoder_layers.len() - 1 {
            let in_size = encoder_layers[i];
            let out_size = encoder_layers[i + 1];

            let weight = Array2::from_shape_fn((in_size, out_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (in_size + out_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(out_size);

            encoder_weights.push(weight);
            encoder_biases.push(bias);
        }

        // Initialize ODE function (dynamics in manifold space)
        let mut ode_layer_sizes = vec![self.n_components];
        ode_layer_sizes.extend(&self.hidden_layers);
        ode_layer_sizes.push(self.n_components); // Output same dim as input

        let mut ode_func_weights = Vec::new();
        let mut ode_func_biases = Vec::new();

        for i in 0..ode_layer_sizes.len() - 1 {
            let in_size = ode_layer_sizes[i];
            let out_size = ode_layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((in_size, out_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (in_size + out_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(out_size);

            ode_func_weights.push(weight);
            ode_func_biases.push(bias);
        }

        // Initialize decoder (expand back to original dimension)
        let decoder_layers = [self.n_components, input_dim];
        let mut decoder_weights = Vec::new();
        let mut decoder_biases = Vec::new();

        for i in 0..decoder_layers.len() - 1 {
            let in_size = decoder_layers[i];
            let out_size = decoder_layers[i + 1];

            let weight = Array2::from_shape_fn((in_size, out_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (in_size + out_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });
            let bias = Array1::zeros(out_size);

            decoder_weights.push(weight);
            decoder_biases.push(bias);
        }

        // TrainedNODE
        TrainedNODE {
            ode_func_weights,
            ode_func_biases,
            encoder_weights,
            encoder_biases,
            decoder_weights,
            decoder_biases,
            integration_time: self.integration_time,
            num_time_steps: self.num_time_steps,
            solver_type: self.solver_type.clone(),
        }
    }
}

impl NeuralODE<TrainedNODE> {
    fn encode(&self, x: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = x.to_owned();

        for (weights, biases) in self
            .state
            .encoder_weights
            .iter()
            .zip(self.state.encoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            // No activation for encoder to preserve manifold structure
        }

        Ok(activations)
    }

    fn decode(&self, z: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = z.to_owned();

        for (weights, biases) in self
            .state
            .decoder_weights
            .iter()
            .zip(self.state.decoder_biases.iter())
        {
            activations = activations.dot(weights) + biases;
            // No activation for decoder to preserve reconstruction
        }

        Ok(activations)
    }

    fn ode_func(&self, _t: f64, h: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = h.to_owned();

        for (i, (weights, biases)) in self
            .state
            .ode_func_weights
            .iter()
            .zip(self.state.ode_func_biases.iter())
            .enumerate()
        {
            activations = activations.dot(weights) + biases;
            // Apply activation function except for output layer
            if i < self.state.ode_func_weights.len() - 1 {
                activations.mapv_inplace(|x| x.tanh()); // Tanh for bounded dynamics
            }
        }

        Ok(activations)
    }

    fn solve_ode(&self, h0: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let dt = self.state.integration_time / self.state.num_time_steps as f64;
        let mut h = h0.to_owned();

        for i in 0..self.state.num_time_steps {
            let t = i as f64 * dt;

            match self.state.solver_type {
                ODESolverType::Euler => {
                    // Forward Euler: h_{n+1} = h_n + dt * f(t_n, h_n)
                    let dh_dt = self.ode_func(t, &h.view())?;
                    h = h + dh_dt * dt;
                }
                ODESolverType::RungeKutta4 => {
                    // 4th-order Runge-Kutta
                    let k1 = self.ode_func(t, &h.view())? * dt;
                    let k2 = self.ode_func(t + dt / 2.0, &(&h + &k1 * 0.5).view())? * dt;
                    let k3 = self.ode_func(t + dt / 2.0, &(&h + &k2 * 0.5).view())? * dt;
                    let k4 = self.ode_func(t + dt, &(&h + &k3).view())? * dt;

                    h = h + (k1 + k2 * 2.0 + k3 * 2.0 + k4) / 6.0;
                }
            }
        }

        Ok(h)
    }

    fn forward_pass(&self, x: &ArrayView2<f64>) -> SklResult<(Array2<f64>, Array2<f64>)> {
        // Encode to manifold space
        let h0 = self.encode(x)?;

        // Solve ODE in manifold space
        let h_final = self.solve_ode(&h0.view())?;

        // Decode back to original space
        let x_reconstructed = self.decode(&h_final.view())?;

        Ok((h_final, x_reconstructed))
    }

    pub fn get_manifold_trajectory(&self, x: &ArrayView2<f64>) -> SklResult<Vec<Array2<f64>>> {
        let h0 = self.encode(x)?;
        let dt = self.state.integration_time / self.state.num_time_steps as f64;
        let mut h = h0.clone();
        let mut trajectory = vec![h0];

        for i in 0..self.state.num_time_steps {
            let t = i as f64 * dt;

            match self.state.solver_type {
                ODESolverType::Euler => {
                    let dh_dt = self.ode_func(t, &h.view())?;
                    h = h + dh_dt * dt;
                }
                ODESolverType::RungeKutta4 => {
                    let k1 = self.ode_func(t, &h.view())? * dt;
                    let k2 = self.ode_func(t + dt / 2.0, &(&h + &k1 * 0.5).view())? * dt;
                    let k3 = self.ode_func(t + dt / 2.0, &(&h + &k2 * 0.5).view())? * dt;
                    let k4 = self.ode_func(t + dt, &(&h + &k3).view())? * dt;

                    h = h + (k1 + k2 * 2.0 + k3 * 2.0 + k4) / 6.0;
                }
            }

            trajectory.push(h.clone());
        }

        Ok(trajectory)
    }
}

impl Estimator for NeuralODE<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<Array2<f64>, ()> for NeuralODE<Untrained> {
    type Fitted = NeuralODE<TrainedNODE>;

    fn fit(self, x: &Array2<f64>, _y: &()) -> SklResult<Self::Fitted> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 || n_features == 0 {
            return Err(SklearsError::InvalidInput("Empty input data".to_string()));
        }

        if self.n_components >= n_features {
            return Err(SklearsError::InvalidInput(
                "n_components must be less than number of features".to_string(),
            ));
        }

        if self.integration_time <= 0.0 {
            return Err(SklearsError::InvalidInput(
                "integration_time must be positive".to_string(),
            ));
        }

        if self.num_time_steps == 0 {
            return Err(SklearsError::InvalidInput(
                "num_time_steps must be positive".to_string(),
            ));
        }

        let state = self.initialize_node_weights(n_features);

        // Note: For a complete implementation, we would need to implement
        // the training loop with gradient computation through the ODE solver.
        // This requires adjoint sensitivity method for backpropagation.

        Ok(NeuralODE {
            n_components: self.n_components,
            hidden_layers: self.hidden_layers,
            integration_time: self.integration_time,
            num_time_steps: self.num_time_steps,
            solver_type: self.solver_type,
            learning_rate: self.learning_rate,
            epochs: self.epochs,
            state,
        })
    }
}

impl Transform<Array2<f64>, Array2<f64>> for NeuralODE<TrainedNODE> {
    fn transform(&self, x: &Array2<f64>) -> SklResult<Array2<f64>> {
        let (h_final, _) = self.forward_pass(&x.view())?;
        Ok(h_final)
    }
}

/// Continuous Normalizing Flow (CNF) - Invertible transformation through continuous dynamics
#[derive(Debug, Clone)]
pub struct ContinuousNormalizingFlow<S = Untrained> {
    n_components: usize,
    hidden_layers: Vec<usize>,
    integration_time: f64,
    num_time_steps: usize,
    solver_type: ODESolverType,
    trace_estimator: TraceEstimator,
    learning_rate: f64,
    epochs: usize,
    state: S,
}

#[derive(Debug, Clone)]
pub enum TraceEstimator {
    /// Exact
    Exact, // For small dimensions
    /// Hutchinson
    Hutchinson, // Stochastic trace estimation
    /// RademacherRandom
    RademacherRandom, // Random projection trace estimation
}

#[derive(Debug, Clone)]
pub struct TrainedCNF {
    dynamics_weights: Vec<Array2<f64>>,
    dynamics_biases: Vec<Array1<f64>>,
    integration_time: f64,
    num_time_steps: usize,
    solver_type: ODESolverType,
    trace_estimator: TraceEstimator,
    input_dim: usize,
}

impl ContinuousNormalizingFlow<Untrained> {
    pub fn new(n_components: usize) -> Self {
        Self {
            n_components,
            hidden_layers: vec![64, 32, 64],
            integration_time: 1.0,
            num_time_steps: 20,
            solver_type: ODESolverType::RungeKutta4,
            trace_estimator: TraceEstimator::Hutchinson,
            learning_rate: 0.001,
            epochs: 100,
            state: Untrained,
        }
    }

    pub fn with_hidden_layers(mut self, layers: Vec<usize>) -> Self {
        self.hidden_layers = layers;
        self
    }

    pub fn with_integration_time(mut self, time: f64) -> Self {
        self.integration_time = time;
        self
    }

    pub fn with_num_time_steps(mut self, steps: usize) -> Self {
        self.num_time_steps = steps;
        self
    }

    pub fn with_solver_type(mut self, solver: ODESolverType) -> Self {
        self.solver_type = solver;
        self
    }

    pub fn with_trace_estimator(mut self, estimator: TraceEstimator) -> Self {
        self.trace_estimator = estimator;
        self
    }

    pub fn with_learning_rate(mut self, lr: f64) -> Self {
        self.learning_rate = lr;
        self
    }

    pub fn with_epochs(mut self, epochs: usize) -> Self {
        self.epochs = epochs;
        self
    }

    fn initialize_cnf_weights(&self, input_dim: usize) -> TrainedCNF {
        let mut rng = thread_rng();

        // Initialize dynamics function (velocity field)
        let mut dynamics_layer_sizes = vec![self.n_components];
        dynamics_layer_sizes.extend(&self.hidden_layers);
        dynamics_layer_sizes.push(self.n_components); // Output same dimension as input

        let mut dynamics_weights = Vec::new();
        let mut dynamics_biases = Vec::new();

        for i in 0..dynamics_layer_sizes.len() - 1 {
            let in_size = dynamics_layer_sizes[i];
            let out_size = dynamics_layer_sizes[i + 1];

            let weight = Array2::from_shape_fn((in_size, out_size), |(_, _)| {
                Normal::new(0.0, (2.0 / (in_size + out_size) as f64).sqrt())
                    .expect("operation should succeed")
                    .sample(&mut rng)
            });

            // Initialize biases to zero except for the final layer (small random bias)
            let bias = if i == dynamics_layer_sizes.len() - 2 {
                Array1::from_shape_fn(out_size, |_| {
                    Normal::new(0.0, 0.01)
                        .expect("operation should succeed")
                        .sample(&mut rng)
                })
            } else {
                Array1::zeros(out_size)
            };

            dynamics_weights.push(weight);
            dynamics_biases.push(bias);
        }

        // TrainedCNF
        TrainedCNF {
            dynamics_weights,
            dynamics_biases,
            integration_time: self.integration_time,
            num_time_steps: self.num_time_steps,
            solver_type: self.solver_type.clone(),
            trace_estimator: self.trace_estimator.clone(),
            input_dim,
        }
    }
}

impl ContinuousNormalizingFlow<TrainedCNF> {
    fn validate_input_dim(&self, data: &ArrayView2<f64>) -> SklResult<()> {
        if data.ncols() != self.state.input_dim {
            return Err(SklearsError::InvalidInput(format!(
                "Expected input with {} features, but received {}",
                self.state.input_dim,
                data.ncols()
            )));
        }

        Ok(())
    }

    fn split_input(&self, data: &ArrayView2<f64>) -> (Array2<f64>, Option<Array2<f64>>) {
        let latent = data
            .slice(scirs2_core::ndarray::s![.., 0..self.n_components])
            .to_owned();

        if self.state.input_dim > self.n_components {
            let residual = data
                .slice(scirs2_core::ndarray::s![
                    ..,
                    self.n_components..self.state.input_dim
                ])
                .to_owned();
            (latent, Some(residual))
        } else {
            (latent, None)
        }
    }

    fn combine_latent(&self, latent: &Array2<f64>, residual: Option<&Array2<f64>>) -> Array2<f64> {
        if self.state.input_dim == self.n_components {
            return latent.clone();
        }

        let mut combined = Array2::zeros((latent.nrows(), self.state.input_dim));
        combined
            .slice_mut(scirs2_core::ndarray::s![.., 0..self.n_components])
            .assign(latent);

        if let Some(residual) = residual {
            combined
                .slice_mut(scirs2_core::ndarray::s![
                    ..,
                    self.n_components..self.state.input_dim
                ])
                .assign(residual);
        }

        combined
    }

    fn integrate_latent(
        &self,
        latent0: &Array2<f64>,
        forward: bool,
    ) -> SklResult<(Array2<f64>, Array1<f64>)> {
        let mut latent = latent0.clone();
        let mut log_det = Array1::zeros(latent.nrows());

        if self.state.num_time_steps == 0 {
            return Ok((latent, log_det));
        }

        let base_dt = self.state.integration_time / self.state.num_time_steps as f64;
        let dt_signed = if forward { base_dt } else { -base_dt };

        for step in 0..self.state.num_time_steps {
            let t = if forward {
                step as f64 * base_dt
            } else {
                self.state.integration_time - step as f64 * base_dt
            };

            let trace = self.compute_trace_jacobian(t, &latent.view())?;
            let trace_contrib = trace.mapv(|val| val * dt_signed);
            log_det = log_det + trace_contrib;

            match self.state.solver_type {
                ODESolverType::Euler => {
                    let dz_dt = self.dynamics(t, &latent.view())?;
                    latent = latent + dz_dt * dt_signed;
                }
                ODESolverType::RungeKutta4 => {
                    let k1 = self.dynamics(t, &latent.view())? * dt_signed;
                    let k2 = self.dynamics(t + dt_signed / 2.0, &(&latent + &k1 * 0.5).view())?
                        * dt_signed;
                    let k3 = self.dynamics(t + dt_signed / 2.0, &(&latent + &k2 * 0.5).view())?
                        * dt_signed;
                    let k4 = self.dynamics(t + dt_signed, &(&latent + &k3).view())? * dt_signed;

                    latent = latent + (k1 + k2 * 2.0 + k3 * 2.0 + k4) / 6.0;
                }
            }
        }

        Ok((latent, log_det))
    }

    fn dynamics(&self, _t: f64, z: &ArrayView2<f64>) -> SklResult<Array2<f64>> {
        let mut activations = z.to_owned();

        for (i, (weights, biases)) in self
            .state
            .dynamics_weights
            .iter()
            .zip(self.state.dynamics_biases.iter())
            .enumerate()
        {
            activations = activations.dot(weights) + biases;
            // Apply activation function except for output layer
            if i < self.state.dynamics_weights.len() - 1 {
                activations.mapv_inplace(|x| x.tanh()); // Smooth, bounded activation
            }
        }

        Ok(activations)
    }

    fn compute_trace_jacobian(&self, _t: f64, z: &ArrayView2<f64>) -> SklResult<Array1<f64>> {
        let epsilon = 1e-6;
        let (n_samples, n_dims) = z.dim();

        match self.state.trace_estimator {
            TraceEstimator::Exact => {
                // Compute exact trace using finite differences (expensive for large dimensions)
                let mut traces = Array1::zeros(n_samples);

                for sample_idx in 0..n_samples {
                    let z_sample = z.slice(scirs2_core::ndarray::s![sample_idx, ..]).to_owned();
                    let mut trace = 0.0;

                    for dim_idx in 0..n_dims {
                        // Compute partial derivative using finite differences
                        let mut z_plus = z_sample.clone();
                        let mut z_minus = z_sample.clone();

                        z_plus[dim_idx] += epsilon;
                        z_minus[dim_idx] -= epsilon;

                        let z_plus_2d = z_plus.view().insert_axis(scirs2_core::ndarray::Axis(0));
                        let z_minus_2d = z_minus.view().insert_axis(scirs2_core::ndarray::Axis(0));

                        let f_plus = self.dynamics(_t, &z_plus_2d)?;
                        let f_minus = self.dynamics(_t, &z_minus_2d)?;

                        // Partial derivative of f_i with respect to z_i
                        let partial_deriv =
                            (f_plus[[0, dim_idx]] - f_minus[[0, dim_idx]]) / (2.0 * epsilon);
                        trace += partial_deriv;
                    }

                    traces[sample_idx] = trace;
                }

                Ok(traces)
            }
            TraceEstimator::Hutchinson => {
                // Hutchinson's stochastic trace estimator: Tr(J) ≈ E[ε^T J ε] where ε ~ Rademacher
                let mut rng = thread_rng();
                let mut traces = Array1::zeros(n_samples);

                for sample_idx in 0..n_samples {
                    let z_sample = z.slice(scirs2_core::ndarray::s![sample_idx, ..]).to_owned();

                    // Generate random Rademacher vector
                    let epsilon_vec = Array1::from_shape_fn(n_dims, |_| {
                        if rng.random::<f64>() < 0.5 {
                            -1.0
                        } else {
                            1.0
                        }
                    });

                    // Compute J * ε using finite differences
                    let z_plus = &z_sample + &epsilon_vec * epsilon;
                    let z_minus = &z_sample - &epsilon_vec * epsilon;

                    let z_plus_2d = z_plus.view().insert_axis(scirs2_core::ndarray::Axis(0));
                    let z_minus_2d = z_minus.view().insert_axis(scirs2_core::ndarray::Axis(0));

                    let f_plus = self.dynamics(_t, &z_plus_2d)?;
                    let f_minus = self.dynamics(_t, &z_minus_2d)?;

                    let jv = (f_plus.slice(scirs2_core::ndarray::s![0, ..]).to_owned()
                        - f_minus.slice(scirs2_core::ndarray::s![0, ..]).to_owned())
                        / (2.0 * epsilon);

                    // ε^T * (J * ε) = ε^T * jv
                    let trace_estimate = epsilon_vec.dot(&jv);
                    traces[sample_idx] = trace_estimate;
                }

                Ok(traces)
            }
            TraceEstimator::RademacherRandom => {
                // Alternative stochastic estimator
                let mut rng = thread_rng();
                let mut traces = Array1::zeros(n_samples);

                for sample_idx in 0..n_samples {
                    let z_sample = z.slice(scirs2_core::ndarray::s![sample_idx, ..]).to_owned();

                    // Generate random Gaussian vector
                    let epsilon_vec = Array1::from_shape_fn(n_dims, |_| {
                        Normal::new(0.0, 1.0)
                            .expect("operation should succeed")
                            .sample(&mut rng)
                    });

                    let z_plus = &z_sample + &epsilon_vec * epsilon;
                    let z_minus = &z_sample - &epsilon_vec * epsilon;

                    let z_plus_2d = z_plus.view().insert_axis(scirs2_core::ndarray::Axis(0));
                    let z_minus_2d = z_minus.view().insert_axis(scirs2_core::ndarray::Axis(0));

                    let f_plus = self.dynamics(_t, &z_plus_2d)?;
                    let f_minus = self.dynamics(_t, &z_minus_2d)?;

                    let jv = (f_plus.slice(scirs2_core::ndarray::s![0, ..]).to_owned()
                        - f_minus.slice(scirs2_core::ndarray::s![0, ..]).to_owned())
                        / (2.0 * epsilon);

                    let trace_estimate = epsilon_vec.dot(&jv);
                    traces[sample_idx] = trace_estimate;
                }

                Ok(traces)
            }
        }
    }

    pub fn forward_flow(&self, z0: &ArrayView2<f64>) -> SklResult<(Array2<f64>, Array1<f64>)> {
        self.validate_input_dim(z0)?;
        let (latent0, residual) = self.split_input(z0);
        let (latent_final, log_det) = self.integrate_latent(&latent0, true)?;
        let combined = self.combine_latent(&latent_final, residual.as_ref());

        Ok((combined, log_det))
    }

    pub fn backward_flow(&self, z1: &ArrayView2<f64>) -> SklResult<(Array2<f64>, Array1<f64>)> {
        self.validate_input_dim(z1)?;
        let (latent0, residual) = self.split_input(z1);
        let (latent_initial, log_det) = self.integrate_latent(&latent0, false)?;
        let combined = self.combine_latent(&latent_initial, residual.as_ref());

        Ok((combined, log_det))
    }

    pub fn log_likelihood(
        &self,
        x: &ArrayView2<f64>,
        base_log_prob: f64,
    ) -> SklResult<Array1<f64>> {
        let (_, log_det_jac) = self.forward_flow(x)?;

        // log p(x) = log p(z) + log |det J|
        // where z = f(x) and J is the Jacobian of the transformation
        let log_likelihood = Array1::from_elem(x.nrows(), base_log_prob) + log_det_jac;

        Ok(log_likelihood)
    }

    pub fn sample(
        &self,
        n_samples: usize,
        base_distribution: &dyn Fn(usize, usize) -> Array2<f64>,
    ) -> SklResult<Array2<f64>> {
        // Sample from base distribution
        let latent_target = base_distribution(n_samples, self.n_components);
        let (latent_source, _) = self.integrate_latent(&latent_target, false)?;

        if self.state.input_dim == self.n_components {
            Ok(latent_source)
        } else {
            let mut combined = Array2::zeros((n_samples, self.state.input_dim));
            combined
                .slice_mut(scirs2_core::ndarray::s![.., 0..self.n_components])
                .assign(&latent_source);
            Ok(combined)
        }
    }
}

impl Estimator for ContinuousNormalizingFlow<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<Array2<f64>, ()> for ContinuousNormalizingFlow<Untrained> {
    type Fitted = ContinuousNormalizingFlow<TrainedCNF>;

    fn fit(self, x: &Array2<f64>, _y: &()) -> SklResult<Self::Fitted> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 || n_features == 0 {
            return Err(SklearsError::InvalidInput("Empty input data".to_string()));
        }

        if self.n_components > n_features {
            return Err(SklearsError::InvalidInput(
                "n_components should not exceed number of features".to_string(),
            ));
        }

        if self.integration_time <= 0.0 {
            return Err(SklearsError::InvalidInput(
                "integration_time must be positive".to_string(),
            ));
        }

        if self.num_time_steps == 0 {
            return Err(SklearsError::InvalidInput(
                "num_time_steps must be positive".to_string(),
            ));
        }

        let state = self.initialize_cnf_weights(n_features);

        // Note: For a complete implementation, we would need to implement
        // the training loop with maximum likelihood estimation and
        // gradient computation through the CNF.

        Ok(ContinuousNormalizingFlow {
            n_components: self.n_components,
            hidden_layers: self.hidden_layers,
            integration_time: self.integration_time,
            num_time_steps: self.num_time_steps,
            solver_type: self.solver_type,
            trace_estimator: self.trace_estimator,
            learning_rate: self.learning_rate,
            epochs: self.epochs,
            state,
        })
    }
}

impl Transform<Array2<f64>, Array2<f64>> for ContinuousNormalizingFlow<TrainedCNF> {
    fn transform(&self, x: &Array2<f64>) -> SklResult<Array2<f64>> {
        self.validate_input_dim(&x.view())?;
        let (latent0, _) = self.split_input(&x.view());
        let (latent_final, _) = self.integrate_latent(&latent0, true)?;
        Ok(latent_final)
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_autoencoder_basic() {
        let data = Array2::from_shape_fn((100, 10), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let autoencoder = AutoencoderManifold::new(5)
            .with_epochs(10)
            .with_batch_size(16);

        let fitted = autoencoder
            .fit(&data, &())
            .expect("operation should succeed");
        let encoded = fitted.transform(&data).expect("operation should succeed");

        assert_eq!(encoded.ncols(), 5);
        assert_eq!(encoded.nrows(), 100);
    }

    #[test]
    fn test_autoencoder_invalid_params() {
        let data = Array2::from_shape_fn((50, 5), |(i, j)| i as f64 + j as f64);
        let autoencoder = AutoencoderManifold::new(10); // n_components > n_features

        assert!(autoencoder.fit(&data, &()).is_err());
    }

    #[test]
    fn test_variational_autoencoder_basic() {
        let vae = VariationalAutoencoder::new(3).with_epochs(5).with_beta(0.5);

        assert_eq!(vae.n_components, 3);
        assert_eq!(vae.beta, 0.5);
    }

    #[test]
    fn test_autoencoder_configuration() {
        let autoencoder = AutoencoderManifold::new(3)
            .with_hidden_layers(vec![64, 32])
            .with_learning_rate(0.01)
            .with_batch_size(64)
            .with_epochs(50);

        assert_eq!(autoencoder.hidden_layers, vec![64, 32]);
        assert_eq!(autoencoder.learning_rate, 0.01);
        assert_eq!(autoencoder.batch_size, 64);
        assert_eq!(autoencoder.epochs, 50);
    }

    #[test]
    fn test_autoencoder_empty_data() {
        let data = Array2::zeros((0, 5));
        let autoencoder = AutoencoderManifold::new(2);

        assert!(autoencoder.fit(&data, &()).is_err());
    }

    #[test]
    fn test_adversarial_autoencoder_basic() {
        let data = Array2::from_shape_fn((100, 10), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let aae = AdversarialAutoencoder::new(5)
            .with_epochs(10)
            .with_batch_size(16)
            .with_adversarial_weight(0.5);

        let fitted = aae.fit(&data, &()).expect("operation should succeed");
        let encoded = fitted.transform(&data).expect("operation should succeed");

        assert_eq!(encoded.ncols(), 5);
        assert_eq!(encoded.nrows(), 100);
    }

    #[test]
    fn test_adversarial_autoencoder_configuration() {
        let aae = AdversarialAutoencoder::new(3)
            .with_hidden_layers(vec![64, 32])
            .with_discriminator_layers(vec![32, 16])
            .with_learning_rate(0.01)
            .with_adversarial_weight(2.0)
            .with_prior_type(PriorType::Uniform);

        assert_eq!(aae.hidden_layers, vec![64, 32]);
        assert_eq!(aae.discriminator_layers, vec![32, 16]);
        assert_eq!(aae.learning_rate, 0.01);
        assert_eq!(aae.adversarial_weight, 2.0);
        matches!(aae.prior_type, PriorType::Uniform);
    }

    #[test]
    fn test_adversarial_autoencoder_prior_types() {
        let gaussian_aae = AdversarialAutoencoder::new(3).with_prior_type(PriorType::Gaussian);
        assert!(matches!(gaussian_aae.prior_type, PriorType::Gaussian));

        let uniform_aae = AdversarialAutoencoder::new(3).with_prior_type(PriorType::Uniform);
        assert!(matches!(uniform_aae.prior_type, PriorType::Uniform));

        let categorical_aae =
            AdversarialAutoencoder::new(3).with_prior_type(PriorType::Categorical(5));
        assert!(matches!(
            categorical_aae.prior_type,
            PriorType::Categorical(5)
        ));
    }

    #[test]
    fn test_adversarial_autoencoder_invalid_params() {
        let data = Array2::from_shape_fn((50, 5), |(i, j)| i as f64 + j as f64);
        let aae = AdversarialAutoencoder::new(10); // n_components > n_features

        assert!(aae.fit(&data, &()).is_err());
    }

    #[test]
    fn test_adversarial_autoencoder_empty_data() {
        let data = Array2::zeros((0, 5));
        let aae = AdversarialAutoencoder::new(2);

        assert!(aae.fit(&data, &()).is_err());
    }

    #[test]
    fn test_neural_ode_basic() {
        let data = Array2::from_shape_fn((50, 8), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let node = NeuralODE::new(3)
            .with_integration_time(0.5)
            .with_num_time_steps(5)
            .with_solver_type(ODESolverType::RungeKutta4);

        let fitted = node.fit(&data, &()).expect("operation should succeed");
        let encoded = fitted.transform(&data).expect("operation should succeed");

        assert_eq!(encoded.ncols(), 3);
        assert_eq!(encoded.nrows(), 50);
    }

    #[test]
    fn test_neural_ode_configuration() {
        let node = NeuralODE::new(4)
            .with_hidden_layers(vec![32, 16])
            .with_integration_time(2.0)
            .with_num_time_steps(20)
            .with_solver_type(ODESolverType::Euler)
            .with_learning_rate(0.01)
            .with_epochs(50);

        assert_eq!(node.hidden_layers, vec![32, 16]);
        assert_eq!(node.integration_time, 2.0);
        assert_eq!(node.num_time_steps, 20);
        assert!(matches!(node.solver_type, ODESolverType::Euler));
        assert_eq!(node.learning_rate, 0.01);
        assert_eq!(node.epochs, 50);
    }

    #[test]
    fn test_neural_ode_solver_types() {
        let euler_node = NeuralODE::new(2).with_solver_type(ODESolverType::Euler);
        assert!(matches!(euler_node.solver_type, ODESolverType::Euler));

        let rk4_node = NeuralODE::new(2).with_solver_type(ODESolverType::RungeKutta4);
        assert!(matches!(rk4_node.solver_type, ODESolverType::RungeKutta4));
    }

    #[test]
    fn test_neural_ode_invalid_params() {
        let data = Array2::from_shape_fn((30, 5), |(i, j)| i as f64 + j as f64);

        // n_components >= n_features
        let node = NeuralODE::new(10);
        assert!(node.fit(&data, &()).is_err());

        // Invalid integration time
        let node = NeuralODE::new(2).with_integration_time(-1.0);
        assert!(node.fit(&data, &()).is_err());

        // Invalid time steps
        let node = NeuralODE::new(2).with_num_time_steps(0);
        assert!(node.fit(&data, &()).is_err());
    }

    #[test]
    fn test_neural_ode_empty_data() {
        let data = Array2::zeros((0, 5));
        let node = NeuralODE::new(2);

        assert!(node.fit(&data, &()).is_err());
    }

    #[test]
    fn test_neural_ode_manifold_trajectory() {
        let data = Array2::from_shape_fn((10, 6), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let node = NeuralODE::new(2).with_num_time_steps(3);

        let fitted = node.fit(&data, &()).expect("operation should succeed");
        let trajectory = fitted
            .get_manifold_trajectory(&data.view())
            .expect("operation should succeed");

        // Should have initial state + 3 time steps = 4 total states
        assert_eq!(trajectory.len(), 4);

        // Each state should have the same dimensions
        for state in &trajectory {
            assert_eq!(state.ncols(), 2); // n_components
            assert_eq!(state.nrows(), 10); // n_samples
        }
    }

    #[test]
    fn test_continuous_normalizing_flow_basic() {
        let data = Array2::from_shape_fn((30, 4), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let cnf = ContinuousNormalizingFlow::new(3)
            .with_integration_time(0.5)
            .with_num_time_steps(8)
            .with_trace_estimator(TraceEstimator::Hutchinson);

        let fitted = cnf.fit(&data, &()).expect("operation should succeed");
        let transformed = fitted.transform(&data).expect("operation should succeed");

        assert_eq!(transformed.ncols(), 3);
        assert_eq!(transformed.nrows(), 30);
    }

    #[test]
    fn test_continuous_normalizing_flow_configuration() {
        let cnf = ContinuousNormalizingFlow::new(2)
            .with_hidden_layers(vec![32, 16, 32])
            .with_integration_time(2.0)
            .with_num_time_steps(25)
            .with_solver_type(ODESolverType::Euler)
            .with_trace_estimator(TraceEstimator::Exact)
            .with_learning_rate(0.005)
            .with_epochs(150);

        assert_eq!(cnf.hidden_layers, vec![32, 16, 32]);
        assert_eq!(cnf.integration_time, 2.0);
        assert_eq!(cnf.num_time_steps, 25);
        assert!(matches!(cnf.solver_type, ODESolverType::Euler));
        assert!(matches!(cnf.trace_estimator, TraceEstimator::Exact));
        assert_eq!(cnf.learning_rate, 0.005);
        assert_eq!(cnf.epochs, 150);
    }

    #[test]
    fn test_cnf_trace_estimator_types() {
        let exact_cnf =
            ContinuousNormalizingFlow::new(2).with_trace_estimator(TraceEstimator::Exact);
        assert!(matches!(exact_cnf.trace_estimator, TraceEstimator::Exact));

        let hutchinson_cnf =
            ContinuousNormalizingFlow::new(2).with_trace_estimator(TraceEstimator::Hutchinson);
        assert!(matches!(
            hutchinson_cnf.trace_estimator,
            TraceEstimator::Hutchinson
        ));

        let rademacher_cnf = ContinuousNormalizingFlow::new(2)
            .with_trace_estimator(TraceEstimator::RademacherRandom);
        assert!(matches!(
            rademacher_cnf.trace_estimator,
            TraceEstimator::RademacherRandom
        ));
    }

    #[test]
    fn test_cnf_forward_backward_flow() {
        let data = Array2::from_shape_fn((5, 3), |(i, j)| i as f64 + j as f64);
        let cnf = ContinuousNormalizingFlow::new(3)
            .with_num_time_steps(5)
            .with_integration_time(1.0);

        let fitted = cnf.fit(&data, &()).expect("operation should succeed");

        // Test forward flow
        let (z, log_det_forward) = fitted
            .forward_flow(&data.view())
            .expect("operation should succeed");
        assert_eq!(z.dim(), data.dim());
        assert_eq!(log_det_forward.len(), data.nrows());

        // Test backward flow
        let (x_reconstructed, log_det_backward) = fitted
            .backward_flow(&z.view())
            .expect("operation should succeed");
        assert_eq!(x_reconstructed.dim(), data.dim());
        assert_eq!(log_det_backward.len(), data.nrows());

        // Check that forward and backward flows are approximately inverses
        let reconstruction_error = (&data - &x_reconstructed).mapv(|x| x.abs()).sum();
        assert!(reconstruction_error < 1.0); // Allow some numerical error
    }

    #[test]
    fn test_cnf_log_likelihood() {
        let data = Array2::from_shape_fn((10, 2), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let cnf = ContinuousNormalizingFlow::new(2).with_num_time_steps(5);

        let fitted = cnf.fit(&data, &()).expect("operation should succeed");
        let log_likelihood = fitted
            .log_likelihood(&data.view(), 0.0)
            .expect("operation should succeed");

        assert_eq!(log_likelihood.len(), data.nrows());
        // Check that all likelihood values are finite
        for &ll in log_likelihood.iter() {
            assert!(ll.is_finite());
        }
    }

    #[test]
    fn test_cnf_sampling() {
        let data = Array2::from_shape_fn((20, 3), |(i, j)| i as f64 * 0.1 + j as f64 * 0.01);
        let cnf = ContinuousNormalizingFlow::new(3).with_num_time_steps(5);

        let fitted = cnf.fit(&data, &()).expect("operation should succeed");

        // Define a simple Gaussian base distribution
        let gaussian_sampler = |n_samples: usize, n_dims: usize| -> Array2<f64> {
            let mut rng = thread_rng();
            Array2::from_shape_fn((n_samples, n_dims), |(_, _)| {
                Normal::new(0.0, 1.0)
                    .expect("operation should succeed")
                    .sample(&mut rng)
            })
        };

        let samples = fitted
            .sample(15, &gaussian_sampler)
            .expect("operation should succeed");
        assert_eq!(samples.nrows(), 15);
        assert_eq!(samples.ncols(), 3);

        // Check that samples are finite
        for &val in samples.iter() {
            assert!(val.is_finite());
        }
    }

    #[test]
    fn test_cnf_invalid_params() {
        let data = Array2::from_shape_fn((25, 4), |(i, j)| i as f64 + j as f64);

        // n_components > n_features
        let cnf = ContinuousNormalizingFlow::new(6);
        assert!(cnf.fit(&data, &()).is_err());

        // Invalid integration time
        let cnf = ContinuousNormalizingFlow::new(2).with_integration_time(-0.5);
        assert!(cnf.fit(&data, &()).is_err());

        // Invalid time steps
        let cnf = ContinuousNormalizingFlow::new(2).with_num_time_steps(0);
        assert!(cnf.fit(&data, &()).is_err());
    }

    #[test]
    fn test_cnf_empty_data() {
        let data = Array2::zeros((0, 4));
        let cnf = ContinuousNormalizingFlow::new(2);

        assert!(cnf.fit(&data, &()).is_err());
    }
}