Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mathevil

A comprehensive Rust math library providing numpy-style mathematical functions for all primitive numeric types.

Features

Trigonometric Functions

  • sin, cos, tan — Standard trigonometric functions
  • arcsin/asin, arccos/acos, arctan/atan — Inverse trigonometric functions
  • hypot — Hypotenuse (sqrt(x² + y²))
  • arctan2/atan2 — Four-quadrant arctangent
  • degrees/rad2deg, radians/deg2rad — Angle conversions
  • unwrap — Unwrap phase angles

Hyperbolic Functions

  • sinh, cosh, tanh — Hyperbolic functions
  • arcsinh/asinh, arccosh/acosh, arctanh/atanh — Inverse hyperbolic functions

Rounding

  • round/around — Round to nearest (ties away from zero)
  • rint — Round to nearest (ties to even, banker's rounding)
  • fix, trunc — Round toward zero
  • floor — Round toward negative infinity
  • ceil — Round toward positive infinity

Sums, Products, Differences

  • sum, prod — Sum and product of elements
  • nansum, nanprod — NaN-aware sum and product
  • cumsum/cumulative_sum, cumprod/cumulative_prod — Cumulative operations
  • nancumsum, nancumprod — NaN-aware cumulative operations
  • diff — N-th discrete difference
  • ediff1d — Consecutive differences with padding
  • gradient — Numerical gradient
  • trapezoid — Trapezoidal integration

Exponents and Logarithms

  • exp, expm1, exp2 — Exponential functions
  • log, log10, log2, log1p — Logarithmic functions
  • logaddexp, logaddexp2 — Log-sum-exp (overflow-safe)

Arithmetic Operations

  • add, subtract, multiply, divide — Element-wise arithmetic
  • power/pow, float_power — Element-wise exponentiation
  • floor_divide, true_divide — Division variants
  • fmod/mod_fn/remainder — Remainder operations
  • modf — Fractional and integral parts
  • divmod — Quotient and remainder
  • reciprocal, positive, negative — Unary operations

Floating Point Routines

  • signbit — Sign bit inspection
  • copysign — Sign copying
  • frexp — Mantissa/exponent decomposition
  • ldexp — Multiply by power of two
  • nextafter — Next representable value
  • spacing — Distance to adjacent value

Extrema Finding

  • max/amax, min/amin — Array maximum/minimum
  • maximum, minimum — Element-wise maximum/minimum
  • fmax, fmin — NaN-aware element-wise operations
  • nanmax, nanmin — NaN-tolerant array operations

Handling Complex Numbers

  • angle — Phase angle
  • real, imag — Real/imaginary parts
  • conj/conjugate — Complex conjugate

Miscellaneous

  • convolve — Discrete linear convolution
  • clip — Limit values to a range
  • sqrt, cbrt — Square root and cube root
  • square — Element-wise square
  • absolute/fabs — Absolute value
  • sign — Sign indication
  • heaviside — Heaviside step function
  • nan_to_num — Replace NaN/Inf with specified values
  • real_if_close — Return real parts if imaginary parts are near zero
  • interp — Linear interpolation
  • sinc — Normalized sinc function
  • i0 — Modified Bessel function of the first kind, order 0

Statistics & Rational

  • gcd, lcm — Greatest common divisor and least common multiple
  • gcd_ew, lcm_ew — Element-wise GCD/LCM
  • mean, median, mode — Statistical measures

Usage

Add to your Cargo.toml:

[dependencies]
mathevil = "0.2"

Example

use mathevil::*;

// Trigonometric
let s = sin([0.0, std::f64::consts::FRAC_PI_2]);
assert_eq!(s, vec![0.0, 1.0]);

// Arithmetic
let r = add([1.0, 2.0, 3.0], [4.0, 5.0, 6.0]);
assert_eq!(r, vec![5.0, 7.0, 9.0]);

// Rounding
let r = floor([1.7, -1.2, 3.0]);
assert_eq!(r, vec![1.0, -2.0, 3.0]);

// Exponents
let e = exp([0.0, 1.0, 2.0]);

// Extrema
let m = max([1.0, 5.0, 3.0]);
assert_eq!(m, Some(5.0));

// Interpolation
let result = interp([0.5, 1.5], [0.0, 1.0, 2.0], [0.0, 2.0, 4.0], None, None);
assert_eq!(result, vec![1.0, 3.0]);

Design

All functions are element-wise and work on any iterator of Numeric types (all Rust primitive numeric types: i8i128, u8u128, f32, f64, isize, usize). Results are returned as Vec<f64>.

License

MIT

About

useful math functions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages