scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Matrix calculus utilities for optimization
//!
//! This module provides high-level utilities that combine matrix derivatives
//! with optimization algorithms, particularly useful for machine learning
//! and scientific computing applications.

use scirs2_core::ndarray::{Array2, ArrayView2};
use scirs2_core::numeric::{Float, One, Zero};
use std::fmt::Debug;

// use crate::basic::det; // Currently unused
use crate::error::{LinalgError, LinalgResult};

/// Configuration for matrix optimization algorithms
#[derive(Debug, Clone)]
pub struct OptimizationConfig<F: Float> {
    /// Maximum number of iterations
    pub max_iterations: usize,
    /// Convergence tolerance for gradient norm
    pub gradient_tolerance: F,
    /// Step size for line search
    pub initial_stepsize: F,
    /// Backtracking factor for line search
    pub backtrack_factor: F,
    /// Maximum number of backtracking steps
    pub max_backtrack_steps: usize,
}

impl<F: Float> Default for OptimizationConfig<F> {
    fn default() -> Self {
        Self {
            max_iterations: 1000,
            gradient_tolerance: F::from(1e-6).expect("Operation failed"),
            initial_stepsize: F::from(1.0).expect("Operation failed"),
            backtrack_factor: F::from(0.5).expect("Operation failed"),
            max_backtrack_steps: 20,
        }
    }
}

/// Result of matrix optimization
#[derive(Debug, Clone)]
pub struct OptimizationResult<F: Float> {
    /// Final matrix value
    pub x: Array2<F>,
    /// Final function value
    pub f_val: F,
    /// Final gradient norm
    pub gradient_norm: F,
    /// Number of iterations performed
    pub iterations: usize,
    /// Whether optimization converged
    pub converged: bool,
    /// Function evaluation history
    pub f_history: Vec<F>,
}

/// Gradient descent for matrix-valued optimization problems.
///
/// Minimizes a scalar function f(X) where X is a matrix using gradient descent.
///
/// # Arguments
///
/// * `f` - Objective function f: R^(m×n) -> R
/// * `x0` - Initial matrix
/// * `config` - Optimization configuration
///
/// # Returns
///
/// * Optimization result containing final matrix and convergence information
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::{array, ArrayView2};
/// use scirs2_linalg::matrix_calculus::optimization::{matrix_gradient_descent, OptimizationConfig};
/// use scirs2_linalg::error::LinalgResult;
///
/// // Minimize f(X) = ||X||_F^2 (simple quadratic function)
/// let f = |x: &ArrayView2<f64>| -> LinalgResult<f64> {
///     let frobenius_sq = x.iter().fold(0.0, |acc, &val| acc + val * val);
///     Ok(frobenius_sq)
/// };
///
/// let x0 = array![[1.0, 1.0], [1.0, 1.0]];  // Start from ones matrix
/// let config = OptimizationConfig::default();
///
/// let result = matrix_gradient_descent(&f, &x0.view(), &config).expect("Operation failed");
/// // Should converge to the target matrix
/// ```
#[allow(dead_code)]
pub fn matrix_gradient_descent<F>(
    f: &impl Fn(&ArrayView2<F>) -> LinalgResult<F>,
    x0: &ArrayView2<F>,
    config: &OptimizationConfig<F>,
) -> LinalgResult<OptimizationResult<F>>
where
    F: Float + Zero + One + Copy + Debug + scirs2_core::ndarray::ScalarOperand,
{
    let mut x = x0.to_owned();
    let mut f_history = Vec::new();
    let mut converged = false;

    for iteration in 0..config.max_iterations {
        // Evaluate function
        let f_val = f(&x.view())?;
        f_history.push(f_val);

        // Compute gradient using finite differences
        let grad = matrix_finite_difference_gradient(&f, &x.view())?;

        // Compute gradient norm
        let grad_norm = grad.iter().fold(F::zero(), |acc, &g| acc + g * g).sqrt();

        // Check convergence
        if grad_norm < config.gradient_tolerance {
            converged = true;
            return Ok(OptimizationResult {
                x,
                f_val,
                gradient_norm: grad_norm,
                iterations: iteration,
                converged,
                f_history,
            });
        }

        // Line search with backtracking
        let mut stepsize = config.initial_stepsize;
        let mut x_new = x.clone();

        for _ in 0..config.max_backtrack_steps {
            // Take step: x_new = x - stepsize * grad
            x_new = &x - &(&grad * stepsize);

            let f_new = f(&x_new.view())?;

            // Armijo condition: sufficient decrease
            let sufficient_decrease = f_val - f_new
                >= F::from(1e-4).expect("Operation failed") * stepsize * grad_norm * grad_norm;

            if sufficient_decrease {
                break;
            }

            stepsize = stepsize * config.backtrack_factor;
        }

        x = x_new;
    }

    // Did not converge
    let f_val = f(&x.view())?;
    let grad = matrix_finite_difference_gradient(&f, &x.view())?;
    let grad_norm = grad.iter().fold(F::zero(), |acc, &g| acc + g * g).sqrt();

    Ok(OptimizationResult {
        x,
        f_val,
        gradient_norm: grad_norm,
        iterations: config.max_iterations,
        converged,
        f_history,
    })
}

