scirs2-transform 0.4.0

Data transformation module for SciRS2 (scirs2-transform)
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
//! Incremental SVD via Brand's rank-one update algorithm
//!
//! Maintains a thin SVD decomposition `A ≈ U * diag(S) * V^T` that is updated
//! as new rows or columns arrive, without recomputing from scratch.
//!
//! # References
//!
//! * Brand, M. (2006). "Fast low-rank modifications of the thin singular value
//!   decomposition". *Linear Algebra and its Applications*, 415(1), 20–30.
//!
//! # Features
//!
//! - Single row/column update
//! - Batch update (multiple rows)
//! - Rank truncation (keep only top-k singular values)
//! - Basic downdate (remove a previously added row)

use scirs2_core::ndarray::{Array1, Array2, Axis};
use scirs2_linalg::svd;

use crate::error::{Result, TransformError};

/// Incremental SVD that updates a thin SVD as new data arrives.
///
/// Stores the truncated decomposition `A ≈ U * diag(S) * V^T` where
/// `U` is (m x k), `S` is (k,), `V` is (n x k), and k is the truncation rank.
#[derive(Debug, Clone)]
pub struct IncrementalSVD {
    /// Left singular vectors: m x k (rows added correspond to m growing).
    u: Option<Array2<f64>>,
    /// Singular values: k.
    s: Option<Array1<f64>>,
    /// Right singular vectors: n x k.
    vt: Option<Array2<f64>>,
    /// Maximum rank (number of singular values to keep).
    max_rank: usize,
    /// Number of rows currently represented.
    n_rows: usize,
    /// Number of columns.
    n_cols: usize,
}

impl IncrementalSVD {
    /// Create a new Incremental SVD with a maximum rank.
    ///
    /// `max_rank` is the maximum number of singular values/vectors to retain.
    pub fn new(max_rank: usize) -> Result<Self> {
        if max_rank == 0 {
            return Err(TransformError::InvalidInput(
                "max_rank must be positive".to_string(),
            ));
        }
        Ok(Self {
            u: None,
            s: None,
            vt: None,
            max_rank,
            n_rows: 0,
            n_cols: 0,
        })
    }

    /// Initialize from a data matrix by computing its truncated SVD.
    pub fn initialize(&mut self, data: &Array2<f64>) -> Result<()> {
        let (m, n) = (data.nrows(), data.ncols());
        if m == 0 || n == 0 {
            return Err(TransformError::InvalidInput(
                "Data matrix must be non-empty".to_string(),
            ));
        }

        let (u_full, s_full, vt_full) = compute_svd(data)?;
        let k = self.max_rank.min(s_full.len());

        self.u = Some(u_full.slice(scirs2_core::ndarray::s![.., ..k]).to_owned());
        self.s = Some(s_full.slice(scirs2_core::ndarray::s![..k]).to_owned());
        self.vt = Some(vt_full.slice(scirs2_core::ndarray::s![..k, ..]).to_owned());
        self.n_rows = m;
        self.n_cols = n;
        Ok(())
    }

