1use crate::dense::{Mat, lincomb, matmul, norm1, require_square};
6use crate::error::Result;
7use crate::lu::LuFactorization;
8use crate::scalar::Scalar;
9use ndarray::{Array2, ArrayBase, Data, Ix2};
10
11const B3: [f64; 4] = [120.0, 60.0, 12.0, 1.0];
13const B5: [f64; 6] = [30240.0, 15120.0, 3360.0, 420.0, 30.0, 1.0];
14const B7: [f64; 8] = [17297280.0, 8648640.0, 1995840.0, 277200.0, 25200.0, 1512.0, 56.0, 1.0];
15const B9: [f64; 10] = [17643225600.0, 8821612800.0, 2075673600.0, 302702400.0, 30270240.0, 2162160.0, 110880.0, 3960.0, 90.0, 1.0];
16const B13: [f64; 14] = [
17 64764752532480000.0,
18 32382376266240000.0,
19 7771770303897600.0,
20 1187353796428800.0,
21 129060195264000.0,
22 10559470521600.0,
23 670442572800.0,
24 33522128640.0,
25 1323241920.0,
26 40840800.0,
27 960960.0,
28 16380.0,
29 182.0,
30 1.0,
31];
32const THETA: [(usize, f64); 4] = [(3, 1.495_585_217_958_292e-2), (5, 2.539_398_330_063_23e-1), (7, 9.504_178_996_162_932e-1), (9, 2.097_847_961_257_068)];
34const THETA13: f64 = 5.371920351148152;
35
36pub fn expm<T: Scalar, S: Data<Elem = T>>(a: &ArrayBase<S, Ix2>) -> Result<Array2<T>> {
44 let a = Mat::from_array(a)?;
45 require_square(a.rows, a.cols)?;
46 let n = a.rows;
47 let eye = Mat::<T>::identity(n);
48 let norm = norm1(&a);
49 let a2 = matmul(&a, &a);
50 for (m, theta) in THETA {
52 if norm <= theta {
53 let b: &[f64] = match m {
54 3 => &B3,
55 5 => &B5,
56 7 => &B7,
57 _ => &B9,
58 };
59 let (mut u, mut v) = (Mat::zeros(n, n), Mat::zeros(n, n));
60 let mut power = eye.clone();
61 for k in 0..=m / 2 {
62 u = lincomb(T::one(), &u, T::from_real(b[2 * k + 1]), &power);
63 v = lincomb(T::one(), &v, T::from_real(b[2 * k]), &power);
64 if k < m / 2 {
65 power = matmul(&power, &a2);
66 }
67 }
68 return Ok(pade_quotient(&matmul(&a, &u), &v)?.into_array());
69 }
70 }
71 let s = (norm / THETA13).log2().ceil().max(0.0) as i32;
73 let scale = T::from_real(0.5f64.powi(s));
74 let a1 = Mat { rows: n, cols: n, data: a.data.iter().map(|&v| v * scale).collect() };
75 let a2 = matmul(&a1, &a1);
76 let a4 = matmul(&a2, &a2);
77 let a6 = matmul(&a4, &a2);
78 let b = |k: usize| T::from_real(B13[k]);
79 let sum3 = |c6: T, c4: T, c2: T, c0: T| {
80 let t = lincomb(c6, &a6, c4, &a4);
81 let t = lincomb(T::one(), &t, c2, &a2);
82 lincomb(T::one(), &t, c0, &eye)
83 };
84 let inner_u = sum3(b(13), b(11), b(9), T::zero());
85 let u = lincomb(T::one(), &matmul(&a6, &inner_u), T::one(), &sum3(b(7), b(5), b(3), b(1)));
86 let u = matmul(&a1, &u);
87 let inner_v = sum3(b(12), b(10), b(8), T::zero());
88 let v = lincomb(T::one(), &matmul(&a6, &inner_v), T::one(), &sum3(b(6), b(4), b(2), b(0)));
89 let mut r = pade_quotient(&u, &v)?;
90 for _ in 0..s {
91 r = matmul(&r, &r);
92 }
93 Ok(r.into_array())
94}
95fn pade_quotient<T: Scalar>(u: &Mat<T>, v: &Mat<T>) -> Result<Mat<T>> {
99 let n = u.rows;
100 let lu = LuFactorization::new(lincomb(T::one(), v, -T::one(), u))?;
101 let rhs = lincomb(T::one(), v, T::one(), u);
102 let mut out = Mat::zeros(n, n);
103 for j in 0..n {
104 let col: Vec<T> = (0..n).map(|i| rhs.at(i, j)).collect();
105 for (i, x) in lu.solve_vec(&col).into_iter().enumerate() {
106 *out.at_mut(i, j) = x;
107 }
108 }
109 Ok(out)
110}