mech_stats/
lib.rs

1#![no_main]
2#![allow(warnings)]
3#[macro_use]
4extern crate mech_core;
5extern crate paste;
6
7use mech_core::*;
8
9#[cfg(feature = "vector3")]
10use nalgebra::Vector3;
11#[cfg(feature = "vectord")]
12use nalgebra::DVector;
13#[cfg(feature = "vector2")]
14use nalgebra::Vector2;
15#[cfg(feature = "vector4")]
16use nalgebra::Vector4;
17#[cfg(feature = "rowdvector")]
18use nalgebra::RowDVector;
19#[cfg(feature = "row_vectord")]
20use nalgebra::RowDVector;
21#[cfg(feature = "matrix1")]
22use nalgebra::Matrix1;
23#[cfg(feature = "matrix3")]
24use nalgebra::Matrix3;
25#[cfg(feature = "matrix4")]
26use nalgebra::Matrix4;
27#[cfg(feature = "row_vector3")]
28use nalgebra::RowVector3;
29#[cfg(feature = "row_vector4")]
30use nalgebra::RowVector4;
31#[cfg(feature = "row_vector2")]
32use nalgebra::RowVector2;
33#[cfg(feature = "matrixd")]
34use nalgebra::DMatrix;
35#[cfg(feature = "matrix2x3")]
36use nalgebra::Matrix2x3;
37#[cfg(feature = "matrix3x2")]
38use nalgebra::Matrix3x2;
39#[cfg(feature = "matrix2")]
40use nalgebra::Matrix2;
41
42use paste::paste;
43use std::ops::*;
44use std::fmt::Debug;
45
46#[cfg(feature = "sum")]
47pub mod sum_column;
48#[cfg(feature = "sum")]
49pub mod sum_row;
50
51#[cfg(feature = "sum")]
52pub use self::sum_column::*;
53#[cfg(feature = "sum")]
54pub use self::sum_row::*;
55
56#[macro_export]  
57macro_rules! impl_stats_unop {
58  ($struct_name:ident, $arg_type:ty, $out_type:ty, $op:ident) => {
59    #[derive(Debug)]
60    struct $struct_name<T> {
61      arg: Ref<$arg_type>,
62      out: Ref<$out_type>,
63    }
64    impl<T> MechFunctionFactory for $struct_name<T>
65    where
66      T: Copy + Debug + Clone + Sync + Send + 'static + 
67      Add<Output = T> + AddAssign +
68      CompileConst + ConstElem + AsValueKind +
69      Zero + One +
70      PartialEq + PartialOrd,
71      Ref<$out_type>: ToValue
72    {
73      fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
74        match args {
75          FunctionArgs::Unary(out, arg) => {
76            let arg = unsafe{ arg.as_unchecked().clone() };
77            let out = unsafe{ out.as_unchecked().clone() };
78            Ok(Box::new($struct_name { arg, out }))
79          }
80          _ => Err(MechError2::new(
81              IncorrectNumberOfArguments { expected: 2, found: args.len() },
82              None
83            ).with_compiler_loc()
84          ),
85        }
86      }
87    }
88    impl<T> MechFunctionImpl for $struct_name<T>
89    where
90      T: Copy + Debug + Clone + Sync + Send + 'static + 
91      Add<Output = T> + AddAssign +
92      Zero + One +
93      PartialEq + PartialOrd,
94      Ref<$out_type>: ToValue
95    {
96      fn solve(&self) {
97        let arg_ptr = self.arg.as_ptr();
98        let out_ptr = self.out.as_mut_ptr();
99        $op!(arg_ptr,out_ptr);
100      }
101      fn out(&self) -> Value { self.out.to_value() }
102      fn to_string(&self) -> String { format!("{:#?}", self) }
103    }
104    #[cfg(feature = "compiler")]
105    impl<T> MechFunctionCompiler for $struct_name<T> 
106    where
107      T: CompileConst + ConstElem + AsValueKind,
108    {
109      fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
110        let name = format!("{}<{}>", stringify!($struct_name), T::as_value_kind());
111        compile_unop!(name, self.out, self.arg, ctx, FeatureFlag::Custom(hash_str("stats/sum")) );
112      }
113    }};}
114
115#[macro_export]    
116macro_rules! impls_stas {
117  ($name:ident, $arg_type:ty, $out_type:ty, $op:ident) => {
118    impl_stats_unop!($name, $arg_type, $out_type, $op);
119    register_fxn_descriptor!($name, u8, "u8", u16, "u16", u32, "u32", u64, "u64", u128, "u128", i8, "i8", i16, "i16", i32, "i32", i64, "i64", i128, "i128", f32, "f32", f64, "f64", C64, "complex", R64, "rational");
120  };
121}