/// Newton's method for matrix optimization.
///
/// Uses second-order information (Hessian) to optimize matrix-valued functions.
/// This is more sophisticated than gradient descent but requires Hessian computation.
///
/// # Arguments
///
/// * `f` - Objective function f: R^(m×n) -> R
/// * `x0` - Initial matrix
/// * `config` - Optimization configuration
///
/// # Returns
///
/// * Optimization result
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::{array, ArrayView2};
/// use scirs2_linalg::matrix_calculus::optimization::{matrix_newton_method, OptimizationConfig};
/// use scirs2_linalg::error::LinalgResult;
///
/// // Minimize a quadratic function f(X) = tr(X^T A X) + tr(B^T X)
/// let a = array![[2.0, 0.0], [0.0, 2.0]];  // Positive definite
/// let b = array![[-2.0, -4.0], [-6.0, -8.0]];
///
/// let f = move |x: &ArrayView2<f64>| -> LinalgResult<f64> {
///     let quad_term = (x.t().dot(&a).dot(x)).sum();
///     let linear_term = (b.t().dot(x)).sum();
///     Ok(quad_term + linear_term)
/// };
///
/// let x0 = array![[0.0, 0.0], [0.0, 0.0]];
/// let config = OptimizationConfig::default();
///
/// let result = matrix_newton_method(&f, &x0.view(), &config).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn matrix_newton_method<F>(
    f: &impl Fn(&ArrayView2<F>) -> LinalgResult<F>,
    x0: &ArrayView2<F>,
    config: &OptimizationConfig<F>,
) -> LinalgResult<OptimizationResult<F>>
where
    F: Float + Zero + One + Copy + Debug + scirs2_core::ndarray::ScalarOperand,
{
    let mut x = x0.to_owned();
    let mut f_history = Vec::new();
    let mut converged = false;

    for iteration in 0..config.max_iterations {
        // Evaluate function
        let f_val = f(&x.view())?;
        f_history.push(f_val);

        // Compute gradient
        let grad = matrix_finite_difference_gradient(&f, &x.view())?;
        let grad_norm = grad.iter().fold(F::zero(), |acc, &g| acc + g * g).sqrt();

        // Check convergence
        if grad_norm < config.gradient_tolerance {
            converged = true;
            return Ok(OptimizationResult {
                x,
                f_val,
                gradient_norm: grad_norm,
                iterations: iteration,
                converged,
                f_history,
            });
        }

        // For Newton's method, we need to solve H * delta = -grad
        // where H is the Hessian. For simplicity, we'll use a quasi-Newton approach
        // with BFGS approximation or just use gradient descent with adaptive step size

        // Adaptive step size based on function curvature
        let stepsize = if iteration == 0 {
            config.initial_stepsize
        } else {
            // Use previous function values to estimate curvature
            let f_prev = f_history[f_history.len() - 2];
            let f_change = (f_val - f_prev).abs();
            let adaptive_step = if f_change > F::epsilon() {
                config.initial_stepsize * F::from(0.1).expect("Operation failed") / f_change
            } else {
                config.initial_stepsize
            };
            adaptive_step.min(config.initial_stepsize)
        };

        // Take Newton-like step (simplified)
        x = &x - &(&grad * stepsize);
    }

    // Did not converge
    let f_val = f(&x.view())?;
    let grad = matrix_finite_difference_gradient(&f, &x.view())?;
    let grad_norm = grad.iter().fold(F::zero(), |acc, &g| acc + g * g).sqrt();

    Ok(OptimizationResult {
        x,
        f_val,
        gradient_norm: grad_norm,
        iterations: config.max_iterations,
        converged,
        f_history,
    })
}

