Class Qb
Represents a rational number with a base-dependent expansion, denoted ℚb.
Inherited Members
Namespace: MathLib
Assembly: MathLib.dll
Syntax
public class Qb : Q, IEquatable<Q>, IComparable<Q>, IEquatable<Qb>
Remarks
The class Qb extends Q by adopting a base, and thus defining a base-specific coefficient expansion of the rational number, with a preperiodic and a periodic part. These types of expansions are commonly used in numeral systems like p-ary expansions (e.g. decimal or binary) or in p-adic systems.
Each number is expressed in terms of a base, denoted by Base, with a preperiodic part (initial terminating sequence) and a periodic part (repeating sequence).
Each coefficient is indexed according to the exponent of the base Base. The index i
corresponds to the
coefficient c_i
associated with the term c_i · Base^i
, where the value of i
decreases as we move from
left to right in the expansion.
The first (leftmost) coefficient has index FirstExponent, which corresponds to the largest
exponent. The index decreases through the expansion, reflecting the successive exponents of the base, including negative
exponents for terms after the radix point (radix point occurs between c_0
and c_-1
).
This consistent indexing system aligns with the mathematical representation of the number as a series in base Base:
q = Σ (c_i · Base^i)
where the index i
decreases from FirstExponent to negative values as the expansion proceeds.
Overview of concepts and properties, for a full (ultimately periodic) expansion:
|--------------------------------------------------------|
Indexes (example): |-- 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 --|
Numeric parts: |-- IntegralPart --|-- FractionalPart --|
Length properties: |-- IntegralLength --|-- FractionalLength --|
Numeric parts: |-- PreperiodicPart --|-- PeriodicPart --|
Length properties: |-- PreperiodicLength --|-- Period --|
|-- Length --|
Indexes (Exponents): • (FirstExponent=8) • (FirstPeriodicExponent=-2)
• (Radix point, after c_0)
The class methods must never throw exceptions for arithmetic operations. Instead, they return NaN for undefined results (such as from divide by zero).
Constructors
| Edit this page View SourceQb(Q, int)
Extends a rational number to a Qb extension with the specific base base_
.
Declaration
public Qb(Q q, int base_)
Parameters
Type | Name | Description |
---|---|---|
Q | q | A rational number of type Q |
int | base_ | The base of the rational number extension. |
Qb(bool, BaseInt, BaseInt, int)
Constructor that creates a Qb from the defining parts of a base-specific expansion.
Declaration
public Qb(bool negative, BaseInt prePeriodicPart, BaseInt periodicPart, int firstExponent = -1)
Parameters
Type | Name | Description |
---|---|---|
bool | negative | A boolean indicating if the number is negative. |
BaseInt | prePeriodicPart | The preperiodic part as a BaseInt. |
BaseInt | periodicPart | The periodic part as a BaseInt>. |
int | firstExponent | The exponent index of the first coefficient of the number. |
Remarks
This constructor will derive the Numerator and Denominator from the parts.
Qb(BigInteger, BigInteger, int)
Constructor for a Qb that takes a numerator, denominator and a base. The rational number is automatically normalized to its simplest form.
Declaration
public Qb(BigInteger numerator, BigInteger denominator, int base_)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | numerator | The numerator of the rational number. |
BigInteger | denominator | The denominator of the rational number. Must be non-zero. |
int | base_ | The base of the rational number extension. |
Remarks
If you already have an object of type Q, then the constructor Qb(Q, int) will be faster.
Or alternatively, you can simply call InBase(int) on the Q instance
Properties
| Edit this page View SourceBase
Gets the base of the rational number extension.
Declaration
public int Base { get; }
Property Value
Type | Description |
---|---|
int |
FirstExponent
Index (= exponent value) of the first coefficient
(of the base Base expansion)
Declaration
public int FirstExponent { get; }
Property Value
Type | Description |
---|---|
int |
Remarks
This value is simply the length of IntegralPart - 1:
The range of possible values is [-1..∞[
, since a number either starts before the radix point, or directly after it.
coefficient
at index FirstExponent is the true first coefficient in the expansion, which can be zero iff
FirstExponent is -1.
Formula:
FirstExponent = IntegralPart.Length(Base) - 1
FirstPeriodicExponent
Index (= exponent value) of the first coefficient of the periodic part (of the base B expansion).
Declaration
public int FirstPeriodicExponent { get; }
Property Value
Type | Description |
---|---|
int |
FractionalLength
Length of the fractional part (of the base B expansion).
Declaration
public int FractionalLength { get; }
Property Value
Type | Description |
---|---|
int |
FullInteger
Numeric concatenation of the preperiodic and periodic parts.
Declaration
public BaseInt FullInteger { get; }
Property Value
Type | Description |
---|---|
BaseInt |
IntegralLength
Length of the integer part (of the base B expansion).
Declaration
public int IntegralLength { get; }
Property Value
Type | Description |
---|---|
int |
Remarks
This value is simply the base-specific length of IntegralPart.
Formula:
IntegralLength = IntegralPart.Length(Base)
Length
Total length of the preperiodic and periodic parts (of the base B expansion).
Declaration
public int Length { get; }
Property Value
Type | Description |
---|---|
int |
NaN
Returns a NaN Qb instance.
Declaration
public static Qb NaN { get; }
Property Value
Type | Description |
---|---|
Qb |
Period
Length of the periodic part (of the base B expansion).
Declaration
public int Period { get; }
Property Value
Type | Description |
---|---|
int |
PeriodicPart
The periodic part.
Declaration
public BaseInt PeriodicPart { get; }
Property Value
Type | Description |
---|---|
BaseInt |
Remarks
Contains the periodic coefficients (including zero-padding), and the period (=length of the periodic part).
PreperiodicLength
Length of the preperiodic part (of the base B expansion).
Declaration
public int PreperiodicLength { get; }
Property Value
Type | Description |
---|---|
int |
PreperiodicPart
The preperiodic part.
Declaration
public BaseInt PreperiodicPart { get; }
Property Value
Type | Description |
---|---|
BaseInt |
Remarks
Contains the preperiodic coefficients (including zero-padding), and the length of the preperiodic part.
Methods
| Edit this page View SourceCoefficients()
Returns the coefficient (or digit) expansion of a rational number in the specified base, expressed as a sequence of integers (modulo b).
Declaration
public IEnumerable<int> Coefficients()
Returns
Type | Description |
---|---|
IEnumerable<int> | An enumerable sequence of integers representing the coefficients in base Base. |
Remarks
The coefficients are obtained from the fractional expansions generated by recursively shifting the radix point of the rational number in base Base.
Specifically, the coefficients are derived by taking the ShiftedFractions() and scaling them to the range [0..Base-1]
.
Thus, each coefficient is a number modulo Base.
Equals(Qb?)
Indicates whether the current object is equal to another object of the same type.
Declaration
public bool Equals(Qb? other)
Parameters
Type | Name | Description |
---|---|---|
Qb | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
bool | true if the current object is equal to the |
RepetendShiftLeft()
Returns a new Q with a repetend (interpreted as an integer) that is base shifted 1 step to the left compared to the original repetend (interpreted as an integer).
Declaration
public Q RepetendShiftLeft()
Returns
Type | Description |
---|---|
Q | A new Q instance with the base-shifted repetend. |
RepetendShiftRight()
Returns a new Q with a repetend (interpreted as an integer) that is base shifted 1 step to the right compared to the original repetend (interpreted as an integer).
Declaration
public Q RepetendShiftRight()
Returns
Type | Description |
---|---|
Q | A new Q instance with the base-shifted repetend. |
ShiftedFractions()
Returns the left-to-right expansion of the rational number in the specified base as an infinite sequence of fractions. Each fraction corresponds to the fractional part remaining after successive base-specific left shifts.
Declaration
public IEnumerable<(BigInteger Integer, Q Fraction)> ShiftedFractions()
Returns
Type | Description |
---|---|
IEnumerable<(BigInteger Integer, Q Fraction)> | An enumerable sequence of rational numbers representing the shifted fractional parts in base Base. |
Remarks
This method first applies a normalization step to the rational number, effectively shifting the radix point to the right (in base Base),
such that the integer part is zero, leaving only the fractional part. The first element of the sequence is
this fractional remainder.
Subsequently, at each step, the fractional part is shifted left by the base (* Base), and the next fractional part is extracted by removing the integer part.
This process generates an infinite sequence of fractional remainders, which represent the underlying structure of the rational number in the given base. These fractions can be used to derive coefficients or compute the period of the expansion.
ToString()
Returns the default string representation of the rational number
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string |
Overrides
| Edit this page View SourceToStringExpanded(int)
Converts the current instance of Qb to its expanded string representation in the given base.
Declaration
public string ToStringExpanded(int coefficientCount = 16)
Parameters
Type | Name | Description |
---|---|---|
int | coefficientCount | The number of coefficients to include, starting from the leftmost. (default=16) |
Returns
Type | Description |
---|---|
string | The expanded string representation of the current instance of Qb. |
Remarks
The sign of a number is not encoded by (contained in) the expanded representation, since some Q and -Q can have the same string representation. However, negative numbers, completes the generation, by enabling us to generate all ultimately periodic expansions, including those that are not covered by the positive rational numbers.
Examples
Q | Expansion | Mode |
---|---|---|
0 | .0000000000000000 | FEO=FIO, in any base |
1₂ | 1.000000000000000 | FEO |
-1₂ | .1111111111111111 | FIO |
103/16₂ | 110.0111000000000 | FEO |
-103/16₂ | 110.0110111111111 | FIO |
5/24₂ | .0011010101010101 | FEO=FIO |
-5/24₂ | .0011010101010101 | FEO=FIO |
3/45₅ | .3333333333333333 | FEO=FIO |
-3/45₅ | .3333333333333333 | FEO=FIO |
537/11₃ | 1210.211002110021 | FEO=FIO |
-537/11₃ | 1210.211002110021 | FEO=FIO |
ToStringExpandedSigned(int)
Declaration
public string ToStringExpandedSigned(int coefficientCount = 16)
Parameters
Type | Name | Description |
---|---|---|
int | coefficientCount |
Returns
Type | Description |
---|---|
string |
ToStringPeriodic()
Declaration
public string ToStringPeriodic()
Returns
Type | Description |
---|---|
string |
ToStringRepetend()
Declaration
public string ToStringRepetend()
Returns
Type | Description |
---|---|
string |
ToStringRotations()
Declaration
public string ToStringRotations()
Returns
Type | Description |
---|---|
string |