rmatrix_ks 0.5.2

matrix and some algebra in Rust
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
//! # matrix::extra
//!
//! Additional mathematical functions,
//! such as matrix decomposition, eigenvalue computation,
//! and solving systems of linear equations, etc.

use crate::{
    matrix::{
        math::{inverse, row_reduce},
        matrix::Matrix,
        utils::{apply, transpose},
        vector::{dot_product, euclidean_norm, index_c, layer_product},
    },
    number::traits::{fractional::Fractional, realfloat::RealFloat},
};

/// Calculate the PLU decomposition of the matrix.
///
/// p * m = l * u
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{extra::plu_decomposition, matrix::Matrix},
///     number::instances::double::Double,
/// };
///
/// fn main() {
///     let m = Matrix::<Double, 3, 3>::of(
///         &[0.0, 5.0, 22.0 / 3.0, 4.0, 2.0, 1.0, 2.0, 7.0, 9.0].map(|e| Double::of(e)),
///     )
///     .unwrap();
///     let (p, l, u) = plu_decomposition(&m);
///     assert!((p * m).equals(&(l * u)));
/// }
/// ```
pub fn plu_decomposition<N, const R: usize, const C: usize>(
    m: &Matrix<N, R, C>,
) -> (Matrix<N, R, R>, Matrix<N, R, R>, Matrix<N, R, C>)
where
    N: Fractional,
{
    let (_, p, lt, reduced) = row_reduce(m);
    (
        p,
        inverse(&lt).expect(concat!(
            "Error[matrix::extra::plu_decomposition]: ",
            "Failed to retrieve the inverse of 'lt'."
        )),
        reduced,
    )
}

/// Use the Gram-Schmidt process to compute the QR decomposition of a REAL matrix.
///
/// # Examples
///
/// ## Tall matrix
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{extra::qr_decomposition_gs, matrix::Matrix, utils::transpose},
///     number::instances::double::Double,
/// };
///
/// fn main() {
///     let m = Matrix::<Double, 3, 2>::of(&[1.0, 0.0, 0.0, 1.0, 1.0, 1.0].map(Double::of)).unwrap();
///     let (q, r) = qr_decomposition_gs(&m).unwrap();
///     let q_expect = Matrix::<Double, 3, 2>::of(
///         &[
///             1.0 / 2.0f64.sqrt(),
///             -1.0 / 6.0f64.sqrt(),
///             0.0,
///             (2.0 / 3.0f64).sqrt(),
///             1.0 / 2.0f64.sqrt(),
///             1.0 / 6.0f64.sqrt(),
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(q, q_expect);
///     let r_expect = Matrix::<Double, 2, 2>::of(
///         &[
///             2.0f64.sqrt(),
///             1.0 / 2.0f64.sqrt(),
///             0.0,
///             (3.0 / 2.0f64).sqrt(),
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(r, r_expect);
///     // Q^T Q = I
///     assert_eq!(transpose(&q) * q, Matrix::<Double, 2, 2>::eyes());
/// }
/// ```
///
/// ## Square matrix
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{extra::qr_decomposition_gs, matrix::Matrix, utils::transpose},
///     number::instances::double::Double,
/// };
///
/// fn main() {
///     let m =
///         Matrix::<Double, 3, 3>::of(&[1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0].map(Double::of))
///             .unwrap();
///     let (q, r) = qr_decomposition_gs(&m).unwrap();
///     let q_expect = Matrix::<Double, 3, 3>::of(
///         &[
///             1.0 / 2.0f64.sqrt(),
///             1.0 / 6.0f64.sqrt(),
///             -1.0 / 3.0f64.sqrt(),
///             1.0 / 2.0f64.sqrt(),
///             -1.0 / 6.0f64.sqrt(),
///             1.0 / 3.0f64.sqrt(),
///             0.0,
///             2.0 / 6.0f64.sqrt(),
///             1.0 / 3.0f64.sqrt(),
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(q, q_expect);
///     let r_expect = Matrix::<Double, 3, 3>::of(
///         &[
///             2.0 / 2.0f64.sqrt(),
///             1.0 / 2.0f64.sqrt(),
///             1.0 / 2.0f64.sqrt(),
///             0.0,
///             3.0 / 6.0f64.sqrt(),
///             1.0 / 6.0f64.sqrt(),
///             0.0,
///             0.0,
///             2.0 / 3.0f64.sqrt(),
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(r, r_expect);
/// }
/// ```
///
/// ## Warnings
///
/// <div class="warning">
///
/// The Gram-Schmidt process,
/// when applied to non-square matrices,
/// results in `Q` that only satisfies the condition of having orthonormal columns.
/// For square matrices with linearly independent columns, i.e., full rank matrices,
/// the resulting `Q` is an orthogonal matrix.
///
/// The matrix should be a tall matrix or a square matrix, i.e., `R >= C`.
///
/// **_The Gram-Schmidt process is inherently numerically unstable._**
///
/// </div>
pub fn qr_decomposition_gs<N, const R: usize, const C: usize>(
    m: &Matrix<N, R, C>,
) -> Option<(Matrix<N, R, C>, Matrix<N, C, C>)>
where
    N: RealFloat,
{
    if R < C {
        eprintln!(concat!(
            "Error[matrix::extra::qr_decomposition_gs]: ",
            "The matrix should be a tall matrix or a square matrix"
        ));
        None
    } else {
        let mut q = Matrix::<N, R, C>::default();
        for colum in 1..=C {
            // A = [ a1 | a2 | ... | an ]
            let a = apply(
                &m.get_column(colum).expect(&format!(
                    concat!(
                        "Error[matrix::extra::qr_decomposition_gs]: ",
                        "Failed to retrieve the {}-th column of the matrix"
                    ),
                    colum,
                )),
                |e: &N| e.clone(),
            );
            // u1 = a1
            // uk = ak - sum((ak . en) en, {n, 1, k - 1})
            let mut u = a.clone();
            for k in 1..colum {
                // Q = [ e1 | e2 | ... | en ]
                let en = apply(
                    &q.get_column(k).expect(&format!(
                        concat!(
                            "Error[matrix::extra::qr_decomposition_gs]: ",
                            "Failed to retrieve the {}-th column of the matrix"
                        ),
                        k,
                    )),
                    |e: &N| e.clone(),
                );
                // uk(n) = uk(n - 1) - en (uk(n - 1) . en)
                u = u.clone() - en.clone() * dot_product(u, en)
            }
            let u_norm = euclidean_norm(&u);
            for row in 1..=R {
                // ek = uk / ||uk||
                q[(row, colum)] = index_c(&u, row).clone() / u_norm.clone();
            }
        }
        // R = Upper {  a_c . e_r } and R = Q^T A
        let r = transpose(&q) * m.clone();
        Some((q, r))
    }
}