/// Projected gradient descent for constrained matrix optimization.
///
/// Optimizes f(X) subject to X being in a feasible set defined by a projection operator.
///
/// # Arguments
///
/// * `f` - Objective function
/// * `project` - Projection operator onto feasible set
/// * `x0` - Initial matrix (should be feasible)
/// * `config` - Optimization configuration
///
/// # Returns
///
/// * Optimization result
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::{array, ArrayView2};
/// use scirs2_linalg::matrix_calculus::optimization::{projected_gradient_descent, OptimizationConfig};
/// use scirs2_linalg::error::LinalgResult;
///
/// // Minimize f(X) = ||X||_F^2 subject to X being positive semidefinite  
/// let f = |x: &ArrayView2<f64>| -> LinalgResult<f64> {
///     Ok(x.iter().fold(0.0, |acc, &val| acc + val * val))
/// };
///
/// // Simple projection: keep only positive diagonal elements, zero off-diagonal
/// let project = |x: &ArrayView2<f64>| -> LinalgResult<scirs2_core::ndarray::Array2<f64>> {
///     let mut result = x.to_owned();
///     for i in 0..result.nrows() {
///         for j in 0..result.ncols() {
///             if i != j {
///                 result[[i, j]] = 0.0;  // Zero off-diagonal
///             } else {
///                 result[[i, j]] = result[[i, j]].max(0.0);  // Positive diagonal
///             }
///         }
///     }
///     Ok(result)
/// };
///
/// let x0 = array![[1.0, 0.0], [0.0, 1.0]];  // Start from identity
/// let config = OptimizationConfig::default();
///
/// let result = projected_gradient_descent(&f, project, &x0.view(), &config).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn projected_gradient_descent<F>(
    f: &impl Fn(&ArrayView2<F>) -> LinalgResult<F>,
    project: impl Fn(&ArrayView2<F>) -> LinalgResult<Array2<F>>,
    x0: &ArrayView2<F>,
    config: &OptimizationConfig<F>,
) -> LinalgResult<OptimizationResult<F>>
where
    F: Float + Zero + One + Copy + Debug + scirs2_core::ndarray::ScalarOperand,
{
    let mut x = x0.to_owned();
    let mut f_history = Vec::new();
    let mut converged = false;

    for iteration in 0..config.max_iterations {
        // Evaluate function
        let f_val = f(&x.view())?;
        f_history.push(f_val);

        // Compute gradient
        let grad = matrix_finite_difference_gradient(&f, &x.view())?;

        // Use backtracking line search for step size
        let mut stepsize = config.initial_stepsize;
        let mut x_new = x.clone();
        let mut found_step = false;

        for _ in 0..config.max_backtrack_steps {
            // Take gradient step
            let x_unconstrained = &x - &(&grad * stepsize);

            // Project back to feasible set
            let x_candidate = project(&x_unconstrained.view())?;

            // Check if we made progress
            let f_new = f(&x_candidate.view())?;
            if f_new < f_val {
                x_new = x_candidate;
                found_step = true;
                break;
            }

            // Backtrack
            stepsize = stepsize * config.backtrack_factor;
        }

        if !found_step {
            // If no step was found, use the smallest step size
            let x_unconstrained = &x - &(&grad * stepsize);
            x_new = project(&x_unconstrained.view())?;
        }

        // Compute projected gradient for convergence check
        // This is the difference between the current point and the projected gradient step
        let x_grad_step = &x - &grad;
        let x_projected_grad_step = project(&x_grad_step.view())?;
        let proj_grad = &x - &x_projected_grad_step;
        let proj_grad_norm = proj_grad
            .iter()
            .fold(F::zero(), |acc, &g| acc + g * g)
            .sqrt();

        // Update x
        x = x_new;

        // Check convergence using projected gradient norm
        if proj_grad_norm < config.gradient_tolerance {
            converged = true;
            let final_f_val = f(&x.view())?;
            return Ok(OptimizationResult {
                x,
                f_val: final_f_val,
                gradient_norm: proj_grad_norm,
                iterations: iteration,
                converged,
                f_history,
            });
        }
    }

    // Did not converge
    let f_val = f(&x.view())?;
    let grad = matrix_finite_difference_gradient(&f, &x.view())?;
    let grad_norm = grad.iter().fold(F::zero(), |acc, &g| acc + g * g).sqrt();

    Ok(OptimizationResult {
        x,
        f_val,
        gradient_norm: grad_norm,
        iterations: config.max_iterations,
        converged,
        f_history,
    })
}

