Statistical, Vector, and Matrix Operations Library - Datas 
This Rust library provides statistical, vector algebra, and matrix operations. It includes methods for calculating statistical measures (mean, median, variance, etc.), operations on vectors (addition, dot product, scalar multiplication), and matrix manipulation (addition, multiplication, row swapping, etc.).
Modules
1. Statistical Functions
The Mean struct provides statistical functions for both f64 and i64 types and its functions are self-explanatory:
-
Mean:
mean_fmean_i
-
Weighted Average:
weighted_average_fweighted_average_i
-
Median:
median_fmedian_i
-
Mode(here i tried some generics usage but i still need some clarification):
mode: Calculates the mode for anyHash,Ord, andClonetypes.mode_f64: Specialized mode calculation forf64numbers.
-
Variance and Standard Deviation:
variance_fvariance_istandard_deviation_fstandard_deviation_i
2. CalculusError for Error Calculation
The CalculusError struct provides error calculation methods for both f64 and i64 values:
-
Absolute Error:
absolute_error_fabsolute_error_i
-
Relative Error:
relative_error_frelative_error_i
3. Vector Operations
The Vector struct supports basic vector algebra for both i64 and f64 types:
-
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. Matrix Operations
The Matrix struct provides support for matrix operations with i64 data types:
-
Creation:
new: Initializes a matrix from aVec<Vec<i64>>. Returns an error if the rows have inconsistent column sizes.
-
Addition:
add: Adds two matrices element-wise. Returns an error if the matrices have mismatched dimensions.
-
Row Swapping:
swap_row: Swaps two rows of the matrix. Returns an error if the row indices are out of bounds.
-
Scalar Multiplication:
scalar_multiplication: Multiplies each element of the matrix by a scalar.
-
Matrix Multiplication:
matrix_multiplication: Multiplies two matrices. Returns an error if the number of columns in the first matrix does not match the number of rows in the second matrix.
5. MatrixError for Error Handling
The MatrixError enum handles errors specific to matrix operations:
InconsistentColumnSizes: Raised when matrix rows have different column sizes.MultiplicationDimensionMismatch: Raised when matrix dimensions are incompatible for multiplication.DimensionMismatch: Raised when matrices have different dimensions during addition.RowOutOfBound: Raised when trying to swap rows that don't exist.
6. VectorError for Error Handling
The VectorError enum handles errors during vector operations:
DimensionMismatch: Raised when vectors of different dimensions are used in an operation.
Tests
The library includes comprehensive unit tests, covering:
- Statistical functions: mean, weighted average, median, mode, variance, and standard deviation for
f64andi64. - Vector operations: addition, scalar multiplication, dot product, and magnitude calculation.
- Matrix operations: addition, scalar multiplication, matrix multiplication, row swapping, and error handling for dimension mismatches.
How to Use
- Add to your Cargo.toml:
[]
= "0.1.8"
- Example Usage:
use Mean;
use Vector;
use Matrix;
// 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!;
// Matrix operations
let matrix1 = new.unwrap;
let matrix2 = new.unwrap;
let product = matrix1.matrix_multiplication.unwrap;
println!;
This library offers efficient and flexible handling of common statistical, vector-based, and matrix-based operations, making it a powerful tool for mathematical computation in Rust projects. Contributions are welcome, as I aim to expand functionality and improve generic handling.