Rings for MAT 685
Mathematical ring implementation to demonstrate templates and inheritance
|
templated modular arithmetic, making no assumption about the modulus More...
#include <mod.hpp>
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 | |
T | 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 | |
T | 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 | |
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.
|
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