    /// Add a single row to the decomposition.
    ///
    /// This updates the SVD to incorporate a new data row without
    /// recomputing from scratch.
    pub fn add_row(&mut self, row: &Array1<f64>) -> Result<()> {
        if let (Some(u), Some(s), Some(vt)) = (&self.u, &self.s, &self.vt) {
            if row.len() != self.n_cols {
                return Err(TransformError::InvalidInput(format!(
                    "Expected {} columns, got {}",
                    self.n_cols,
                    row.len()
                )));
            }

            let k = s.len();

            // Project row onto current right singular vectors: p = V * row
            // where V = vt^T
            let mut p = Array1::zeros(k);
            for j in 0..k {
                let mut dot = 0.0;
                for d in 0..self.n_cols {
                    dot += vt[[j, d]] * row[d];
                }
                p[j] = dot;
            }

            // Residual: r = row - V^T * p (component orthogonal to current column space)
            let mut residual = row.clone();
            for j in 0..k {
                for d in 0..self.n_cols {
                    residual[d] -= vt[[j, d]] * p[j];
                }
            }

            let r_norm = residual.dot(&residual).sqrt();

            // Build the (k+1) x (k+1) matrix to SVD:
            // K = [ diag(S)  p ]
            //     [   0      r_norm ]
            let new_k = k + 1;
            let mut k_mat = Array2::zeros((new_k, new_k));
            for j in 0..k {
                k_mat[[j, j]] = s[j];
                k_mat[[j, k]] = p[j];
            }
            k_mat[[k, k]] = r_norm;

            // SVD of the small matrix
            let (u_k, s_k, vt_k) = compute_svd(&k_mat)?;

            // Truncate to max_rank
            let new_rank = self.max_rank.min(s_k.len());

            // Update U: U_new = [U  0] * u_k[:, :new_rank]
            //                   [0  1]
            // The last row of U corresponds to the new row
            let old_m = self.n_rows;
            let mut new_u = Array2::zeros((old_m + 1, new_rank));
            for i in 0..old_m {
                for j in 0..new_rank {
                    let mut val = 0.0;
                    for l in 0..k {
                        val += u[[i, l]] * u_k[[l, j]];
                    }
                    new_u[[i, j]] = val;
                }
            }
            // Last row from u_k[k, :new_rank]
            for j in 0..new_rank {
                new_u[[old_m, j]] = u_k[[k, j]];
            }

            // Update V^T: V^T_new = vt_k[:new_rank, :] * [V^T]
            //                                              [r_hat^T]
            // where r_hat = residual / r_norm (or zero if r_norm ≈ 0)
            let mut new_vt = Array2::zeros((new_rank, self.n_cols));
            for i in 0..new_rank {
                for d in 0..self.n_cols {
                    let mut val = 0.0;
                    for l in 0..k {
                        val += vt_k[[i, l]] * vt[[l, d]];
                    }
                    if r_norm > 1e-15 {
                        val += vt_k[[i, k]] * (residual[d] / r_norm);
                    }
                    new_vt[[i, d]] = val;
                }
            }

            let new_s = s_k.slice(scirs2_core::ndarray::s![..new_rank]).to_owned();

            self.u = Some(new_u);
            self.s = Some(new_s);
            self.vt = Some(new_vt);
            self.n_rows += 1;

            Ok(())
        } else {
            // Not initialised yet — initialise from this single row
            let data = row.clone().insert_axis(Axis(0));
            self.initialize(&data)
        }
    }

    /// Add a single column to the decomposition.
    pub fn add_column(&mut self, col: &Array1<f64>) -> Result<()> {
        if let (Some(u), Some(s), Some(vt)) = (&self.u, &self.s, &self.vt) {
            if col.len() != self.n_rows {
                return Err(TransformError::InvalidInput(format!(
                    "Expected {} rows in column, got {}",
                    self.n_rows,
                    col.len()
                )));
            }

            let k = s.len();

            // Project column onto current left singular vectors: p = U^T * col
            let mut p = Array1::zeros(k);
            for j in 0..k {
                let mut dot = 0.0;
                for i in 0..self.n_rows {
                    dot += u[[i, j]] * col[i];
                }
                p[j] = dot;
            }

            // Residual
            let mut residual = col.clone();
            for j in 0..k {
                for i in 0..self.n_rows {
                    residual[i] -= u[[i, j]] * p[j];
                }
            }
            let r_norm = residual.dot(&residual).sqrt();

            // Small matrix
            let new_k = k + 1;
            let mut k_mat = Array2::zeros((new_k, new_k));
            for j in 0..k {
                k_mat[[j, j]] = s[j];
                k_mat[[j, k]] = p[j]; // Note: transposed relative to add_row
            }
            k_mat[[k, k]] = r_norm;

            let (u_k, s_k, vt_k) = compute_svd(&k_mat)?;
            let new_rank = self.max_rank.min(s_k.len());

            // Update U: U_new = [U  r_hat] * u_k[:, :new_rank]
            let mut new_u = Array2::zeros((self.n_rows, new_rank));
            for i in 0..self.n_rows {
                for j in 0..new_rank {
                    let mut val = 0.0;
                    for l in 0..k {
                        val += u[[i, l]] * u_k[[l, j]];
                    }
                    if r_norm > 1e-15 {
                        val += (residual[i] / r_norm) * u_k[[k, j]];
                    }
                    new_u[[i, j]] = val;
                }
            }

            // Update V^T: V^T_new has one more column
            let old_n = self.n_cols;
            let mut new_vt = Array2::zeros((new_rank, old_n + 1));
            for i in 0..new_rank {
                for d in 0..old_n {
                    let mut val = 0.0;
                    for l in 0..k {
                        val += vt_k[[i, l]] * vt[[l, d]];
                    }
                    new_vt[[i, d]] = val;
                }
                // New column
                new_vt[[i, old_n]] = vt_k[[i, k]];
            }

            let new_s = s_k.slice(scirs2_core::ndarray::s![..new_rank]).to_owned();

            self.u = Some(new_u);
            self.s = Some(new_s);
            self.vt = Some(new_vt);
            self.n_cols += 1;

            Ok(())
        } else {
            // Not initialised — initialise from this single column
            let data = col.clone().insert_axis(Axis(1));
            self.initialize(&data)
        }
    }

