scirs2-linalg 0.4.0

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
//! Fractional matrix functions and advanced matrix power computations

use scirs2_core::ndarray::{Array2, ArrayView2};
use scirs2_core::numeric::{Float, NumAssign, One};
use std::iter::Sum;

use crate::eigen::eigh;
use crate::error::{LinalgError, LinalgResult};
use crate::solve::solve_multiple;
use crate::validation::validate_decomposition;

/// Compute the fractional matrix power A^p using various methods.
///
/// This function provides multiple algorithms for computing fractional
/// matrix powers, including eigendecomposition and Padé approximation methods.
///
/// # Arguments
///
/// * `a` - Input square matrix
/// * `p` - Power (real number, can be fractional)
/// * `method` - Method to use ("eigen", "pade", "schur")
///
/// # Returns
///
/// * Matrix power A^p
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::fractionalmatrix_power;
///
/// let a = array![[4.0_f64, 0.0], [0.0, 9.0]];
/// let a_half = fractionalmatrix_power(&a.view(), 0.5, "eigen").expect("Operation failed");
/// // Should be approximately [[2.0, 0.0], [0.0, 3.0]]
/// ```
#[allow(dead_code)]
pub fn fractionalmatrix_power<F>(a: &ArrayView2<F>, p: F, method: &str) -> LinalgResult<Array2<F>>
where
    F: Float
        + NumAssign
        + Sum
        + One
        + 'static
        + Send
        + Sync
        + scirs2_core::ndarray::ScalarOperand
        + std::fmt::Display,
{
    validate_decomposition(a, "Fractional matrix power computation", true)?;

    let n = a.nrows();

    // Special case for p = 0 (returns identity)
    if p.abs() < F::epsilon() {
        return Ok(Array2::eye(n));
    }

    // Special case for p = 1 (returns the matrix itself)
    if (p - F::one()).abs() < F::epsilon() {
        return Ok(a.to_owned());
    }

    match method {
        "eigen" => {
            // Use eigendecomposition method
            eigendecomposition_power(a, p)
        }
        "pade" => {
            // Use Padé approximation method
            pade_fractional_power(a, p)
        }
        "schur" => {
            // Use Schur decomposition method (simplified implementation)
            // For now, fall back to eigendecomposition
            eigendecomposition_power(a, p)
        }
        _ => Err(LinalgError::InvalidInputError(format!(
            "Unknown method '{}'. Supported methods: 'eigen', 'pade', 'schur'",
            method
        ))),
    }
}

/// Compute matrix power using eigendecomposition.
///
/// For symmetric matrices uses eigh (real eigenvalues), for general matrices
/// uses the Schur decomposition to compute the matrix power.
fn eigendecomposition_power<F>(a: &ArrayView2<F>, p: F) -> LinalgResult<Array2<F>>
where
    F: Float
        + NumAssign
        + Sum
        + One
        + 'static
        + Send
        + Sync
        + scirs2_core::ndarray::ScalarOperand
        + std::fmt::Display,
{
    let n = a.nrows();

    // Special case for diagonal matrix
    let mut is_diagonal = true;
    for i in 0..n {
        for j in 0..n {
            if i != j && a[[i, j]].abs() > F::epsilon() {
                is_diagonal = false;
                break;
            }
        }
        if !is_diagonal {
            break;
        }
    }

    if is_diagonal {
        let mut result = Array2::<F>::zeros((n, n));
        for i in 0..n {
            let val = a[[i, i]];
            if val < F::zero() && !is_integer(p) {
                return Err(LinalgError::InvalidInputError(
                    "Cannot compute real fractional power of negative number".to_string(),
                ));
            }
            result[[i, i]] = val.powf(p);
        }
        return Ok(result);
    }

    // Check if the matrix is symmetric (within tolerance)
    let mut is_symmetric = true;
    let sym_tol = F::epsilon() * F::from(n as f64 * 100.0).unwrap_or(F::one());
    for i in 0..n {
        for j in (i + 1)..n {
            if (a[[i, j]] - a[[j, i]]).abs() > sym_tol {
                is_symmetric = false;
                break;
            }
        }
        if !is_symmetric {
            break;
        }
    }

    if is_symmetric {
        // Use eigh for symmetric matrices (real eigenvalues guaranteed)
        return symmetric_eigendecomposition_power(a, p);
    }

    // For general non-symmetric matrices, use Schur decomposition:
    // A = Q T Q^T, then A^p = Q T^p Q^T
    // T is upper triangular (real Schur form), compute T^p via Parlett recurrence
    general_schur_power(a, p)
}

