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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use ;
/// ### cross(a, b)
///
/// **Mathematical vector operation**
///
/// Calculates the cross product of two 3-dimensional vectors represented as slices of `f64`.
///
/// The cross product of two vectors results in a third vector that is perpendicular to both of the original vectors.
/// It is calculated as follows:
///
/// cross_product = (a_j * b_k - a_k * b_j, // x-component
/// a_k * b_i - a_i * b_k, // y-component
/// a_i * b_j - a_j * b_i) // z-component
///
/// where:
/// - The first vector has components a_i, a_j, a_k
/// - The second vector has components b_i, b_j, b_k
///
/// This operation produces a new vector with components calculated from these formulas.
///
/// ### Panics
///
/// This function will panic if the slices `a` and `b` are not of length 3, due to the `assert_eq!` checks.
///
/// ### Usage
///
/// The developer must ensure that `a` and `b` are slices of length 3, representing vectors in 3D space.
/// The function returns a new vector that is the cross product of the two input vectors.
///
/// ### Example
///
/// ```rust
/// use mathlab::math::cross;
///
/// // First cross product
/// let a = vec![1.0, 0.0, 0.0]; // Represents vector a = 1*i + 0*j + 0*k
/// let b = vec![0.0, 1.0, 0.0]; // Represents vector b = 0*i + 1*j + 0*k
/// let a_cross_b = cross(&a, &b);
/// println!("{:?}", a_cross_b); // Outputs [0.0, 0.0, 1.0], which is i x j = k
///
/// // Second cross product
/// let c = vec![0.0, 1.0, 0.0]; // Represents vector c = 0*i + 1*j + 0*k
/// let d = vec![1.0, 0.0, 0.0]; // Represents vector d = 1*i + 0*j + 0*k
/// let c_cross_d = cross(&c, &d);
/// println!("{:?}", c_cross_d); // Outputs [0.0, 0.0, -1.0], which is j x i = -k
///
/// // Third cross product
/// let e = vec![0.0, 1.0, 2.0]; // Represents vector e = 0*i + 1*j + 2*k
/// let f = vec![3.0, 4.0, 5.0]; // Represents vector f = 3*i + 4*j + 5*k
/// let e_cross_f = cross(&e, &f);
/// println!("{:?}", e_cross_f); // Outputs [-3.0, 6.0, -3.0], which is e x f.
/// // Calculation: (1*5 - 2*4, 2*3 - 0*5, 0*4 - 1*3) = (-3, 6, -3)
/// ```
/// <small>End Function Documentation</small>
/// ### dot(a, b)
///
/// **Mathematical vector operation**
///
/// Calculates the dot product of two slices of `f64`.
///
/// ### Panics
///
/// This function will panic if the lengths of the slices `a` and `b` are not equal,
/// due to the `assert_eq!` check.
///
/// ### Usage
///
/// The developer is responsible for ensuring that `a` and `b` are of the same length before calling this function.
/// If there's a possibility of different lengths, handle that case externally or use an alternative approach.
///
/// ### Example
///
/// ```rust
/// use mathlab::math::dot;
/// let a = vec![0.0, 1.0, 2.0, -1.0];
/// let b = vec![3.0, 4.0, 5.0, 5.0];
/// let result = dot(&a, &b);
/// println!("{}", result); // Outputs 9.0
/// ```
/// /// <small>End Fun Doc</small>
/// ### monolist(x, size)
///
/// Generating function
///
/// The `monolist` function generates a list of specified length containing only a given float value `x`.
/// The maximum allowed length is limited to 1 million to prevent excessive resource consumption.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{monolist, fix64};
/// assert_eq!(monolist(0.1, 0), []);
/// assert_eq!(monolist(0.1, 1000000000), monolist(0.1, 1000000000)); // if size > 1 million { function will replace size with 0 } -> []
/// assert_eq!(monolist(-1.0, 2), [-1.0, -1.0]);
/// assert_eq!(monolist(0.0, 2), [0.0, 0.0]);
/// assert_eq!(monolist(1.0, 2), [1.0, 1.0]);
/// assert_eq!(monolist(0.1 + 0.2, 2), [0.3, 0.3]);
/// assert_eq!(monolist(0.30000000000000004, 2), [0.3, 0.3]);
/// ```
/// <small>End Fun Doc</small>
/// ### range(x, step, size, order)
///
/// Generating function
///
/// The `range` function generates a sequence of floating-point numbers starting from x, with a specified step value,
/// producing up to size elements, ordered either ascending ("asc") or descending ("desc").
///
/// The function returns an empty vector if:
/// - size exceeds 1,000,000 (to prevent excessively large sequences)
/// - step is nonpositive
/// - order is not "asc" or "desc"
///
/// This function is useful when you need a sequence of a specific size, especially in cases where
/// the total range is not explicitly known or when you want to generate a fixed number of evenly spaced points.
///
/// ### Arguments
///
/// * x - The starting value of the sequence.
/// * step - The interval between consecutive numbers; must be positive.
/// * size - The number of elements to generate; capped at 1,000,000 for safety.
/// * order - A string slice indicating the sequence order: "asc" for ascending, "desc" for descending.
///
/// ### Returns
///
/// A vector containing the sequence of f64 values, ordered according to the order parameter.
///
/// ### Examples
/// ```rust
/// use mathlab::math::range;
/// // For the order argument, use "asc" for ascending order or "desc" for descending order, otherwise the function will return [].
/// assert_eq!(range(0.0, 0.1, 10, "abcd"), []);
/// assert_eq!(range(0.0, 0.1, 0, "asc"), []); // The parameter size must be from 1 to 1 million.
/// assert_eq!(range(0.0, 0.1, 1000000000, "asc"), []); // The parameter size must be from 1 to 1 million.
/// assert_eq!(range(1.0, 1.0, 10, "asc"), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]);
/// assert_eq!(range(0.0, 0.1, 10, "asc"), [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]);
/// assert_eq!(range(0.0, 0.1, 10, "desc"), [0.0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9]);
/// assert_eq!(range(0.9, 0.1, 10, "desc"), [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]);
/// assert_eq!(range(0.0, 2.0, 3, "asc"), [0.0, 2.0, 4.0]);
/// assert_eq!(range(4.0, 2.0, 3, "desc"), [4.0, 2.0, 0.0]);
/// ```
/// <small>End Fun Doc</small>
/// ### range_from_to(from, to, step)
///
/// Generating function
///
/// The `range_from_to` function generates a sequence of f64 numbers from from to to with a specified step.
///
/// This function automatically determines the direction of the sequence (ascending or descending)
/// based on the input parameters and adjusts the step sign accordingly to produce a correct range.
///
/// # Arguments
///
/// * from - The starting value of the sequence.
/// * to - The ending value of the sequence.
/// * step - The increment/decrement between consecutive elements; must be positive. The function will adjust the sign based on direction.
///
/// # Returns
///
/// A vector containing the generated sequence of f64 values, starting from from and proceeding towards to,
/// inclusive of the endpoint if the range aligns exactly with the step increments.
///
/// # Notes
/// - If step is less than or equal to zero, the function returns an empty vector.
/// - The function handles both ascending and descending ranges.
/// - The sequence is generated with floating-point precision, and the values are processed through fix64 for formatting or rounding.
/// - The number of elements in the returned vector depends on the distance between from and to and the provided step.
///
/// # Examples
///
/// ```rust
/// use mathlab::math::range_from_to;
/// // Ascending range from 0.0 to 1.0 with step 0.2
/// assert_eq!(
/// range_from_to(0.0, 1.0, 0.2),
/// vec![0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
/// );
///
/// // Descending range from 1.0 to 0.0 with step 0.2
/// assert_eq!(
/// range_from_to(1.0, 0.0, 0.2),
/// vec![1.0, 0.8, 0.6, 0.4, 0.2, 0.0]
/// );
///
/// // Range from -1.0 to 1.0 with step 0.5
/// assert_eq!(
/// range_from_to(-1.0, 1.0, 0.5),
/// vec![-1.0, -0.5, 0.0, 0.5, 1.0]
/// );
///
/// // When to equals from, should produce a single-element vector
/// assert_eq!(
/// range_from_to(2.0, 2.0, 0.1),
/// vec![2.0]
/// );
///
/// // Negative step should produce an empty vector
/// assert_eq!(
/// range_from_to(0.0, 1.0, -0.1),
/// Vec::<f64>::new()
/// );
///
/// assert_eq!(range_from_to(0.0, 1000000.0, 0.5), []); // The maximum size of the vector must not be more than 1 million.
/// ```
/// <small>End Fun Doc</small>
/// ### to_fixed(x, decimal_places)
///
/// Fixation Function
///
/// The `to_fixed` function converts a floating-point number `x` to a string with a specified
/// number of decimal places, returning a fixed-point string representation.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{to_fixed, fix, is_nan_f64, INF_F64 as inf, NAN_F64 as NaN};
/// assert_eq!(to_fixed(0.5235987755982988, 3), "0.524");
/// assert_eq!(to_fixed(0.5235987755982928, 15), "0.523598775598293");
/// assert_eq!(to_fixed(0.5235987755982928, 1), "0.5");
/// assert_eq!(to_fixed(0.5235987755982928, 0), "1");
/// assert_eq!(to_fixed(0.0, 0), "0");
/// assert_eq!(to_fixed(inf, 0), "inf");
/// assert_eq!(to_fixed(inf, 0), "inf");
/// assert_eq!(to_fixed(NaN, 0), "NaN");
/// assert!(is_nan_f64(fix(NaN, 0)));
/// assert_eq!(to_fixed(0.1 + 0.2, 15), "0.3");
/// ```
/// <small>End Fun Doc</small>
/// ### hypot(x)
///
/// Geometric Function
///
/// The `hypot` function calculates the Euclidean norm (also known as the magnitude or length) of a vector in n-dimensional space.
///
/// ### Examples
/// ```rust
/// use mathlab::math::{hypot, INF_F64 as inf, is_nan_f64 as is_nan};
/// assert_eq!(hypot(&[0.0]), 0.0);
/// assert_eq!(hypot(&[1.0]), 1.0);
/// assert_eq!(hypot(&[1.0 / 0.0]), inf);
/// assert!(is_nan(hypot(&[0.0 / 0.0])));
/// assert_eq!(hypot(&[4.0]), 4.0);
/// assert_eq!(hypot(&[3.0, 4.0]), 5.0);
/// assert_eq!(hypot(&[4.0, 2.0, 4.0]), 6.0);
/// assert_eq!(hypot(&[-3.0, -4.0]), 5.0);
/// assert_eq!(hypot(&[-4.0]), 4.0);
/// ```
/// <small>End Fun Doc</small>
/////////////////// RAND FUNCTION ///////////////////
use ;
/// Returns the current UTC time as a `SystemTime`.
///
/// This function uses the standard library function to get the current time.
/// Returns the duration since the UNIX epoch (January 1, 1970).
///
/// This function retrieves the current time and calculates how long it has been since
/// the UNIX epoch, returning it as a `Duration`.
/// Returns the current time in nanoseconds (within the last second).
///
/// The result is the remainder of the nanoseconds since the epoch, modulo 1,000,000,
/// effectively returning nanoseconds as a value between 0 and 999,999.
/// Returns the current time in milliseconds (within the last second).
///
/// The result is the remainder of the milliseconds since the epoch, modulo 1,000,
/// effectively returning milliseconds as a value between 0 and 999.
/// Returns the current second (within the last minute).
///
/// This function retrieves the current seconds since the UNIX epoch
/// and returns it as a value between 0 and 59.
/// ### string_to_u64(s)
///
/// Converts a string into a `u64`.
///
/// This function attempts to parse the provided string `s` into a `u64`.
/// If successful, it returns `Ok(u64)`, otherwise an error of type `ParseIntError`.
///
/// ### Examples
/// ```
/// use mathlab::math::string_to_u64;
///
/// let result = string_to_u64("12345".to_string());
/// assert_eq!(result, Ok(12345));
/// ```
/// <small>End Fun Doc</small>
/// Formats a `u64` number to ensure it is a 10-digit representation.
///
/// If the number has more than 10 digits, it truncates the leftmost digits.
/// If it has fewer than 10 digits, it pads the number with leading zeros
/// to ensure it has exactly 10 digits. format_number_10d(123) ==> 0000000123
/// Generates a formatted string combining nanos, millis, and seconds.
///
/// This function creates a string representation of the current time using nanoseconds,
/// milliseconds, and seconds, then formats it as a `u64` ensuring it is 10 digits.
/// ### rand(size)
///
/// Generates a pseudo-random `u64` number within the specified digit size.
///
/// The `size` parameter determines the number of digits for the generated random number.
/// The function will cap the `size` at 19 if requested larger to ensure it fits within the
/// limits of `u64`, preventing overflow.
///
/// ### Parameters
/// - `size`: The desired number of digits for the random number. Must be greater than 0.
///
/// ### Returns
/// A `u64` random number with digit count as specified, capped at 19.
///
/// ### Examples
/// ```rust
/// use mathlab::math::rand;
///
/// fn main() {
/// // Generating and printing random numbers of different sizes
/// println!("Random number (size 1): {:?}", rand(1));
/// println!("Random number (size 2): {:?}", rand(2));
/// println!("Random number (size 3): {:?}", rand(3));
/// println!("Random number (size 6): {:?}", rand(6));
/// println!("Random number (size 15): {:?}", rand(15));
/// println!("Random number (size 19): {:?}", rand(19));
/// println!("Random number (size 19, requested 25): {:?}", rand(25)); // Size capped at 19
/// }
/// ```
/// <small>End Fun Doc</small>
/////////////////// END RAND FUNCTION ///////////////////