mech_math/ops/
div.rs

1#[macro_use]
2use crate::*;
3use num_traits::*;
4#[cfg(feature = "matrix")]
5use mech_core::matrix::Matrix;
6
7// Div ------------------------------------------------------------------------
8
9macro_rules! div_op {
10  ($lhs:expr, $rhs:expr, $out:expr) => {
11    unsafe {
12      *$out = *$lhs / *$rhs;
13    }
14  };
15}
16
17macro_rules! div_vec_op {
18  ($lhs:expr, $rhs:expr, $out:expr) => {
19    unsafe {
20      *$out = (&*$lhs).component_div(&*$rhs);
21    }
22  };
23}
24
25macro_rules! div_scalar_lhs_op {
26  ($lhs:expr, $rhs:expr, $out:expr) => {
27    unsafe {
28      for i in 0..(&*$lhs).len() {
29        (&mut *$out)[i] = (&*$lhs)[i] / *$rhs;
30      }
31    }
32  };
33}
34
35macro_rules! div_scalar_rhs_op {
36  ($lhs:expr, $rhs:expr, $out:expr) => {
37    unsafe {
38      for i in 0..(&*$rhs).len() {
39        (&mut *$out)[i] = *$lhs / (&*$rhs)[i];
40      }
41    }
42  };
43}
44
45macro_rules! div_mat_vec_op {
46  ($lhs:expr, $rhs:expr, $out:expr) => {
47    unsafe {
48      let mut out_deref = &mut (*$out);
49      let lhs_deref = &(*$lhs);
50      let rhs_deref = &(*$rhs);
51      for (mut col, lhs_col) in out_deref.column_iter_mut().zip(lhs_deref.column_iter()) {
52        for i in 0..col.len() {
53          col[i] = lhs_col[i] / rhs_deref[i];
54        }
55      }
56    }
57  };}
58    
59macro_rules! div_vec_mat_op {
60  ($lhs:expr, $rhs:expr, $out:expr) => {
61    unsafe {
62      let mut out_deref = &mut (*$out);
63      let lhs_deref = &(*$lhs);
64      let rhs_deref = &(*$rhs);
65      for (mut col, rhs_col) in out_deref.column_iter_mut().zip(rhs_deref.column_iter()) {
66        for i in 0..col.len() {
67          col[i] = lhs_deref[i] / rhs_col[i];
68        }
69      }
70    }
71  };}
72
73macro_rules! div_mat_row_op {
74  ($lhs:expr, $rhs:expr, $out:expr) => {
75    unsafe {
76      let mut out_deref = &mut (*$out);
77      let lhs_deref = &(*$lhs);
78      let rhs_deref = &(*$rhs);
79      for (mut row, lhs_row) in out_deref.row_iter_mut().zip(lhs_deref.row_iter()) {
80        for i in 0..row.len() {
81          row[i] = lhs_row[i] / rhs_deref[i];
82        }
83      }
84    }
85  };}
86
87macro_rules! div_row_mat_op {
88  ($lhs:expr, $rhs:expr, $out:expr) => {
89    unsafe {
90      let mut out_deref = &mut (*$out);
91      let lhs_deref = &(*$lhs);
92      let rhs_deref = &(*$rhs);
93      for (mut row, rhs_row) in out_deref.row_iter_mut().zip(rhs_deref.row_iter()) {
94        for i in 0..row.len() {
95          row[i] = lhs_deref[i] / rhs_row[i];
96        }
97      }
98    }
99  };} 
100
101impl_math_fxns!(Div);
102
103fn impl_div_fxn(lhs_value: Value, rhs_value: Value) -> MResult<Box<dyn MechFunction>> {
104  impl_binop_match_arms!(
105    Div,
106    register_fxn_descriptor_inner,
107    (lhs_value, rhs_value),
108    I8,   i8,   "i8";
109    I16,  i16,  "i16";
110    I32,  i32,  "i32";
111    I64,  i64,  "i64";
112    I128, i128, "i128";
113    U8,   u8,   "u8";
114    U16,  u16,  "u16";
115    U32,  u32,  "u32";
116    U64,  u64,  "u64";
117    U128, u128, "u128";
118    F32,  f32,  "f32";
119    F64,  f64,  "f64";
120    R64, R64, "rational";
121    C64, C64, "complex";
122  )
123}
124
125impl_mech_binop_fxn!(MathDiv,impl_div_fxn,"math/div");