/// Compute matrix power for symmetric matrices via eigh.
fn symmetric_eigendecomposition_power<F>(a: &ArrayView2<F>, p: F) -> LinalgResult<Array2<F>>
where
    F: Float
        + NumAssign
        + Sum
        + One
        + 'static
        + Send
        + Sync
        + scirs2_core::ndarray::ScalarOperand
        + std::fmt::Display,
{
    let n = a.nrows();
    let (eigenvalues, eigenvectors) = eigh(a, None)?;

    // Check that all eigenvalues are non-negative for non-integer powers
    if !is_integer(p) {
        for i in 0..eigenvalues.len() {
            if eigenvalues[i] < F::zero() {
                return Err(LinalgError::InvalidInputError(
                    "Cannot compute real fractional power of matrix with negative eigenvalues"
                        .to_string(),
                ));
            }
        }
    }

    // Build result = V * diag(lambda^p) * V^T
    let mut result = Array2::<F>::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            let mut sum = F::zero();
            for kk in 0..eigenvalues.len() {
                let lam_p = eigenvalues[kk].powf(p);
                sum += eigenvectors[[i, kk]] * lam_p * eigenvectors[[j, kk]];
            }
            result[[i, j]] = sum;
        }
    }

    Ok(result)
}

/// Compute matrix power for general matrices via real Schur decomposition.
///
/// Uses A = Q T Q^T where T is upper quasi-triangular. For T^p we use
/// Parlett's recurrence for the upper triangular part.
fn general_schur_power<F>(a: &ArrayView2<F>, p: F) -> LinalgResult<Array2<F>>
where
    F: Float
        + NumAssign
        + Sum
        + One
        + 'static
        + Send
        + Sync
        + scirs2_core::ndarray::ScalarOperand
        + std::fmt::Display,
{
    let n = a.nrows();

    // Compute Schur decomposition: schur() returns (Q, T) where A = Q T Q^T
    // Note: the schur function returns (Z, T) where Z=Q is orthogonal
    let (q_mat, t_mat) = crate::schur(a)?;

    // Compute T^p using the diagonal + Parlett recurrence
    // For the real Schur form, the diagonal blocks are 1x1 (real eigenvalues)
    // or 2x2 (complex conjugate pairs). We handle the 1x1 case and
    // approximate 2x2 blocks.

    // Start with f(T) = T^p using the triangular power recurrence
    let mut f_t = Array2::<F>::zeros((n, n));

    // Compute diagonal elements: f(T)_{ii} = T_{ii}^p
    for i in 0..n {
        let t_ii = t_mat[[i, i]];
        if t_ii < F::zero() && !is_integer(p) {
            return Err(LinalgError::InvalidInputError(format!(
                "Cannot compute real fractional power: Schur diagonal element {} is negative",
                t_ii
            )));
        }
        if t_ii.abs() < F::epsilon() {
            if p > F::zero() {
                f_t[[i, i]] = F::zero();
            } else {
                return Err(LinalgError::SingularMatrixError(
                    "Cannot compute negative power of singular matrix".to_string(),
                ));
            }
        } else {
            f_t[[i, i]] = t_ii.powf(p);
        }
    }

    // Parlett recurrence for off-diagonal: column by column, bottom to top
    // f(T)_{ij} = T_{ij} * (f(T)_{jj} - f(T)_{ii}) / (T_{jj} - T_{ii})
    //           + sum_{k=i+1}^{j-1} (f(T)_{ik} * T_{kj} - T_{ik} * f(T)_{kj}) / (T_{jj} - T_{ii})
    for j in 1..n {
        for i in (0..j).rev() {
            let diff = t_mat[[j, j]] - t_mat[[i, i]];

            if diff.abs() > F::epsilon() * F::from(100.0).unwrap_or(F::one()) {
                let mut val = t_mat[[i, j]] * (f_t[[j, j]] - f_t[[i, i]]) / diff;

                for kk in (i + 1)..j {
                    val += (f_t[[i, kk]] * t_mat[[kk, j]] - t_mat[[i, kk]] * f_t[[kk, j]]) / diff;
                }

                f_t[[i, j]] = val;
            } else {
                // Confluent case: eigenvalues are equal
                // Use the derivative: f(T)_{ij} = T_{ij} * p * T_{ii}^{p-1}
                let t_ii = t_mat[[i, i]];
                if t_ii.abs() > F::epsilon() {
                    f_t[[i, j]] = t_mat[[i, j]] * p * t_ii.powf(p - F::one());
                } else {
                    f_t[[i, j]] = F::zero();
                }
            }
        }
    }

    // Result = Q * f(T) * Q^T
    // First compute temp = Q * f(T)
    let mut temp = Array2::<F>::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            let mut sum = F::zero();
            for kk in 0..n {
                sum += q_mat[[i, kk]] * f_t[[kk, j]];
            }
            temp[[i, j]] = sum;
        }
    }

    // Then result = temp * Q^T
    let mut result = Array2::<F>::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            let mut sum = F::zero();
            for kk in 0..n {
                sum += temp[[i, kk]] * q_mat[[j, kk]];
            }
            result[[i, j]] = sum;
        }
    }

    Ok(result)
}

