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
//! Rounding and sign functions for Vector<f32>
//!
//! This module provides rounding, truncation, and sign-related operations:
//! - Rounding: `floor`, `ceil`, `round`, `trunc`
//! - Parts: `fract` (fractional part)
//! - Sign: `signum`, `copysign`, `neg`
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
use crate::backends::neon::NeonBackend;
#[cfg(target_arch = "wasm32")]
use crate::backends::wasm::WasmBackend;
use crate::backends::VectorBackend;
use crate::vector::Vector;
use crate::{dispatch_unary_op, Result, TruenoError};
impl Vector<f32> {
/// Computes the floor (round down to nearest integer) of each element.
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[3.7, -2.3, 5.0]);
/// let result = v.floor()?;
/// assert_eq!(result.as_slice(), &[3.0, -3.0, 5.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn floor(&self) -> Result<Vector<f32>> {
let mut result_data = vec![0.0; self.len()];
if !self.data.is_empty() {
dispatch_unary_op!(self.backend, floor, &self.data, &mut result_data);
}
Ok(Vector { data: result_data, backend: self.backend })
}
/// Computes the ceiling (round up to nearest integer) of each element.
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[3.2, -2.7, 5.0]);
/// let result = v.ceil()?;
/// assert_eq!(result.as_slice(), &[4.0, -2.0, 5.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn ceil(&self) -> Result<Vector<f32>> {
let mut result_data = vec![0.0; self.len()];
if !self.data.is_empty() {
dispatch_unary_op!(self.backend, ceil, &self.data, &mut result_data);
}
Ok(Vector { data: result_data, backend: self.backend })
}
/// Rounds each element to the nearest integer.
///
/// Uses "round half away from zero" strategy:
/// - 0.5 rounds to 1.0, 1.5 rounds to 2.0, -1.5 rounds to -2.0, etc.
/// - Positive halfway cases round up, negative halfway cases round down.
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[3.2, 3.7, -2.3, -2.8]);
/// let result = v.round()?;
/// assert_eq!(result.as_slice(), &[3.0, 4.0, -2.0, -3.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn round(&self) -> Result<Vector<f32>> {
let mut result_data = vec![0.0; self.len()];
if !self.data.is_empty() {
dispatch_unary_op!(self.backend, round, &self.data, &mut result_data);
}
Ok(Vector { data: result_data, backend: self.backend })
}
/// Truncates each element toward zero (removes fractional part).
///
/// Truncation always moves toward zero:
/// - Positive values: equivalent to floor() (e.g., 3.7 → 3.0)
/// - Negative values: equivalent to ceil() (e.g., -3.7 → -3.0)
/// - This differs from floor() which always rounds down
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[3.7, -2.7, 5.0]);
/// let result = v.trunc()?;
/// assert_eq!(result.as_slice(), &[3.0, -2.0, 5.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn trunc(&self) -> Result<Vector<f32>> {
let trunc_data: Vec<f32> = self.data.iter().map(|x| x.trunc()).collect();
Ok(Vector { data: trunc_data, backend: self.backend })
}
/// Returns the fractional part of each element.
///
/// The fractional part has the same sign as the original value:
/// - Positive: fract(3.7) = 0.7
/// - Negative: fract(-3.7) = -0.7
/// - Decomposition property: x = trunc(x) + fract(x)
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[3.7, -2.3, 5.0]);
/// let result = v.fract()?;
/// // Fractional parts: 0.7, -0.3, 0.0
/// assert!((result.as_slice()[0] - 0.7).abs() < 1e-5);
/// assert!((result.as_slice()[1] - (-0.3)).abs() < 1e-5);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn fract(&self) -> Result<Vector<f32>> {
let fract_data: Vec<f32> = self.data.iter().map(|x| x.fract()).collect();
Ok(Vector { data: fract_data, backend: self.backend })
}
/// Returns the sign of each element.
///
/// Returns:
/// - `1.0` if the value is positive (including +0.0 and +∞)
/// - `-1.0` if the value is negative (including -0.0 and -∞)
/// - `NaN` if the value is NaN
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let v = Vector::from_slice(&[5.0, -3.0, 0.0, -0.0]);
/// let result = v.signum()?;
/// assert_eq!(result.as_slice(), &[1.0, -1.0, 1.0, -1.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn signum(&self) -> Result<Vector<f32>> {
let signum_data: Vec<f32> = self.data.iter().map(|x| x.signum()).collect();
Ok(Vector { data: signum_data, backend: self.backend })
}
/// Returns a vector with the magnitude of `self` and the sign of `sign`.
///
/// For each element pair, takes the magnitude from `self` and the sign from `sign`.
/// Equivalent to `abs(self\[i\])` with the sign of `sign\[i\]`.
///
/// # Arguments
///
/// * `sign` - Vector providing the sign for each element
///
/// # Errors
///
/// Returns `TruenoError::SizeMismatch` if vectors have different lengths.
///
/// # Examples
///
/// ```
/// use trueno::Vector;
///
/// let magnitude = Vector::from_slice(&[5.0, 3.0, 2.0]);
/// let sign = Vector::from_slice(&[-1.0, 1.0, -1.0]);
/// let result = magnitude.copysign(&sign)?;
/// assert_eq!(result.as_slice(), &[-5.0, 3.0, -2.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn copysign(&self, sign: &Self) -> Result<Vector<f32>> {
if self.len() != sign.len() {
return Err(TruenoError::SizeMismatch { expected: self.len(), actual: sign.len() });
}
let copysign_data: Vec<f32> =
self.data.iter().zip(sign.data.iter()).map(|(mag, sgn)| mag.copysign(*sgn)).collect();
Ok(Vector { data: copysign_data, backend: self.backend })
}
/// Element-wise minimum of two vectors.
///
/// Returns a new vector where each element is the minimum of the corresponding
/// elements from self and other.
///
/// NaN handling: Prefers non-NaN values (NAN.min(x) = x).
///
/// # Examples
/// ```
/// use trueno::Vector;
/// let a = Vector::from_slice(&[1.0, 5.0, 3.0]);
/// let b = Vector::from_slice(&[2.0, 3.0, 4.0]);
/// let result = a.minimum(&b)?;
/// assert_eq!(result.as_slice(), &[1.0, 3.0, 3.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn minimum(&self, other: &Self) -> Result<Vector<f32>> {
if self.len() != other.len() {
return Err(TruenoError::SizeMismatch { expected: self.len(), actual: other.len() });
}
let minimum_data: Vec<f32> =
self.data.iter().zip(other.data.iter()).map(|(a, b)| a.min(*b)).collect();
Ok(Vector { data: minimum_data, backend: self.backend })
}
/// Element-wise maximum of two vectors.
///
/// Returns a new vector where each element is the maximum of the corresponding
/// elements from self and other.
///
/// NaN handling: Prefers non-NaN values (NAN.max(x) = x).
///
/// # Examples
/// ```
/// use trueno::Vector;
/// let a = Vector::from_slice(&[1.0, 5.0, 3.0]);
/// let b = Vector::from_slice(&[2.0, 3.0, 4.0]);
/// let result = a.maximum(&b)?;
/// assert_eq!(result.as_slice(), &[2.0, 5.0, 4.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn maximum(&self, other: &Self) -> Result<Vector<f32>> {
if self.len() != other.len() {
return Err(TruenoError::SizeMismatch { expected: self.len(), actual: other.len() });
}
let maximum_data: Vec<f32> =
self.data.iter().zip(other.data.iter()).map(|(a, b)| a.max(*b)).collect();
Ok(Vector { data: maximum_data, backend: self.backend })
}
/// Element-wise negation (unary minus).
///
/// Returns a new vector where each element is the negation of the corresponding
/// element from self.
///
/// Properties: Double negation is identity: -(-x) = x
///
/// # Examples
/// ```
/// use trueno::Vector;
/// let a = Vector::from_slice(&[1.0, -2.0, 3.0]);
/// let result = a.neg()?;
/// assert_eq!(result.as_slice(), &[-1.0, 2.0, -3.0]);
/// # Ok::<(), trueno::TruenoError>(())
/// ```
pub fn neg(&self) -> Result<Vector<f32>> {
let neg_data: Vec<f32> = self.data.iter().map(|x| -x).collect();
Ok(Vector { data: neg_data, backend: self.backend })
}
}