1use crate::IsFloat;
2use crate::Result;
3use crate::Jeeperr;
4use crate::linalg::Matrix;
5
6pub trait BooleanMatrixOperations<T: IsFloat + std::fmt::Debug + Copy + Clone> {
8 fn equal(matrix: &Matrix<T>, number: T) -> Vec<bool>;
11
12 fn less_than(matrix: &Matrix<T>, number: T) -> Vec<bool>;
15 fn less_equal(matrix: &Matrix<T>, number: T) -> Vec<bool>;
18
19 fn greater_than(matrix: &Matrix<T>, number: T) -> Vec<bool>;
22 fn greater_equal(matrix: &Matrix<T>, number: T) -> Vec<bool>;
25
26 fn equal_mat(left_matrix: &Matrix<T>, right_matrix: &Matrix<T>) -> Result<Vec<bool>>;
31
32 fn less_than_mat(left_matrix: &Matrix<T>, right_matrix: &Matrix<T>) -> Result<Vec<bool>>;
37 fn less_equal_mat(left_matrix: &Matrix<T>, right_matrix: &Matrix<T>) -> Result<Vec<bool>>;
42
43 fn greater_than_mat(left_matrix: &Matrix<T>, right_matrix: &Matrix<T>) -> Result<Vec<bool>>;
48 fn greater_equal_mat(left_matrix: &Matrix<T>, right_matrix: &Matrix<T>) -> Result<Vec<bool>>;
53}
54
55impl BooleanMatrixOperations<f32> for Matrix<f32> {
56 fn equal(matrix: &Matrix<f32>, number: f32) -> Vec<bool> {
59 let output_bools: Vec<bool> = matrix.get_data().into_iter()
60 .map(|&item| item == number)
61 .collect::<Vec<bool>>();
62
63 return output_bools
64 }
65
66 fn less_than(matrix: &Matrix<f32>, number: f32) -> Vec<bool> {
69 let output_bools: Vec<bool> = matrix.get_data().into_iter()
70 .map(|&item| item < number)
71 .collect::<Vec<bool>>();
72
73 return output_bools
74 }
75
76 fn less_equal(matrix: &Matrix<f32>, number: f32) -> Vec<bool> {
79 let output_bools: Vec<bool> = matrix.get_data().into_iter()
80 .map(|&item| item <= number)
81 .collect::<Vec<bool>>();
82
83 return output_bools
84 }
85
86 fn greater_than(matrix: &Matrix<f32>, number: f32) -> Vec<bool> {
89 let output_bools: Vec<bool> = matrix.get_data().into_iter()
90 .map(|&item| item > number)
91 .collect::<Vec<bool>>();
92
93 return output_bools
94 }
95
96 fn greater_equal(matrix: &Matrix<f32>, number: f32) -> Vec<bool> {
99 let output_bools: Vec<bool> = matrix.get_data().into_iter()
100 .map(|&item| item >= number)
101 .collect::<Vec<bool>>();
102
103 return output_bools
104 }
105
106 fn equal_mat(left_matrix: &Matrix<f32>, right_matrix: &Matrix<f32>) -> Result<Vec<bool>> {
111 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
112 return Err(Jeeperr::DimensionError)
113 }
114
115 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
116
117 let output_bools: Vec<bool> = (0..n_elements).into_iter()
118 .map(|idx| left_matrix.lindex(idx) == right_matrix.lindex(idx))
119 .collect::<Vec<bool>>();
120
121 return Ok(output_bools)
122 }
123
124 fn less_than_mat(left_matrix: &Matrix<f32>, right_matrix: &Matrix<f32>) -> Result<Vec<bool>> {
129 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
130 return Err(Jeeperr::DimensionError)
131 }
132
133 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
134
135 let output_bools: Vec<bool> = (0..n_elements).into_iter()
136 .map(|idx| left_matrix.lindex(idx) < right_matrix.lindex(idx))
137 .collect::<Vec<bool>>();
138
139 return Ok(output_bools)
140 }
141
142 fn less_equal_mat(left_matrix: &Matrix<f32>, right_matrix: &Matrix<f32>) -> Result<Vec<bool>> {
147 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
148 return Err(Jeeperr::DimensionError)
149 }
150
151 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
152
153 let output_bools: Vec<bool> = (0..n_elements).into_iter()
154 .map(|idx| left_matrix.lindex(idx) <= right_matrix.lindex(idx))
155 .collect::<Vec<bool>>();
156
157 return Ok(output_bools)
158 }
159
160 fn greater_than_mat(left_matrix: &Matrix<f32>, right_matrix: &Matrix<f32>) -> Result<Vec<bool>> {
165 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
166 return Err(Jeeperr::DimensionError)
167 }
168
169 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
170
171 let output_bools: Vec<bool> = (0..n_elements).into_iter()
172 .map(|idx| left_matrix.lindex(idx) > right_matrix.lindex(idx))
173 .collect::<Vec<bool>>();
174
175 return Ok(output_bools)
176 }
177
178 fn greater_equal_mat(left_matrix: &Matrix<f32>, right_matrix: &Matrix<f32>) -> Result<Vec<bool>> {
183 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
184 return Err(Jeeperr::DimensionError)
185 }
186
187 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
188
189 let output_bools: Vec<bool> = (0..n_elements).into_iter()
190 .map(|idx| left_matrix.lindex(idx) >= right_matrix.lindex(idx))
191 .collect::<Vec<bool>>();
192
193 return Ok(output_bools)
194 }
195}
196
197impl BooleanMatrixOperations<f64> for Matrix<f64> {
198 fn equal(matrix: &Matrix<f64>, number: f64) -> Vec<bool> {
201 let output_bools: Vec<bool> = matrix.get_data().into_iter()
202 .map(|&item| item == number)
203 .collect::<Vec<bool>>();
204
205 return output_bools
206 }
207
208 fn less_than(matrix: &Matrix<f64>, number: f64) -> Vec<bool> {
211 let output_bools: Vec<bool> = matrix.get_data().into_iter()
212 .map(|&item| item < number)
213 .collect::<Vec<bool>>();
214
215 return output_bools
216 }
217
218 fn less_equal(matrix: &Matrix<f64>, number: f64) -> Vec<bool> {
221 let output_bools: Vec<bool> = matrix.get_data().into_iter()
222 .map(|&item| item <= number)
223 .collect::<Vec<bool>>();
224
225 return output_bools
226 }
227
228 fn greater_than(matrix: &Matrix<f64>, number: f64) -> Vec<bool> {
231 let output_bools: Vec<bool> = matrix.get_data().into_iter()
232 .map(|&item| item > number)
233 .collect::<Vec<bool>>();
234
235 return output_bools
236 }
237
238 fn greater_equal(matrix: &Matrix<f64>, number: f64) -> Vec<bool> {
241 let output_bools: Vec<bool> = matrix.get_data().into_iter()
242 .map(|&item| item >= number)
243 .collect::<Vec<bool>>();
244
245 return output_bools
246 }
247
248 fn equal_mat(left_matrix: &Matrix<f64>, right_matrix: &Matrix<f64>) -> Result<Vec<bool>> {
253 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
254 return Err(Jeeperr::DimensionError)
255 }
256
257 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
258
259 let output_bools: Vec<bool> = (0..n_elements).into_iter()
260 .map(|idx| left_matrix.lindex(idx) == right_matrix.lindex(idx))
261 .collect::<Vec<bool>>();
262
263 return Ok(output_bools)
264 }
265
266 fn less_than_mat(left_matrix: &Matrix<f64>, right_matrix: &Matrix<f64>) -> Result<Vec<bool>> {
271 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
272 return Err(Jeeperr::DimensionError)
273 }
274
275 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
276
277 let output_bools: Vec<bool> = (0..n_elements).into_iter()
278 .map(|idx| left_matrix.lindex(idx) < right_matrix.lindex(idx))
279 .collect::<Vec<bool>>();
280
281 return Ok(output_bools)
282 }
283
284 fn less_equal_mat(left_matrix: &Matrix<f64>, right_matrix: &Matrix<f64>) -> Result<Vec<bool>> {
289 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
290 return Err(Jeeperr::DimensionError)
291 }
292
293 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
294
295 let output_bools: Vec<bool> = (0..n_elements).into_iter()
296 .map(|idx| left_matrix.lindex(idx) <= right_matrix.lindex(idx))
297 .collect::<Vec<bool>>();
298
299 return Ok(output_bools)
300 }
301
302 fn greater_than_mat(left_matrix: &Matrix<f64>, right_matrix: &Matrix<f64>) -> Result<Vec<bool>> {
307 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
308 return Err(Jeeperr::DimensionError)
309 }
310
311 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
312
313 let output_bools: Vec<bool> = (0..n_elements).into_iter()
314 .map(|idx| left_matrix.lindex(idx) > right_matrix.lindex(idx))
315 .collect::<Vec<bool>>();
316
317 return Ok(output_bools)
318 }
319
320 fn greater_equal_mat(left_matrix: &Matrix<f64>, right_matrix: &Matrix<f64>) -> Result<Vec<bool>> {
325 if left_matrix.get_rows() != right_matrix.get_rows() || left_matrix.get_cols() != right_matrix.get_cols() {
326 return Err(Jeeperr::DimensionError)
327 }
328
329 let n_elements: usize = left_matrix.get_rows() * left_matrix.get_cols();
330
331 let output_bools: Vec<bool> = (0..n_elements).into_iter()
332 .map(|idx| left_matrix.lindex(idx) >= right_matrix.lindex(idx))
333 .collect::<Vec<bool>>();
334
335 return Ok(output_bools)
336 }
337}