C(++)ECCO
C++ Error Control COding: a header-only library for ECC simulations and experiments, modeling complete coding systems across arbitrary finite fields and complex inter-field relationships - Christian Senger <senger@inue.uni-stuttgart.de>
Loading...
Searching...
No Matches
CECCO::Rationals< T > Class Template Reference

Field of rational numbers ℚ = { p/q : p, q ∈ ℤ, q ≠ 0 } with selectable precision. More...

#include <fields.hpp>

Inheritance diagram for CECCO::Rationals< T >:
Inheritance graph

Public Member Functions

 Rationals (int n=0, int d=1)
 Construct n / d, simplified to lowest terms with positive denominator.
 Rationals (const Rationals &other)=default
 Rationals (Rationals &&other)=default
constexpr Rationalsoperator= (int l)
 Assign the integer l as l / 1.
constexpr Rationalsoperator= (const Rationals &rhs)=default
Rationalsoperator= (Rationals &&rhs)=default
constexpr bool operator== (const Rationals< T > &rhs) const
 Cross-multiplication equality.
constexpr Rationals operator- () const &
 Additive inverse (lvalue overload returns a copy with negated numerator).
constexpr Rationalsoperator- () &&
 Additive inverse (rvalue overload negates in place).
constexpr Rationalsoperator+= (const Rationals &rhs)
 *this += rhs, result kept in lowest terms
constexpr Rationalsoperator-= (const Rationals &rhs)
 *this -= rhs, result kept in lowest terms
constexpr Rationalsoperator*= (const Rationals &rhs)
 *this *= rhs, result kept in lowest terms
Rationalsoperator/= (const Rationals &rhs)
 *this /= rhs; throws std::invalid_argument if rhs is zero
Rationalsrandomize ()
 Random rational (bounded numerator and denominator), simplified.
Rationalsrandomize_force_change ()
 Like randomize but guaranteed to differ from the current value.
size_t get_multiplicative_order () const
 Multiplicative order.
size_t get_additive_order () const noexcept
 Additive order: 1 for zero, 0 (infinite) otherwise (characteristic 0).
constexpr bool has_positive_sign () const noexcept
 Sign predicate (true iff numerator and denominator share their sign).
constexpr bool is_zero () const noexcept
 True iff numerator is zero.
constexpr Rationalserase () noexcept
 Mark this element as erased.
constexpr Rationalsunerase () noexcept
 Clear the erasure flag, resetting to additive identity 0/1.
constexpr bool is_erased () const noexcept
 Test whether this element is currently erased (encoded as denominator == 0).
constexpr const T & get_numerator () const noexcept
 Numerator (sign carrier).
constexpr const T & get_denominator () const noexcept
 Denominator (always positive after simplification).
Public Member Functions inherited from CECCO::details::Field< Rationals< InfInt > >
Rationals< InfInt > & operator= (int l)=delete
 Assign from int — derived must implement.
constexpr bool operator!= (const Rationals< InfInt > &rhs) const
 Inequality, defined as !(*this == rhs).
constexpr Rationals< InfIntoperator+ () const &
 Unary + on an lvalue: returns a copy.
Rationals< InfIntoperator- () const &noexcept=delete
 Additive inverse on an lvalue — derived must implement.
Rationals< InfInt > & operator+= (const Rationals< InfInt > &rhs) noexcept=delete
 *this += rhs — derived must implement
Rationals< InfInt > & operator-= (const Rationals< InfInt > &rhs) noexcept=delete
 *this -= rhs — derived must implement
Rationals< InfInt > & operator*= (const Rationals< InfInt > &rhs) noexcept=delete
 *this *= rhs — derived must implement
Rationals< InfInt > & operator/= (const Rationals< InfInt > &rhs)=delete
 *this /= rhs; throws std::invalid_argument if rhs is zero — derived must implement
Fieldrandomize ()=delete
 Uniform random element of the field — derived must implement; may return the same value.
Fieldrandomize_force_change ()=delete
 Like randomize but guaranteed to differ from the current value — derived must implement.
