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
use crate::{
number::{c64, Number},
MatrixError,
};
use rayon::prelude::*;
use std::error::Error;
pub mod or_un;
pub mod sy_he;
pub mod tr;
pub mod mm;
pub mod operations;
pub mod operators;
pub mod svd;
pub mod trf;
pub mod tri;
pub mod trs;
#[derive(Clone, Debug, Default, Hash, PartialEq)]
pub struct Matrix<T = f64>
where
T: Number,
{
rows: usize,
cols: usize,
elems: Vec<T>,
}
impl From<Box<dyn Error + Send + Sync>> for MatrixError {
fn from(e: Box<dyn Error + Send + Sync>) -> Self {
MatrixError::Others(e)
}
}
impl<T> Matrix<T>
where
T: Number,
{
pub fn new(rows: usize, cols: usize) -> Self {
Self {
rows,
cols,
elems: vec![T::default(); rows * cols],
}
}
pub fn from(rows: usize, elems: Vec<T>) -> Result<Self, MatrixError> {
let cols = elems.len() / rows;
if elems.len() != rows * cols {
return Err(MatrixError::DimensionMismatch);
}
Ok(Self { rows, cols, elems })
}
pub fn same_size(&self, rhs: &Matrix<T>) -> bool {
self.rows == rhs.rows && self.cols == rhs.cols
}
pub fn rows(&self) -> usize {
self.rows
}
pub fn cols(&self) -> usize {
self.cols
}
pub fn vec(self) -> Vec<T> {
self.elems
}
pub fn elems(&self) -> &[T] {
&self.elems
}
pub fn elems_mut(&mut self) -> &mut [T] {
&mut self.elems
}
pub fn reshape(mut self, rows: usize) -> Self {
self.rows = rows;
self.cols = self.elems.len() / rows;
self
}
pub fn eject_row(&self, index: usize) -> Vec<T> {
(0..self.cols)
.into_iter()
.map(|j| self[(index, j)])
.collect()
}
pub fn eject_sub_matrix(
&self,
start_i: usize,
start_j: usize,
rows: usize,
cols: usize,
) -> Matrix<T> {
Matrix::from(
rows,
(0..cols)
.into_iter()
.flat_map(|j| (0..rows).into_iter().map(move |i| (i, j)))
.map(|(i, j)| self[(start_i + i, start_j + j)])
.collect(),
)
.unwrap()
}
}
impl Matrix<f64> {
pub fn to_complex(&self) -> Matrix<c64> {
Matrix::<c64>::from(
self.rows,
self.elems.par_iter().map(|&e| c64::new(e, 0.0)).collect(),
)
.unwrap()
}
}
impl Matrix<c64> {
pub fn to_real(&self) -> (Matrix<f64>, Matrix<f64>) {
(
Matrix::from(self.rows, self.elems.par_iter().map(|e| e.re).collect()).unwrap(),
Matrix::from(self.rows, self.elems.par_iter().map(|e| e.im).collect()).unwrap(),
)
}
}
pub trait Vector<T>
where
T: Number,
{
fn row_mat(self) -> Matrix<T>;
fn col_mat(self) -> Matrix<T>;
}
impl<T> Vector<T> for Vec<T>
where
T: Number,
{
fn row_mat(self) -> Matrix<T> {
Matrix::<T>::from(1, self).unwrap()
}
fn col_mat(self) -> Matrix<T> {
Matrix::<T>::from(self.len(), self).unwrap()
}
}