mathlab/functions/num_vec/mod.rs
1use super::num::{add, divi, mult, nrt, perimeter, pow, rem, subt};
2
3/// ### add_num_vec(x, y)
4///
5/// Operation Function
6///
7/// The `add_num_vec` function adds a float `x` to each element in a slice `y` of floats,
8/// returning a new vector containing the sums.
9///
10/// ### Examples
11/// ```rust
12/// use mathlab::math::{add, add_num_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_num_vec(0.1, &[0.0, 0.1, 0.2]), [0.1, 0.2, 0.30000000000000004]);
16/// assert_eq!(fround_vec(&add_num_vec(0.1, &[0.0, 0.1, 0.2])), [0.1, 0.2, 0.3]);
17/// ```
18/// <small>End Fun Doc</small>
19pub fn add_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
20 y.iter().map(|&y| add(x, y)).collect()
21}
22
23/// ### subt_num_vec(x, y)
24///
25/// Operation Function
26///
27/// The `subt_num_vec` function subtracts a float `x` from each element in a slice `y` of floats,
28/// returning a new vector containing the differences.
29///
30/// ### Examples
31/// ```rust
32/// use mathlab::math::{subt, subt_num_vec, fround_vec};
33/// assert_eq!(subt(0.1, 0.2), -0.1);
34/// assert_eq!(subt(0.1, 0.2) as f32, -0.1);
35/// assert_eq!(subt_num_vec(0.1, &[0.0, 0.1, 0.2, 0.3]), [0.1, 0.0, -0.1, -0.19999999999999998]);
36/// assert_eq!(fround_vec(&subt_num_vec(0.1, &[0.0, 0.1, 0.2, 0.3])), [0.1, 0.0, -0.1, -0.2]);
37/// ```
38/// <small>End Fun Doc</small>
39pub fn subt_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
40 y.iter().map(|&y| subt(x, y)).collect()
41}
42
43/// ### mult_num_vec(x, y)
44///
45/// Operation Function
46///
47/// The `mult_num_vec` multiplies a float `x` by each element in a slice `y` of floats,
48/// returning a new vector containing the results.
49///
50/// ### Examples
51/// ```rust
52/// use mathlab::math::{mult, mult_num_vec, fround_vec};
53/// assert_eq!(mult(0.1, 0.2), 0.020000000000000004);
54/// assert_eq!(mult(0.1, 0.2) as f32, 0.02);
55/// assert_eq!(mult_num_vec(0.1, &[0.0, 0.1, 0.2]), [0.0, 0.010000000000000002, 0.020000000000000004]);
56/// assert_eq!(fround_vec(&mult_num_vec(0.1, &[0.0, 0.1, 0.2])), [0.0, 0.01, 0.02]);
57/// ```
58/// <small>End Fun Doc</small>
59pub fn mult_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
60 y.iter().map(|&y| mult(x, y)).collect()
61}
62
63/// ### divi_num_vec(x, y)
64///
65/// Operation Function
66///
67/// The `divi_num_vec` divides each element in a slice `y` of floats by a float `x`,
68/// returning a new vector containing the results.
69///
70/// ### Examples
71/// ```rust
72/// use mathlab::math::{divi, divi_num_vec, fround_vec};
73/// assert_eq!(divi(0.2, 0.3), 0.6666666666666667);
74/// assert_eq!(divi(0.2, 0.3) as f32, 0.6666667);
75/// //assert_eq!(divi_num_vec(0.2, &[0.0, 0.1, 0.2, 0.3]), [inf, 2.0, 1.0, 0.6666666666666666]);
76/// //assert_eq!(fround_vec(&divi_num_vec(0.1, &[0.0, 0.1, 0.2, 0.3])), [inf, 2.0, 1.0, 0.6666667]);
77/// ```
78/// <small>End Fun Doc</small>
79pub fn divi_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
80 y.iter().map(|&y| divi(x, y)).collect()
81}
82
83/// ### pow_num_vec(x, y)
84///
85/// Operation Function
86///
87/// The `pow_num_vec` raises each element in a slice `y` of floats to the power of a float `x`,
88/// returning a new vector containing the results.
89///
90/// ### Examples
91/// ```rust
92/// use mathlab::math::{pow, pow_num_vec, INF_F64};
93/// assert_eq!(pow(0.0, 1.0), 0.0);
94/// assert_eq!(pow(2.0 , -3.0), 0.125);
95/// assert_eq!(pow_num_vec(2.0, &[-3.0, 0.0, 4.0, INF_F64]), [0.125, 1.0, 16.0, INF_F64]);
96/// ```
97/// <small>End Fun Doc</small>
98pub fn pow_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
99 y.iter().map(|&y| pow(x, y)).collect()
100}
101
102/// ### rem_num_vec(x, y)
103///
104/// Operation Function
105///
106/// The `rem_num_vec` function computes the remainders obtained after dividing each element in `y` by another fixed number `x`, and accumulates these leftovers into a new vector.
107///
108/// ### Examples
109/// ```rust
110/// use mathlab::math::{rem, rem_num_vec, is_nan_f64, INF_F64};
111/// assert!(is_nan_f64(rem(0.0, 0.0)));
112/// assert!(is_nan_f64(rem(1.0, 0.0)));
113/// assert!(is_nan_f64(rem(INF_F64, 0.0)));
114/// assert!(is_nan_f64(rem(INF_F64, 2.0)));
115/// assert!(is_nan_f64(rem(INF_F64, INF_F64)));
116/// assert!(is_nan_f64(rem(INF_F64, INF_F64)));
117/// assert_eq!(rem_num_vec(3.0, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]), [0.0, 1.0, 0.0, 3.0, 3.0, 3.0, 3.0]);
118/// ```
119/// <small>End Fun Doc</small>
120pub fn rem_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
121 y.iter().map(|&y| rem(x, y)).collect()
122}
123
124/// ### nrt_num_vec(x, n)
125///
126/// Operation Function
127///
128/// The `nrt_num_vec` function calculates the `n-th` root of `x` for each power in the given vector `n`,
129/// and returns the results as a vector of floating-point numbers.
130///
131/// ### Examples
132/// ```rust
133/// use mathlab::math::{nrt, nrt_num_vec};
134/// assert_eq!(nrt(27.0, 3.0), 3.0);
135/// assert_eq!(nrt_num_vec(81.0, &[1.0, 2.0, 4.0]), [81.0, 9.0, 3.0]);
136/// ```
137/// <small>End Fun Doc</small>
138pub fn nrt_num_vec(x: f64, n: &[f64]) -> Vec<f64> {
139 n.iter().map(|&n| nrt(x, n)).collect()
140}
141
142/// ### perimeter_num_vec(x, y)
143///
144/// Geometry Function
145///
146/// The `perimeter_num_vec` function calculates the perimeter of each element in a slice `y` of floats, given a reference `x`, and returns a new vector containing the perimeters.
147///
148/// ### Examples
149/// ```rust
150/// use mathlab::math::{perimeter_num_vec, INF_F64 as inf};
151/// assert_eq!(perimeter_num_vec(1.0, &[0.0, 1.0, 2.0, inf]), [2.0, 4.0, 6.0, inf]);
152/// ```
153/// <small>End Fun Doc</small>
154pub fn perimeter_num_vec(x: f64, y: &[f64]) -> Vec<f64> {
155 y.iter().map(|&y| perimeter(x, y)).collect()
156}