size_t get_multiplicative_order () const=delete
 Smallest k > 0 with this^k == 1; throws std::invalid_argument if *this is zero.
size_t get_additive_order () const=delete
 Smallest k > 0 with k * *this == 0; for finite fields of characteristic p this is p (or 1 for zero); for ℚ it is 1 for zero and 0 (infinite) otherwise.
bool has_positive_sign () const noexcept=delete
 True if the element is "positive" (always true for finite fields; sign of numerator for ℚ).
bool is_zero () const noexcept=delete
 True if *this is the additive identity.
Fielderase () noexcept=delete
 Mark this element as erased (out-of-field marker for erasure decoding).
Fieldunerase () noexcept=delete
 Clear the erasure flag, resetting to additive identity.
bool is_erased () const noexcept=delete
 Test whether this element is currently erased.

Static Public Member Functions

static const std::string get_info ()
 Human-readable description: "rational number".
static constexpr size_t get_characteristic () noexcept
 Characteristic of ℚ: 0.

Additional Inherited Members

Protected Member Functions inherited from CECCO::details::Field< Rationals< InfInt > >
 ~Field () noexcept=default
Protected Member Functions inherited from CECCO::details::Base
 Base ()=default

Detailed Description

template<SignedIntType T = InfInt>
class CECCO::Rationals< T >

Field of rational numbers ℚ = { p/q : p, q ∈ ℤ, q ≠ 0 } with selectable precision.

Template Parameters
TNumerator/denominator type satisfying CECCO::SignedIntType (default InfInt)

Characteristic 0. Values are kept in lowest terms with positive denominator at all times, so equality is numerator_a * denominator_b == numerator_b * denominator_a. Construction with a zero denominator throws std::invalid_argument.

Pick T = InfInt for true ℚ — a fixed-width T (e.g. int, long long) caps numerator and denominator and silently overflows past that range.

Usage_Example

Rationals<> a(3, 4);
Rationals<> b(5, 6);
auto c = a + b; // 19/12 (auto-simplified)
auto d = a / b; // 9/10
std::cout << c; // "19/12"
Rationals(int n=0, int d=1)
Construct n / d, simplified to lowest terms with positive denominator.
Definition fields.hpp:576

Definition at line 471 of file fields.hpp.

Constructor & Destructor Documentation

◆ Rationals() [1/3]

template<SignedIntType T>
CECCO::Rationals< T >::Rationals ( int n = 0,
int d = 1 )

Construct n / d, simplified to lowest terms with positive denominator.

Parameters
nNumerator (default 0)
dDenominator (default 1)
Exceptions
std::invalid_argumentif d == 0

Definition at line 576 of file fields.hpp.

◆ Rationals() [2/3]

template<SignedIntType T = InfInt>
CECCO::Rationals< T >::Rationals ( const Rationals< T > & other)
default

◆ Rationals() [3/3]

template<SignedIntType T = InfInt>
CECCO::Rationals< T >::Rationals ( Rationals< T > && other)
default

Member Function Documentation

◆ erase()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::erase ( )
constexprnoexcept

Mark this element as erased.

Warning
Erased elements must not participate in field arithmetic; correct use is the caller's responsibility (cf. CECCO_ERASURE_SUPPORT).

Definition at line 717 of file fields.hpp.

◆ get_additive_order()

template<SignedIntType T>
size_t CECCO::Rationals< T >::get_additive_order ( ) const
noexcept

Additive order: 1 for zero, 0 (infinite) otherwise (characteristic 0).

Definition at line 708 of file fields.hpp.

◆ get_characteristic()

template<SignedIntType T = InfInt>
constexpr size_t CECCO::Rationals< T >::get_characteristic ( )
inlinestaticconstexprnoexcept

Characteristic of ℚ: 0.

Definition at line 536 of file fields.hpp.

◆ get_denominator()

template<SignedIntType T = InfInt>
const T & CECCO::Rationals< T >::get_denominator ( ) const
inlineconstexprnoexcept

Denominator (always positive after simplification).

Definition at line 563 of file fields.hpp.