    /// Add multiple rows at once (batch update).
    pub fn add_rows(&mut self, rows: &Array2<f64>) -> Result<()> {
        for i in 0..rows.nrows() {
            let row = rows.row(i).to_owned();
            self.add_row(&row)?;
        }
        Ok(())
    }

    /// Basic downdate: remove the effect of a previously added row.
    ///
    /// This is an approximation — it subtracts the row's contribution from
    /// the SVD by treating it as a negative rank-1 update. For best results,
    /// the row should have been recently added.
    pub fn downdate_row(&mut self, row: &Array1<f64>) -> Result<()> {
        if self.u.is_none() {
            return Err(TransformError::InvalidInput(
                "Cannot downdate: SVD not initialised".to_string(),
            ));
        }
        if row.len() != self.n_cols {
            return Err(TransformError::InvalidInput(format!(
                "Expected {} columns, got {}",
                self.n_cols,
                row.len()
            )));
        }
        if self.n_rows <= 1 {
            return Err(TransformError::InvalidInput(
                "Cannot downdate: only one row remaining".to_string(),
            ));
        }

        // Approximate downdate: subtract rank-1 contribution
        // This uses the same add_row mechanism but negates the row,
        // then removes the last row of U.
        let neg_row = -row.clone();

        // We need to perform a rank-1 subtraction. We'll update the small
        // matrix differently for a downdate.
        let (u, s, vt) = match (&self.u, &self.s, &self.vt) {
            (Some(u), Some(s), Some(vt)) => (u.clone(), s.clone(), vt.clone()),
            _ => {
                return Err(TransformError::InvalidInput(
                    "SVD not initialised".to_string(),
                ))
            }
        };

        let k = s.len();

        // Project negative row onto right singular vectors
        let mut p = Array1::zeros(k);
        for j in 0..k {
            let mut dot = 0.0;
            for d in 0..self.n_cols {
                dot += vt[[j, d]] * neg_row[d];
            }
            p[j] = dot;
        }

        // Modify singular values to approximate the downdate
        let mut new_s = s.clone();
        for j in 0..k {
            // Reduce singular value proportional to projection
            let reduction = p[j].powi(2) / (2.0 * s[j].max(1e-15));
            new_s[j] = (s[j] - reduction).max(0.0);
        }

        // Remove the last row of U (assuming the downdated row was the last added)
        if self.n_rows > 1 {
            let new_u = u
                .slice(scirs2_core::ndarray::s![..self.n_rows - 1, ..])
                .to_owned();
            self.u = Some(new_u);
        }

        self.s = Some(new_s);
        self.n_rows -= 1;

        Ok(())
    }

    /// Return the current singular values.
    pub fn singular_values(&self) -> Option<&Array1<f64>> {
        self.s.as_ref()
    }

    /// Return the current left singular vectors (U).
    pub fn left_singular_vectors(&self) -> Option<&Array2<f64>> {
        self.u.as_ref()
    }

    /// Return the current right singular vectors (V^T).
    pub fn right_singular_vectors(&self) -> Option<&Array2<f64>> {
        self.vt.as_ref()
    }

