rmatrix_ks 0.6.6

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
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
//! # matrix::vector
//!
//! Definitions of column vectors and row vectors,
//! along with related functions.
//!
//! Default vectors are column vectors,
//! so most functions are implemented only for column vectors.
//! For row vectors, you can first transpose them into column vectors
//! and then use the corresponding functions.

use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};

use crate::{
    matrix::Matrix,
    number::{
        instances::integer::Integer,
        traits::{number::Number, real::Real, realfloat::RealFloat, realfrac::RealFrac},
    },
};

/// Constructing a row vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{Matrix, vector::row_vector},
///     number::instances::word8::Word8,
/// };
///
/// let rv = row_vector(3, &[1, 2, 3].map(Word8::of));
/// let rv_expect = Matrix::of(1, 3, &[1, 2, 3].map(Word8::of));
/// assert_eq!(rv, rv_expect);
/// ```
pub fn row_vector<N>(dim: usize, data: &[N]) -> Option<Matrix<N>>
where
    N: Clone,
{
    Matrix::of(1, dim, data)
}
/// Determine whether the given matrix is a row vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{
///         Matrix,
///         vector::{is_row_vector, row_vector},
///     },
///     number::instances::word8::Word8,
/// };
///
/// let v = row_vector(3, &[1, 0, 0].map(Word8::of)).unwrap();
/// assert!(is_row_vector(&v));
/// let m = Matrix::of(2, 2, &[1, 0, 2, 5].map(Word8::of)).unwrap();
/// assert!(!is_row_vector(&m));
/// ```
pub fn is_row_vector<N>(v: &Matrix<N>) -> bool { v.row == 1 }

/// Constructing a column vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{Matrix, vector::column_vector},
///     number::instances::word8::Word8,
/// };
///
/// let cv = column_vector(3, &[1, 2, 3].map(Word8::of));
/// let cv_expect = Matrix::of(3, 1, &[1, 2, 3].map(Word8::of));
/// assert_eq!(cv, cv_expect);
/// ```
pub fn column_vector<N>(dim: usize, data: &[N]) -> Option<Matrix<N>>
where
    N: Clone,
{
    Matrix::of(dim, 1, data)
}

/// Determine whether the given matrix is a column vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{
///         Matrix,
///         vector::{column_vector, is_column_vector},
///     },
///     number::instances::word8::Word8,
/// };
///
/// let v = column_vector(3, &[1, 0, 0].map(Word8::of)).unwrap();
/// assert!(is_column_vector(&v));
/// let m = Matrix::of(2, 2, &[1, 0, 2, 5].map(Word8::of)).unwrap();
/// assert!(!is_column_vector(&m));
/// ```
pub fn is_column_vector<N>(v: &Matrix<N>) -> bool { v.column == 1 }

/// Used to obtain the basis vector.
///
/// The `index` represents the index of the basis vector.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{Matrix, vector::basis_vector},
///     number::instances::int8::Int8,
/// };
///
/// let e1_a: Matrix<Int8> =
///     Matrix::of(3, 1, &[Int8::of(0), Int8::of(1), Int8::of(0)]).unwrap();
/// let e1_b = basis_vector::<Int8>(3, 2);
/// assert_eq!(e1_a, e1_b);
/// ```
pub fn basis_vector<N>(dim: usize, index: usize) -> Matrix<N>
where
    N: Number,
{
    let mut basis = Matrix::<N>::defaults(dim, 1);
    basis[(index, 1)] = N::one();
    basis
}

/// Calculate the dot product of two column vectors.
///
/// # Panics
///
/// The dimensions of the column vectors involved in the function must match.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, dot_product},
///     number::instances::int8::Int8,
/// };
///
/// let v1 = column_vector::<Int8>(3, &[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2 = column_vector::<Int8>(3, &[Int8::of(4), Int8::of(5), Int8::of(6)]).unwrap();
/// assert_eq!(dot_product(&v1, &v2), Int8::of(32));
/// ```
pub fn dot_product<N>(v1: &Matrix<N>, v2: &Matrix<N>) -> N
where
    N: Real,
{
    assert!(
        is_column_vector(v1) && is_column_vector(v2) && v1.row == v2.row,
        concat!(
            "Error[matrix::vector::dot_product]: ",
            "Only column vectors with matching dimensions can be multiplied."
        )
    );

    v1.inner
        .par_iter()
        .zip(v2.inner.par_iter())
        .map(|(e1, e2)| e1.clone() * e2.clone())
        .reduce(|| N::zero(), |acc, e| acc + e)
}

