Regina Calculation Engine
|
Represents a single-variable Laurent polynomial with coefficients of type T.
More...
#include <maths/laurent.h>
Public Types | |
typedef T | Coefficient |
The type of each coefficient of the polynomial. More... | |
Public Member Functions | |
Laurent () | |
Creates the zero polynomial. More... | |
Laurent (long exponent) | |
Creates the polynomial x^d for the given exponent d. More... | |
Laurent (const Laurent< T > &value) | |
Creates a new copy of the given polynomial. More... | |
template<typename U > | |
Laurent (const Laurent< U > &value) | |
Creates a new copy of the given polynomial. More... | |
Laurent (Laurent< T > &&value) noexcept | |
Moves the contents of the given polynomial to this new polynomial. More... | |
template<typename iterator > | |
Laurent (long minExp, iterator begin, iterator end) | |
Creates a new polynomial from the given sequence of coefficients. More... | |
Laurent (long minExp, std::initializer_list< T > coefficients) | |
Creates a new polynomial from a hard-coded sequence of coefficients. More... | |
~Laurent () | |
Destroys this polynomial. More... | |
void | init () |
Sets this to become the zero polynomial. More... | |
void | init (long exponent) |
Sets this to become the polynomial x^d for the given exponent d. More... | |
template<typename iterator > | |
void | init (long minExp, iterator begin, iterator end) |
Sets this to become the polynomial described by the given sequence of coefficients. More... | |
long | minExp () const |
Returns the smallest exponent that appears in this polynomial with a non-zero coefficient. More... | |
long | maxExp () const |
Returns the largest exponent that appears in this polynomial with a non-zero coefficient. More... | |
bool | isZero () const |
Returns whether this is the zero polynomial. More... | |
const T & | operator[] (long exp) const |
Returns the given coefficient of this polynomial. More... | |
void | set (long exp, const T &value) |
Changes the given coefficient of this polynomial. More... | |
bool | operator== (const Laurent< T > &rhs) const |
Tests whether this and the given polynomial are equal. More... | |
bool | operator!= (const Laurent< T > &rhs) const |
Tests whether this and the given polynomial are not equal. More... | |
Laurent & | operator= (const Laurent< T > &value) |
Sets this to be a copy of the given polynomial. More... | |
template<typename U > | |
Laurent & | operator= (const Laurent< U > &value) |
Sets this to be a copy of the given polynomial. More... | |
Laurent & | operator= (Laurent< T > &&value) noexcept |
Moves the contents of the given polynomial to this polynomial. More... | |
void | swap (Laurent< T > &other) |
Swaps the contents of this and the given polynomial. More... | |
void | shift (long s) |
Multiplies this polynomial by x^s for some integer s. More... | |
void | scaleUp (long k) |
Multiplies all exponents in this polynomial by k for some integer k. More... | |
void | scaleDown (long k) |
Divides all exponents in this polynomial by k for some integer k. More... | |
void | negate () |
Negates this polynomial. More... | |
Laurent & | operator*= (const T &scalar) |
Multiplies this polynomial by the given constant. More... | |
Laurent & | operator/= (const T &scalar) |
Divides this polynomial by the given constant. More... | |
Laurent & | operator+= (const Laurent< T > &other) |
Adds the given polynomial to this. More... | |
Laurent & | operator-= (const Laurent< T > &other) |
Subtracts the given polynomial from this. More... | |
Laurent & | operator*= (const Laurent< T > &other) |
Multiplies this by the given polynomial. More... | |
void | writeTextShort (std::ostream &out, bool utf8=false, const char *variable=nullptr) const |
Writes this polynomial to the given output stream, using the given variable name instead of x . More... | |
std::string | str (const char *variable) const |
Returns this polynomial as a human-readable string, using the given variable name instead of x . More... | |
std::string | utf8 (const char *variable) const |
Returns this polynomial as a human-readable string using unicode characters, using the given variable name instead of x . More... | |
template<typename U > | |
Laurent< T > & | operator= (const Laurent< U > &other) |
void | writeTextLong (std::ostream &out) const |
A default implementation for detailed output. More... | |
std::string | str () const |
Returns a short text representation of this object. More... | |
std::string | utf8 () const |
Returns a short text representation of this object using unicode characters. More... | |
std::string | detail () const |
Returns a detailed text representation of this object. More... | |
Friends | |
template<typename U > | |
Laurent< U > | operator+ (const Laurent< U > &, const Laurent< U > &) |
template<typename U > | |
Laurent< U > | operator+ (Laurent< U > &&, Laurent< U > &&) |
template<typename U > | |
Laurent< U > | operator- (const Laurent< U > &, const Laurent< U > &) |
template<typename U > | |
Laurent< U > | operator- (const Laurent< U > &, Laurent< U > &&) |
template<typename U > | |
Laurent< U > | operator- (Laurent< U > &&, Laurent< U > &&) |
template<typename U > | |
Laurent< U > | operator* (const Laurent< U > &, const Laurent< U > &) |
Represents a single-variable Laurent polynomial with coefficients of type T.
A Laurent polynomial differs from an ordinary polynomial in that it allows negative exponents (so, unlike the Polynomial class, you can represent both 2+3x
and 1+1/x
).
The type T must represent a ring with no zero divisors. In particular, it must:
x = int
and tests of the form x == int
and x < int
;This means that Regina's numerical types such as Integer and Rational are supported, but native data types such as int and long are not (since they have no zero-initialising default constructor).
This class is designed to avoid deep copies wherever possible. In particular, it supports C++11 move constructors and move assignment. Functions that take or return objects by value are designed to be just as efficient as working with references or pointers (any variations to this are noted in the method documentation), and long chains of operators such as a = b * c + d
do not make unwanted deep copies.
The underlying storage method for this class is dense (i.e., all coefficients are explicitly stored, including zero coefficients).
See also the class Laurent2, which describes Laurent polynomials in two variables.
typedef T regina::Laurent< T >::Coefficient |
The type of each coefficient of the polynomial.
|
inline |
Creates the zero polynomial.
|
inlineexplicit |
Creates the polynomial x^d
for the given exponent d.
exponent | the exponent to use for the new polynomial. |
|
inline |
Creates a new copy of the given polynomial.
This constructor induces a deep copy of value.
A note for developers: even though this routine is identical to the templated copy constructor, it must be declared and implemented separately. Otherwise the compiler might create its own (incorrect) copy constructor automatically.
value | the polynomial to clone. |
|
inline |
Creates a new copy of the given polynomial.
This constructor induces a deep copy of value.
value | the polynomial to clone. |
|
inlinenoexcept |
Moves the contents of the given polynomial to this new polynomial.
This is a fast (constant time) operation.
The polynomial that was passed (value) will no longer be usable.
value | the polynomial to move. |
|
inline |
Creates a new polynomial from the given sequence of coefficients.
The coefficients should appear in order from the smallest exponent term to the largest. The first coefficient in the sequence will be associated with the exponent minExp.
There is no problem if the first and/or last coefficient in the sequence is zero. An empty sequence will be treated as the zero polynomial.
This constructor induces a deep copy of the given range.
minExp | the exponent corresponding to the first coefficient in the sequence. |
begin | the beginning of the sequence of coefficients. |
end | a past-the-end iterator indicating the end of the sequence of coefficients. |
|
inline |
Creates a new polynomial from a hard-coded sequence of coefficients.
This constructor takes a C++11 initialiser list, which should contain the coefficients in order from the smallest exponent term to the largest. The first coefficient in the sequence will be associated with the exponent minExp.
There is no problem if the first and/or last coefficient in the sequence is zero. An empty sequence will be treated as the zero polynomial.
minExp | the exponent corresponding to the first coefficient in the sequence. |
coefficients | the full sequence of coefficients. |
|
inline |
Destroys this polynomial.
|
inherited |
Returns a detailed text representation of this object.
This text may span many lines, and should provide the user with all the information they could want. It should be human-readable, should not contain extremely long lines (which cause problems for users reading the output in a terminal), and should end with a final newline. There are no restrictions on the underlying character set.
|
inline |
Sets this to become the zero polynomial.
|
inline |
Sets this to become the polynomial x^d
for the given exponent d.
exponent | the new exponent to use for this polynomial. |
void regina::Laurent< T >::init | ( | long | minExp, |
iterator | begin, | ||
iterator | end | ||
) |
Sets this to become the polynomial described by the given sequence of coefficients.
The coefficients should appear in order from the smallest exponent term to the largest. The first coefficient in the sequence will be associated with the exponent minExp.
There is no problem if the first and/or last coefficient in the sequence is zero. An empty sequence will be treated as the zero polynomial.
This routine induces a deep copy of the given range.
minExp | the exponent corresponding to the first coefficient in the sequence. |
begin | the beginning of the sequence of coefficients. |
end | a past-the-end iterator indicating the end of the sequence of coefficients. |
|
inline |
Returns whether this is the zero polynomial.
true
if and only if this is the zero polynomial.
|
inline |
Returns the largest exponent that appears in this polynomial with a non-zero coefficient.
If this is the zero polynomial, then this routine returns 0.
|
inline |
Returns the smallest exponent that appears in this polynomial with a non-zero coefficient.
If this is the zero polynomial, then this routine returns 0.
|
inline |
Negates this polynomial.
This field element is changed directly.
|
inline |
Tests whether this and the given polynomial are not equal.
rhs | the polynomial to compare with this. |
true
if and only if this and the given polynomial are not equal. Laurent< T > & regina::Laurent< T >::operator*= | ( | const T & | scalar | ) |
Multiplies this polynomial by the given constant.
scalar | the scalar factor to multiply by. |
Laurent< T > & regina::Laurent< T >::operator*= | ( | const Laurent< T > & | other | ) |
Multiplies this by the given polynomial.
The given polynomial need not have the same minimum and/or maximum exponents as this.
other | the polynomial to multiply this by. |
|
inline |
Adds the given polynomial to this.
The given polynomial need not have the same minimum and/or maximum exponents as this.
+
operator instead, which is better able to avoid this deep copy where possible.other | the polynomial to add to this. |
|
inline |
Subtracts the given polynomial from this.
The given polynomial need not have the same minimum and/or maximum exponents as this.
other | the polynomial to subtract from this. |
|
inline |
Divides this polynomial by the given constant.
This uses the division operator /= for the coefficient type T.
scalar | the scalar factor to divide by. |
Laurent< T > & regina::Laurent< T >::operator= | ( | const Laurent< T > & | value | ) |
Sets this to be a copy of the given polynomial.
This and the given polynomial need not have the same minimum and/or maximum exponents.
This operator induces a deep copy of value.
A note to developers: although this is identical to the templated assignment operator, it must be declared and implemented separately. See the copy constructor for further details.
value | the polynomial to copy. |
Laurent& regina::Laurent< T >::operator= | ( | const Laurent< U > & | value | ) |
Sets this to be a copy of the given polynomial.
This and the given polynomial need not have the same minimum and/or maximum exponents.
This operator induces a deep copy of value.
value | the polynomial to copy. |
|
inlinenoexcept |
Moves the contents of the given polynomial to this polynomial.
This is a fast (constant time) operation.
This and the given polynomial need not have the same minimum and/or maximum exponents.
The polynomial that was passed (value) will no longer be usable.
value | the polynomial to move. |
|
inline |
Tests whether this and the given polynomial are equal.
rhs | the polynomial to compare with this. |
true
if and only if this and the given polynomial are equal.
|
inline |
Returns the given coefficient of this polynomial.
There are no restrictions on the exponent exp.
poly[exp] = value
. In contrast, C++ users must use the separate routine set(), due to the fact that in C++ this square bracket operator is const.exp | the exponent of the term whose coefficient should be returned. |
void regina::Laurent< T >::scaleDown | ( | long | k | ) |
Divides all exponents in this polynomial by k for some integer k.
This is equivalent to replacing the variable x of the polynomial with x1/k.
Both positive and negative scaling factors k are allowed.
k | the scaling factor to divide exponents by. |
void regina::Laurent< T >::scaleUp | ( | long | k | ) |
Multiplies all exponents in this polynomial by k for some integer k.
This is equivalent to replacing the variable x of the polynomial with xk.
Both positive and negative scaling factors k are allowed.
k | the scaling factor to multiply exponents by. |
void regina::Laurent< T >::set | ( | long | exp, |
const T & | value | ||
) |
Changes the given coefficient of this polynomial.
There are no restrictions on the exponent exp, and the new coefficient value may be zero.
Note, however, that it is expensive to set a non-zero coefficient whose exponent is larger than maxExp() or smaller than minExp(), since this will typically require deallocating and reallocating the full list of coefficients.
In contrast, setting a zero coefficient for the exponent maxExp() or minExp() is cheap, even though the range of non-zero coefficients changes as a result.
p[exp] = value
.exp | the exponent of the term whose coefficient should be changed. |
value | the new value of this coefficient. |
|
inline |
Multiplies this polynomial by x^s
for some integer s.
s | the power of x to multiply by. |
|
inherited |
Returns a short text representation of this object.
This text should be human-readable, should fit on a single line, and should not end with a newline. Where possible, it should use plain ASCII characters.
__str__()
.
|
inline |
Returns this polynomial as a human-readable string, using the given variable name instead of x
.
variable | the symbol to use for the variable in this polynomial. This may be null , in which case the default variable x will be used. |
|
inline |
Swaps the contents of this and the given polynomial.
This is a fast (constant time) operation.
This and the given polynomial do not need to have the same minimum and/or maximum exponents.
other | the polynomial whose contents should be swapped with this. |
|
inherited |
Returns a short text representation of this object using unicode characters.
Like str(), this text should be human-readable, should fit on a single line, and should not end with a newline. In addition, it may use unicode characters to make the output more pleasant to read. This string will be encoded in UTF-8.
|
inline |
Returns this polynomial as a human-readable string using unicode characters, using the given variable name instead of x
.
This is similar to the output from str(), except that it uses unicode characters to make the output more pleasant to read. In particular, it makes use of superscript digits for exponents and a wider minus sign.
The string is encoded in UTF-8.
variable | the symbol to use for the variable in this polynomial. This may be null , in which case the default variable x will be used. |
|
inlineinherited |
A default implementation for detailed output.
This routine simply calls T::writeTextShort() and appends a final newline.
out | the output stream to which to write. |
void regina::Laurent< T >::writeTextShort | ( | std::ostream & | out, |
bool | utf8 = false , |
||
const char * | variable = nullptr |
||
) | const |
Writes this polynomial to the given output stream, using the given variable name instead of x
.
If utf8 is passed as true
then unicode superscript characters will be used for exponents and the minus sign; these will be encoded using UTF-8. This will make the output nicer, but will require more complex fonts to be available on the user's machine.
out | the output stream to which to write. |
utf8 | true if unicode characters may be used. |
variable | the symbol to use for the variable in this polynomial. This may be null , in which case the default variable x will be used. |