mech_compare/
min.rs

1use crate::*;
2use mech_core::*;
3#[cfg(feature = "matrix")]
4use mech_core::matrix::Matrix;
5
6// Min ------------------------------------------------------------------------
7
8macro_rules! min_scalar_lhs_op {
9  ($lhs:expr, $rhs:expr, $out:expr) => {
10    unsafe {
11      for i in 0..(&*$lhs).len() {
12        let a = (&*$lhs)[i].clone();
13        let b = (*$rhs).clone();
14        (&mut *$out)[i] =
15          if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) {
16            a
17          } else {
18            b
19          };
20      }
21    }
22  };
23}
24
25macro_rules! min_scalar_lhs_op {
26  ($lhs:expr, $rhs:expr, $out:expr) => {
27    unsafe {
28      for i in 0..(&*$lhs).len() {
29        let a = (&*$lhs)[i].clone();
30        let b = (*$rhs).clone();
31        (&mut *$out)[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
32      }
33    }
34  };
35}
36
37macro_rules! min_scalar_rhs_op {
38  ($lhs:expr, $rhs:expr, $out:expr) => {
39    unsafe {
40      for i in 0..(&*$rhs).len() {
41        let a = (*$lhs).clone();
42        let b = (&*$rhs)[i].clone();
43        (&mut *$out)[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
44      }
45    }
46  };
47}
48
49macro_rules! min_vec_op {
50  ($lhs:expr, $rhs:expr, $out:expr) => {
51    unsafe {
52      for i in 0..(&*$lhs).len() {
53        let a = (&*$lhs)[i].clone();
54        let b = (&*$rhs)[i].clone();
55        (&mut *$out)[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
56      }
57    }
58  };
59}
60
61macro_rules! min_op {
62  ($lhs:expr, $rhs:expr, $out:expr) => {
63    unsafe {
64      let a = (*$lhs).clone();
65      let b = (*$rhs).clone();
66      (*$out) = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
67    }
68  };
69}
70
71macro_rules! min_mat_vec_op {
72  ($lhs:expr, $rhs:expr, $out:expr) => {
73    unsafe {
74      let mut out_deref = &mut (*$out);
75      let lhs_deref = &(*$lhs);
76      let rhs_deref = &(*$rhs);
77      for (mut col, lhs_col) in out_deref.column_iter_mut().zip(lhs_deref.column_iter()) {
78        for i in 0..col.len() {
79          let a = lhs_col[i].clone();
80          let b = rhs_deref[i].clone();
81          col[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
82        }
83      }
84    }
85  };
86}
87
88macro_rules! min_vec_mat_op {
89  ($lhs:expr, $rhs:expr, $out:expr) => {
90    unsafe {
91      let mut out_deref = &mut (*$out);
92      let lhs_deref = &(*$lhs);
93      let rhs_deref = &(*$rhs);
94      for (mut col, rhs_col) in out_deref.column_iter_mut().zip(rhs_deref.column_iter()) {
95        for i in 0..col.len() {
96          let a = lhs_deref[i].clone();
97          let b = rhs_col[i].clone();
98          col[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
99        }
100      }
101    }
102  };
103}
104
105macro_rules! min_mat_row_op {
106  ($lhs:expr, $rhs:expr, $out:expr) => {
107    unsafe {
108      let mut out_deref = &mut (*$out);
109      let lhs_deref = &(*$lhs);
110      let rhs_deref = &(*$rhs);
111      for (mut row, lhs_row) in out_deref.row_iter_mut().zip(lhs_deref.row_iter()) {
112        for i in 0..row.len() {
113          let a = lhs_row[i].clone();
114          let b = rhs_deref[i].clone();
115          row[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
116        }
117      }
118    }
119  };
120}
121
122macro_rules! min_row_mat_op {
123  ($lhs:expr, $rhs:expr, $out:expr) => {
124    unsafe {
125      let mut out_deref = &mut (*$out);
126      let lhs_deref = &(*$lhs);
127      let rhs_deref = &(*$rhs);
128      for (mut row, rhs_row) in out_deref.row_iter_mut().zip(rhs_deref.row_iter()) {
129        for i in 0..row.len() {
130          let a = lhs_deref[i].clone();
131          let b = rhs_row[i].clone();
132          row[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Greater) { a } else { b };
133        }
134      }
135    }
136  };
137}
138
139
140impl_compare_fxns2!(Min);
141
142fn impl_min_fxn(lhs_value: Value, rhs_value: Value) -> MResult<Box<dyn MechFunction>> {
143  impl_binop_match_arms!(
144    Min,
145    register_fxn_descriptor_inner,
146    (lhs_value, rhs_value),
147    I8,   i8, "i8";
148    I16,  i16, "i16";
149    I32,  i32, "i32";
150    I64,  i64, "i64";
151    I128, i128, "i128";
152    U8,   u8, "u8";
153    U16,  u16, "u16";
154    U32,  u32, "u32";
155    U64,  u64, "u64";
156    U128, u128, "u128";
157    F32,  f32, "f32";
158    F64,  f64, "f64";
159    R64, R64, "rational";
160    C64, C64, "complex";
161  )
162}
163
164impl_mech_binop_fxn!(CompareMin,impl_min_fxn,"compare/min");
165