mathlab/functions/vec_vec/
mod.rs

1use super::num::{add, divi, mult, nrt, perimeter, pow, rem, subt};
2
3/// ### add_vec_vec(x, y)
4///
5/// Operation Function
6///
7/// The `add_vec_vec` function adds corresponding elements of vectors `x` and `y` using the add function,
8/// then returns a new vector containing their sums.
9///
10/// ### Examples
11/// ```rust
12/// use mathlab::math::{add, add_vec_vec, fround_vec};
13/// assert_eq!(add(0.1, 0.2), 0.30000000000000004);
14/// assert_eq!(add(0.1, 0.2) as f32, 0.3);
15/// 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]);
16/// 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]);
17/// ```
18/// <small>End Fun Doc</small>
19pub fn add_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
20    x.iter().zip(y.iter()).map(|(&x, &y)| add(x, y)).collect()
21}
22
23/// ### subt_vec_vec(x, y)
24///
25/// Operation Function
26///
27/// The `subt_vec_vec` function subtracts corresponding elements of vectors `x` and `y` using the subt function,
28/// then returns a new vector containing their differences.
29///
30/// ### Examples
31/// ```rust
32/// use mathlab::math::{subt, subt_vec_vec};
33/// assert_eq!(subt(0.1, 0.2), -0.1);
34/// 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]);
35/// ```
36/// <small>End Fun Doc</small>
37pub fn subt_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
38    x.iter().zip(y.iter()).map(|(&x, &y)| subt(x, y)).collect()
39}
40
41/// ### mult_vec_vec(x, y)
42///
43/// Operation Function
44///
45/// The `mult_vec_vec` function multiplies corresponding elements of vectors `x` and `y` using the mult function,
46/// then returns a new vector containing their results.
47///
48/// ### Examples
49/// ```rust
50/// use mathlab::math::{mult, mult_vec_vec, fround_vec};
51/// assert_eq!(mult(0.1, 0.2), 0.020000000000000004);
52/// assert_eq!(mult(0.1, 0.2) as f32, 0.02);
53/// 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]);
54/// 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]);
55/// ```
56/// <small>End Fun Doc</small>
57pub fn mult_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
58    x.iter().zip(y.iter()).map(|(&x, &y)| mult(x, y)).collect()
59}
60
61/// ### divi_vec_vec(x, y)
62///
63/// Operation Function
64///
65/// The `divi_vec_vec` function divides corresponding elements of vectors `x` and `y` using the divi function,
66/// then returns a new vector containing their results.
67///
68/// ### Examples
69/// ```rust
70/// use mathlab::math::{divi, divi_vec_vec, INF_F64};
71/// assert_eq!(divi(0.1, 0.2), 0.5);
72/// 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]);
73/// ```
74/// <small>End Fun Doc</small>
75pub fn divi_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
76    x.iter().zip(y.iter()).map(|(&x, &y)| divi(x, y)).collect()
77}
78
79/// ### pow_vec_vec(x, y)
80///
81/// Operation Function
82///
83/// The `pow_vec_vec` function computes the power of corresponding elements of vectors `x` and `y` using the pow function,
84/// then returns a new vector containing their exponents.
85///
86/// ### Examples
87/// ```rust
88/// use mathlab::math::{pow, pow_vec_vec};
89/// assert_eq!(pow(0.1, 0.2), 0.6309573444801932);
90/// 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]);
91/// ```
92/// <small>End Fun Doc</small>
93pub fn pow_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
94    x.iter().zip(y.iter()).map(|(&x, &y)| pow(x, y)).collect()
95}
96
97/// ### rem_vec_vec(x, y)
98///
99/// Operation Function
100///
101/// The `rem_vec_vec` function takes in two slices of floating-point numbers `x` and `y` and generates
102/// a new vector of remainders by applying the remainder operator rem to their corresponding elements.
103///
104/// ### Examples
105/// ```rust
106/// use mathlab::math::{rem, rem_vec_vec, is_nan_f64, INF_F64};
107/// assert!(is_nan_f64(rem(0.0, 0.0)));
108/// assert!(is_nan_f64(rem(1.0, 0.0)));
109/// assert!(is_nan_f64(rem(INF_F64, 0.0)));
110/// assert!(is_nan_f64(rem(INF_F64, 2.0)));
111/// assert!(is_nan_f64(rem(INF_F64, INF_F64)));
112/// assert!(is_nan_f64(rem(INF_F64, INF_F64)));
113/// 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]);
114/// ```
115/// <small>End Fun Doc</small>
116pub fn rem_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
117    x.iter().zip(y.iter()).map(|(&x, &y)| rem(x, y)).collect()
118}
119
120/// ### nrt_vec_vec(x, n)
121///
122/// Operation Function
123///
124/// The `nrt_vec_vec` function calculates the `n-th` root of each element in the input vector `x`, using the corresponding power
125/// from the input vector `n`, and returns the results as a vector of floating-point numbers.
126///
127/// ### Examples
128/// ```rust
129/// use mathlab::math::{nrt, nrt_vec_vec};
130/// assert_eq!(nrt(27.0, 3.0), 3.0);
131/// 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]);
132/// ```
133/// <small>End Fun Doc</small>
134pub fn nrt_vec_vec(x: &[f64], n: &[f64]) -> Vec<f64> {
135    x.iter().zip(n.iter()).map(|(&x, &n)| nrt(x, n)).collect()
136}
137
138/// ### perimeter_vec_vec(x, y)
139///
140/// Geometry Function
141///
142/// 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.
143///
144/// ### Examples
145/// ```rust
146/// use mathlab::math::{perimeter_vec_vec, INF_F64 as inf};
147/// 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]);
148/// ```
149/// <small>End Fun Doc</small>
150pub fn perimeter_vec_vec(x: &[f64], y: &[f64]) -> Vec<f64> {
151    x.iter()
152        .zip(y.iter())
153        .map(|(&x, &y)| perimeter(x, y))
154        .collect()
155}