/// Calculate the cross product of two 3-dim column vectors.
///
/// # Panics
///
/// Only 3-dim column vectors can be crossed.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, cross_product},
///     number::instances::int8::Int8,
/// };
///
/// let v1 = column_vector::<Int8>(3, &[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2 = column_vector::<Int8>(3, &[Int8::of(4), Int8::of(5), Int8::of(6)]).unwrap();
/// assert_eq!(
///     cross_product(v1, v2),
///     column_vector::<Int8>(3, &[Int8::of(-3), Int8::of(6), Int8::of(-3)]).unwrap()
/// );
/// ```
pub fn cross_product<N>(v1: Matrix<N>, v2: Matrix<N>) -> Matrix<N>
where
    N: Number,
{
    assert!(
        v1.shape() == (3, 1) && v2.shape() == (3, 1),
        concat!(
            "Error[matrix::vector::cross_product]: ",
            "Only three-dimensional column vectors can be crossed."
        )
    );

    let mut inner = vec![N::zero(); 3];
    inner[0] = v1[(2, 1)].clone() * v2[(3, 1)].clone() - v1[(3, 1)].clone() * v2[(2, 1)].clone();
    inner[1] = v1[(3, 1)].clone() * v2[(1, 1)].clone() - v1[(1, 1)].clone() * v2[(3, 1)].clone();
    inner[2] = v1[(1, 1)].clone() * v2[(2, 1)].clone() - v1[(2, 1)].clone() * v2[(1, 1)].clone();
    Matrix {
        inner,
        row: 3,
        column: 1,
    }
}

/// Construct a matrix using one column vector and one row vector.
///
/// # Panics
///
/// Only one column vector and one row vector can be used for the layer product.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::{
///         Matrix,
///         vector::{column_vector, layer_product, row_vector},
///     },
///     number::instances::int8::Int8,
/// };
///
/// let v1 = column_vector::<Int8>(3, &[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2 = row_vector::<Int8>(3, &[Int8::of(4), Int8::of(5), Int8::of(6)]).unwrap();
/// let data = [
///     4i8, 5i8, 6i8, // row1
///     8i8, 10i8, 12i8, // row2
///     12i8, 15i8, 18i8, // row3
/// ]
/// .map(|e| Int8::of(e));
/// let m = Matrix::<Int8>::of(3, 3, &data).unwrap();
/// assert_eq!(layer_product(&v1, &v2), m);
/// ```
pub fn layer_product<N>(v1: &Matrix<N>, v2: &Matrix<N>) -> Matrix<N>
where
    N: Number,
{
    assert!(
        is_column_vector(v1) && is_row_vector(v2),
        concat!(
            "Error[matrix::vector::layer_product]: ",
            "Only one column vector and one row vector ",
            "can be used for the layer product."
        )
    );

    let mut inner = Vec::with_capacity(v1.row * v2.column);
    for row_index in 1..=v1.row {
        for column_index in 1..=v2.column {
            inner.push(v1[(row_index, 1)].clone() * v2[(1, column_index)].clone());
        }
    }
    Matrix {
        inner,
        row: v1.row,
        column: v2.column,
    }
}

