Rings for MAT 685
Mathematical ring implementation to demonstrate templates and inheritance
Public Member Functions | Protected Member Functions | Protected Attributes | Related Functions | List of all members
Rings::Mod< T, m > Class Template Reference

templated modular arithmetic, making no assumption about the modulus More...

#include <mod.hpp>

Inheritance diagram for Rings::Mod< T, m >:
Rings::Commutative_Ring_Element Rings::Ring_Element

Public Member Functions

 Mod ()
 initializes to 0
 
 Mod (T v)
 initializes to give value
 
 Mod (const Mod< T, m > &other)
 copy constructor
 
virtual bool is_one () const override
 True iff assigned value is congruent to 1
 
virtual bool is_zero () const override
 True iff assigned value is congruent to 0
 
virtual bool has_inverse () const override
 True iff assigned value is relatively prime to modulus
 
virtual Mod< T, m > & operator+ (const Ring_Element &other) const override
 implements modular addition
 
virtual Mod< T, m > & operator- (const Ring_Element &other) const override
 implements modular subtraction
 
virtual Mod< T, m > & operator* (const Ring_Element &other) const override
 implements modular multiplication
 
virtual Mod< T, m > & operator* (const T &other) const
 multiplication of value with element of base type
 
const Mod< T, m > & operator= (const Ring_Element &)
 assignment operator may be needed
 
virtual bool operator== (const Ring_Element &) const override
 comparison: other element has same value
 
virtual bool operator!= (const Ring_Element &) const override
 comparison: other element has different value
 
get_value () const
 returns value of this modulus
 
- Public Member Functions inherited from Rings::Commutative_Ring_Element
virtual bool is_commutative () const override
 Duh.
 
- Public Member Functions inherited from Rings::Ring_Element
virtual bool is_cancellable () const
 should be True iff element can cancel across an equation; e.g., \(ax=ay\Rightarrow x=y\).
 

Protected Member Functions

void adjust_value ()
 ensures value is at least 0 but less than modulus
 
virtual void check_inverse ()
 decides whether this is invertible and sets flag accordingly; True iff \(\gcd(v,m)=1\) (where \(v\) is value)
 
void check_modulus ()
 checks modulus to ensure we aren't trying to do the impossible More...
 

Protected Attributes

value
 value of the element
 
bool invertible
 whether the element is invertible
 

Related Functions

(Note that these are not member functions.)

template<typename T , T m>
constexpr Mod< T, m > prevent_zero_modulus
 used to prevent the use of a zero modulus
 
template<typename T , T m>
ostream & operator<< (ostream &os, const Mod< T, m > &x)
 prints only value, not the modulus
 

Detailed Description

template<typename T, T m>
class Rings::Mod< T, m >

templated modular arithmetic, making no assumption about the modulus

The first template value indicates the base type (e.g., int, long). This code assumes you’re working with something int -like so no guarantees if you try something else. The second template indicates the modulus. See the warning.

Warning
If the modulus is nonzero then you get what you’re asking for!
Examples:
test_polynomial.cpp, and test_rings.cpp.

Member Function Documentation

◆ check_modulus()

template<typename T, T m>
void Rings::Mod< T, m >::check_modulus ( )
inlineprotected

checks modulus to ensure we aren't trying to do the impossible

To minimize the penalty of checking and throwing, we use this only with the default constructor and initialize one below


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