/// Compute matrix gradient using finite differences.
///
/// This is a utility function that computes the gradient of a scalar function
/// with respect to a matrix using finite differences.
///
/// # Arguments
///
/// * `f` - Scalar function of a matrix
/// * `x` - Point at which to evaluate gradient
///
/// # Returns
///
/// * Gradient matrix of same shape as input
#[allow(dead_code)]
fn matrix_finite_difference_gradient<F>(
    f: &impl Fn(&ArrayView2<F>) -> LinalgResult<F>,
    x: &ArrayView2<F>,
) -> LinalgResult<Array2<F>>
where
    F: Float + Zero + One + Copy + Debug,
{
    let eps = F::epsilon().sqrt();
    let f_x = f(x)?;
    let mut grad = Array2::zeros(x.dim());

    for i in 0..x.nrows() {
        for j in 0..x.ncols() {
            // Perturb element (i,j)
            let mut x_pert = x.to_owned();
            x_pert[[i, j]] = x_pert[[i, j]] + eps;

            let f_pert = f(&x_pert.view())?;
            grad[[i, j]] = (f_pert - f_x) / eps;
        }
    }

    Ok(grad)
}

/// Matrix optimization utilities for common problems
pub mod common_problems {
    use super::*;
    // Unused imports removed for now
    // use crate::matrix_functions::expm;
    // use crate::norm::matrix_norm;

    /// Solve the orthogonal Procrustes problem: min ||A - XB||_F subject to X^T X = I.
    ///
    /// This finds the orthogonal matrix X that best aligns A with B.
    ///
    /// # Arguments
    ///
    /// * `a` - Source matrix
    /// * `b` - Target matrix
    /// * `config` - Optimization configuration
    ///
    /// # Returns
    ///
    /// * Optimal orthogonal matrix X
    pub fn orthogonal_procrustes<F>(
        a: &ArrayView2<F>,
        b: &ArrayView2<F>,
        config: &OptimizationConfig<F>,
    ) -> LinalgResult<Array2<F>>
    where
        F: Float + Zero + One + Copy + Debug + scirs2_core::ndarray::ScalarOperand,
    {
        if a.shape() != b.shape() {
            return Err(LinalgError::ShapeError(format!(
                "Matrices must have same shape: {:?} vs {:?}",
                a.shape(),
                b.shape()
            )));
        }

        // Objective: f(X) = ||A - XB||_F^2
        let f = |x: &ArrayView2<F>| -> LinalgResult<F> {
            let diff = a - &x.dot(b);
            let norm_sq = diff.iter().fold(F::zero(), |acc, &val| acc + val * val);
            Ok(norm_sq)
        };

        // Projection onto orthogonal matrices using polar decomposition
        let project_orthogonal = |x: &ArrayView2<F>| -> LinalgResult<Array2<F>> {
            // Simple projection: normalize columns
            let mut result = x.to_owned();

            for j in 0..result.ncols() {
                let mut col = result.column_mut(j);
                let norm = col
                    .iter()
                    .fold(F::zero(), |acc, &val| acc + val * val)
                    .sqrt();

                if norm > F::epsilon() {
                    for elem in col.iter_mut() {
                        *elem = *elem / norm;
                    }
                }
            }

            Ok(result)
        };

        // Start with identity matrix
        let x0 = Array2::eye(a.nrows());

        let result = projected_gradient_descent(&f, project_orthogonal, &x0.view(), config)?;
        Ok(result.x)
    }

