renf_elem_class.hpp β€” Elements of Real Embedded Number Fields

Class renf_elem_class

An element of a Real Embedded Number Field.

Each element lives in a fixed number field. If the number field is an extension of the rationals of degree $d$, its elements are represented by rational polynomials of degree $d - 1$.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
auto a = eantic::renf_elem_class(*K, std::vector{-1, 1});
std::cout << a;
// -> (x-1 ~ 0.41421356)

The underlying data can be accessed directly, e.g., with num_vector and den.

Note that elements do not need to worry about the lifetime of the number field they are contained in. The number field will be kept alive by smart pointers as long as elements in it are around:

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto gen = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1")->gen();
std::cout << gen;
// -> (x ~ 1.4142136)

renf_elem_class()

Create the zero element of the rationals.

renf_elem_class(integer/rational)

(1) renf_elem_class(short)

(2) renf_elem_class(unsigned short)

(3) renf_elem_class(int)

(4) renf_elem_class(unsigned int)

(5) renf_elem_class(long)

(6) renf_elem_class(unsigned long)

(7) renf_elem_class(long long)

(8) renf_elem_class(unsigned long long)

(9) renf_elem_class(const mpz_class&)

(10) renf_elem_class(const fmpz_t)

(11) renf_elem_class(const mpq_class&)

(12) renf_elem_class(const fmpq_t)

Create an element in the rationals.

These are shortcuts equivalent to calling renf_elem_class(*renf_class::make(), value).

renf_elem_class(const renf_class& k)

Create the zero element in the field k.

renf_elem_class(const renf_class&, integer/rational)

(1) renf_elem_class(const renf_class& k, short)

(2) renf_elem_class(const renf_class& k, unsigned short)

(3) renf_elem_class(const renf_class& k, int)

(4) renf_elem_class(const renf_class& k, unsigned int)

(5) renf_elem_class(const renf_class& k, long)

(6) renf_elem_class(const renf_class& k, unsigned long)

(7) renf_elem_class(const renf_class& k, long long)

(8) renf_elem_class(const renf_class& k, unsigned long long)

(9) renf_elem_class(const renf_class& k, const mpz_class&)

(10) renf_elem_class(const renf_class& k, const fmpz_t)

(11) renf_elem_class(const renf_class& k, const mpq_class&)

(12) renf_elem_class(const renf_class& k, const fmpq_t)

Create a rational element in the field k.

renf_elem_class(const renf_class& k, const renf_elem_class&)

Create an element in the field k from an existing element.

This attempts to coerce the element into the field k. Currently, this is only implemented in trivial cases.

renf_elem_class(const renf_class& k, const string&)

Create an element in the field k from a string representation of its underlying polynomial.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
eantic::renf_elem_class(*K, "x") == K->gen()
// -> true

renf_elem_class(vector<integer/rational>)

(1) renf_elem_class(const renf_class& k, const vector<int> &)

(2) renf_elem_class(const renf_class& k, const vector<unsigned int> &)

(3) renf_elem_class(const renf_class& k, const vector<long> &)

(4) renf_elem_class(const renf_class& k, const vector<unsigned long> &)

(5) renf_elem_class(const renf_class& k, const vector<mpz_class> &)

(6) renf_elem_class(const renf_class& k, const vector<mpq_class> &)

Create the element $\sum c_i a^i$ where $a$ is the generator.

The number of coefficients must not exceed the degree of the field.

operator=(integer/rational)

(1) renf_elem_class& operator=(short)

(2) renf_elem_class& operator=(unsigned short)

(3) renf_elem_class& operator=(int)

(4) renf_elem_class& operator=(unsigned int)

(5) renf_elem_class& operator=(long)

(6) renf_elem_class& operator=(unsigned long)

(7) renf_elem_class& operator=(long long)

(8) renf_elem_class& operator=(unsigned long long)

(9) renf_elem_class& operator=(const mpz_class&)

(10) renf_elem_class& operator=(const mpq_class&)

(11) renf_elem_class& operator=(const fmpz_t)

(12) renf_elem_class& operator=(const fmpq_t)

Reset this element to a rational number.

The parent of the element is not preserved, i.e., it’s always the rational field. To create a rational element in the number field, use the corresponding constructor.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
auto a = K->gen();
a.parent() == *K
// -> true
a = 1;
a.parent() == *K
// -> false
a = eantic::renf_elem_class(*K, 1);
a.parent() == *K
// -> true

operator=(number field element)

(1) renf_elem_class& operator=(const renf_elem_class&)

(2) renf_elem_class& operator=(renf_elem_class&&)

