MultiplicationAlgorithm

Enum MultiplicationAlgorithm 

Source
pub enum MultiplicationAlgorithm {
    Standard,
    Schoolbook,
    Karatsuba,
    ToomCook,
    FFT,
}
Expand description

Multiplication algorithms for multi-limb arithmetic.

This enum defines different algorithms for multiplying large multi-precision integers (BigInt). Each algorithm offers different trade-offs between performance, memory usage, and complexity. The choice of algorithm depends on the size of the operands and the computational requirements.

§Performance Characteristics

  • Standard/Schoolbook: O(n²) - Simple, good for small numbers
  • Karatsuba: O(n^1.585) - Good balance for medium-sized numbers
  • Toom-Cook: O(n^1.465) - Better for large numbers, more complex
  • FFT: O(n log n) - Optimal for very large numbers, highest overhead

§Memory Usage

Different algorithms have different memory requirements and may allocate temporary BigInt values during computation.

§Algorithm Selection

The library automatically selects appropriate algorithms based on operand sizes, but this enum allows manual override for specific use cases.

Variants§

§

Standard

Standard long multiplication (schoolbook algorithm).

This is the traditional “grade school” multiplication method that multiplies each digit of one number by each digit of the other. Simple to implement and optimal for small numbers.

Complexity: O(n²) Memory: O(1) additional space Best for: Numbers with < 1000 bits

§

Schoolbook

Alias for Standard multiplication algorithm.

Provided for clarity and compatibility. Same performance characteristics as Standard.

§

Karatsuba

Karatsuba multiplication algorithm.

Reduces the multiplication of two n-digit numbers to three multiplications of n/2-digit numbers using the identity: (a₁·2^(n/2) + a₀) · (b₁·2^(n/2) + b₀) = a₁b₁·2^n + (a₁b₁ + a₀b₀ - (a₀+a₁)(b₀+b₁))·2^(n/2) + a₀b₀

Complexity: O(n^1.585) Memory: O(n) additional space Best for: Numbers with 1000-10000 bits

§

ToomCook

Toom-Cook multiplication algorithm (Toom-3 variant).

Generalizes Karatsuba by splitting numbers into more parts and evaluating at multiple points. Uses polynomial interpolation to reconstruct the result. This implementation uses a simplified 3-way split with evaluation at points 0, 1, -1, 2, and ∞.

Complexity: O(n^1.465) Memory: O(n) additional space Best for: Numbers with 10000-50000 bits

§

FFT

Fast Fourier Transform (FFT) based multiplication.

Uses FFT to multiply large numbers by converting them to the frequency domain. Requires the numbers to be treated as coefficients of polynomials. Most efficient asymptotically but has high constant factors and memory requirements.

Complexity: O(n log n) Memory: O(n) additional space Best for: Numbers with >50000 bits Note: May fall back to Toom-Cook for smaller sizes due to overhead

Trait Implementations§

Source§

impl Clone for MultiplicationAlgorithm

Source§

fn clone(&self) -> MultiplicationAlgorithm

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MultiplicationAlgorithm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for MultiplicationAlgorithm

Source§

fn eq(&self, other: &MultiplicationAlgorithm) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for MultiplicationAlgorithm

Source§

impl Eq for MultiplicationAlgorithm

Source§

impl StructuralPartialEq for MultiplicationAlgorithm

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.