/// Use the Householder method to compute the QR decomposition of a REAL matrix.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{extra::qr_decomposition_h, matrix::Matrix},
///     number::instances::double::Double,
/// };
///
/// fn main() {
///     let m = Matrix::<Double, 4, 3>::of(
///         &[1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, -1.0, 1.0, 0.0, 4.0].map(Double::of),
///     )
///     .unwrap();
///     let (q, r) = qr_decomposition_h(&m);
///     let q_expect = Matrix::<Double, 4, 4>::of(
///         &[
///             -0.5,
///             -0.5,
///             1.0 / (2.0 * 13.0f64.sqrt()),
///             -5.0 / (2.0 * 13.0f64.sqrt()),
///             -0.5,
///             -0.5,
///             -1.0 / (2.0 * 13.0f64.sqrt()),
///             5.0 / (2.0 * 13.0f64.sqrt()),
///             -0.5,
///             0.5,
///             -5.0 / (2.0 * 13.0f64.sqrt()),
///             -1.0 / (2.0 * 13.0f64.sqrt()),
///             -0.5,
///             0.5,
///             5.0 / (2.0 * 13.0f64.sqrt()),
///             1.0 / (2.0 * 13.0f64.sqrt()),
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(q, q_expect);
///     let r_expect = Matrix::<Double, 4, 3>::of(
///         &[
///             -2.0,
///             -1.0,
///             -2.0,
///             0.0,
///             -1.0,
///             1.0,
///             0.0,
///             0.0,
///             13.0f64.sqrt(),
///             0.0,
///             0.0,
///             0.0,
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(r, r_expect);
/// }
/// ```
pub fn qr_decomposition_h<N, const R: usize, const C: usize>(
    m: &Matrix<N, R, C>,
) -> (Matrix<N, R, R>, Matrix<N, R, C>)
where
    N: RealFloat,
{
    let mut q = Matrix::<N, R, R>::eyes();
    let mut r = m.clone();
    let two = N::one() + N::one();
    for column in 1..=C.min(R) {
        // x = col(m, c)
        let mut x = apply(
            &r.get_column(column).expect(&format!(
                concat!(
                    "Error[matrix::extra::qr_decomposition_gs]: ",
                    "Failed to retrieve the {}-th column of the matrix"
                ),
                column,
            )),
            |e| e.clone(),
        );
        // forall ei in x where i < c is zero
        for index in 1..column {
            x[(index, 1)] = N::zero();
        }
        let x_norm = euclidean_norm(&x);
        // v = x but v[col] = x[col] + x_norm * signum(x[col])
        let mut v = x.clone();
        v[(column, 1)] = x[(column, 1)].clone() + x_norm * x[(column, 1)].sign_number();
        // p = I - 2 / (v^T . v) (v * v^T)
        let p = Matrix::<N, R, R>::eyes()
            - layer_product(v.clone(), transpose(&v)) * two.clone() / dot_product(v.clone(), v);
        // r = pn p_{n - 1} ... p1 m
        r = p.clone() * r;
        // q = p1 p2 ... pn
        q = q * p;
    }
    (q, r)
}

