mathhook_core/functions/elementary/
hyperbolic_eval.rs

1//! Hyperbolic function evaluation
2
3use crate::core::{Expression, Number};
4
5/// Evaluate hyperbolic sine sinh(x)
6///
7/// # Mathematical Definition
8///
9/// sinh(x) = (e^x - e^(-x)) / 2
10///
11/// # Arguments
12///
13/// * `arg` - Expression to compute sinh of
14///
15/// # Returns
16///
17/// Hyperbolic sine expression
18///
19/// # Examples
20///
21/// ```
22/// use mathhook_core::functions::elementary::hyperbolic_eval::sinh;
23/// use mathhook_core::expr;
24///
25/// let result = sinh(&expr!(0));
26/// assert_eq!(result, expr!(0));
27/// ```
28pub fn sinh(arg: &Expression) -> Expression {
29    match arg {
30        Expression::Number(Number::Integer(0)) => Expression::integer(0),
31        Expression::Number(Number::Float(f)) => Expression::float(f.sinh()),
32        Expression::Number(Number::Integer(i)) => Expression::float((*i as f64).sinh()),
33        _ => Expression::function("sinh", vec![arg.clone()]),
34    }
35}
36
37/// Evaluate hyperbolic cosine cosh(x)
38///
39/// # Mathematical Definition
40///
41/// cosh(x) = (e^x + e^(-x)) / 2
42///
43/// # Arguments
44///
45/// * `arg` - Expression to compute cosh of
46///
47/// # Returns
48///
49/// Hyperbolic cosine expression
50///
51/// # Examples
52///
53/// ```
54/// use mathhook_core::functions::elementary::hyperbolic_eval::cosh;
55/// use mathhook_core::expr;
56///
57/// let result = cosh(&expr!(0));
58/// assert_eq!(result, expr!(1));
59/// ```
60pub fn cosh(arg: &Expression) -> Expression {
61    match arg {
62        Expression::Number(Number::Integer(0)) => Expression::integer(1),
63        Expression::Number(Number::Float(f)) => Expression::float(f.cosh()),
64        Expression::Number(Number::Integer(i)) => Expression::float((*i as f64).cosh()),
65        _ => Expression::function("cosh", vec![arg.clone()]),
66    }
67}
68
69/// Evaluate hyperbolic tangent tanh(x)
70///
71/// # Mathematical Definition
72///
73/// tanh(x) = sinh(x) / cosh(x) = (e^x - e^(-x)) / (e^x + e^(-x))
74///
75/// # Arguments
76///
77/// * `arg` - Expression to compute tanh of
78///
79/// # Returns
80///
81/// Hyperbolic tangent expression
82///
83/// # Examples
84///
85/// ```
86/// use mathhook_core::functions::elementary::hyperbolic_eval::tanh;
87/// use mathhook_core::expr;
88///
89/// let result = tanh(&expr!(0));
90/// assert_eq!(result, expr!(0));
91/// ```
92pub fn tanh(arg: &Expression) -> Expression {
93    match arg {
94        Expression::Number(Number::Integer(0)) => Expression::integer(0),
95        Expression::Number(Number::Float(f)) => Expression::float(f.tanh()),
96        Expression::Number(Number::Integer(i)) => Expression::float((*i as f64).tanh()),
97        _ => Expression::function("tanh", vec![arg.clone()]),
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_sinh_zero() {
107        assert_eq!(sinh(&Expression::integer(0)), Expression::integer(0));
108    }
109
110    #[test]
111    fn test_cosh_zero() {
112        assert_eq!(cosh(&Expression::integer(0)), Expression::integer(1));
113    }
114
115    #[test]
116    fn test_tanh_zero() {
117        assert_eq!(tanh(&Expression::integer(0)), Expression::integer(0));
118    }
119}