
Nœther
Nœther provides traits and blanket implementations for algebraic structures, from basic ones like magmas to more complex ones like fields, vector spaces, and tensors. It defines a comprehensive hierarchy of mathematical abstractions, organized in a modular structure that follows the natural progression of abstract algebra. It leans heavily on the basic traits available in std::ops and num_traits while adding rich mathematical documentation with formal definitions.
Table of Contents
- Background
- Features
- Installation
- Usage
- Core Concepts
- Hierarchy of Algebraic Structures
- Operator Traits for Algebraic Structures
- API Overview
- Advanced Usage
- Performance
- Roadmap
- Contributing
- License
Background
Named after Emmy Nœther, a pioneering mathematician in abstract algebra, this library aims to bridge the gap between abstract mathematics and practical programming in Rust. It enables developers to work with mathematical concepts in a type-safe, efficient, and expressive manner.
The goal is to provide a common interface for working with algebraic structures in Rust. Interestingly, these traits can be used to categorize implementations of various structs based on the properties they satisfy, and be applied in most cases for anything from scalar values to n-dimensional arrays.
Inspirations
Nœther draws inspiration from several existing libraries and projects in the field of computational algebra:
-
simba: A Rust crate for SIMD-accelerated algebra.
-
alga: A Rust library for abstract algebra, providing solid mathematical abstractions for algebra-focused applications. alga defines and organizes basic building blocks of general algebraic structures through trait inheritance.
-
algebra: A Rust library for abstract algebra that organizes a wide range of structures into a logically consistent framework. It aims to create composable libraries and APIs based on algebraic classifications.
These libraries demonstrate the power and utility of representing algebraic structures in programming languages. Nœther builds upon their ideas, aiming to provide a comprehensive and ergonomic framework for working with algebraic structures in Rust.
Other notable inspirations from different programming languages include:
- Haskell's Numeric Prelude and Edward A. Kmett's algebra package
- Agda's algebra module
- Idris' algebra module
- Scala's spire
Nœther also draws insights from academic papers in the field:
- The Scratchpad II Type System: Domains and Subdomains
- Fundamental Algebraic Concepts in Concept-Enabled C++
Nœther aims to bring the best ideas from these libraries and research to the Rust ecosystem, while taking advantage of Rust's unique features like zero-cost abstractions and powerful type system.
Features
- Traits for a wide range of algebraic structures (e.g., Magma, Semigroup, Monoid, Group, Ring, Field)
- Marker traits for important algebraic properties (e.g., Associativity, Commutativity)
- Blanket implementations to reduce boilerplate code
- Support for both built-in and custom types
- Zero-cost abstractions leveraging Rust's type system
- Modular organization by mathematical domain
- Comprehensive UTF-8 mathematical documentation with formal definitions
- Type-level dimension handling for compile-time dimensional analysis
- Linear algebra abstractions including vector spaces, modules, and tensors
- Advanced structures like bilinear forms and tensor products
Installation
Add this to your Cargo.toml:
[]
= "0.3.0"
Usage
Here is a rough example of Z₅ (integers modulo 5) using Nœther:
use ;
use ;
;
This example shows how to construct a well factored finite field using Nœther, leveraging Rust's native operators and traits.
Core Concepts
- Algebraic Structures: Traits representing mathematical structures with specific properties and operations.
- Marker Traits: Traits like
AssociativeandCommutativefor compile-time property checks. - Blanket Implementations: Automatic implementations of higher-level traits based on more fundamental ones.
- Zero-Cost Abstractions: Leveraging Rust's type system for efficiency without runtime overhead.
- Extensibility: The library is designed to be easily extended with new types and structures.
- Type Safety: Ensuring operations maintain closure within the same type and catching errors at compile-time.
Hierarchy of Algebraic Structures
┌─────┐
│ Set │
└──┬──┘
│
┌──▼──┐
│Magma│
└──┬──┘
┌────────────────┼────────────────┐
│ │ │
┌─────▼─────┐ ┌─────▼─────┐ ┌─────▼─────┐
│Quasigroup │ │ Semigroup │ │Semilattice│
└─────┬─────┘ └─────┬─────┘ └───────────┘
│ │
┌───▼───┐ ┌───▼───┐
│ Loop │ │Monoid │
└───┬───┘ └───┬───┘
│ │
└────────┐ ┌─────┘
│ │
┌──▼─▼──┐
│ Group │
└───┬───┘
│
┌────────▼────────┐
│ Abelian Group │
└────────┬────────┘
│
┌────▼────┐
│Semiring │
└────┬────┘
│
┌────▼────┐
│ Ring │
└────┬────┘
┌───────────────────────┐
│ │
┌─────▼─────┐ ┌─────▼─────┐
│ Module │ │Commutative│
└───────────┘ │ Ring │
└─────┬─────┘
│
┌────────▼────────┐
│ Integral Domain │
└────────┬────────┘
│
┌─────────────▼─────────────┐
│Unique Factorization Domain│
└─────────────┬─────────────┘
│
┌───────────▼───────────┐
│Principal Ideal Domain │
└───────────┬───────────┘
│
┌────────▼────────┐
│Euclidean Domain │
└────────┬────────┘
│
┌───▼───┐
│ Field │────────────────────────┐
└───┬───┘ │
┌─────────┴──────────┐ │
│ │ │
┌─────▼─────┐ ┌─────▼─────┐ ┌─────▼─────┐
│ Finite │ │ Infinite │ │ Vector │
│ Field │ │ Field │ │ Space │
└─────┬─────┘ └───────────┘ └───────────┘
│
┌─────▼─────┐
│ Field │
│ Extension │
└─────┬─────┘
│
┌─────▼─────┐
│ Extension │
│ Tower │
└───────────┘
Operator Traits for Algebraic Structures
This module defines traits for various operators and their properties, providing a foundation for implementing algebraic structures in Rust.
An algebraic structure consists of a set with one or more binary operations. Let $S$ be a set (Self) and $\bullet$ be a binary operation on $S$. Here are the key properties a binary operation may possess, organized from simplest to most complex:
- (Closure) $\forall a, b \in S, a \bullet b \in S$ - Guaranteed by the operators provided
- (Totality) $\forall a, b \in S, a \bullet b$ is defined - Guaranteed by Rust
- (Commutativity) $\forall a, b \in S, a \bullet b = b \bullet a$ - Marker trait
- (Associativity) $\forall a, b, c \in S, (a \bullet b) \bullet c = a \bullet (b \bullet c)$ - Marker trait
- (Distributivity) $\forall a, b, c \in S, a * (b + c) = (a * b) + (a * c)$ - Marker trait
The traits and blanket implementations provided serve several important purposes:
-
Closure: All
Closed*traits ensure that operations on a type always produce a result of the same type. This is crucial for defining algebraic structures. -
Reference Operations: The
*Refvariants of traits allow for more efficient operations when the right-hand side can be borrowed, which is common in many algorithms. -
Marker Traits: Traits like
Commutative,Associative, etc., allow types to declare which algebraic properties they satisfy. This can be used for compile-time checks and to enable more generic implementations of algorithms. -
Extensibility: New types that implement the standard traits (like
Add,Sub, etc.) will automatically get the closed trait implementations, making the system more extensible and future-proof. -
Type Safety: These traits help in catching type-related errors at compile-time, ensuring that operations maintain closure within the same type.
-
Generic Programming: These traits enable more expressive generic programming, allowing functions and structs to be generic over types that are closed under certain operations or satisfy certain algebraic properties.
API Overview
Nœther provides traits for various algebraic structures, including:
Magma: Set with a binary operationSemigroup: Associative magmaMonoid: Semigroup with identity elementGroup: Monoid where every element has an inverseRing: Set with two operations (addition and multiplication) satisfying certain axiomsField: Commutative ring where every non-zero element has a multiplicative inverseVectorSpace: An abelian group with scalar multiplication over a fieldModule: Similar to a vector space, but over a ring instead of a fieldPolynomial: Represents polynomials over a fieldFieldExtension: Represents field extensions
Each trait comes with methods defining the operations and properties of the respective algebraic structure. For a complete list of traits and their methods, please refer to the API documentation.
Advanced Usage
Nœther's power lies in its ability to express complex mathematical concepts and algorithms generically. Here's an
example of a function that works with any type implementing the Field trait:
use Field;
// This function works for any type implementing the Field trait
You can use this function with any type that implements the Field trait, whether it's a built-in numeric type or a
custom type like our Z5 from the earlier example.
Integrating External Numeric Types
Noether can be extended to work with third-party numeric types by creating wrappers that implement the appropriate traits. This section demonstrates how to integrate external numeric libraries with Noether's type system.
Example: Complex Numbers
Here's an example of how to wrap the num_complex crate's Complex type to work with Noether's algebraic structure traits:
use ;
use FieldExtension;
use VectorSpace;
use ;
use ;
/// A wrapper around `num_complex::Complex` that implements Noether's algebraic traits.
///
/// This wrapper provides a complex number implementation that satisfies Noether's
/// algebraic structure traits, including `Field` and `FieldExtension` traits.
///
/// # Type Parameters
///
/// * `T` - The type of the real and imaginary parts.
///
/// # Examples
///
/// ```
/// use noether::primitives::Complex;
///
/// // Create a complex number
/// let z = Complex::<f64>::new(3.0, 4.0);
/// let w = Complex::<f64>::new(1.0, 2.0);
///
/// // Perform field operations
/// let sum = z + w;
/// let product = z * w;
/// let quotient = z / w;
/// ```
// Implement the standard arithmetic operations by delegating to the inner Complex type
// Implement num_traits::Inv for Complex
// Implement Euclid trait for Euclidean division
// Implement Noether's algebraic marker traits for Complex
// Special implementation for f64 which satisfies all the trait requirements
// This allows us to specifically implement VectorSpace and FieldExtension for Complex<f64>
// Special implementation for f64 which satisfies all the trait requirements
// Similar implementations for f32
This example showcases the key steps for wrapping external numeric types in Noether:
- Create a wrapper struct around the external type
- Implement basic arithmetic operations by delegating to the wrapped type
- Implement Noether's marker traits for algebraic properties
- Implement specific algebraic structure traits (like
VectorSpaceandFieldExtension)
Benefits of Integration
By wrapping external types in this way, you gain several advantages:
- Type Safety: Ensure operations maintain mathematical correctness
- Algebraic Abstraction: Express algorithms in terms of abstract structures rather than concrete types
- Generic Algorithms: Write code that works with any type satisfying certain algebraic properties
- Mathematical Documentation: Clarify the mathematical properties of your numeric types
This approach allows you to combine the power of specialized numeric libraries with Noether's comprehensive algebraic framework.
Performance
Nœther is designed with performance in mind, leveraging Rust's zero-cost abstractions. The use of trait-based polymorphism allows for efficient, monomorphized code when used with concrete types.
However, as with any abstract library, be aware that extensive use of dispatch (e.g., through trait objects) may incur some runtime cost. In most cases, the compiler can optimize away the abstractions, resulting in performance equivalent to hand-written implementations.
Roadmap
For detailed development plans, see the ROADMAP.md file, which outlines future directions including:
- Core algebraic traits and properties
- Category theory foundations
- Advanced algebraic systems
- Abstract linear algebra and multilinear algebra
- Type system improvements for compile-time verification
Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests on the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
We hope that Nœther will be a valuable tool for cryptographers, mathematicians, scientists, and developers working with algebraic structures in Rust. If you have any questions, suggestions, or feedback, please don't hesitate to open an issue on our GitHub repository or contact the maintainers.
Happy coding with Nœther!