Regina Calculation Engine
Public Types | Public Member Functions | Friends | List of all members
regina::Polynomial< T > Class Template Reference

Represents a single-variable polynomial with coefficients of type T. More...

#include <maths/polynomial.h>

Inheritance diagram for regina::Polynomial< T >:
regina::ShortOutput< Polynomial< T >, true > regina::Output< Polynomial< T >, supportsUtf8 >

Public Types

typedef T Coefficient
 The type of each coefficient of the polynomial. More...
 

Public Member Functions

 Polynomial ()
 Creates the zero polynomial. More...
 
 Polynomial (size_t degree)
 Creates the polynomial x^d for the given degree d. More...
 
 Polynomial (const Polynomial< T > &value)
 Creates a new copy of the given polynomial. More...
 
template<typename U >
 Polynomial (const Polynomial< U > &value)
 Creates a new copy of the given polynomial. More...
 
 Polynomial (Polynomial< T > &&value) noexcept
 Moves the contents of the given polynomial to this new polynomial. More...
 
template<typename iterator >
 Polynomial (iterator begin, iterator end)
 Creates a new polynomial from the given sequence of coefficients. More...
 
 Polynomial (std::initializer_list< T > coefficients)
 Creates a new polynomial from a hard-coded sequence of coefficients. More...
 
 ~Polynomial ()
 Destroys this polynomial. More...
 
void init ()
 Sets this to become the zero polynomial. More...
 
void init (size_t degree)
 Sets this to become the polynomial x^d for the given degree d. More...
 
template<typename iterator >
void init (iterator begin, iterator end)
 Sets this to become the polynomial described by the given sequence of coefficients. More...
 
size_t degree () const
 Returns the degree of this polynomial. More...
 
bool isZero () const
 Returns whether this is the zero polynomial. More...
 
bool isMonic () const
 Returns whether this polynomial is monic. More...
 
const T & leading () const
 Returns the leading coefficient of this polynomial. More...
 
const T & operator[] (size_t exp) const
 Returns the given coefficient of this polynomial. More...
 
void set (size_t exp, const T &value)
 Changes the given coefficient of this polynomial. More...
 
bool operator== (const Polynomial< T > &rhs) const
 Tests whether this and the given polynomial are equal. More...
 
bool operator!= (const Polynomial< T > &rhs) const
 Tests whether this and the given polynomial are not equal. More...
 
Polynomialoperator= (const Polynomial< T > &value)
 Sets this to be a copy of the given polynomial. More...
 
template<typename U >
Polynomialoperator= (const Polynomial< U > &value)
 Sets this to be a copy of the given polynomial. More...
 
Polynomialoperator= (Polynomial< T > &&value) noexcept
 Moves the contents of the given polynomial to this polynomial. More...
 
void swap (Polynomial< T > &other)
 Swaps the contents of this and the given polynomial. More...
 
void negate ()
 Negates this polynomial. More...
 
Polynomialoperator*= (const T &scalar)
 Multiplies this polynomial by the given constant. More...
 
Polynomialoperator/= (const T &scalar)
 Divides this polynomial by the given constant. More...
 
Polynomialoperator+= (const Polynomial< T > &other)
 Adds the given polynomial to this. More...
 
Polynomialoperator-= (const Polynomial< T > &other)
 Subtracts the given polynomial from this. More...
 
Polynomialoperator*= (const Polynomial< T > &other)
 Multiplies this by the given polynomial. More...
 
Polynomialoperator/= (const Polynomial< T > &other)
 Divides this by the given polynomial. More...
 
void divisionAlg (const Polynomial< T > &divisor, Polynomial< T > &quotient, Polynomial< T > &remainder) const
 Divides this by the given divisor, and extracts both the quotient and the remainder. More...
 
template<typename U >
void gcdWithCoeffs (const Polynomial< U > &other, Polynomial< T > &gcd, Polynomial< T > &u, Polynomial< T > &v) const
 Calculates the greatest common divisor of this and the given polynomial, and finds a linear combination of these polynomials that gives this gcd. More...
 
void writeTextShort (std::ostream &out, bool utf8=false, const char *variable=0) 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 >
Polynomial< T > & operator= (const Polynomial< U > &value)
 
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 >
Polynomial< U > operator+ (const Polynomial< U > &, const Polynomial< U > &)
 
template<typename U >
Polynomial< U > operator- (const Polynomial< U > &, const Polynomial< U > &)
 
template<typename U >
Polynomial< U > operator- (const Polynomial< U > &, Polynomial< U > &&)
 
template<typename U >
Polynomial< U > operator- (Polynomial< U > &&, Polynomial< U > &&)
 
template<typename U >
Polynomial< U > operator* (const Polynomial< U > &, const Polynomial< U > &)
 

Detailed Description

template<typename T>
class regina::Polynomial< T >

Represents a single-variable polynomial with coefficients of type T.

All exponents in the polynomial must be non-negative (so you can represent 2+3x but not 1+1/x).

The type T must represent a ring with no zero divisors. In particular, it must:

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).

The underlying storage method for this class is dense (i.e., all coefficients are explicitly stored, including zero coefficients).

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.

Python:\n In Python, the class Polynomial refers to the specific
template class Polynomial<Rational>.

Member Function Documentation

◆ detail()

std::string regina::Output< Polynomial< T > , supportsUtf8 >::detail
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.

Returns
a detailed text representation of this object.

◆ str()

std::string regina::Output< Polynomial< T > , supportsUtf8 >::str
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.

Python:\n In addition to str(), this is also used as the
Python "stringification" function str().
Returns
a short text representation of this object.

◆ utf8()

std::string regina::Output< Polynomial< T > , supportsUtf8 >::utf8
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.

Returns
a short text representation of this object.

◆ writeTextLong()

void regina::ShortOutput< Polynomial< T > , supportsUtf8 >::writeTextLong ( std::ostream &  out) const
inlineinherited

A default implementation for detailed output.

This routine simply calls T::writeTextShort() and appends a final newline.

Python:\n Not present.
Parameters
outthe output stream to which to write.

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

Copyright © 1999-2020, The Regina development team
This software is released under the GNU General Public License, with some additional permissions; see the source code for details.
For further information, or to submit a bug or other problem, please contact Ben Burton (bab@maths.uq.edu.au).