mathlab/functions/vec_vec/mod.rs
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
use super::num::{add, divi, mult, nrt, perimeter, pow, rem, subt};
/// ### add_vec_vec(x, y)
///
/// Operation Function
///
/// The `add_vec_vec` function adds corresponding elements of vectors `x` and `y` using the add function,
/// then returns a new vector containing their sums.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{add, add_vec_vec, fround_vec};
/// assert_eq!(add(0.1, 0.2), 0.30000000000000004);
/// assert_eq!(add(0.1, 0.2) as f32, 0.3);
/// assert_eq!(add_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0]), [0.3, 0.30000000000000004, 0.30000000000000004, 0.3]);
/// assert_eq!(fround_vec(&add_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0])), [0.3, 0.3, 0.3, 0.3]);
/// ```
/// <small>End Fun Doc</small>
pub fn add_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter().zip(y.iter()).map(|(&x, &y)| add(x, y)).collect()
}
/// ### subt_vec_vec(x, y)
///
/// Operation Function
///
/// The `subt_vec_vec` function subtracts corresponding elements of vectors `x` and `y` using the subt function,
/// then returns a new vector containing their differences.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{subt, subt_vec_vec};
/// assert_eq!(subt(0.1, 0.2), -0.1);
/// assert_eq!(subt_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0]), [-0.3, -0.1, 0.1, 0.3]);
/// ```
/// <small>End Fun Doc</small>
pub fn subt_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter().zip(y.iter()).map(|(&x, &y)| subt(x, y)).collect()
}
/// ### mult_vec_vec(x, y)
///
/// Operation Function
///
/// The `mult_vec_vec` function multiplies corresponding elements of vectors `x` and `y` using the mult function,
/// then returns a new vector containing their results.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{mult, mult_vec_vec, fround_vec};
/// assert_eq!(mult(0.1, 0.2), 0.020000000000000004);
/// assert_eq!(mult(0.1, 0.2) as f32, 0.02);
/// assert_eq!(mult_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0]), [0.0, 0.020000000000000004, 0.020000000000000004, 0.0]);
/// assert_eq!(fround_vec(&mult_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0])), [0.0, 0.02, 0.02, 0.0]);
/// ```
/// <small>End Fun Doc</small>
pub fn mult_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter().zip(y.iter()).map(|(&x, &y)| mult(x, y)).collect()
}
/// ### divi_vec_vec(x, y)
///
/// Operation Function
///
/// The `divi_vec_vec` function divides corresponding elements of vectors `x` and `y` using the divi function,
/// then returns a new vector containing their results.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{divi, divi_vec_vec, INF_F64};
/// assert_eq!(divi(0.1, 0.2), 0.5);
/// assert_eq!(divi_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0]), [0.0, 0.5, 2.0, INF_F64]);
/// ```
/// <small>End Fun Doc</small>
pub fn divi_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter().zip(y.iter()).map(|(&x, &y)| divi(x, y)).collect()
}
/// ### pow_vec_vec(x, y)
///
/// Operation Function
///
/// The `pow_vec_vec` function computes the power of corresponding elements of vectors `x` and `y` using the pow function,
/// then returns a new vector containing their exponents.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{pow, pow_vec_vec};
/// assert_eq!(pow(0.1, 0.2), 0.6309573444801932);
/// assert_eq!(pow_vec_vec(&[0.0, 0.1, 0.2, 0.3], &[0.3, 0.2, 0.1, 0.0]), [0.0, 0.6309573444801932, 0.8513399225207846, 1.0]);
/// ```
/// <small>End Fun Doc</small>
pub fn pow_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter().zip(y.iter()).map(|(&x, &y)| pow(x, y)).collect()
}
/// ### rem_vec_vec(x, y)
///
/// Operation Function
///
/// The `rem_vec_vec` function takes in two slices of floating-point numbers `x` and `y` and generates
/// a new vector of remainders by applying the remainder operator rem to their corresponding elements.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{rem, rem_vec_vec, is_nan_f64, INF_F64};
/// assert!(is_nan_f64(rem(0.0, 0.0)));
/// assert!(is_nan_f64(rem(1.0, 0.0)));
/// assert!(is_nan_f64(rem(INF_F64, 0.0)));
/// assert!(is_nan_f64(rem(INF_F64, 2.0)));
/// assert!(is_nan_f64(rem(INF_F64, INF_F64)));
/// assert!(is_nan_f64(rem(INF_F64, INF_F64)));
/// assert_eq!(rem_vec_vec(&[1.0, 2.0, 3.0, 4.0], &[4.0, 3.0, 2.0, 1.0]), [1.0, 2.0, 1.0, 0.0]);
/// ```
/// <small>End Fun Doc</small>
pub fn rem_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter().zip(y.iter()).map(|(&x, &y)| rem(x, y)).collect()
}
/// ### nrt_vec_vec(x, n)
///
/// Operation Function
///
/// The `nrt_vec_vec` function calculates the `n-th` root of each element in the input vector `x`, using the corresponding power
/// from the input vector `n`, and returns the results as a vector of floating-point numbers.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{nrt, nrt_vec_vec};
/// assert_eq!(nrt(27.0, 3.0), 3.0);
/// assert_eq!(nrt_vec_vec(&[1.0, 4.0, 27.0, 9.0], &[1.0, 2.0, 3.0, 2.0]), [1.0, 2.0, 3.0, 3.0]);
/// ```
/// <small>End Fun Doc</small>
pub fn nrt_vec_vec(x: &[f64], n: &[f64]) -> Vec<f64> {
x.iter().zip(n.iter()).map(|(&x, &n)| nrt(x, n)).collect()
}
/// ### perimeter_vec_vec(x, y)
///
/// Geometry Function
///
/// The `perimeter_vec_vec` function calculates the total length of boundaries between corresponding vectors in two slices `x` and `y`, returning a vector of results.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{perimeter_vec_vec, INF_F64 as inf};
/// assert_eq!(perimeter_vec_vec(&[0.0, 1.0, 1.0, 2.0, inf], &[0.0, 0.0, 1.0, 1.0, 1.0]), [0.0, 2.0, 4.0, 6.0, inf]);
/// ```
/// <small>End Fun Doc</small>
pub fn perimeter_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
x.iter()
.zip(y.iter())
.map(|(&x, &y)| perimeter(x, y))
.collect()
}