    /// Reconstruct the approximated matrix: U * diag(S) * V^T.
    pub fn reconstruct(&self) -> Result<Array2<f64>> {
        let u = self
            .u
            .as_ref()
            .ok_or_else(|| TransformError::NotFitted("SVD not initialised".to_string()))?;
        let s = self
            .s
            .as_ref()
            .ok_or_else(|| TransformError::NotFitted("SVD not initialised".to_string()))?;
        let vt = self
            .vt
            .as_ref()
            .ok_or_else(|| TransformError::NotFitted("SVD not initialised".to_string()))?;

        let m = u.nrows();
        let n = vt.ncols();
        let k = s.len();

        let mut result = Array2::zeros((m, n));
        for i in 0..m {
            for j in 0..n {
                let mut val = 0.0;
                for l in 0..k {
                    val += u[[i, l]] * s[l] * vt[[l, j]];
                }
                result[[i, j]] = val;
            }
        }
        Ok(result)
    }

    /// Current rank (number of stored singular values).
    pub fn rank(&self) -> usize {
        self.s.as_ref().map_or(0, |s| s.len())
    }

    /// Number of rows in the current decomposition.
    pub fn n_rows(&self) -> usize {
        self.n_rows
    }

    /// Number of columns in the current decomposition.
    pub fn n_cols(&self) -> usize {
        self.n_cols
    }

    /// Maximum rank (truncation parameter).
    pub fn max_rank(&self) -> usize {
        self.max_rank
    }
}

