mathrc 0.2.1

Rust Mathematics Library
Documentation

MathRC

CI Crates.io Docs.rs Downloads Rust

MathRC is a lightweight Rust mathematics library that provides basic mathematical utilities, fraction handling, functional analysis, and a simple arithmetic expression calculator.

Installation

Add MathRC to your Cargo.toml:

[dependencies]

mathrc = "0.2.0"

Or, if you want to use the Git repository directly:

[dependencies]

mathrc = { git = "https://github.com/as7ar/mathrc" }

Usage

Basic arithmetic

use mathrc::math::Math;

fn main() {
    let sum = Math::add(10, 5);
    let difference = Math::sub(10, 5);
    let product = Math::mul(10, 5);
    let quotient = Math::div(10, 5);

    println!("sum: {}", sum);
    println!("difference: {}", difference);
    println!("product: {}", product);
    println!("quotient: {}", quotient);
}

Mathematical constants

use mathrc::math::Math;

fn main() {
    println!("PI: {}", Math::PI);
    println!("E: {}", Math::E);
    println!("INF: {}", Math::INF);
}

Square root and absolute value

use mathrc::math::Math;

fn main() {
    let result = Math::sqrt(16);

    match result {
        Some(value) => println!("sqrt: {}", value),
        None => println!("cannot calculate square root of a negative number"),
    }

    let abs = Math::abs(-10);
    println!("absolute value: {}", abs);
}

Power and logarithm

use mathrc::math::Math;

fn main() {
    let powered = Math::pow(2, 3);
    let logged = Math::log(8, 2);

    println!("2^3 = {}", powered);

    if let Some(value) = logged {
        println!("log base 2 of 8 = {}", value);
    }
}

GCD and LCM

use mathrc::math::Math;

fn main() {
    let gcd = Math::gcd(12, 18);
    let lcm = Math::lcm(12, 18);

    println!("gcd: {}", gcd);
    println!("lcm: {}", lcm);
}

Factorial

use mathrc::math::Math;

fn main() {
    let math = Math;
    let result = math.factorial(5);
    println!("5! = {}", result);
}

Calculator

use mathrc::calculator::Calculator;

fn main() {
    let result = Calculator::calc("3 + 2 * 4");

    match result {
        Some(value) => println!("result: {}", value),
        None => println!("invalid expression"),
    }
}

Supported calculator operators:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
(...) Parentheses

The tokenizer also supports the following LaTeX-style operators:

Input Description
\times Multiplication
\div Division

Example:

use mathrc::calculator::Calculator;

fn main() {
    let result = Calculator::calc("6 \\div 2");
    assert_eq!(result, Some(3.0));
}

Decimal to fraction

use mathrc::calculator::Calculator;

fn main() {
    let fraction = Calculator::dec_to_frac(0.5);
    println!("{}", fraction);
}

Output:

1/2

Functional Analysis

The Func module provides numerical calculus operations including derivatives and integrals.

use mathrc::function::function::Func;

fn main() {
    let f = Func::new(|x| x * x);
    
    // Evaluate function
    println!("f(5) = {}", f.call(5));
    
    // Calculate derivative
    let df = f.derivative();
    println!("f'(5) = {}", df.call(5));
    
    // Calculate integral
    let integral = Func::new(|x| x).integral();
    println!("∫x dx from 0 to 10 = {}", integral.call(10.0));
}

Status

MathRC is currently in early development. APIs may change as the library grows.

License

This project is licensed under the GPL-3.0 license.