#algebra #operation #space #module #field #ring #rings

nightly harness-algebra

The library for algebraic structures

10 releases (5 breaking)

new 0.6.0 May 23, 2025
0.5.0 May 21, 2025
0.4.0 May 18, 2025
0.3.0 May 17, 2025
0.1.1 Apr 29, 2025

#160 in Math

Download history 267/week @ 2025-04-25 328/week @ 2025-05-02 302/week @ 2025-05-09 414/week @ 2025-05-16

1,311 downloads per month
Used in harness-space

MIT license

195KB
3K SLoC

Algebra Crate

A Rust library providing algebraic structures and operations, with a focus on modular arithmetic and abstract algebra concepts.

Crates.io - harness-algebra docs.rs - harness-algebra License: AGPL v3

Features

  • Modular Arithmetic: Create custom modular number types with the modular! macro
  • Abstract Algebra: Implementations of fundamental algebraic structures:
    • Groups (both Abelian and Non-Abelian)
    • Rings
    • Fields
    • Modules
    • Vector Spaces

Usage

Add this to your Cargo.toml:

[dependencies]
harness-algebra = "*"

Modular Arithmetic

Create a new modular number type using the modular! macro:

use harness_algebra::{group::Group, modular, ring::Ring};

// Create a type for numbers modulo 7
modular!(Mod7, u32, 7);

let a = Mod7::new(3);
let b = Mod7::new(5);

// Addition: 3 + 5 = 8 ≡ 1 (mod 7)
let sum = a + b;
assert_eq!(sum.value(), 1);

// Multiplication: 3 * 5 = 15 ≡ 1 (mod 7)
let product = a * b;
assert_eq!(product.value(), 1);

Vector Spaces

Create and manipulate vectors over any field, such as the finite field of integers modulo 7:

use harness_algebra::{vector::{Vector, VectorSpace}, ring::Field, modular};

modular!(Mod7, u32, 7);
prime_field!(Mod7);
impl Field for Mod7 {
    fn multiplicative_inverse(&self) -> Self {
        todo!("Implement multiplicative inverse for Mod7")
    }
}

let v1 = Vector::<3, Mod7>([Mod7::new(1), Mod7::new(2), Mod7::new(3)]);
let v2 = Vector::<3, Mod7>([Mod7::new(4), Mod7::new(5), Mod7::new(6)]);
let sum = v1 + v2;

Documentation

The complete API documentation is available on docs.rs.

Modules

  • arithmetic: Basic arithmetic traits and operations
  • group: Group theory abstractions and implementations
  • ring: Ring theory abstractions and implementations
  • module: Module theory abstractions and implementations
  • vector: Vector space abstractions and implementations
  • modular: Modular arithmetic abstractions and implementations

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.

Examples

use algebra::{Group, Ring};

modular!(Mod7, u32, 7);

// Group operations
let a = Mod7::new(3);
let inverse = a.inverse();  // 4 (mod 7)
let identity = Mod7::identity();  // 0 (mod 7)

// Ring operations
let one = Mod7::one();  // 1 (mod 7)
let zero = Mod7::zero();  // 0 (mod 7)

Dependencies

~150KB