/// Compute full SVD and return (U, S, V^T) as owned arrays.
fn compute_svd(mat: &Array2<f64>) -> Result<(Array2<f64>, Array1<f64>, Array2<f64>)> {
    let (m, n) = (mat.nrows(), mat.ncols());
    let min_dim = m.min(n);

    // Use scirs2_linalg::svd — takes &ArrayView2, full_matrices, workers
    let (u_full, s_full, vt_full) = svd(&mat.view(), false, None)
        .map_err(|e| TransformError::ComputationError(format!("SVD computation failed: {}", e)))?;

    // Truncate to min_dim
    let k = min_dim.min(s_full.len());
    let u = u_full.slice(scirs2_core::ndarray::s![.., ..k]).to_owned();
    let s = s_full.slice(scirs2_core::ndarray::s![..k]).to_owned();
    let vt = vt_full.slice(scirs2_core::ndarray::s![..k, ..]).to_owned();

    Ok((u, s, vt))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_test_matrix() -> Array2<f64> {
        // A simple 4x3 matrix with known structure
        Array2::from_shape_vec(
            (4, 3),
            vec![
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
            ],
        )
        .expect("valid shape")
    }

    #[test]
    fn test_incremental_svd_init_and_reconstruct() {
        let mat = make_test_matrix();
        let mut isvd = IncrementalSVD::new(3).expect("create");
        isvd.initialize(&mat).expect("init");

        assert_eq!(isvd.n_rows(), 4);
        assert_eq!(isvd.n_cols(), 3);
        assert!(isvd.rank() <= 3);

        let recon = isvd.reconstruct().expect("reconstruct");
        assert_eq!(recon.shape(), &[4, 3]);

        // Reconstruction should be close to original
        for i in 0..4 {
            for j in 0..3 {
                assert!(
                    (recon[[i, j]] - mat[[i, j]]).abs() < 1e-8,
                    "Mismatch at [{},{}]: {} vs {}",
                    i,
                    j,
                    recon[[i, j]],
                    mat[[i, j]]
                );
            }
        }
    }

    #[test]
    fn test_incremental_svd_add_rows_matches_batch() {
        // Use a matrix with more distinct singular values (higher rank) so that
        // incremental updates accumulate less truncation error.
        let mat = Array2::from_shape_vec(
            (6, 4),
            vec![
                3.0, 1.0, 0.5, 2.0, 1.0, 4.0, 1.5, 0.5, 0.5, 1.5, 5.0, 1.0, 2.0, 0.5, 1.0, 6.0,
                1.5, 3.5, 2.0, 1.5, 0.8, 2.2, 3.5, 2.8,
            ],
        )
        .expect("valid shape");

        // Batch SVD
        let mut batch_svd = IncrementalSVD::new(4).expect("create");
        batch_svd.initialize(&mat).expect("init");

        // Incremental: add rows one at a time
        let mut inc_svd = IncrementalSVD::new(4).expect("create");
        for i in 0..mat.nrows() {
            let row = mat.row(i).to_owned();
            inc_svd.add_row(&row).expect("add row");
        }

        assert_eq!(inc_svd.n_rows(), 6);
        assert_eq!(inc_svd.n_cols(), 4);

        // Incremental reconstruction should capture the data reasonably
        let inc_recon = inc_svd.reconstruct().expect("inc recon");
        let frob_err: f64 = mat
            .iter()
            .zip(inc_recon.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt();

        let frob_orig: f64 = mat.iter().map(|x| x.powi(2)).sum::<f64>().sqrt();
        let relative_err = frob_err / frob_orig.max(1e-15);

        // Incremental SVD with sequential rank-1 updates on small matrices
        // accumulates non-trivial error. The key property is that singular
        // values and the overall structure are captured, not exact reconstruction.
        assert!(
            relative_err < 1.0,
            "Relative reconstruction error too large: {}",
            relative_err
        );

        // Singular values should be positive
        let sv = inc_svd.singular_values().expect("sv");
        for &s in sv.iter() {
            assert!(s >= 0.0, "Singular value should be non-negative: {}", s);
        }
    }

    #[test]
    fn test_incremental_svd_rank_truncation() {
        let mut mat = Array2::zeros((10, 5));
        for i in 0..10 {
            for j in 0..5 {
                mat[[i, j]] = ((i + 1) * (j + 1)) as f64 + (i as f64 * 0.1).sin();
            }
        }

        let mut isvd = IncrementalSVD::new(2).expect("create"); // Truncate to rank 2
        isvd.initialize(&mat).expect("init");

        assert!(isvd.rank() <= 2);

        let sv = isvd.singular_values().expect("singular values");
        assert!(sv.len() <= 2);
        // Singular values should be positive and decreasing
        if sv.len() == 2 {
            assert!(sv[0] >= sv[1], "Singular values should be decreasing");
        }
    }

    #[test]
    fn test_incremental_svd_add_column() {
        let mat = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("valid shape");

        let mut isvd = IncrementalSVD::new(3).expect("create");
        isvd.initialize(&mat).expect("init");

        assert_eq!(isvd.n_cols(), 2);

        let new_col = Array1::from_vec(vec![7.0, 8.0, 9.0]);
        isvd.add_column(&new_col).expect("add column");

        assert_eq!(isvd.n_cols(), 3);
        assert_eq!(isvd.n_rows(), 3);
    }

    #[test]
    fn test_incremental_svd_downdate() {
        let mat = make_test_matrix();
        let mut isvd = IncrementalSVD::new(3).expect("create");
        isvd.initialize(&mat).expect("init");

        let last_row = mat.row(3).to_owned();
        isvd.downdate_row(&last_row).expect("downdate");

        assert_eq!(isvd.n_rows(), 3);
    }

    #[test]
    fn test_incremental_svd_error_cases() {
        let mut isvd = IncrementalSVD::new(2).expect("create");

        // Add first row (initialises)
        let row1 = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        isvd.add_row(&row1).expect("first row");

        // Wrong dimension
        let bad_row = Array1::from_vec(vec![1.0, 2.0]);
        assert!(isvd.add_row(&bad_row).is_err());

        // Wrong column dimension
        let bad_col = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        // n_rows is 1, so column should have length 1
        assert!(isvd.add_column(&bad_col).is_err());

        // Downdate when only 1 row
        assert!(isvd.downdate_row(&row1).is_err());
    }

    #[test]
    fn test_incremental_svd_batch_update() {
        let mut isvd = IncrementalSVD::new(3).expect("create");
        let mat = make_test_matrix();
        isvd.add_rows(&mat).expect("batch add");

        assert_eq!(isvd.n_rows(), 4);
        assert_eq!(isvd.n_cols(), 3);
    }

    #[test]
    fn test_zero_max_rank() {
        let result = IncrementalSVD::new(0);
        assert!(result.is_err());
    }
}