Reset this element to another element.

The parent of the element is not preserved, i.e., the resulting parent is always the parent of the argument.

const renf_class& parent() const

Return the containing number field.

bool is_zero() const

Return whether this element is the zero element in its field.

bool is_one() const

Return whether this element is the one element in its field.

bool is_integer() const

Return whether this element is a rational integer.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
K->gen().is_integer()
// -> false

K->one().is_integer()
// -> true

bool is_rational() const

Return whether this element is a rational number.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
K->gen().is_rational()
// -> false

K->one().is_rational()
// -> true

mpz_class num() const

Return the numerator of this element.

The element must be rational for this to work, see num_content for non-rational elements.

mpz_class den() const

Return the denominator of this element.

For non-rational elements, this returns the least common multiple of the denominators of the coefficients when written as $\sum c_i a^i$ where $a$ is the field generator.

operator mpz_class() const

Convert this element to a GMP integer.

The element must be an integer.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << static_cast<mpz_class>(K->one());
// -> 1

operator mpq_class() const

Convert this element to a GMP rational number.

The element must be a rational.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << static_cast<mpq_class>(K->one());
// -> 1

vector<mpz_class> num_vector() const

Return the numerators of the coefficients of this element when written as $\sum c_i a^i$ where $a$ is the field generator.

string to_string() const

(1) string to_string(int flags) const

(2) operator std::string() const

Return a string representation of this element.

The optional flag of to_string() is passed on to renf_elem_get_str_pretty internally. Other than that, to_string() and the explicit string conversion are just aliases of each other:

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
K->gen().to_string() == static_cast<std::string>(K->gen())
// -> true

mpz_class num_content() const

Return the greatest common divisor of num_vector.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << (4*K->gen() + 2).num_content();
// -> 2
std::cout << K->zero().num_content();
// -> 0

mpz_class floor() const

Return the integer floor of this element.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << K->gen().floor();
// -> 1
std::cout << (-K->gen()).floor();
// -> -2

mpz_class ceil() const

Return the integer ceil of this element.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << K->gen().ceil();
// -> 2
std::cout << (-K->gen()).ceil();
// -> -1

int sgn() const

Return the sign of this element.

Returns one of -1, 0 or +1.

operator double() const

Return the double closest to this element.

Ties are broken with ARF_RND_NEAR.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << static_cast<double>(K->gen());
// -> 1.41421

renf_elem_class operator-() const

Return the negative of this element.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
std::cout << -K->gen();
// -> (-x ~ -1.4142136)

renf_elem_class operator+() const

Return the positive of this element.

Returns the element unchanged.

operator bool() const

Return whether this element is non-zero.

#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
bool(K->gen())
// -> true

Arithmetic in the Number Field

(1) renf_elem_class& operator+=(const renf_elem_class&)

(2) renf_elem_class& operator-=(const renf_elem_class&)

(3) renf_elem_class& operator*=(const renf_elem_class&)

(4) renf_elem_class& operator/=(const renf_elem_class&)

ELements in a number field support the usual arithmetic operators +, -, *, /.

Internally, these operators are derived from the corresponding inplace operators +=, -=, *=, /=. Note that arithmetic between elements in different number fields is currently only supported in trivial cases.

Relational Operators

(1) friend bool operator==(const renf_elem_class&, const renf_elem_class&)

(2) friend bool operator<(const renf_elem_class&, const renf_elem_class&)

Elements can be compared with the usual operators ==, !=, <, <=, >=, >.

Internally, these operators are all derived from the definition of == and <.

mpz_class floordiv(const renf_elem_class& rhs) const

Return the integer floor of the division of this element by rhs.

renf_elem_class pow(int exponent) const

Return an integer power of this element.

Arithmetic with Integers & Rationals

(1) renf_elem_class& operator+=(short)

(2) renf_elem_class& operator-=(short)

(3) renf_elem_class& operator*=(short)

(4) renf_elem_class& operator/=(short)

(5) renf_elem_class& operator+=(unsigned short)

(6) renf_elem_class& operator-=(unsigned short)

(7) renf_elem_class& operator*=(unsigned short)

(8) renf_elem_class& operator/=(unsigned short)

(9) renf_elem_class& operator+=(int)

(10) renf_elem_class& operator-=(int)

(11) renf_elem_class& operator*=(int)

(12) renf_elem_class& operator/=(int)

(13) renf_elem_class& operator+=(unsigned int)

(14) renf_elem_class& operator-=(unsigned int)

(15) renf_elem_class& operator*=(unsigned int)

(16) renf_elem_class& operator/=(unsigned int)

