arr_rs/math/operations/hyperbolic.rs
1use crate::{
2 core::prelude::*,
3 errors::prelude::*,
4 numeric::prelude::*,
5};
6
7/// `ArrayTrait` - Array Hyperbolic functions
8pub trait ArrayHyperbolic<N: Numeric> where Self: Sized + Clone {
9
10 /// Compute the hyperbolic sine of array elements
11 ///
12 /// # Examples
13 ///
14 /// ```
15 /// use arr_rs::prelude::*;
16 ///
17 /// let arr = Array::flat(vec![-1., 0., 1.]).unwrap();
18 /// assert_eq!(Array::flat(vec![-1.1752011936438014, 0., 1.1752011936438014]), arr.sinh());
19 /// ```
20 ///
21 /// # Errors
22 ///
23 /// may returns `ArrayError`
24 fn sinh(&self) -> Result<Array<N>, ArrayError>;
25
26 /// Compute the hyperbolic cosine of array elements
27 ///
28 /// # Examples
29 ///
30 /// ```
31 /// use arr_rs::prelude::*;
32 ///
33 /// let arr = Array::flat(vec![-1., 0., 1.]).unwrap();
34 /// assert_eq!(Array::flat(vec![1.5430806348152437, 1., 1.5430806348152437]), arr.cosh());
35 /// ```
36 ///
37 /// # Errors
38 ///
39 /// may returns `ArrayError`
40 fn cosh(&self) -> Result<Array<N>, ArrayError>;
41
42 /// Compute the hyperbolic tangent of array elements
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// use arr_rs::prelude::*;
48 ///
49 /// let arr = Array::flat(vec![-1., 0., 1.]).unwrap();
50 /// assert_eq!(Array::flat(vec![-0.7615941559557649, 0., 0.7615941559557649]), arr.tanh());
51 /// ```
52 ///
53 /// # Errors
54 ///
55 /// may returns `ArrayError`
56 fn tanh(&self) -> Result<Array<N>, ArrayError>;
57
58 /// Compute the inverse hyperbolic sine of array elements
59 ///
60 /// # Examples
61 ///
62 /// ```
63 /// use arr_rs::prelude::*;
64 ///
65 /// let arr = Array::flat(vec![-1., 0., 1.]).unwrap();
66 /// assert_eq!(Array::flat(vec![-0.881373587019543, 0., 0.881373587019543]), arr.asinh());
67 /// ```
68 ///
69 /// # Errors
70 ///
71 /// may returns `ArrayError`
72 fn asinh(&self) -> Result<Array<N>, ArrayError>;
73
74 /// Compute the inverse hyperbolic cosine of array elements
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// use arr_rs::prelude::*;
80 ///
81 /// let arr = Array::flat(vec![1., 2., 3.]).unwrap();
82 /// assert_eq!(Array::flat(vec![0., 1.3169578969248166, 1.762747174039086]), arr.acosh());
83 /// ```
84 ///
85 /// # Errors
86 ///
87 /// may returns `ArrayError`
88 fn acosh(&self) -> Result<Array<N>, ArrayError>;
89
90 /// Compute the inverse hyperbolic tangent of array elements
91 ///
92 /// # Examples
93 ///
94 /// ```
95 /// use arr_rs::prelude::*;
96 ///
97 /// let arr = Array::flat(vec![-1., 0., 1.]).unwrap();
98 /// assert_eq!(Array::flat(vec![-f64::INFINITY, 0., f64::INFINITY]), arr.atanh());
99 /// ```
100 ///
101 /// # Errors
102 ///
103 /// may returns `ArrayError`
104 fn atanh(&self) -> Result<Array<N>, ArrayError>;
105}
106
107impl <N: Numeric> ArrayHyperbolic<N> for Array<N> {
108
109 fn sinh(&self) -> Result<Self, ArrayError> {
110 self.map(|i| N::from(i.to_f64().sinh()))
111 }
112
113 fn cosh(&self) -> Result<Self, ArrayError> {
114 self.map(|i| N::from(i.to_f64().cosh()))
115 }
116
117 fn tanh(&self) -> Result<Self, ArrayError> {
118 self.map(|i| N::from(i.to_f64().tanh()))
119 }
120
121 fn asinh(&self) -> Result<Self, ArrayError> {
122 self.map(|i| N::from(i.to_f64().asinh()))
123 }
124
125 fn acosh(&self) -> Result<Self, ArrayError> {
126 self.map(|i| N::from(i.to_f64().acosh()))
127 }
128
129 fn atanh(&self) -> Result<Self, ArrayError> {
130 self.map(|i| N::from(i.to_f64().atanh()))
131 }
132}
133
134impl <N: Numeric> ArrayHyperbolic<N> for Result<Array<N>, ArrayError> {
135
136 fn sinh(&self) -> Self {
137 self.clone()?.sinh()
138 }
139
140 fn cosh(&self) -> Self {
141 self.clone()?.cosh()
142 }
143
144 fn tanh(&self) -> Self {
145 self.clone()?.tanh()
146 }
147
148 fn asinh(&self) -> Self {
149 self.clone()?.asinh()
150 }
151
152 fn acosh(&self) -> Self {
153 self.clone()?.acosh()
154 }
155
156 fn atanh(&self) -> Self {
157 self.clone()?.atanh()
158 }
159}