mech_math/trig/
cot.rs

1use crate::*;
2use mech_core::*;
3use num_traits::*;
4#[cfg(feature = "matrix")]
5use mech_core::matrix::Matrix;
6
7// Cot ------------------------------------------------------------------------
8
9use libm::{tan, tanf};
10macro_rules! cot_op {
11  ($arg:expr, $out:expr) => {
12    unsafe{(*$out) = 1.0 / tan((*$arg));}
13  };}
14
15macro_rules! cot_vec_op {
16  ($arg:expr, $out:expr) => {
17    unsafe {
18      for i in 0..(*$arg).len() {
19        ((&mut (*$out))[i]) = 1.0 / tan(((&(*$arg))[i]));
20      }}};}
21
22macro_rules! cotf_op {
23  ($arg:expr, $out:expr) => {
24    unsafe{(*$out) = 1.0 / tanf((*$arg));}
25  };}  
26
27macro_rules! cotf_vec_op {
28  ($arg:expr, $out:expr) => {
29    unsafe {
30      for i in 0..(*$arg).len() {
31        ((&mut (*$out))[i]) = 1.0 / tanf(((&(*$arg))[i]));
32      }}};}
33
34#[cfg(feature = "f32")]
35impl_math_unop!(MathCot, f32, cotf, FeatureFlag::Custom(hash_str("math/cot")));
36#[cfg(feature = "f64")]
37impl_math_unop!(MathCot, f64, cot, FeatureFlag::Custom(hash_str("math/cot")));
38
39fn impl_cot_fxn(lhs_value: Value) -> MResult<Box<dyn MechFunction>> {
40  impl_urnop_match_arms2!(
41    MathCot,
42    (lhs_value),
43    F32 => MatrixF32, F32, f32::zero(), "f32";
44    F64 => MatrixF64, F64, f64::zero(), "f64";
45  )
46}
47
48pub struct MathCot {}
49
50impl NativeFunctionCompiler for MathCot {
51  fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
52    if arguments.len() != 1 {
53      return Err(MechError2::new(IncorrectNumberOfArguments { expected: 1, found: arguments.len() }, None).with_compiler_loc());
54    }
55    let input = arguments[0].clone();
56    match impl_cot_fxn(input.clone()) {
57      Ok(fxn) => Ok(fxn),
58      Err(_) => {
59        match (input) {
60          (Value::MutableReference(input)) => {impl_cot_fxn(input.borrow().clone())}
61          x => Err(MechError2::new(
62              UnhandledFunctionArgumentKind1 { arg: x.kind(), fxn_name: "math/cot".to_string() },
63              None
64            ).with_compiler_loc()
65          ),
66        }
67      }
68    }
69  }
70}
71
72register_descriptor! {
73  FunctionCompilerDescriptor {
74    name: "math/cot",
75    ptr: &MathCot{},
76  }
77}