fermat

Crates.iofermat
lib.rsfermat
version0.1.1
created_at2025-11-06 07:23:46.682803+00
updated_at2025-11-06 07:23:46.682803+00
descriptionA collection of math algorithms
homepage
repositoryhttps://github.com/bankusy/fermat
max_upload_size
id1919214
size119,768
bankusy (bankusy)

documentation

README

Fermat

A comprehensive Rust library implementing all mathematical concepts from elementary to high school level. Every function includes detailed formula explanations and educational documentation.

License: MIT OR Apache-2.0 Rust

Overview

Fermat is an educational Rust library that organizes mathematical concepts into well-documented, easy-to-understand code. Perfect for students, educators, and developers who need reliable mathematical algorithms with comprehensive explanations.

Key Features

  • 📚 Comprehensive Coverage: All math concepts from elementary to high school
  • 📖 Detailed Documentation: Every function includes formulas and explanations
  • ✅ Well Tested: 144+ test cases ensuring correctness
  • 🎯 Educational Focus: Designed to help people learn mathematics
  • 🔒 Type Safe: Built with Rust's type system for safety and correctness

Modules

1. Arithmetic (arithmetic)

Basic operations, fractions, decimals, GCD/LCM, percentages, complex numbers, and more.

2. Algebra (algebra)

Polynomials, equations, functions, logarithms, sets, transformations, and more.

3. Geometry (geometry)

Shapes, trigonometry, vectors, plane geometry, area, volume, and surface area calculations.

4. Calculus (calculus)

Sequences, limits, derivatives, integrals, and optimization problems.

5. Statistics (statistics)

Probability, distributions, statistical measures, and regression analysis.

Installation

Add this to your Cargo.toml:

[dependencies]
fermat = "0.1.0"

Or install from Git (if available):

[dependencies]
fermat = { git = "https://github.com/0xE8225/fermat" }

Quick Start

use fermat::*;

fn main() {
    // Arithmetic
    assert_eq!(add(3.0, 4.0), 7.0);
    assert_eq!(gcd(12, 18), 6);
    assert_eq!(lcm(12, 18), 36);

    // Geometry
    let circle_area = area_circle(5.0); // π × 5²
    let triangle_area = area_triangle(10.0, 5.0); // (10 × 5) / 2

    // Algebra
    let f = |x| x * x + 2.0 * x + 1.0;
    let result = evaluate_function(f, 3.0); // f(3) = 16

    // Calculus
    let coeffs = vec![1.0, -5.0, 6.0]; // x² - 5x + 6
    let deriv = derivative_polynomial(&coeffs); // 2x - 5

    // Statistics
    let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let mean = mean(&data); // 3.0
    let std_dev = standard_deviation(&data);
}

Examples

Arithmetic

use fermat::arithmetic::*;

// Basic operations
let sum = add(10.0, 5.0);           // 15.0
let product = multiply(3.0, 4.0);   // 12.0

// GCD and LCM
let g = gcd(48, 18);                // 6
let l = lcm(12, 18);                // 36

// Fractions
let f1 = Fraction::new(1, 2);
let f2 = Fraction::new(1, 3);
let sum_frac = add_fractions(&f1, &f2); // 5/6

// Percentages
let discount = discount_percentage(100.0, 80.0); // 20.0%
let pct_change = percentage_change(100.0, 120.0); // 20.0%

Algebra

use fermat::algebra::*;

// Evaluate polynomial: f(x) = x² - 5x + 6
let coeffs = vec![1.0, -5.0, 6.0];
let value = evaluate_polynomial(&coeffs, 2.0); // f(2) = 0

// Polynomial operations
let p1 = vec![1.0, 1.0];  // x + 1
let p2 = vec![1.0, 2.0];  // x + 2
let product = multiply_polynomials(&p1, &p2); // x² + 3x + 2

// Logarithms
let log_val = log_base(2.0, 8.0); // log₂(8) ≈ 3.0

// Functions
let f = |x| x * 2.0;
let g = |x| x + 1.0;
let composed = compose_functions(f, g, 1.0); // (f∘g)(1) = 4.0

Geometry

use fermat::geometry::*;

// Areas and perimeters
let circle_area = area_circle(5.0);         // π × 25
let rect_area = area_rectangle(4.0, 5.0);    // 20.0
let triangle_area = area_triangle(10.0, 6.0); // 30.0

// 3D shapes
let volume = volume_sphere(3.0);            // (4/3) × π × 27
let surface = surface_area_cylinder(2.0, 5.0); // 2Ï€r(r + h)

// Trigonometry
let sin_val = sin_ratio(3.0, 5.0);         // sin(θ) = 0.6
let side = law_of_sines_find_side(5.0, std::f64::consts::PI/6.0, std::f64::consts::PI/3.0);

// Vectors
let v1 = Vector2D::new(3.0, 4.0);
let v2 = Vector2D::new(1.0, 2.0);
let sum = v1 + v2;                          // Vector2D { x: 4.0, y: 6.0 }
let dot = dot_product_2d(&v1, &v2);        // 11.0

