NTLib - Number Theory Library 0.9
Loading...
Searching...
No Matches
ntlib::mod_int< T, m > Class Template Referenceexport
module mod_int

Class representing an element of a ring of integers modulo another positive integer. The modulus is provided at compile-time. More...

Public Member Functions

 mod_int () noexcept
 Default constructor. Initializes the value to zero.
 mod_int (T n) noexcept
 Construct a new element from a given value.
 operator T () const noexcept
 Returns the value.
mod_intoperator++ () noexcept
 Prefix increment operator.
mod_int operator++ (int) noexcept
 Postfix increment operator.
mod_intoperator-- () noexcept
 Prefix decrement operator.
mod_int operator-- (int) noexcept
 Postfix decrement operator.
mod_intoperator+= (mod_int rhs) noexcept
 Compound plus operator for modular addition.
mod_intoperator-= (mod_int rhs) noexcept
 Compound minus operator for modular subtraction.
mod_intoperator*= (mod_int rhs) noexcept
 Compound times operator for modular multiplication.
mod_int operator+ () const noexcept
 Unary plus operator. Does nothing.
mod_int operator- () const noexcept
 Unary minus operator to negate value.
void invert ()
 Inverts the value.

Friends

mod_int operator+ (mod_int lhs, mod_int rhs) noexcept
 Plus operator for modular addition.
mod_int operator- (mod_int lhs, mod_int rhs) noexcept
 Minus operator for modular subtraction.
mod_int operator* (mod_int lhs, mod_int rhs) noexcept
 Times operator for modular multiplication.
bool operator== (mod_int lhs, mod_int rhs) noexcept
 Equality comparison operator.

Detailed Description

template<Integer T, T m>
requires (m > 0)
class ntlib::mod_int< T, m >

Class representing an element of a ring of integers modulo another positive integer. The modulus is provided at compile-time.

Note
To avoid overflows, the type T must be able to hold
  • \(2m\) for addition and subtraction,
  • \(m^2\) for multiplication. This is ensured at compile-time. For this to work, T must provide std::numeric_limits<T>::max().
Template Parameters
TAn integer-like type.
mThe modulus.

Constructor & Destructor Documentation

◆ mod_int()

template<Integer T, T m>
ntlib::mod_int< T, m >::mod_int ( T n)
inlineexplicitnoexcept

Construct a new element from a given value.

Parameters
nThe given value.

Member Function Documentation

◆ invert()

template<Integer T, T m>
void ntlib::mod_int< T, m >::invert ( )
inline

Inverts the value.

Note
This requires that the current value is coprime to the modulus.

◆ operator T()

template<Integer T, T m>
ntlib::mod_int< T, m >::operator T ( ) const
inlineexplicitnoexcept

Returns the value.

Returns
The value in the range \([0, m)\).

◆ operator*=()

template<Integer T, T m>
mod_int & ntlib::mod_int< T, m >::operator*= ( mod_int< T, m > rhs)
inlinenoexcept

Compound times operator for modular multiplication.

Note
Contains a compile-time check to ensure that no overflow can occur.
Parameters
rhsThe second factor.
Returns
Reference to the result.

◆ operator+()

template<Integer T, T m>
mod_int ntlib::mod_int< T, m >::operator+ ( ) const
inlinenoexcept

Unary plus operator. Does nothing.

Returns
The same instance as before.

◆ operator++() [1/2]

template<Integer T, T m>
mod_int & ntlib::mod_int< T, m >::operator++ ( )
inlinenoexcept

Prefix increment operator.

Returns
Reference to the result (with new value).

◆ operator++() [2/2]

template<Integer T, T m>
mod_int ntlib::mod_int< T, m >::operator++ ( int )
inlinenoexcept

Postfix increment operator.

Returns
The old value.

◆ operator+=()

template<Integer T, T m>
mod_int & ntlib::mod_int< T, m >::operator+= ( mod_int< T, m > rhs)
inlinenoexcept

Compound plus operator for modular addition.

Note
Contains a compile-time check to ensure that no overflow can occur.
Parameters
rhsThe second summand.
Returns
Reference to the result.

◆ operator-()

template<Integer T, T m>
mod_int ntlib::mod_int< T, m >::operator- ( ) const
inlinenoexcept

Unary minus operator to negate value.

Returns
The negated instance.

◆ operator--() [1/2]

template<Integer T, T m>
mod_int & ntlib::mod_int< T, m >::operator-- ( )
inlinenoexcept

Prefix decrement operator.

Returns
Reference to the result (with the new value).

◆ operator--() [2/2]

template<Integer T, T m>
mod_int ntlib::mod_int< T, m >::operator-- ( int )
inlinenoexcept

Postfix decrement operator.

Returns
The old value.

◆ operator-=()

template<Integer T, T m>
mod_int & ntlib::mod_int< T, m >::operator-= ( mod_int< T, m > rhs)
inlinenoexcept

Compound minus operator for modular subtraction.

Note
Contains a compile-time check to ensure that no overflow can occur.
Parameters
rhsThe subtrahend.
Returns
Reference to the result.

◆ operator*

template<Integer T, T m>
mod_int operator* ( mod_int< T, m > lhs,
mod_int< T, m > rhs )
friend

Times operator for modular multiplication.

Parameters
lhsThe first factor.
rhsThe second factor.
Returns
The product of lhs and rhs.

◆ operator+

template<Integer T, T m>
mod_int operator+ ( mod_int< T, m > lhs,
mod_int< T, m > rhs )
friend

Plus operator for modular addition.

Parameters
lhsThe first summand.
rhsThe second summand.
Returns
The sum of lhs and rhs.

◆ operator-

template<Integer T, T m>
mod_int operator- ( mod_int< T, m > lhs,
mod_int< T, m > rhs )
friend

Minus operator for modular subtraction.

Parameters
lhsThe minuend.
rhsThe subtrahend.
Returns
The difference of lhs and rhs.

◆ operator==

template<Integer T, T m>
bool operator== ( mod_int< T, m > lhs,
mod_int< T, m > rhs )
friend

Equality comparison operator.

Parameters
lhsThe first instance.
rhsThe second instance.
Returns
Whether lhs and rhs are equal.

The documentation for this class was generated from the following file: