GCC Code Coverage Report
Directory: . Exec Total Coverage
File: build-coverage/deps/include/poly/polyxx/polynomial.h Lines: 1 1 100.0 %
Date: 2021-08-17 Branches: 0 0 0.0 %

Line Exec Source
1
#pragma once
2
3
#include <iosfwd>
4
#include <vector>
5
6
#include "../polynomial.h"
7
#include "assignment.h"
8
#include "integer.h"
9
#include "interval.h"
10
#include "interval_assignment.h"
11
#include "sign_condition.h"
12
#include "value.h"
13
#include "variable.h"
14
15
namespace poly {
16
17
  /**
18
   * Implements a wrapper for lp_polynomial_t.
19
   */
20
42899
  class Polynomial {
21
    /** The actual polynomial. */
22
    deleting_unique_ptr<lp_polynomial_t> mPoly;
23
24
   public:
25
    /** Create from a lp_polynomial_t pointer, claiming it's ownership. */
26
    Polynomial(lp_polynomial_t* poly);
27
    /** Copy from an internal lp_polynomial_t pointer. */
28
    Polynomial(const lp_polynomial_t* poly);
29
    /** Construct a zero polynomial from an interval context pointer. */
30
    Polynomial(const lp_polynomial_context_t* c);
31
    /** Construct a zero polynomial from a custom context. */
32
    Polynomial(const Context& c);
33
    /** Construct a zero polynomial. */
34
    Polynomial();
35
36
    /** Construct from a variable and a custom context. */
37
    Polynomial(const Context& c, Variable v);
38
    /** Construct from a variable. */
39
    Polynomial(Variable v);
40
41
    /** Construct i * v^n from a custom context. */
42
    Polynomial(const Context& c, Integer i, Variable v, unsigned n);
43
    /** Construct i * v^n. */
44
    Polynomial(Integer i, Variable v, unsigned n);
45
46
    /** Construct from an integer and a custom context. */
47
    Polynomial(const Context& c, Integer i);
48
    /** Construct from an integer. */
49
    Polynomial(Integer i);
50
51
    /** Construct from an integer and a custom context. */
52
    Polynomial(const Context& c, long i);
53
    /** Construct from an integer. */
54
    Polynomial(long i);
55
56
    /** Copy from a Polynomial. */
57
    Polynomial(const Polynomial& p);
58
    /** Move from a Polynomial. */
59
    Polynomial(Polynomial&& p);
60
61
    /** Copy from a Polynomial. */
62
    Polynomial& operator=(const Polynomial& p);
63
    /** Move from a Polynomial. */
64
    Polynomial& operator=(Polynomial&& p);
65
66
    /** Get a non-const pointer to the internal lp_polynomial_t. Handle with
67
     * care!
68
     */
69
    lp_polynomial_t* get_internal();
70
    /** Get a const pointer to the internal lp_polynomial_t. */
71
    const lp_polynomial_t* get_internal() const;
72
    /** Release the lp_polynomial_t pointer. This yields ownership of the
73
     * returned pointer. */
74
    lp_polynomial_t* release();
75
  };
76
77
  /** Swap two polynomials. */
78
  void swap(Polynomial& lhs, Polynomial& rhs);
79
80
  /** Return the hash of a polynomial. */
81
  std::size_t hash(const Polynomial& p);
82
83
  /** Stream the given Polynomial to an output stream. */
84
  std::ostream& operator<<(std::ostream& os, const Polynomial& p);
85
86
  /** Check if the given polynomial is zero. */
87
  bool is_zero(const Polynomial& p);
88
  /** Check if the given polynomial is constant. */
89
  bool is_constant(const Polynomial& p);
90
  /** Check if the given polynomial is linear. */
91
  bool is_linear(const Polynomial& p);
92
  /** Check if the leading coefficient is constant. */
93
  bool is_lc_constant(const Polynomial& p);
94
  /** Get the sign of the leading coefficient. Assumes is_lc_constant(p). */
95
  int lc_sgn(const Polynomial& p);
96
  /** Obtain the degree of the given polynomial in its main variable. */
97
  std::size_t degree(const Polynomial& p);
98
  /** Obtain the main variable of the given polynomial. */
99
  Variable main_variable(const Polynomial& p);
100
  /** Obtain the k'th coefficient of a polynomial. */
101
  Polynomial coefficient(const Polynomial& p, std::size_t k);
102
  /** Obtain the leading coefficient of a polynomial. */
103
  Polynomial leading_coefficient(const Polynomial& p);
104
  /** Obtain all non-constant coefficients of a polynomial. */
105
  std::vector<Polynomial> coefficients(const Polynomial& p);
106
107
  /** Check if the given polynomial is univariate. */
108
  bool is_univariate(const Polynomial& p);
109
  /** Converts a polynomial to a univariate polynomial. Assumes
110
   * is_univariate(p). */
111
  UPolynomial to_univariate(const Polynomial& p);
112
  /** Check if the given polynomial is univariate over a given assignment and
113
   * the main variable is unassigned. */
114
  bool is_univariate_over_assignment(const Polynomial& p, const Assignment& a);
115
  /** Check if the given polynomial is completely assigned over a given
116
   * assignment. */
117
  bool is_assigned_over_assignment(const Polynomial& p, const Assignment& a);
118
  /** Compute the sign of a polynomial over an assignment. */
119
  int sgn(const Polynomial& p, const Assignment& a);
120
  /** Evaluate a polynomial over an assignment. */
121
  Value evaluate(const Polynomial& p, const Assignment& a);
122
  /** Evaluate a polynomial constraint over an assignment. */
123
  bool evaluate_constraint(const Polynomial& p, const Assignment& a,
124
                           SignCondition sc);
125
  /** Evaluate a polynomial over an interval assignment. The result is only an
126
   * approximation. */
127
  Interval evaluate(const Polynomial& p, const IntervalAssignment& a);
128
129
  /** Compare polynomials. */
130
  bool operator==(const Polynomial& lhs, const Polynomial& rhs);
131
  /** Compare polynomials. */
132
  bool operator!=(const Polynomial& lhs, const Polynomial& rhs);
133
  /** Compare polynomials. */
134
  bool operator<(const Polynomial& lhs, const Polynomial& rhs);
135
  /** Compare polynomials. */
136
  bool operator<=(const Polynomial& lhs, const Polynomial& rhs);
137
  /** Compare polynomials. */
138
  bool operator>(const Polynomial& lhs, const Polynomial& rhs);
139
  /** Compare polynomials. */
140
  bool operator>=(const Polynomial& lhs, const Polynomial& rhs);
141
142
  /** Add two polynomials. */
143
  Polynomial operator+(const Polynomial& lhs, const Polynomial& rhs);
144
  /** Add a polynomial and an integer. */
145
  Polynomial operator+(const Polynomial& lhs, const Integer& rhs);
146
  /** Add an integer and a polynomial. */
147
  Polynomial operator+(const Integer& lhs, const Polynomial& rhs);
148
  /** Add and assign two polynomials. */
149
  Polynomial& operator+=(Polynomial& lhs, const Polynomial& rhs);
150
  /** Compute lhs += rhs1 * rhs2. */
151
  Polynomial& add_mul(Polynomial& lhs, const Polynomial& rhs1, const Polynomial& rhs2);
152
153
  /** Unary negation of a polynomial. */
154
  Polynomial operator-(const Polynomial& p);
155
  /** Subtract two polynomials. */
156
  Polynomial operator-(const Polynomial& lhs, const Polynomial& rhs);
157
  /** Subtract an integer from a polynomial. */
158
  Polynomial operator-(const Polynomial& lhs, const Integer& rhs);
159
  /** Subtract a polynomial from an integer. */
160
  Polynomial operator-(const Integer& lhs, const Polynomial& rhs);
161
  /** Subtract and assign two polynomials. */
162
  Polynomial& operator-=(Polynomial& lhs, const Polynomial& rhs);
163
  /** Compute lhs -= rhs1 * rhs2. */
164
  Polynomial& sub_mul(Polynomial& lhs, const Polynomial& rhs1, const Polynomial& rhs2);
165
166
  /** Multiply two polynomials. */
167
  Polynomial operator*(const Polynomial& lhs, const Polynomial& rhs);
168
  /** Multiply a polynomial and an integer. */
169
  Polynomial operator*(const Polynomial& lhs, const Integer& rhs);
170
  /** Multiply an integer and a polynomial. */
171
  Polynomial operator*(const Integer& lhs, const Polynomial& rhs);
172
  /** Multiply and assign two polynomials. */
173
  Polynomial& operator*=(Polynomial& lhs, const Polynomial& rhs);
174
175
  /** Multiply with x^n where x is the main variable. */
176
  Polynomial shl(const Polynomial& lhs, unsigned n);
177
  /** Compute a polynomial to some power. */
178
  Polynomial pow(const Polynomial& lhs, unsigned exp);
179
180
  /** Checks whether lhs divides rhs. */
181
  bool divides(const Polynomial& lhs, const Polynomial& rhs);
182
  /** Compute the quotient of two polynomials, assuming divides(rhs, lhs). */
183
  Polynomial div(const Polynomial& lhs, const Polynomial& rhs);
184
  /** Compute the remainder of two polynomials. */
185
  Polynomial rem(const Polynomial& lhs, const Polynomial& rhs);
186
  /** Compute the pseudo-remainder of two polynomials. */
187
  Polynomial prem(const Polynomial& lhs, const Polynomial& rhs);
188
  /** Compute the sparse pseudo-remainder of two polynomials. */
189
  Polynomial sprem(const Polynomial& lhs, const Polynomial& rhs);
190
  /** Compute quotient and remainder of two polynomials. */
191
  std::pair<Polynomial, Polynomial> div_rem(const Polynomial& lhs,
192
                                            const Polynomial& rhs);
193
194
  /** Compute the derivative of a polynomial (in its main variable). */
195
  Polynomial derivative(const Polynomial& p);
196
197
  /** Compute the gcd of two polynomials. */
198
  Polynomial gcd(const Polynomial& p, const Polynomial& q);
199
  /** Compute the lcm of two polynomials. */
200
  Polynomial lcm(const Polynomial& p, const Polynomial& q);
201
202
  /** Compute the content of a polynomial. */
203
  Polynomial content(const Polynomial& p);
204
  /** Compute the primitive part of a polynomial. */
205
  Polynomial primitive_part(const Polynomial& p);
206
  /** Compute the content and the primitive part of a polynomial. */
207
  std::pair<Polynomial, Polynomial> content_primitive_part(const Polynomial& p);
208
209
  /** Compute the resultant of two polynomials. */
210
  Polynomial resultant(const Polynomial& p, const Polynomial& q);
211
  /** Compute the discriminant of a polynomial. */
212
  Polynomial discriminant(const Polynomial& p);
213
214
  /** Compute the principal subresultant coefficients of two polynomials. */
215
  std::vector<Polynomial> psc(const Polynomial& p, const Polynomial& q);
216
217
  /**
218
   * Compute a square-free factorization of a polynomial.
219
   * Attention: this does not yield a full factorization!
220
   */
221
  std::vector<Polynomial> square_free_factors(const Polynomial& p);
222
  /** Compute a content-free factorization of a polynomial. */
223
  std::vector<Polynomial> content_free_factors(const Polynomial& p);
224
225
  /** Isolate the real roots of a Polynomial with respect to an Assignment for
226
   * all but the main variable. */
227
  std::vector<Value> isolate_real_roots(const Polynomial& p,
228
                                        const Assignment& a);
229
230
  std::vector<Interval> infeasible_regions(const Polynomial& p,
231
                                           const Assignment& a,
232
                                           SignCondition sc);
233
234
}  // namespace poly