◆ get_info()

template<SignedIntType T = InfInt>
const std::string CECCO::Rationals< T >::get_info ( )
inlinestatic

Human-readable description: "rational number".

Definition at line 530 of file fields.hpp.

◆ get_multiplicative_order()

template<SignedIntType T>
size_t CECCO::Rationals< T >::get_multiplicative_order ( ) const

Multiplicative order.

Returns
1 for 1/1, 2 for −1/1, 0 (interpreted as infinite) for everything else
Exceptions
std::invalid_argumentif *this is zero

Definition at line 695 of file fields.hpp.

◆ get_numerator()

template<SignedIntType T = InfInt>
const T & CECCO::Rationals< T >::get_numerator ( ) const
inlineconstexprnoexcept

Numerator (sign carrier).

Definition at line 561 of file fields.hpp.

◆ has_positive_sign()

template<SignedIntType T = InfInt>
bool CECCO::Rationals< T >::has_positive_sign ( ) const
inlineconstexprnoexcept

Sign predicate (true iff numerator and denominator share their sign).

Definition at line 539 of file fields.hpp.

◆ is_erased()

template<SignedIntType T = InfInt>
bool CECCO::Rationals< T >::is_erased ( ) const
inlineconstexprnoexcept

Test whether this element is currently erased (encoded as denominator == 0).

Definition at line 557 of file fields.hpp.

◆ is_zero()

template<SignedIntType T = InfInt>
bool CECCO::Rationals< T >::is_zero ( ) const
inlineconstexprnoexcept

True iff numerator is zero.

Definition at line 544 of file fields.hpp.

◆ operator*=()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::operator*= ( const Rationals< T > & rhs)
constexpr

*this *= rhs, result kept in lowest terms

Definition at line 634 of file fields.hpp.

◆ operator+=()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::operator+= ( const Rationals< T > & rhs)
constexpr

*this += rhs, result kept in lowest terms

Definition at line 608 of file fields.hpp.

◆ operator-() [1/2]

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::operator- ( ) &&
constexpr

Additive inverse (rvalue overload negates in place).

Definition at line 599 of file fields.hpp.

◆ operator-() [2/2]

template<SignedIntType T>
Rationals< T > CECCO::Rationals< T >::operator- ( ) const &
constexpr

Additive inverse (lvalue overload returns a copy with negated numerator).

Definition at line 589 of file fields.hpp.

◆ operator-=()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::operator-= ( const Rationals< T > & rhs)
constexpr

*this -= rhs, result kept in lowest terms

Definition at line 621 of file fields.hpp.

◆ operator/=()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::operator/= ( const Rationals< T > & rhs)

*this /= rhs; throws std::invalid_argument if rhs is zero

Definition at line 647 of file fields.hpp.

◆ operator=() [1/3]

template<SignedIntType T = InfInt>
Rationals & CECCO::Rationals< T >::operator= ( const Rationals< T > & rhs)
constexprdefault

◆ operator=() [2/3]

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::operator= ( int l)
constexpr

Assign the integer l as l / 1.

Definition at line 582 of file fields.hpp.

◆ operator=() [3/3]

template<SignedIntType T = InfInt>
Rationals & CECCO::Rationals< T >::operator= ( Rationals< T > && rhs)
default

◆ operator==()

template<SignedIntType T = InfInt>
bool CECCO::Rationals< T >::operator== ( const Rationals< T > & rhs) const
inlineconstexpr

Cross-multiplication equality.

Definition at line 492 of file fields.hpp.

◆ randomize()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::randomize ( )

Random rational (bounded numerator and denominator), simplified.

Definition at line 661 of file fields.hpp.

◆ randomize_force_change()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::randomize_force_change ( )

Like randomize but guaranteed to differ from the current value.

Definition at line 675 of file fields.hpp.

◆ unerase()

template<SignedIntType T>
Rationals< T > & CECCO::Rationals< T >::unerase ( )
constexprnoexcept

Clear the erasure flag, resetting to additive identity 0/1.

Definition at line 723 of file fields.hpp.


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