mech_math/
cos.rs

1use crate::*;
2use mech_core::*;
3
4// Cos ------------------------------------------------------------------------
5
6use libm::{cos,cosf};
7macro_rules! cos_op {
8  ($arg:expr, $out:expr) => {
9    unsafe{(*$out).0 = cos((*$arg).0);}
10  };}
11
12macro_rules! cos_vec_op {
13  ($arg:expr, $out:expr) => {
14    unsafe {
15      for i in 0..(*$arg).len() {
16        ((*$out)[i]).0 = cos(((*$arg)[i]).0);
17      }}};}
18
19macro_rules! cosf_op {
20  ($arg:expr, $out:expr) => {
21    unsafe{(*$out).0 = cosf((*$arg).0);}
22  };}  
23
24macro_rules! cosf_vec_op {
25  ($arg:expr, $out:expr) => {
26    unsafe {
27      for i in 0..(*$arg).len() {
28        ((*$out)[i]).0 = cosf(((*$arg)[i]).0);
29      }}};}
30
31impl_math_urop!(MathCos, F32, cosf);
32impl_math_urop!(MathCos, F64, cos);
33
34
35fn impl_cos_fxn(lhs_value: Value) -> Result<Box<dyn MechFunction>, MechError> {
36  impl_urnop_match_arms2!(
37    MathCos,
38    (lhs_value),
39    F32 => MatrixF32, F32, F32::zero(), "F32";
40    F64 => MatrixF64, F64, F64::zero(), "F64";
41  )
42}
43
44pub struct MathCos {}
45
46impl NativeFunctionCompiler for MathCos {
47  fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
48    if arguments.len() != 1 {
49      return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments});
50    }
51    let input = arguments[0].clone();
52    match impl_cos_fxn(input.clone()) {
53      Ok(fxn) => Ok(fxn),
54      Err(_) => {
55        match (input) {
56          (Value::MutableReference(input)) => {impl_cos_fxn(input.borrow().clone())}
57          x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
58        }
59      }
60    }
61  }
62}