Statistical and Vector Operations Library
This Rust library provides statistical operations and vector algebra functionalities, working with both f64 and i64 data types. It also includes methods for calculating common statistical measures (mean, median, mode, variance, standard deviation) and operations on vectors (addition, dot product, magnitude).
Modules
1. Mean and Statistical Functions
The Mean struct includes statistical functions for both f64 and i64 types:
-
Mean:
mean_f: Calculates the mean of a list off64numbers.mean_i: Calculates the mean of a list ofi64numbers.
-
Weighted Average:
weighted_average_f: Calculates the weighted average forf64numbers.weighted_average_i: Calculates the weighted average fori64numbers.
-
Median:
median_f: Finds the median forf64numbers.median_i: Finds the median fori64numbers.
-
Mode:
mode: Calculates the mode for anyHash,Ord, andClonetypes.mode_f64: Specialized mode calculation forf64numbers.
-
Variance and Standard Deviation:
variance_f: Computes variance forf64numbers.variance_i: Computes variance fori64numbers.standard_deviation_f: Computes standard deviation forf64numbers.standard_deviation_i: Computes standard deviation fori64numbers.
2. CalculusError for Error Calculation
The CalculusError struct provides error calculation methods for both f64 and i64 values:
-
Absolute Error:
absolute_error_f: Calculates the absolute error forf64.absolute_error_i: Calculates the absolute error fori64.
-
Relative Error:
relative_error_f: Calculates the relative error forf64.relative_error_i: Calculates the relative error fori64.
3. Vector Operations
The Vector struct is implemented for both i64 and f64 types and supports basic vector algebra:
-
Creation:
new: Initializes a new vector withi64orf64components.
-
Magnitude:
magnitude: Computes the magnitude (Euclidean length) of the vector.
-
Addition:
add: Adds twoi64vectors (mutates the first vector).add_f: Adds twof64vectors (mutates the first vector).add_i: Adds ani64vector to anf64vector (mutates thef64vector).
-
Dot Product:
dot_product: Computes the dot product of twoi64vectors.dot_product_f: Computes the dot product of twof64vectors.dot_product_i: Computes the dot product between anf64andi64vector.
-
Scalar Multiplication:
- Overloaded multiplication (
Multrait) for bothi64andf64scalar multiplication.
- Overloaded multiplication (
4. VectorError for Error Handling
The VectorError enum handles errors during vector operations:
DimensionMismatch: Error raised when vectors of different dimensions are used in an operation.
Tests
The library is fully tested with unit tests, covering:
- Calculation of mean, weighted average, median, mode, variance, and standard deviation for both
f64andi64. - Error handling for mismatched vector dimensions.
- Vector addition, scalar multiplication, and dot product operations.
- Magnitude computation for vectors.
How to Use
- Add to your Cargo.toml:
[]
= "0.1.6"
- Example Usage:
use Mean;
use Vector;
// Statistical calculations
let data = vec!;
let mean = mean_f;
println!;
// Vector operations
let vec1 = new;
let vec2 = new;
let dot_product = vec1.dot_product.unwrap;
println!;
This library offers efficient and flexible handling of common statistical and vector-based operations, making it a useful tool for mathematical computation in Rust projects. Still, it needs much work and improvements such as a more generic approach, so feel free to contribute.