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
use crate::{Matrix, Number};
pub mod inv;
pub mod operators;
pub mod powf;
pub mod powi;
#[derive(Clone, Debug, Default, Hash)]
pub struct DiagonalMatrix<T = f64>
where
T: Number,
{
d: Vec<T>,
}
impl<T> DiagonalMatrix<T>
where
T: Number,
{
pub fn new(d: Vec<T>) -> Self {
Self { d }
}
pub fn identity(n: usize) -> Self {
Self::new(vec![T::one(); n])
}
pub fn n(&self) -> usize {
self.d.len()
}
pub fn d(&self) -> &[T] {
&self.d
}
pub fn eject(self) -> Vec<T> {
self.d
}
pub fn mat(&self) -> Matrix<T> {
let n = self.d.len();
let mut mat = Matrix::<T>::new(n, n);
for i in 0..n {
mat[i][i] = self.d[i];
}
mat
}
}