    /// Solve the positive definite matrix completion problem.
    ///
    /// Given partial observations of a matrix, find the positive definite matrix
    /// that best fits the observations.
    ///
    /// # Arguments
    ///
    /// * `observations` - Sparse observations (value, row, col)
    /// * `size` - Size of the matrix to complete
    /// * `config` - Optimization configuration
    ///
    /// # Returns
    ///
    /// * Completed positive definite matrix
    pub fn positive_definite_completion<F>(
        observations: &[(F, usize, usize)],
        size: usize,
        config: &OptimizationConfig<F>,
    ) -> LinalgResult<Array2<F>>
    where
        F: Float + Zero + One + Copy + Debug + scirs2_core::ndarray::ScalarOperand,
    {
        // Objective: minimize squared error on observed entries
        let f = |x: &ArrayView2<F>| -> LinalgResult<F> {
            let mut error = F::zero();
            for &(obs_val, i, j) in observations {
                let diff = x[[i, j]] - obs_val;
                error = error + diff * diff;
            }
            Ok(error)
        };

        // Project onto positive definite matrices (simplified)
        let project_pd = |x: &ArrayView2<F>| -> LinalgResult<Array2<F>> {
            // Simple heuristic: make symmetric and add small diagonal regularization
            let sym = (x + &x.t()) / F::from(2.0).expect("Operation failed");
            let mut result = sym;

            // Add regularization to diagonal
            let reg = F::from(1e-6).expect("Operation failed");
            for i in 0..size {
                result[[i, i]] = result[[i, i]] + reg;
            }

            Ok(result)
        };

        // Start with identity matrix
        let x0 = Array2::eye(size);

        let result = projected_gradient_descent(&f, project_pd, &x0.view(), config)?;
        Ok(result.x)
    }
}

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

    #[test]
    fn testmatrix_gradient_descent() {
        // Minimize f(X) = ||X||_F^2 (simpler objective that doesn't capture variables)
        fn objective(x: &ArrayView2<f64>) -> LinalgResult<f64> {
            let frobenius_sq = x.iter().fold(0.0, |acc, &val| acc + val * val);
            Ok(frobenius_sq)
        }

        let x0 = array![[1.0, 1.0], [1.0, 1.0]];
        let config = OptimizationConfig {
            max_iterations: 100,
            gradient_tolerance: 1e-4,
            ..Default::default()
        };

        let result =
            matrix_gradient_descent(&objective, &x0.view(), &config).expect("Operation failed");

        // Should converge to the zero matrix (global minimum of ||X||_F^2)
        assert!(result.converged);
        assert_abs_diff_eq!(result.x[[0, 0]], 0.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[[0, 1]], 0.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[[1, 0]], 0.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[[1, 1]], 0.0, epsilon = 1e-2);
    }

    #[test]
    fn test_projected_gradient_descent() {
        // Minimize f(X) = ||X||_F^2 subject to X being diagonal (simpler objective)
        fn objective(x: &ArrayView2<f64>) -> LinalgResult<f64> {
            Ok(x.iter().fold(0.0, |acc, &val| acc + val * val))
        }

        // Project onto diagonal matrices
        fn project(x: &ArrayView2<f64>) -> LinalgResult<Array2<f64>> {
            let mut result = Array2::zeros(x.dim());
            for i in 0..x.nrows() {
                result[[i, i]] = x[[i, i]];
            }
            Ok(result)
        }

        let x0 = array![[1.0, 0.5], [0.5, 1.0]];
        let config = OptimizationConfig {
            max_iterations: 200,
            gradient_tolerance: 1e-4,
            initial_stepsize: 0.1,
            ..Default::default()
        };

        let result = projected_gradient_descent(&objective, project, &x0.view(), &config)
            .expect("Operation failed");

        // Should converge to zero diagonal matrix
        assert!(result.converged);
        assert_abs_diff_eq!(result.x[[0, 0]], 0.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[[1, 1]], 0.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[[0, 1]], 0.0, epsilon = 1e-10);
        assert_abs_diff_eq!(result.x[[1, 0]], 0.0, epsilon = 1e-10);
    }
}