From 9182ab9fca9ee78d875db7d2854eed8d4e87a967 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 12 Jul 2025 01:00:00 +0100 Subject: [PATCH] Add test for PCA fit with n_components greater than n_features to verify behavior --- src/compute/models/pca.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/compute/models/pca.rs b/src/compute/models/pca.rs index 5c03293..f75231d 100644 --- a/src/compute/models/pca.rs +++ b/src/compute/models/pca.rs @@ -50,7 +50,7 @@ mod tests { // 2.0, 2.0 // 3.0, 3.0 let data = Matrix::from_rows_vec(vec![1.0, 1.0, 2.0, 2.0, 3.0, 3.0], 3, 2); - let (n_samples, n_features) = data.shape(); + let (_n_samples, _n_features) = data.shape(); let pca = PCA::fit(&data, 1, 0); // n_components = 1, iters is unused @@ -87,4 +87,28 @@ mod tests { assert!((transformed_data.get(1, 0) - 0.0).abs() < EPSILON); assert!((transformed_data.get(2, 0) - 2.0).abs() < EPSILON); } + + #[test] + fn test_pca_fit_break_branch() { + // Data with 2 features + let data = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2); + let (_n_samples, n_features) = data.shape(); + + // Set n_components greater than n_features to trigger the break branch + let n_components_large = n_features + 1; + let pca = PCA::fit(&data, n_components_large, 0); + + // The components matrix should be initialized with n_components_large rows, + // but only the first n_features rows should be copied from the covariance matrix. + // The remaining rows should be zeros. + assert_eq!(pca.components.rows(), n_components_large); + assert_eq!(pca.components.cols(), n_features); + + // Verify that rows beyond n_features are all zeros + for i in n_features..n_components_large { + for j in 0..n_features { + assert!((pca.components.get(i, j) - 0.0).abs() < EPSILON); + } + } + } }