1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Hyperbolic and inverse hyperbolic functions: `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`
#[cfg(target_arch = "x86_64")]
use crate::backends::avx2::Avx2Backend;
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
use crate::backends::neon::NeonBackend;
use crate::backends::scalar::ScalarBackend;
#[cfg(target_arch = "x86_64")]
use crate::backends::sse2::Sse2Backend;
#[cfg(target_arch = "wasm32")]
use crate::backends::wasm::WasmBackend;
use crate::backends::VectorBackend;
use crate::vector::Vector;
use crate::{Backend, Result, TruenoError};
impl Vector<f32> {
/// Computes the hyperbolic sine (sinh) of each element.
///
/// # Mathematical Definition
///
/// sinh(x) = (e^x - e^(-x)) / 2
///
/// # Properties
///
/// - Domain: (-∞, +∞)
/// - Range: (-∞, +∞)
/// - Odd function: sinh(-x) = -sinh(x)
/// - sinh(0) = 0
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[0.0, 1.0, -1.0]);
/// let result = v.sinh()?;
/// assert!((result.as_slice()[0] - 0.0).abs() < 1e-5);
/// # Ok(())
/// # }
/// ```
pub fn sinh(&self) -> Result<Vector<f32>> {
let sinh_data: Vec<f32> = self.data.iter().map(|x| x.sinh()).collect();
Ok(Vector { data: sinh_data, backend: self.backend })
}
/// Computes the hyperbolic cosine (cosh) of each element.
///
/// # Mathematical Definition
///
/// cosh(x) = (e^x + e^(-x)) / 2
///
/// # Properties
///
/// - Domain: (-∞, +∞)
/// - Range: [1, +∞)
/// - Even function: cosh(-x) = cosh(x)
/// - cosh(0) = 1
/// - Always positive: cosh(x) ≥ 1 for all x
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[0.0, 1.0, -1.0]);
/// let result = v.cosh()?;
/// assert!((result.as_slice()[0] - 1.0).abs() < 1e-5);
/// # Ok(())
/// # }
/// ```
pub fn cosh(&self) -> Result<Vector<f32>> {
let cosh_data: Vec<f32> = self.data.iter().map(|x| x.cosh()).collect();
Ok(Vector { data: cosh_data, backend: self.backend })
}
/// Computes the hyperbolic tangent (tanh) of each element.
///
/// # Mathematical Definition
///
/// tanh(x) = sinh(x) / cosh(x) = (e^x - e^(-x)) / (e^x + e^(-x))
///
/// # Properties
///
/// - Domain: (-∞, +∞)
/// - Range: (-1, 1)
/// - Odd function: tanh(-x) = -tanh(x)
/// - tanh(0) = 0
/// - Bounded: -1 < tanh(x) < 1 for all x
/// - Commonly used as activation function in neural networks
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[0.0, 1.0, -1.0]);
/// let result = v.tanh()?;
/// assert!((result.as_slice()[0] - 0.0).abs() < 1e-5);
/// // All values are in range (-1, 1)
/// assert!(result.as_slice().iter().all(|&x| x > -1.0 && x < 1.0));
/// # Ok(())
/// # }
/// ```
pub fn tanh(&self) -> Result<Vector<f32>> {
if self.data.is_empty() {
return Err(TruenoError::EmptyVector);
}
// OpComplexity::Low - GPU threshold: >100K elements
#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
const GPU_THRESHOLD: usize = usize::MAX; // GPU DISABLED - 2-800x slower, see docs/performance-analysis.md
// Try GPU first for large vectors
#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
{
if self.data.len() >= GPU_THRESHOLD {
use crate::backends::gpu::GpuDevice;
if GpuDevice::is_available() {
let gpu = GpuDevice::new().map_err(TruenoError::InvalidInput)?;
let mut result = vec![0.0; self.data.len()];
if gpu.tanh(&self.data, &mut result).is_ok() {
return Ok(Vector::from_vec(result));
}
}
}
}
let mut result = vec![0.0; self.len()];
// Dispatch to appropriate SIMD backend
// SAFETY: Unsafe block delegates to backend implementation which maintains safety invariants
unsafe {
match self.backend {
Backend::Scalar => {
ScalarBackend::tanh(&self.data, &mut result);
}
#[cfg(target_arch = "x86_64")]
Backend::SSE2 | Backend::AVX => {
Sse2Backend::tanh(&self.data, &mut result);
}
#[cfg(target_arch = "x86_64")]
Backend::AVX2 | Backend::AVX512 => {
Avx2Backend::tanh(&self.data, &mut result);
}
#[cfg(not(target_arch = "x86_64"))]
Backend::SSE2 | Backend::AVX | Backend::AVX2 | Backend::AVX512 => {
ScalarBackend::tanh(&self.data, &mut result);
}
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
Backend::NEON => {
NeonBackend::tanh(&self.data, &mut result);
}
#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))]
Backend::NEON => {
ScalarBackend::tanh(&self.data, &mut result);
}
#[cfg(target_arch = "wasm32")]
Backend::WasmSIMD => {
WasmBackend::tanh(&self.data, &mut result);
}
#[cfg(not(target_arch = "wasm32"))]
Backend::WasmSIMD => {
ScalarBackend::tanh(&self.data, &mut result);
}
Backend::GPU | Backend::Auto => {
// Auto should have been resolved at Vector creation
// GPU falls back to best available SIMD
#[cfg(target_arch = "x86_64")]
{
if is_x86_feature_detected!("avx2") {
Avx2Backend::tanh(&self.data, &mut result);
} else {
Sse2Backend::tanh(&self.data, &mut result);
}
}
#[cfg(not(target_arch = "x86_64"))]
{
ScalarBackend::tanh(&self.data, &mut result);
}
}
}
}
Ok(Vector { data: result, backend: self.backend })
}
/// Computes the inverse hyperbolic sine (asinh) of each element.
///
/// # Mathematical Definition
///
/// asinh(x) = ln(x + sqrt(x² + 1))
///
/// # Properties
///
/// - Domain: (-∞, +∞)
/// - Range: (-∞, +∞)
/// - Odd function: asinh(-x) = -asinh(x)
/// - asinh(0) = 0
/// - Inverse of sinh: asinh(sinh(x)) = x
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[0.0, 1.0, -1.0]);
/// let result = v.asinh()?;
/// assert!((result.as_slice()[0] - 0.0).abs() < 1e-5);
/// # Ok(())
/// # }
/// ```
pub fn asinh(&self) -> Result<Vector<f32>> {
let asinh_data: Vec<f32> = self.data.iter().map(|x| x.asinh()).collect();
Ok(Vector { data: asinh_data, backend: self.backend })
}
/// Computes the inverse hyperbolic cosine (acosh) of each element.
///
/// # Mathematical Definition
///
/// acosh(x) = ln(x + sqrt(x² - 1))
///
/// # Properties
///
/// - Domain: [1, +∞)
/// - Range: [0, +∞)
/// - acosh(1) = 0
/// - Inverse of cosh: acosh(cosh(x)) = x for x >= 0
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[1.0, 2.0, 3.0]);
/// let result = v.acosh()?;
/// assert!((result.as_slice()[0] - 0.0).abs() < 1e-5);
/// # Ok(())
/// # }
/// ```
pub fn acosh(&self) -> Result<Vector<f32>> {
let acosh_data: Vec<f32> = self.data.iter().map(|x| x.acosh()).collect();
Ok(Vector { data: acosh_data, backend: self.backend })
}
/// Computes the inverse hyperbolic tangent (atanh) of each element.
///
/// Domain: (-1, 1)
/// Range: (-∞, +∞)
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[0.0, 0.5, -0.5]);
/// let result = v.atanh()?;
/// // atanh(0) = 0, atanh(0.5) ≈ 0.549, atanh(-0.5) ≈ -0.549
/// # Ok(())
/// # }
/// ```
pub fn atanh(&self) -> Result<Vector<f32>> {
let atanh_data: Vec<f32> = self.data.iter().map(|x| x.atanh()).collect();
Ok(Vector { data: atanh_data, backend: self.backend })
}
}