(17) renf_elem_class& operator+=(long)

(18) renf_elem_class& operator-=(long)

(19) renf_elem_class& operator*=(long)

(20) renf_elem_class& operator/=(long)

(21) renf_elem_class& operator+=(unsigned long)

(22) renf_elem_class& operator-=(unsigned long)

(23) renf_elem_class& operator*=(unsigned long)

(24) renf_elem_class& operator/=(unsigned long)

(25) renf_elem_class& operator+=(long long)

(26) renf_elem_class& operator-=(long long)

(27) renf_elem_class& operator*=(long long)

(28) renf_elem_class& operator/=(long long)

(29) renf_elem_class& operator+=(unsigned long long)

(30) renf_elem_class& operator-=(unsigned long long)

(31) renf_elem_class& operator*=(unsigned long long)

(32) renf_elem_class& operator/=(unsigned long long)

(33) renf_elem_class& operator+=(const mpz_class&)

(34) renf_elem_class& operator-=(const mpz_class&)

(35) renf_elem_class& operator*=(const mpz_class&)

(36) renf_elem_class& operator/=(const mpz_class&)

(37) renf_elem_class& operator+=(const mpq_class&)

(38) renf_elem_class& operator-=(const mpq_class&)

(39) renf_elem_class& operator*=(const mpq_class&)

(40) renf_elem_class& operator/=(const mpq_class&)

Elements in a number field and integers/rationals can be combined with the usual arithmetic operators +, -, *, / and the corresponding inplace operators +=, -=, *=, /=.

Relational Operators with Integers & Rationals

(1) friend bool operator==(const renf_elem_class&, short)

(2) friend bool operator<(const renf_elem_class&, short)

(3) friend bool operator>(const renf_elem_class&, short)

(4) friend bool operator==(const renf_elem_class&, unsigned short)

(5) friend bool operator<(const renf_elem_class&, unsigned short)

(6) friend bool operator>(const renf_elem_class&, unsigned short)

(7) friend bool operator==(const renf_elem_class&, int)

(8) friend bool operator<(const renf_elem_class&, int)

(9) friend bool operator>(const renf_elem_class&, int)

(10) friend bool operator==(const renf_elem_class&, unsigned int)

(11) friend bool operator<(const renf_elem_class&, unsigned int)

(12) friend bool operator>(const renf_elem_class&, unsigned int)

(13) friend bool operator==(const renf_elem_class&, long)

(14) friend bool operator<(const renf_elem_class&, long)

(15) friend bool operator>(const renf_elem_class&, long)

(16) friend bool operator==(const renf_elem_class&, unsigned long)

(17) friend bool operator<(const renf_elem_class&, unsigned long)

(18) friend bool operator>(const renf_elem_class&, unsigned long)

(19) friend bool operator==(const renf_elem_class&, long long)

(20) friend bool operator<(const renf_elem_class&, long long)

(21) friend bool operator>(const renf_elem_class&, long long)

(22) friend bool operator==(const renf_elem_class&, unsigned long long)

(23) friend bool operator<(const renf_elem_class&, unsigned long long)

(24) friend bool operator>(const renf_elem_class&, unsigned long long)

(25) friend bool operator==(const renf_elem_class&, const mpz_class&)

(26) friend bool operator<(const renf_elem_class&, const mpz_class&)

(27) friend bool operator>(const renf_elem_class&, const mpz_class&)

(28) friend bool operator==(const renf_elem_class&, const mpq_class&)

(29) friend bool operator<(const renf_elem_class&, const mpq_class&)

(30) friend bool operator>(const renf_elem_class&, const mpq_class&)

Elements in a number field and integers/rationals can be compared with the usual relational operators ==, !=, <, <=, >=, >.

I/O

(1) friend ostream& operator<<(ostream&, const renf_elem_class&)

(2) friend istream& operator>>(istream&, renf_elem_class&)

Elements can be written to streams and read from streams.

However, reading non-rational elements from a stream is discouraged as it relies on the deprecated set_pword() method.

#include <strstream>
#include <e-antic/renf_class.hpp>
#include <e-antic/renf_elem_class.hpp>

auto K = eantic::renf_class::make("x^2 - 2", "x", "1.4 +/- 1");
auto a = K->gen();
std::strstream s;
s << a << std::ends;
std::cout << s.str();
// -> (x ~ 1.4142136)

s.seekp(0);
// Before we can read from the stream, we must attach the number field to it.
K->set_pword(s);
s >> a;

friend void swap(renf_elem_class& lhs, renf_elem_class& rhs)

Efficiently swap two number field elements.