/// Calculate the convolution of two column vectors.
///
/// # Panics
///
/// Only column vectors can be convolved.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, convolution},
///     number::instances::int8::Int8,
/// };
///
/// let v1 = column_vector::<Int8>(3, &[Int8::of(1), Int8::of(2), Int8::of(3)]).unwrap();
/// let v2 = column_vector::<Int8>(2, &[Int8::of(4), Int8::of(5)]).unwrap();
/// let cv = column_vector::<Int8>(4, &[4, 13, 22, 15].map(|e| Int8::of(e))).unwrap();
/// assert_eq!(convolution(&v1, &v2), cv);
/// ```
pub fn convolution<N>(v1: &Matrix<N>, v2: &Matrix<N>) -> Matrix<N>
where
    N: Number,
{
    assert!(
        is_column_vector(v1) && is_column_vector(v2),
        concat!(
            "Error[matrix::vector::convolution]: ",
            "Only column vectors can be convolved."
        )
    );

    let mut inner = Vec::with_capacity(v1.row + v2.row - 1);
    for index in 1..=(v1.row + v2.row - 1) {
        let mut sum = N::zero();
        for p in 1..=v1.row.min(index) {
            let q = index + 1 - p;
            if (1..=v2.row).contains(&q) {
                sum = sum + v1[(p, 1)].clone() * v2[(q, 1)].clone();
            }
        }
        inner.push(sum);
    }
    Matrix {
        inner,
        row: v1.row + v2.row - 1,
        column: 1,
    }
}

/// Calculate the Euclidean norm.
///
/// Aka L2-norm.
///
/// # Panics
///
/// Only vectors have the Euclidean norm.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, euclidean_norm},
///     number::{
///         instances::float::Float,
///         traits::{floating::Floating, zero::Zero},
///     },
/// };
///
/// let v =
///     column_vector::<Float>(3, &[Float::of(1.0), Float::of(2.0), Float::of(3.0)]).unwrap();
/// assert!((euclidean_norm(&v) - Float::of(14.0).square_root()).is_zero());
/// ```
pub fn euclidean_norm<N>(v: &Matrix<N>) -> N
where
    N: RealFloat,
{
    assert!(
        is_row_vector(v) || is_column_vector(v),
        concat!(
            "Error[matrix::vector::euclidean_norm]: ",
            "Only vectors have the Euclidean norm."
        )
    );

    v.inner
        .par_iter()
        .map(|e| e.clone() * e.clone())
        .reduce(|| N::zero(), |acc, e| acc + e)
        .square_root()
}

/// Normalize the real vector.
///
/// # Panics
///
/// This function can only be used for column vector normalization.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, normalize},
///     number::{instances::float::Float, traits::floating::Floating},
/// };
///
/// let v1 = column_vector::<Float>(3, &[1.0, 1.0, 1.0].map(Float::of)).unwrap();
/// let normalized = normalize(&v1);
/// let normalized_expect = v1 / Float::of(3.0).square_root();
/// assert_eq!(normalized, normalized_expect);
/// ```
pub fn normalize<N>(v: &Matrix<N>) -> Matrix<N>
where
    N: RealFloat,
{
    assert!(
        is_column_vector(v),
        concat!(
            "Error[matrix::vector::normalize]: ",
            "This function can only be used for column vector normalization."
        )
    );

    if v.inner.par_iter().all(|e| e.is_zero()) {
        Matrix::defaults(v.row, 1)
    } else {
        let v_norm = euclidean_norm(v);
        v.clone() / v_norm
    }
}

/// Calculate the maximum norm.
///
/// Aka L_inf-norm.
///
/// # Panics
///
/// This function can only be used for column vectors.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, maximum_norm},
///     number::instances::int8::Int8,
/// };
///
/// let v = column_vector::<Int8>(3, &[Int8::of(2), Int8::of(-5), Int8::of(3)]).unwrap();
/// assert_eq!(maximum_norm(&v), Int8::of(5))
/// ```
pub fn maximum_norm<N>(v: &Matrix<N>) -> N
where
    N: Real,
{
    assert!(
        is_column_vector(v),
        concat!(
            "Error[matrix::vector::maximum_norm]: ",
            "This function can only be used for column vectors."
        )
    );

    let mut norm = N::zero();
    for e in v.linear_iter().map(|e| e.absolute_value()) {
        if e > norm {
            norm = e;
        }
    }
    norm
}

