h_math/lib.rs
1/// h_math
2///
3/// A comprehensive math library for Rust, providing a wide range of mathematical functions and utilities.
4/// The library is designed to be easy to use with traits and generics.
5/// The naming conventions for traits and functions are h_ followed by what the trait and function does.
6///
7/// # Examples
8/// h_average, h_median, h_haramond_product, h_factorial, h_sphere_volume ect.
9///
10/// use h_math::prelude::*; for the best experience, as it will import all the traits and functions into scope.
11/// and since the traits are implemented for primitive types, you can call the functions directly on the values.
12/// also, since the functions and traits always hav h_xxxxxx you won't have to worry about naming conflicts with other libraries.
13///
14/// Please refer to the documentation for each module for more details on the available functions and how to use them.
15///
16/// # Core Mathematics Functions
17///
18/// ## Factorial Trait (`Factorial`)
19/// - **Function**: `h_factorial()`
20/// - **Input**: Any numeric type implementing `Copy + PartialOrd + Into<u32>`
21/// - **Output**: `u64`
22/// - **Purpose**: Calculates the factorial of a number (n!)
23/// - **Formula**: n! = n × (n-1) × (n-2) × ... × 1
24/// - **Panics**: If given a negative number
25///
26/// ## Root Degree Trait (`RootDegree`)
27/// - **Function**: `h_root_degree(degree: u32)`
28/// - **Input**: Self (numeric type), degree (u32)
29/// - **Output**: `f64`
30/// - **Purpose**: Calculates the nth root of a number
31/// - **Formula**: ⁿ√x = x^(1/n)
32///
33/// ## h_sigma Function
34/// - **Signature**: `h_sigma<T>(start: T, repetitions: u32, steps: T) -> f64`
35/// - **Input**: start value, number of repetitions, step size
36/// - **Output**: `f64` (sum)
37/// - **Purpose**: Calculates the sum of an arithmetic sequence
38/// - **Example**: h_sigma(1, 5, 1) sums 1+2+3+4+5 = 15
39///
40/// ## h_arrange_vec Function
41/// - **Signature**: `h_arrange_vec<I>(start: I, stop: I, step: I) -> Vec<f64>`
42/// - **Input**: start value, stop value, step size
43/// - **Output**: `Vec<f64>` containing evenly spaced values
44/// - **Purpose**: Creates a vector of evenly spaced numbers from start to stop
45/// - **Returns empty**: If step is 0, or if direction is invalid (positive step but start > stop)
46///
47/// ## h_pi Function
48/// - **Signature**: `h_pi() -> f64`
49/// - **Input**: None
50/// - **Output**: `f64` (π)
51/// - **Purpose**: Returns the mathematical constant π (3.14159265...)
52///
53/// # Algebra Functions
54///
55/// ## h_quadratic_equation Function
56/// - **Signature**: `h_quadratic_equation<A,B,C>(a: A, b: B, c: C) -> (f64, f64)`
57/// - **Input**: Coefficients a, b, c of ax² + bx + c = 0
58/// - **Output**: `(f64, f64)` tuple with two roots
59/// - **Purpose**: Solves quadratic equations using the quadratic formula
60/// - **Formula**: x = (-b ± √(b² - 4ac)) / 2a
61/// - **Panics**: If discriminant is negative (no real roots)
62///
63/// ## h_simple_eq_checker_x Function
64/// - **Signature**: `h_simple_eq_checker_x<...>(x_value: X, num_left: NL, x_left: XL, num_right: NR, x_right: XR) -> bool`
65/// - **Input**: x value to check, left side numeric coefficient, left side x coefficient, right side numeric coefficient, right side x coefficient
66/// - **Output**: `bool`
67/// - **Purpose**: Checks if a given x value satisfies a linear equation: (x_left × x + num_left) = (x_right × x + num_right)
68///
69/// # Temperature Conversions
70///
71/// ## CelsiusToFahrenheit Trait
72/// - **Function**: `h_celsius_to_fahrenheit()`
73/// - **Input**: Temperature in Celsius
74/// - **Output**: `f64` (Fahrenheit)
75/// - **Formula**: (C × 9/5) + 32
76///
77/// ## FahrenheitToCelsius Trait
78/// - **Function**: `h_fahrenheit_to_celsius()`
79/// - **Input**: Temperature in Fahrenheit
80/// - **Output**: `f64` (Celsius)
81/// - **Formula**: (F - 32) × 5/9
82///
83/// ## CelsiusToKelvin Trait
84/// - **Function**: `h_celsius_to_kelvin()`
85/// - **Input**: Temperature in Celsius
86/// - **Output**: `f64` (Kelvin)
87/// - **Formula**: C + 273.15
88///
89/// ## KelvinToCelsius Trait
90/// - **Function**: `h_kelvin_to_celsius()`
91/// - **Input**: Temperature in Kelvin
92/// - **Output**: `f64` (Celsius)
93/// - **Formula**: K - 273.15
94///
95/// ## FahrenheitToKelvin Trait
96/// - **Function**: `h_fahrenheit_to_kelvin()`
97/// - **Input**: Temperature in Fahrenheit
98/// - **Output**: `f64` (Kelvin)
99/// - **Formula**: ((F - 32) × 5/9) + 273.15
100///
101/// ## KelvinToFahrenheit Trait
102/// - **Function**: `h_kelvin_to_fahrenheit()`
103/// - **Input**: Temperature in Kelvin
104/// - **Output**: `f64` (Fahrenheit)
105/// - **Formula**: ((K - 273.15) × 9/5) + 32
106///
107/// # Length Conversions
108///
109/// ## MetersToKilometers Trait
110/// - **Function**: `h_meters_to_kilometers()`
111/// - **Input**: Distance in meters
112/// - **Output**: `f64` (kilometers)
113/// - **Formula**: m ÷ 1000
114///
115/// ## KilometersToMeters Trait
116/// - **Function**: `h_kilometers_to_meters()`
117/// - **Input**: Distance in kilometers
118/// - **Output**: `f64` (meters)
119/// - **Formula**: km × 1000
120///
121/// ## CentimetersToMeters Trait
122/// - **Function**: `h_centimeters_to_meters()`
123/// - **Input**: Distance in centimeters
124/// - **Output**: `f64` (meters)
125/// - **Formula**: cm ÷ 100
126///
127/// ## MetersToCentimeters Trait
128/// - **Function**: `h_meters_to_centimeters()`
129/// - **Input**: Distance in meters
130/// - **Output**: `f64` (centimeters)
131/// - **Formula**: m × 100
132///
133/// ## CentimetersToMillimeters Trait
134/// - **Function**: `h_centimeters_to_millimeters()`
135/// - **Input**: Distance in centimeters
136/// - **Output**: `f64` (millimeters)
137/// - **Formula**: cm × 10
138///
139/// ## MillimetersToCentimeters Trait
140/// - **Function**: `h_millimeters_to_centimeters()`
141/// - **Input**: Distance in millimeters
142/// - **Output**: `f64` (centimeters)
143/// - **Formula**: mm ÷ 10
144///
145/// ## KilometersToMiles Trait
146/// - **Function**: `h_kilometers_to_miles()`
147/// - **Input**: Distance in kilometers
148/// - **Output**: `f64` (miles)
149/// - **Formula**: km × 0.621371
150///
151/// ## MilesToKilometers Trait
152/// - **Function**: `h_miles_to_kilometers()`
153/// - **Input**: Distance in miles
154/// - **Output**: `f64` (kilometers)
155/// - **Formula**: miles ÷ 0.621371
156///
157/// ## InchesToCentimeters Trait
158/// - **Function**: `h_inches_to_centimeters()`
159/// - **Input**: Distance in inches
160/// - **Output**: `f64` (centimeters)
161/// - **Formula**: inches × 2.54
162///
163/// ## CentimetersToInches Trait
164/// - **Function**: `h_centimeters_to_inches()`
165/// - **Input**: Distance in centimeters
166/// - **Output**: `f64` (inches)
167/// - **Formula**: cm ÷ 2.54
168///
169/// ## CentimetersToDecimeters Trait
170/// - **Function**: `h_centimeters_to_decimeters()`
171/// - **Input**: Distance in centimeters
172/// - **Output**: `f64` (decimeters)
173/// - **Formula**: cm ÷ 10
174///
175/// ## DecimetersToCentimeters Trait
176/// - **Function**: `h_decimeters_to_centimeters()`
177/// - **Input**: Distance in decimeters
178/// - **Output**: `f64` (centimeters)
179/// - **Formula**: dm × 10
180///
181/// # Geometry Functions
182///
183/// ## CircleCircumference Trait
184/// - **Function**: `h_circle_circumference()`
185/// - **Input**: Radius of circle
186/// - **Output**: `f64`
187/// - **Purpose**: Calculates the circumference of a circle
188/// - **Formula**: 2πr
189///
190/// ## CircleArea Trait
191/// - **Function**: `h_circle_area()`
192/// - **Input**: Radius of circle
193/// - **Output**: `f64`
194/// - **Purpose**: Calculates the area of a circle
195/// - **Formula**: πr²
196///
197/// ## h_pythagorean_theorem Function
198/// - **Signature**: `h_pythagorean_theorem<A, B>(a: A, b: B) -> f64`
199/// - **Input**: Two sides of a right triangle (a, b)
200/// - **Output**: `f64` (hypotenuse)
201/// - **Purpose**: Calculates the hypotenuse of a right triangle
202/// - **Formula**: c = √(a² + b²)
203///
204/// ## h_reverse_pythagorean_theorem Function
205/// - **Signature**: `h_reverse_pythagorean_theorem<K, H>(x: K, h: H) -> f64`
206/// - **Input**: One known side (x) and hypotenuse (h)
207/// - **Output**: `f64` (other side)
208/// - **Purpose**: Finds the missing side of a right triangle
209/// - **Formula**: a = √(h² - x²)
210///
211/// ## h_find_equal_legs_from_hypotenuse Function
212/// - **Signature**: `h_find_equal_legs_from_hypotenuse<H>(h: H) -> f64`
213/// - **Input**: Hypotenuse of an isosceles right triangle
214/// - **Output**: `f64` (length of equal legs)
215/// - **Purpose**: Finds the length of equal legs in a 45-45-90 triangle
216/// - **Formula**: leg = √(h²/2)
217///
218/// ## ShortFromLongLeg30_60_90 Trait
219/// - **Function**: `h_short_from_long_leg_30_60_90()`
220/// - **Input**: Long leg of a 30-60-90 triangle
221/// - **Output**: `f64` (short leg)
222/// - **Formula**: short_leg = long_leg ÷ √3
223///
224/// # 3D Geometry Functions
225///
226/// ## SphereVolume Trait
227/// - **Function**: `h_sphere_volume()`
228/// - **Input**: Radius of sphere
229/// - **Output**: `f64`
230/// - **Purpose**: Calculates the volume of a sphere
231/// - **Formula**: V = (4/3)πr³
232///
233/// ## SphereSurfaceArea Trait
234/// - **Function**: `h_sphere_surface_area()`
235/// - **Input**: Radius of sphere
236/// - **Output**: `f64`
237/// - **Purpose**: Calculates the surface area of a sphere
238/// - **Formula**: A = 4πr²
239///
240/// ## CylinderVolume Trait
241/// - **Function**: `h_cylinder_volume(height: H)`
242/// - **Input**: Radius and height of cylinder
243/// - **Output**: `f64`
244/// - **Purpose**: Calculates the volume of a cylinder
245/// - **Formula**: V = πr²h
246///
247/// ## CylinderSurfaceArea Trait
248/// - **Function**: `h_cylinder_surface_area(height: H)`
249/// - **Input**: Radius and height of cylinder
250/// - **Output**: `f64`
251/// - **Purpose**: Calculates the surface area of a cylinder
252/// - **Formula**: A = 2πr² + 2πrh
253///
254/// ## CubeVolume Trait
255/// - **Function**: `h_cube_volume()`
256/// - **Input**: Side length of cube
257/// - **Output**: `f64`
258/// - **Purpose**: Calculates the volume of a cube
259/// - **Formula**: V = s³
260///
261/// ## CubeSurfaceArea Trait
262/// - **Function**: `h_cube_surface_area()`
263/// - **Input**: Side length of cube
264/// - **Output**: `f64`
265/// - **Purpose**: Calculates the surface area of a cube
266/// - **Formula**: A = 6s²
267///
268/// ## ConeVolume Trait
269/// - **Function**: `h_cone_volume(height: H)`
270/// - **Input**: Radius and height of cone
271/// - **Output**: `f64`
272/// - **Purpose**: Calculates the volume of a cone
273/// - **Formula**: V = (1/3)πr²h
274///
275/// ## ConeSurfaceArea Trait
276/// - **Function**: `h_cone_surface_area(height: H)`
277/// - **Input**: Radius and height of cone
278/// - **Output**: `f64`
279/// - **Purpose**: Calculates the surface area of a cone
280/// - **Formula**: A = πr(r + √(r² + h²)) where √(r² + h²) is the slant height
281///
282/// ## RectangularPrismVolume Trait
283/// - **Function**: `h_rectangular_prism_volume(height: H)`
284/// - **Input**: Length/width and height of rectangular prism
285/// - **Output**: `f64`
286/// - **Purpose**: Calculates the volume of a rectangular prism
287/// - **Formula**: V = l × w × h
288///
289/// ## RectangularPrismSurfaceArea Trait
290/// - **Function**: `h_rectangular_prism_surface_area(height: H)`
291/// - **Input**: Length/width and height of rectangular prism
292/// - **Output**: `f64`
293/// - **Purpose**: Calculates the surface area of a rectangular prism
294/// - **Formula**: A = 2(lw + lh + wh)
295///
296/// ## PyramidVolume Trait
297/// - **Function**: `h_pyramid_volume(height: H)`
298/// - **Input**: Base area and height of pyramid
299/// - **Output**: `f64`
300/// - **Purpose**: Calculates the volume of a pyramid
301/// - **Formula**: V = (1/3) × base_area × height
302///
303/// ## SquarePyramidSurfaceArea Trait
304/// - **Function**: `h_square_pyramid_surface_area(height: H)`
305/// - **Input**: Base area and height of square pyramid
306/// - **Output**: `f64`
307/// - **Purpose**: Calculates the surface area of a square pyramid
308/// - **Formula**: A = base_area + 2 × √(base_area/4) × slant_height
309///
310/// # Finance Functions
311///
312/// ## ROI Trait (Return on Investment)
313/// - **Function**: `h_return_on_investment(new_value: f64)`
314/// - **Input**: Initial investment value, new value
315/// - **Output**: `f64` (percentage)
316/// - **Purpose**: Calculates return on investment as a percentage
317/// - **Formula**: ROI = ((new_value - initial) / initial) × 100
318///
319/// ## DiscountedPrice Trait
320/// - **Function**: `h_decreased_price(decrease_percent: f64)`
321/// - **Input**: Original price, discount percentage
322/// - **Output**: `f64` (discounted price)
323/// - **Purpose**: Calculates the price after a percentage discount
324/// - **Formula**: discounted_price = original × (1 - percentage/100)
325///
326/// ## IncreasedPrice Trait
327/// - **Function**: `h_increased_price(increase_percent: f64)`
328/// - **Input**: Original price, increase percentage
329/// - **Output**: `f64` (increased price)
330/// - **Purpose**: Calculates the price after a percentage increase
331/// - **Formula**: new_price = original × (1 + percentage/100)
332///
333/// # Probability Functions
334///
335/// ## h_permutations Function
336/// - **Signature**: `h_permutations<T, S>(total: &T, select: &S) -> u64`
337/// - **Input**: Total items (n), items to select (r)
338/// - **Output**: `u64` (number of permutations)
339/// - **Purpose**: Calculates the number of ways to arrange r items from n items
340/// - **Formula**: P(n,r) = n! / (n-r)!
341///
342/// ## h_combinations Function
343/// - **Signature**: `h_combinations<T, S>(total: &T, select: &S) -> u64`
344/// - **Input**: Total items (n), items to select (r)
345/// - **Output**: `u64` (number of combinations)
346/// - **Purpose**: Calculates the number of ways to choose r items from n items (order doesn't matter)
347/// - **Formula**: C(n,r) = n! / (r! × (n-r)!)
348///
349/// # Statistics Functions
350///
351/// ## Average Trait
352/// - **Function**: `h_average()`
353/// - **Input**: Slice of numeric values `&[T]`
354/// - **Output**: `f64`
355/// - **Purpose**: Calculates the arithmetic mean of a set of numbers
356/// - **Returns 0.0**: If slice is empty
357///
358/// ## Median Trait
359/// - **Function**: `h_median()`
360/// - **Input**: Slice of numeric values `&[T]`
361/// - **Output**: `f64`
362/// - **Purpose**: Calculates the median (middle value) of a set of numbers
363/// - **Behavior**: For even-length sets, returns the average of two middle values
364/// - **Returns 0.0**: If slice is empty
365///
366/// ## Sum Trait
367/// - **Function**: `h_sum()`
368/// - **Input**: Slice or Vec of numeric values
369/// - **Output**: `f64`
370/// - **Purpose**: Calculates the sum of all elements
371///
372/// ## Variance Trait
373/// - **Function**: `h_variance()`
374/// - **Input**: Slice of numeric values `&[T]`
375/// - **Output**: `f64`
376/// - **Purpose**: Calculates the variance (range) between max and min values
377/// - **Formula**: max - min
378/// - **Returns 0.0**: If less than 2 elements
379///
380/// ## ModusMult Trait
381/// - **Function**: `h_modus_mult()`
382/// - **Input**: Slice of numeric values `&[T]`
383/// - **Output**: `Vec<f64>` (all modes)
384/// - **Purpose**: Finds all values that appear most frequently
385/// - **Returns empty**: If no value appears more than once
386///
387/// # Linear Algebra Functions
388///
389/// ## h_hadamard_product Function (private)
390/// - **Signature**: `h_hadamard_product<I, T>(vec1: &[I], vec2: &[T]) -> Vec<f64>`
391/// - **Input**: Two vectors of equal length
392/// - **Output**: `Vec<f64>` (element-wise product)
393/// - **Purpose**: Computes element-wise multiplication of two vectors
394/// - **Panics**: If vectors have different lengths
395///
396/// ## h_2d_dot_product Function
397/// - **Signature**: `h_2d_dot_product<A, B>(vec1: &Vec<A>, vec2: &Vec<B>) -> f64`
398/// - **Input**: Two vectors of equal length
399/// - **Output**: `f64` (scalar)
400/// - **Purpose**: Calculates the dot product of two vectors
401/// - **Formula**: vec1 · vec2 = Σ(a_i × b_i)
402/// - **Panics**: If vectors have different lengths
403///
404/// # Collection/Functionality Traits
405///
406/// ## HashMapValuesToHashSet Trait
407/// - **Function**: `h_hashmap_values_to_hashset()`
408/// - **Input**: HashMap with cloneable values
409/// - **Output**: `HashSet<V>` containing all unique values
410/// - **Purpose**: Extracts all values from a HashMap into a HashSet
411///
412/// ## HashMapKeysToHashSet Trait
413/// - **Function**: `h_hashmap_keys_to_hashset()`
414/// - **Input**: HashMap with cloneable keys
415/// - **Output**: `HashSet<K>` containing all unique keys
416/// - **Purpose**: Extracts all keys from a HashMap into a HashSet
417///
418/// ## ListToHashMap Trait
419/// - **Function**: `h_list_to_hashmap(&self, values: &[V])`
420/// - **Input**: Keys (Vec or slice), values slice
421/// - **Output**: `HashMap<K, V>`
422/// - **Purpose**: Creates a HashMap from a list of keys and a list of values
423/// - **Panics**: If keys and values have different lengths, or if keys contain duplicates
424///
425/// ## ListToHashSet Trait
426/// - **Function**: `h_list_to_hashset()`
427/// - **Input**: Slice or Vec of values
428/// - **Output**: `HashSet<T>` with unique elements
429/// - **Purpose**: Converts a list to a HashSet, removing duplicates
430///
431/// ## Tof64 Trait
432/// - **Function**: `h_f64()`
433/// - **Input**: Any numeric type implementing `Copy + Into<f64>`
434/// - **Output**: `f64`
435/// - **Purpose**: Generic conversion to f64 for any numeric type
436///
437/// ## Toi32 Trait
438/// - **Function**: `h_i32()`
439/// - **Input**: Any numeric type implementing `Copy + Into<i32>`
440/// - **Output**: `i32`
441/// - **Purpose**: Generic conversion to i32 for any numeric type
442///
443/// ## ToVecf64 Trait
444/// - **Function**: `h_to_vec_f64()`
445/// - **Input**: Vec<T> where T implements `Copy + Into<f64>`
446/// - **Output**: `Vec<f64>`
447/// - **Purpose**: Converts a vector of any numeric type to a vector of f64
448///
449/// ## ToVeci32 Trait
450/// - **Function**: `h_to_vec_i32()`
451/// - **Input**: Vec<T> where T implements `Copy + Into<i32>`
452/// - **Output**: `Vec<i32>`
453/// - **Purpose**: Converts a vector of any numeric type to a vector of i32
454///
455/// # Terminal Input Functions
456///
457/// ## h_input_data_single_f64 Function
458/// - **Signature**: `h_input_data_single_f64(length: i32) -> Vec<f64>`
459/// - **Input**:
460/// - `length`: Number of data points to collect (if ≤ 0, collects until user enters "<<")
461/// - **Output**: `Vec<f64>` containing the input numbers
462/// - **Purpose**: Prompts user to enter f64 numbers one at a time
463/// - **Behavior**:
464/// - Skips zero values automatically
465/// - If length > 0, collects exactly that many non-zero numbers
466/// - If length ≤ 0, continues until user enters "<<"
467///
468/// ## h_input_data_single_i32 Function
469/// - **Signature**: `h_input_data_single_i32(length: i32) -> Vec<i32>`
470/// - **Input**:
471/// - `length`: Number of data points to collect (if ≤ 0, collects until user enters "<<")
472/// - **Output**: `Vec<i32>` containing the input integers
473/// - **Purpose**: Prompts user to enter i32 integers one at a time
474/// - **Behavior**:
475/// - Skips zero values automatically
476/// - If length > 0, collects exactly that many non-zero numbers
477/// - If length ≤ 0, continues until user enters "<<"
478///
479/// ## InputType Enum
480/// - **Variants**: `Lowercase`, `Uppercase`, `Letters`, `Integer`
481/// - **Purpose**: Specifies validation requirements for input strings
482/// - **Usage**: Used with the ValidateInput trait to validate string content
483///
484/// ## ValidateInput Trait
485/// - **Function**: `h_validate_input(input_requirements: InputType) -> Result<(), String>`
486/// - **Input**:
487/// - Self: String to validate
488/// - `input_requirements`: InputType enum specifying validation rules
489/// - **Output**: `Result<(), String>`
490/// - Ok(()) if validation passes
491/// - Err(String) with error message listing unexpected characters if validation fails
492/// - **Purpose**: Validates string input against specific requirements
493/// - **Validation Options**:
494/// - `Lowercase`: Only a-z allowed
495/// - `Uppercase`: Only A-Z allowed
496/// - `Letters`: Both uppercase and lowercase letters allowed (A-Z, a-z)
497/// - `Integer`: Only digits 0-9 allowed (must be parseable as i32)
498/// - **Returns Error**: If input is empty or contains invalid characters
499///
500///
501
502
503
504
505
506
507
508
509pub mod prelude;
510
511pub mod algebra;
512pub mod conversions;
513pub mod core_math;
514pub mod finance;
515pub mod functionality;
516pub mod geometry;
517pub mod linear_algebra;
518pub mod probability;
519pub mod statisticss;
520pub mod terminal_input;
521
522
523
524