pub fn abs_vec(x: &[f64]) -> Vec<f64>Expand description
§abs_vec(x)
Native Function
The abs_vec function takes a single parameter, a slice of floating-point numbers (&[f64]),
and returns a vector (Vec<f64>) containing the absolute values of each element in the input slice.
The function iterates over each element in the slice,
computes its absolute value, and collects the results into a new vector.
The abs_vec function can be written in two ways:
use mathlab::math::abs;
let my_x_f64_array = [0.0, -1.0, 3.33, -3.33];
// with a reference (&)
pub fn abs_vec(x: &[f64]) -> Vec<f64> {
x.iter().map(|&x| x.abs()).collect()
}
assert_eq!(abs_vec(&my_x_f64_array), [0.0, 1.0, 3.33, 3.33]);or directly without a reference &
use mathlab::math::abs;
let my_x_f64_array = [0.0, -1.0, 3.33, -3.33];
pub fn abs_vec<const N: usize>(x: [f64; N]) -> Vec<f64> {
x.iter().map(|&x| x.abs()).collect()
}
assert_eq!(abs_vec(my_x_f64_array), vec![0.0, 1.0, 3.33, 3.33]);For most cases, using a slice (&[f64]) is more efficient and flexible,
especially for larger arrays or when the function needs to handle variable-length input.
It avoids the overhead of copying the entire array and allows the function
to work with any contiguous memory block.
§Examples
use mathlab::math::{abs, abs_vec};
let my_x_f64_array = [0.0, -1.0, 3.33, -3.33];
assert_eq!(abs(-1.0), 1.0);
assert_eq!(abs_vec(&my_x_f64_array), [0.0, 1.0, 3.33, 3.33]);End Fun Doc