/// Calculate the root mean square of the column vector.
///
/// # Panics
///
/// This function can only be used for column vectors.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, root_mean_square},
///     number::{
///         instances::float::Float,
///         traits::{floating::Floating, zero::Zero},
///     },
/// };
///
/// let v =
///     column_vector::<Float>(3, &[Float::of(1.0), Float::of(2.0), Float::of(3.0)]).unwrap();
/// assert!((root_mean_square(&v) - Float::of(14.0 / 3.0).square_root()).is_zero());
/// ```
pub fn root_mean_square<N>(v: &Matrix<N>) -> N
where
    N: RealFloat,
{
    assert!(
        is_column_vector(v),
        concat!(
            "Error[matrix::vector::root_mean_square]: ",
            "This function can only be used for column vectors."
        )
    );

    let l2_norm = euclidean_norm(v);
    let r_sqrt = N::from_integer(Integer::of_str(&format!("{}", v.row)).unwrap_or_else(|| {
        panic!(
            "Error[matrix::vector::root_mean_square]: Failed to convert {} from usize to Integer.",
            v.row
        )
    }))
    .square_root();
    l2_norm / r_sqrt
}

/// Calculate the angle between two vectors.
///
/// Expressed in radians.
///
/// # Panics
///
/// This function can only be used for column vectors with matching dimensions.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{angle_between, column_vector},
///     number::{instances::double::Double, traits::zero::Zero},
/// };
///
/// let v1 = column_vector::<Double>(3, &[Double::of(1.0), Double::of(5.0), Double::of(4.0)])
///     .unwrap();
/// let v2 = column_vector::<Double>(3, &[Double::of(8.0), Double::of(-4.0), Double::of(3.0)])
///     .unwrap();
/// let angle = angle_between(&v1, &v2);
/// assert!(angle.is_some());
/// assert_eq!(angle.unwrap(), Double::of(core::f64::consts::PI / 2.0));
///
/// // angle between a vector and itself is zero
/// let self_angle = angle_between(&v1, &v1);
/// assert!(self_angle.is_some());
/// assert!(self_angle.unwrap().is_zero());
///
/// // zero-vector has no angle with any other vector
/// let v3 =
///     column_vector::<Double>(3, &[Double::zero(), Double::zero(), Double::zero()]).unwrap();
/// assert!(angle_between(&v1, &v3).is_none());
/// ```
pub fn angle_between<N>(v1: &Matrix<N>, v2: &Matrix<N>) -> Option<N>
where
    N: RealFloat,
{
    assert!(
        is_column_vector(v1) && is_column_vector(v2) && v1.row == v2.row,
        concat!(
            "Error[matrix::vector::angle_between]: ",
            "This function can only be used for column vectors with matching dimensions."
        )
    );

    let zero_vector = Matrix::defaults(v1.row, 1);
    if v1 == &zero_vector || v2 == &zero_vector {
        eprintln!(concat!(
            "Error[matrix::vector::angle_between]: ",
            "The zero-vector has no angle with other vectors."
        ));
        None
    } else if v1 == v2 {
        Some(N::zero())
    } else {
        Some((dot_product(&normalize(v1), &normalize(v2))).arc_cosine())
    }
}

/// Project one vector onto another vector.
///
/// # Panics
///
/// This function can only be used for column vectors with matching dimensions.
///
/// # Examples
///
/// ```rust
/// use rmatrix_ks::{
///     matrix::vector::{column_vector, project_to},
///     number::instances::float::Float,
/// };
///
/// let v1 = column_vector::<Float>(2, &[3.0, 4.0].map(Float::of)).unwrap();
/// let v2 = column_vector::<Float>(2, &[1.0, 2.0].map(Float::of)).unwrap();
/// let p = project_to(&v1, &v2);
/// assert_eq!(
///     p,
///     column_vector::<Float>(2, &[2.2, 4.4].map(Float::of)).unwrap()
/// );
/// ```
pub fn project_to<N>(from: &Matrix<N>, to: &Matrix<N>) -> Matrix<N>
where
    N: RealFrac,
{
    assert!(
        is_column_vector(from) && is_column_vector(to) && from.row == to.row,
        concat!(
            "Error[matrix::vector::project_to]: ",
            "This function can only be used for column vectors with matching dimensions."
        )
    );

    let p1 = dot_product(to, from);
    let p2 = dot_product(to, to);
    to.clone() * (p1 / p2)
}