mech_stats/
sum_row.rs

1use crate::*;
2use mech_core::*;
3use num_traits::*;
4#[cfg(feature = "matrix")]
5use mech_core::matrix::Matrix;
6
7// Stats Sum Row -----------------------------------------------------------
8
9macro_rules! sum_row_op {
10    ($arg:expr, $out:expr) => {
11      unsafe { 
12        *$out = (*$arg).row_sum();
13      }
14    };}
15
16
17macro_rules! sum_row_op2 {
18  ($arg:expr, $out:expr) => {
19    unsafe {
20      let mut sum = T::zero();
21      for i in 0..(*$arg).len() {
22        sum += (&(*$arg))[i];
23      }
24      (&mut (*$out))[(0, 0)] = sum;
25    }
26  };}
27  
28  #[cfg(all(feature = "matrix1", feature = "matrix1"))]
29  impls_stas!(StatsSumRowM1, Matrix1<T>, Matrix1<T>, sum_row_op);
30  #[cfg(all(feature = "matrix2", feature = "row_vector2"))]
31  impls_stas!(StatsSumRowM2, Matrix2<T>, RowVector2<T>, sum_row_op);
32  #[cfg(all(feature = "matrix3", feature = "row_vector3"))]
33  impls_stas!(StatsSumRowM3, Matrix3<T>, RowVector3<T>, sum_row_op);
34  #[cfg(all(feature = "matrix4", feature = "row_vector4"))]
35  impls_stas!(StatsSumRowM4, Matrix4<T>, RowVector4<T>, sum_row_op);
36  #[cfg(all(feature = "matrix2x3", feature = "row_vector3"))]
37  impls_stas!(StatsSumRowM2x3, Matrix2x3<T>, RowVector3<T>, sum_row_op);
38  #[cfg(all(feature = "matrix3x2", feature = "row_vector2"))]
39  impls_stas!(StatsSumRowM3x2, Matrix3x2<T>, RowVector2<T>, sum_row_op);
40  #[cfg(all(feature = "matrixd", feature = "row_vectord"))]
41  impls_stas!(StatsSumRowMD, DMatrix<T>, RowDVector<T>, sum_row_op);
42  #[cfg(all(feature = "vector2", feature = "matrix1"))]
43  impls_stas!(StatsSumRowV2, Vector2<T>, Matrix1<T>, sum_row_op);
44  #[cfg(all(feature = "vector3", feature = "matrix1"))]
45  impls_stas!(StatsSumRowV3, Vector3<T>, Matrix1<T>, sum_row_op);
46  #[cfg(all(feature = "vector4", feature = "matrix1"))]
47  impls_stas!(StatsSumRowV4, Vector4<T>, Matrix1<T>, sum_row_op); 
48  #[cfg(all(feature = "vectord", feature = "matrix1"))]
49  impls_stas!(StatsSumRowVD, DVector<T>, Matrix1<T>, sum_row_op);
50  #[cfg(all(feature = "vectord", feature = "matrixd", not(feature = "matrix1")))]
51  impls_stas!(StatsSumRowVDMD, DVector<T>, DMatrix<T>, sum_row_op2);
52  #[cfg(all(feature = "row_vector2", feature = "row_vector2"))]
53  impls_stas!(StatsSumRowR2, RowVector2<T>, RowVector2<T>, sum_row_op);
54  #[cfg(all(feature = "row_vector3", feature = "row_vector3"))]
55  impls_stas!(StatsSumRowR3, RowVector3<T>, RowVector3<T>, sum_row_op);
56  #[cfg(all(feature = "row_vector4", feature = "row_vector4"))]
57  impls_stas!(StatsSumRowR4, RowVector4<T>, RowVector4<T>, sum_row_op); 
58  #[cfg(all(feature = "row_vectord", feature = "row_vectord"))]
59  impls_stas!(StatsSumRowRD, RowDVector<T>, RowDVector<T>, sum_row_op);
60  
61  macro_rules! impl_stats_sum_row_match_arms {
62  ($arg:expr, $($input_type:ident, $($target_type:ident, $value_string:tt),+);+ $(;)?) => {
63    paste!{
64      match $arg {
65        $(
66          $(
67            #[cfg(all(feature = $value_string, feature = "row_vector4", feature = "row_vector4"))]
68            Value::[<Matrix $input_type>](Matrix::<$target_type>::RowVector4(arg)) => Ok(Box::new(StatsSumRowR4{arg: arg.clone(), out: Ref::new(RowVector4::from_element($target_type::default())) })),
69            #[cfg(all(feature = $value_string, feature = "row_vector3", feature = "row_vector3"))]
70            Value::[<Matrix $input_type>](Matrix::<$target_type>::RowVector3(arg)) => Ok(Box::new(StatsSumRowR3{arg: arg.clone(), out: Ref::new(RowVector3::from_element($target_type::default())) })),
71            #[cfg(all(feature = $value_string, feature = "row_vector2", feature = "row_vector2"))]
72            Value::[<Matrix $input_type>](Matrix::<$target_type>::RowVector2(arg)) => Ok(Box::new(StatsSumRowR2{arg: arg.clone(), out: Ref::new(RowVector2::from_element($target_type::default())) })),
73            #[cfg(all(feature = $value_string, feature = "vector4", feature = "matrix1"))]
74            Value::[<Matrix $input_type>](Matrix::<$target_type>::Vector4(arg)) => Ok(Box::new(StatsSumRowV4{arg: arg.clone(), out: Ref::new(Matrix1::from_element($target_type::default())) })),
75            #[cfg(all(feature = $value_string, feature = "vector3", feature = "matrix1"))]
76            Value::[<Matrix $input_type>](Matrix::<$target_type>::Vector3(arg)) => Ok(Box::new(StatsSumRowV3{arg: arg.clone(), out: Ref::new(Matrix1::from_element($target_type::default())) })),
77            #[cfg(all(feature = $value_string, feature = "vector2", feature = "matrix1"))]
78            Value::[<Matrix $input_type>](Matrix::<$target_type>::Vector2(arg)) => Ok(Box::new(StatsSumRowV2{arg: arg.clone(), out: Ref::new(Matrix1::from_element($target_type::default())) })),
79            #[cfg(all(feature = $value_string, feature = "matrix4", feature = "row_vector4"))]
80            Value::[<Matrix $input_type>](Matrix::<$target_type>::Matrix4(arg)) => Ok(Box::new(StatsSumRowM4{arg: arg.clone(), out: Ref::new(RowVector4::from_element($target_type::default())) })),
81            #[cfg(all(feature = $value_string, feature = "matrix3", feature = "row_vector3"))]
82            Value::[<Matrix $input_type>](Matrix::<$target_type>::Matrix3(arg)) => Ok(Box::new(StatsSumRowM3{arg: arg.clone(), out: Ref::new(RowVector3::from_element($target_type::default())) })),
83            #[cfg(all(feature = $value_string, feature = "matrix2", feature = "row_vector2"))]
84            Value::[<Matrix $input_type>](Matrix::<$target_type>::Matrix2(arg)) => Ok(Box::new(StatsSumRowM2{arg: arg.clone(), out: Ref::new(RowVector2::from_element($target_type::default())) })),
85            #[cfg(all(feature = $value_string, feature = "matrix1", feature = "matrix1"))]
86            Value::[<Matrix $input_type>](Matrix::<$target_type>::Matrix1(arg)) => Ok(Box::new(StatsSumRowM1{arg: arg.clone(), out: Ref::new(Matrix1::from_element($target_type::default())) })),
87            #[cfg(all(feature = $value_string, feature = "matrix2x3", feature = "row_vector3"))]
88            Value::[<Matrix $input_type>](Matrix::<$target_type>::Matrix2x3(arg)) => Ok(Box::new(StatsSumRowM2x3{arg: arg.clone(), out: Ref::new(RowVector3::from_element($target_type::default())) })),
89            #[cfg(all(feature = $value_string, feature = "matrix3x2", feature = "row_vector2"))]
90            Value::[<Matrix $input_type>](Matrix::<$target_type>::Matrix3x2(arg)) => Ok(Box::new(StatsSumRowM3x2{arg: arg.clone(), out: Ref::new(RowVector2::from_element($target_type::default())) })),
91            #[cfg(all(feature = $value_string, feature = "vectord", feature = "matrix1"))]
92            Value::[<Matrix $input_type>](Matrix::<$target_type>::DVector(arg)) => Ok(Box::new(StatsSumRowVD{arg: arg.clone(), out: Ref::new(Matrix1::from_element($target_type::default())) })),
93            #[cfg(all(feature = $value_string, feature = "vectord", feature = "matrixd", not(feature = "matrix1")))]
94            Value::[<Matrix $input_type>](Matrix::<$target_type>::DVector(arg)) => Ok(Box::new(StatsSumRowVDMD{arg: arg.clone(), out: Ref::new(DMatrix::from_element(1,1,$target_type::default())) })),
95            #[cfg(all(feature = $value_string, feature = "row_vectord", feature = "row_vectord"))]
96            Value::[<Matrix $input_type>](Matrix::<$target_type>::RowDVector(arg)) => Ok(Box::new(StatsSumRowRD{arg: arg.clone(), out: Ref::new(RowDVector::from_element(arg.borrow().len(), $target_type::default())) })),
97            #[cfg(all(feature = $value_string, feature = "matrixd", feature = "row_vectord"))]
98            Value::[<Matrix $input_type>](Matrix::<$target_type>::DMatrix(arg)) => Ok(Box::new(StatsSumRowMD{arg: arg.clone(), out: Ref::new(RowDVector::from_element(arg.borrow().ncols(), $target_type::default())) })),
99          )+
100        )+
101        x => Err(MechError2::new(
102            UnhandledFunctionArgumentKind1 {arg: x.kind(), fxn_name: stringify!(StatsSumRow).to_string() },
103            None
104          ).with_compiler_loc()
105        ),
106      }
107    }
108  }
109}
110
111  
112  fn impl_stats_sum_row_fxn(lhs_value: Value) -> MResult<Box<dyn MechFunction>> {
113    impl_stats_sum_row_match_arms!(
114      lhs_value,
115      I8,   i8,   "i8";
116      I16,  i16,  "i16";
117      I32,  i32,  "i32";
118      I64,  i64,  "i64";
119      I128, i128, "i128";
120      U8,   u8,   "u8";
121      U16,  u16,  "u16";
122      U32,  u32,  "u32";
123      U64,  u64,  "u64";
124      U128, u128, "u128";
125      F32,  f32,  "f32";
126      F64,  f64,  "f64";
127      C64, C64, "complex";
128      R64, R64, "rational";
129    )
130  }
131    
132  impl_mech_urnop_fxn!(StatsSumRow,impl_stats_sum_row_fxn,"stats/sum/row");