Calculus

use fermat::calculus::*;

// Derivatives
let coeffs = vec![1.0, -5.0, 6.0]; // x² - 5x + 6
let deriv_coeffs = derivative_polynomial(&coeffs); // [2.0, -5.0]
let deriv_at_2 = derivative_at(&coeffs, 2.0); // f'(2) = -1

// Integrals
let integral_coeffs = integrate_polynomial(&coeffs); // (x³/3 - 5x²/2 + 6x)
let area = definite_integral(&coeffs, 0.0, 2.0, 100); // Numerical integration

// Limits and sequences
let fib = fibonacci(10); // 55
let arith_sum = arithmetic_sequence_sum(1.0, 1.0, 100); // Sum of 1..100
let geo_sum = geometric_sequence_sum(1.0, 0.5, 10); // Sum of geometric series

Statistics

use fermat::statistics::*;

// Descriptive statistics
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let avg = mean(&data);                      // 3.5
let med = median(&data);                   // 3.5
let modes = mode(&data);                   // All values
let var = variance(&data);                 // Sample variance
let std = standard_deviation(&data);       // Standard deviation

// Probability
let prob = probability(3, 6);              // 0.5
let perm = permutation(5, 3);             // 60
let comb = combination(5, 3);              // 10

// Conditional probability
let p_a = 0.4;
let p_b = 0.3;
let p_a_and_b = 0.1;
let p_a_given_b = conditional_probability(p_a_and_b, p_b);

// Bayes' theorem
let p_disease_given_positive = bayes_theorem(
    0.95,  // P(positive|disease)
    0.01,  // P(disease)
    0.058  // P(positive)
);

// Regression
let x = vec![1.0, 2.0, 3.0, 4.0];
let y = vec![2.0, 4.0, 6.0, 8.0];
let (slope, intercept) = linear_regression(&x, &y); // y = 2x + 0
let r2 = r_squared(&x, &y);                // R² = 1.0 (perfect fit)

Project Structure

fermat/
├── src/
│   ├── lib.rs                  # Main library file
│   ├── arithmetic/             # Arithmetic operations
│   │   ├── mod.rs
│   │   ├── basic_ops.rs        # Basic operations
│   │   ├── fractions.rs        # Fraction operations
│   │   ├── decimals.rs         # Decimal operations
│   │   ├── gcf_lcm.rs          # GCD and LCM
│   │   ├── percentages.rs      # Percentages and ratios
│   │   └── complex.rs          # Complex numbers
│   ├── algebra/                 # Algebraic operations
│   │   ├── mod.rs
│   │   ├── equations.rs        # Equation solving
│   │   ├── functions.rs        # Function operations
│   │   ├── polynomials.rs      # Polynomial operations
│   │   └── logarithms.rs       # Logarithmic functions
│   ├── geometry/               # Geometric calculations
│   │   ├── mod.rs
│   │   ├── shapes.rs           # Area, volume, surface area
│   │   ├── trigonometry.rs     # Trigonometry
│   │   ├── vectors.rs          # Vector operations
│   │   └── plane_geometry.rs   # Plane geometry properties
│   ├── calculus/               # Calculus operations
│   │   ├── mod.rs
│   │   ├── limits.rs           # Limits and sequences
│   │   ├── derivatives.rs      # Differentiation
│   │   └── integrals.rs        # Integration
│   └── statistics/             # Statistical operations
│       ├── mod.rs
│       ├── probability.rs      # Probability and combinatorics
│       ├── distributions.rs    # Statistical distributions
│       └── regression.rs       # Regression analysis
├── Cargo.toml
├── README.md
└── prd.md                      # Product Requirements Document

Documentation

Each function includes:

  • Formula: Mathematical formula used
  • Description: Explanation of the concept
  • Parameters: Input parameters with descriptions
  • Returns: What the function returns
  • Examples: Usage examples in code

View full documentation:

cargo doc --open

Testing

Run all tests:

cargo test

Run tests for a specific module:

cargo test --lib arithmetic
cargo test --lib algebra
cargo test --lib geometry
cargo test --lib calculus
cargo test --lib statistics

Current test coverage: 144 tests (all passing ✅)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Guidelines

  • Follow Rust coding conventions
  • Add tests for new functions
  • Include detailed documentation with formulas
  • Ensure all tests pass before submitting

Educational Use

This library is designed to be educational. Each function serves as a learning resource with:

  • Clear explanations of mathematical concepts
  • Step-by-step formula breakdowns
  • Real-world examples where applicable

Perfect for:

  • Students learning mathematics
  • Teachers creating educational materials
  • Developers needing reliable math functions
  • Anyone wanting to understand math through code

License

Licensed under either of

at your option.

Repository

GitHub Repository

Acknowledgments

  • All mathematical formulas are based on standard elementary to high school curricula
  • Inspired by the need for well-documented, educational mathematical code

Note: This library is designed for educational purposes. For production use requiring high-performance mathematical computations, consider libraries like nalgebra or ndarray for specific domains.

Commit count: 0

cargo fmt