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