/// Use the Householder method to compute the economy-size QR decomposition of a REAL matrix.
///
/// # Panics
///
/// This function requires the use of the `#![feature(generic_const_exprs)]`.
/// # Examples
///
/// ```rust
/// #![allow(incomplete_features)]
/// #![feature(generic_const_exprs)]
///
/// use rmatrix_ks::{
///     matrix::{extra::qr_decomposition_es, matrix::Matrix},
///     number::instances::double::Double,
/// };
///
/// fn main() {
///     let m = Matrix::<Double, 4, 3>::of(
///         &[1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, -1.0, 1.0, 0.0, 4.0].map(Double::of),
///     )
///     .unwrap();
///     let (q, r) = qr_decomposition_es(&m);
///     let q_expect = Matrix::<Double, 4, 3>::of(
///         &[
///             -0.5,
///             -0.5,
///             1.0 / (2.0 * 13.0f64.sqrt()),
///             -0.5,
///             -0.5,
///             -1.0 / (2.0 * 13.0f64.sqrt()),
///             -0.5,
///             0.5,
///             -5.0 / (2.0 * 13.0f64.sqrt()),
///             -0.5,
///             0.5,
///             5.0 / (2.0 * 13.0f64.sqrt()),
///         ]
///         .map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(q, q_expect);
///     let r_expect = Matrix::<Double, 3, 3>::of(
///         &[-2.0, -1.0, -2.0, 0.0, -1.0, 1.0, 0.0, 0.0, 13.0f64.sqrt()].map(Double::of),
///     )
///     .unwrap();
///     assert_eq!(r, r_expect);
/// }
/// ```
pub fn qr_decomposition_es<N, const R: usize, const C: usize>(
    m: &Matrix<N, R, C>,
) -> (
    Matrix<N, R, { Matrix::<N, R, C>::get_diagonal_length() }>,
    Matrix<N, { Matrix::<N, R, C>::get_diagonal_length() }, C>,
)
where
    N: RealFloat,
    [(); Matrix::<N, R, C>::get_diagonal_length()]:,
{
    let (basic_q, basic_r) = qr_decomposition_h(m);
    let thin = Matrix::<N, R, C>::get_diagonal_length();
    let mut q = Matrix::default();
    let mut r = Matrix::default();
    if R > C {
        // If m > n, then qr computes only the first n columns of Q and the first n rows of R.
        for row in 1..=R {
            for column in 1..=thin {
                q[(row, column)] = basic_q[(row, column)].clone();
            }
        }
        r.inner = basic_r.inner[..(thin * C)].to_vec();
    } else {
        // Else the economy-size decomposition is the same as the regular decomposition.
        q.inner = basic_q.inner;
        r.inner = basic_r.inner;
    }
    (q, r)
}

/// solve linear equations
///
/// **TODO**
pub fn linear_solve<N, const R: usize, const C: usize>(
    m: &Matrix<N, R, R>,
    b: &Matrix<N, R, C>,
) -> Matrix<N, R, C>
where
    N: Fractional,
{
    let m_inv = inverse(&m).unwrap();
    m_inv * b.clone()
}

/// eigen values
///
/// **TODO**
pub fn eigen_values() {
    todo!()
}

/// eigen vectors
///
/// **TODO**
pub fn eigen_system() {
    todo!()
}