/// Compute matrix power using Padé approximation.
fn pade_fractional_power<F>(a: &ArrayView2<F>, p: F) -> LinalgResult<Array2<F>>
where
    F: Float
        + NumAssign
        + Sum
        + One
        + 'static
        + Send
        + Sync
        + scirs2_core::ndarray::ScalarOperand
        + std::fmt::Display,
{
    // For simplicity, this implementation uses a basic approach
    // A full Padé implementation would be more complex

    // If p is close to an integer, use repeated multiplication
    if is_integer(p) {
        return integer_power(a, p.to_i32().unwrap_or(0));
    }

    // For non-integer powers close to small rationals, use root extraction
    // This is a simplified implementation
    if (p - F::from(0.5).expect("Operation failed")).abs() < F::epsilon() {
        // Square root case
        use super::exponential::sqrtm;
        return sqrtm(a, 50, F::from(1e-12).expect("Operation failed"));
    }

    // For other cases, fall back to eigendecomposition
    eigendecomposition_power(a, p)
}

/// Compute integer power using repeated squaring.
fn integer_power<F>(a: &ArrayView2<F>, p: i32) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + One + 'static + Send + Sync + scirs2_core::ndarray::ScalarOperand,
{
    let n = a.nrows();

    if p == 0 {
        return Ok(Array2::eye(n));
    }

    if p == 1 {
        return Ok(a.to_owned());
    }

    if p < 0 {
        // Compute A^{-|p|} = (A^{-1})^{|p|}
        let a_inv = solve_multiple(a, &Array2::eye(n).view(), None)?;
        return integer_power(&a_inv.view(), -p);
    }

    // Use repeated squaring for positive powers
    let mut result = Array2::eye(n);
    let mut base = a.to_owned();
    let mut exp = p as u32;

    while exp > 0 {
        if exp % 2 == 1 {
            // Multiply result by base
            let mut temp = Array2::<F>::zeros((n, n));
            for i in 0..n {
                for j in 0..n {
                    for k in 0..n {
                        temp[[i, j]] += result[[i, k]] * base[[k, j]];
                    }
                }
            }
            result = temp;
        }
        // Square the base
        let mut temp = Array2::<F>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                for k in 0..n {
                    temp[[i, j]] += base[[i, k]] * base[[k, j]];
                }
            }
        }
        base = temp;
        exp /= 2;
    }

    Ok(result)
}

/// Apply a general function to a symmetric positive definite matrix.
///
/// This function applies a scalar function f to a symmetric positive definite
/// matrix A using eigendecomposition: f(A) = V * f(D) * V^T
///
/// # Arguments
///
/// * `a` - Input symmetric positive definite matrix
/// * `f` - Function to apply (as a closure)
/// * `check_spd` - Whether to check that the matrix is SPD
///
/// # Returns
///
/// * f(A) where f is applied element-wise to the eigenvalues
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::spdmatrix_function;
///
/// let a = array![[4.0_f64, 0.0], [0.0, 9.0]];
/// let sqrt_a = spdmatrix_function(&a.view(), |x| x.sqrt(), true).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn spdmatrix_function<F, Func>(
    a: &ArrayView2<F>,
    f: Func,
    check_spd: bool,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + One + 'static + Send + Sync + scirs2_core::ndarray::ScalarOperand,
    Func: Fn(F) -> F,
{
    validate_decomposition(a, "SPD matrix function computation", true)?;

    let n = a.nrows();

    // Check if matrix is symmetric
    for i in 0..n {
        for j in 0..n {
            if (a[[i, j]] - a[[j, i]]).abs() > F::epsilon() {
                return Err(LinalgError::InvalidInputError(
                    "Matrix must be symmetric for SPD matrix function".to_string(),
                ));
            }
        }
    }

    // Check if matrix is positive definite if requested
    if check_spd {
        // Quick check: diagonal elements should be positive
        for i in 0..n {
            if a[[i, i]] <= F::zero() {
                return Err(LinalgError::InvalidInputError(
                    "Matrix must be positive definite".to_string(),
                ));
            }
        }
    }

    // For diagonal matrices, apply function directly to diagonal elements
    let mut is_diagonal = true;
    for i in 0..n {
        for j in 0..n {
            if i != j && a[[i, j]].abs() > F::epsilon() {
                is_diagonal = false;
                break;
            }
        }
        if !is_diagonal {
            break;
        }
    }

    if is_diagonal {
        let mut result = Array2::zeros((n, n));
        for i in 0..n {
            result[[i, i]] = f(a[[i, i]]);
        }
        return Ok(result);
    }

    // For general SPD matrices, use eigendecomposition
    // Note: For real symmetric matrices, eigenvalues are real
    let (eigenvalues, eigenvectors) = eigh(a, None)?;

    // Apply function to eigenvalues
    let mut diag = Array2::zeros((n, n));
    for i in 0..n {
        let eigenval = eigenvalues[i];
        if check_spd && eigenval <= F::zero() {
            return Err(LinalgError::InvalidInputError(
                "Matrix is not positive definite (negative eigenvalue found)".to_string(),
            ));
        }
        diag[[i, i]] = f(eigenval);
    }

    // Reconstruct: A_f = V * diag(f(λ)) * V^T
    let temp = eigenvectors.dot(&diag);
    let v_t = eigenvectors.t();
    let result = temp.dot(&v_t);

    Ok(result)
}

/// Helper function to check if a floating point number is close to an integer
fn is_integer<F: Float>(x: F) -> bool {
    (x - x.round()).abs() < F::from(1e-10).unwrap_or(F::epsilon())
}