1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Polynomial constructors and query methods.
use super::types::*;
#[allow(unused_imports)]
use crate::prelude::*;
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::{One, Zero};
impl super::Polynomial {
/// Create the zero polynomial.
#[inline]
pub fn zero() -> Self {
Self {
terms: Vec::new(),
order: MonomialOrder::default(),
}
}
/// Create the one polynomial.
#[inline]
pub fn one() -> Self {
Self::constant(BigRational::one())
}
/// Create a constant polynomial.
pub fn constant(c: BigRational) -> Self {
if c.is_zero() {
Self::zero()
} else {
Self {
terms: vec![Term::constant(c)],
order: MonomialOrder::default(),
}
}
}
/// Create a polynomial from a single variable.
pub fn from_var(var: Var) -> Self {
Self {
terms: vec![Term::from_var(var)],
order: MonomialOrder::default(),
}
}
/// Create a polynomial x^k.
pub fn from_var_power(var: Var, power: u32) -> Self {
if power == 0 {
Self::one()
} else {
Self {
terms: vec![Term::new(
BigRational::one(),
Monomial::from_var_power(var, power),
)],
order: MonomialOrder::default(),
}
}
}
/// Create a polynomial from terms. Normalizes and combines like terms.
pub fn from_terms(terms: impl IntoIterator<Item = Term>, order: MonomialOrder) -> Self {
let mut poly = Self {
terms: terms.into_iter().filter(|t| !t.is_zero()).collect(),
order,
};
poly.normalize();
poly
}
/// Create a polynomial from integer coefficients.
pub fn from_coeffs_int(coeffs: &[(i64, &[(Var, u32)])]) -> Self {
let terms: Vec<Term> = coeffs
.iter()
.map(|(c, powers)| {
Term::new(
BigRational::from_integer(BigInt::from(*c)),
Monomial::from_powers(powers.iter().copied()),
)
})
.collect();
Self::from_terms(terms, MonomialOrder::default())
}
/// Create a linear polynomial a1*x1 + a2*x2 + ... + c.
pub fn linear(coeffs: &[(BigRational, Var)], constant: BigRational) -> Self {
let mut terms: Vec<Term> = coeffs
.iter()
.filter(|(c, _)| !c.is_zero())
.map(|(c, v)| Term::new(c.clone(), Monomial::from_var(*v)))
.collect();
if !constant.is_zero() {
terms.push(Term::constant(constant));
}
Self::from_terms(terms, MonomialOrder::default())
}
/// Create a univariate polynomial from coefficients.
/// `coeffs[i]` is the coefficient of x^i.
pub fn univariate(var: Var, coeffs: &[BigRational]) -> Self {
let terms: Vec<Term> = coeffs
.iter()
.enumerate()
.filter(|(_, c)| !c.is_zero())
.map(|(i, c)| Term::new(c.clone(), Monomial::from_var_power(var, i as u32)))
.collect();
Self::from_terms(terms, MonomialOrder::default())
}
/// Check if the polynomial is zero.
#[inline]
pub fn is_zero(&self) -> bool {
self.terms.is_empty()
}
/// Check if the polynomial is a non-zero constant.
#[inline]
pub fn is_constant(&self) -> bool {
self.terms.len() == 1 && self.terms[0].monomial.is_unit()
}
/// Get the constant value of the polynomial.
///
/// Returns the constant coefficient if the polynomial is constant,
/// or zero if the polynomial is zero.
pub fn constant_value(&self) -> BigRational {
if self.is_zero() {
BigRational::zero()
} else if self.is_constant() {
self.terms[0].coeff.clone()
} else {
BigRational::zero()
}
}
/// Check if the polynomial is one.
pub fn is_one(&self) -> bool {
self.terms.len() == 1 && self.terms[0].monomial.is_unit() && self.terms[0].coeff.is_one()
}
/// Check if the polynomial is univariate.
pub fn is_univariate(&self) -> bool {
if self.terms.is_empty() {
return true;
}
let mut var: Option<Var> = None;
for term in &self.terms {
for vp in term.monomial.vars() {
match var {
None => var = Some(vp.var),
Some(v) if v != vp.var => return false,
_ => {}
}
}
}
true
}
/// Check if the polynomial is linear (all terms have degree <= 1).
pub fn is_linear(&self) -> bool {
self.terms.iter().all(|t| t.monomial.is_linear())
}
/// Get the number of terms.
#[inline]
pub fn num_terms(&self) -> usize {
self.terms.len()
}
/// Get the terms.
#[inline]
pub fn terms(&self) -> &[Term] {
&self.terms
}
/// Get the total degree of the polynomial.
pub fn total_degree(&self) -> u32 {
self.terms
.iter()
.map(|t| t.monomial.total_degree())
.max()
.unwrap_or(0)
}
/// Get the degree with respect to a specific variable.
pub fn degree(&self, var: Var) -> u32 {
self.terms
.iter()
.map(|t| t.monomial.degree(var))
.max()
.unwrap_or(0)
}
/// Get the maximum variable in the polynomial, or NULL_VAR if constant.
pub fn max_var(&self) -> Var {
self.terms
.iter()
.map(|t| t.monomial.max_var())
.filter(|&v| v != NULL_VAR)
.max()
.unwrap_or(NULL_VAR)
}
/// Get all variables in the polynomial.
pub fn vars(&self) -> Vec<Var> {
let mut vars: Vec<Var> = self
.terms
.iter()
.flat_map(|t| t.monomial.vars().iter().map(|vp| vp.var))
.collect();
vars.sort_unstable();
vars.dedup();
vars
}
/// Get the leading term (with respect to monomial order).
#[inline]
pub fn leading_term(&self) -> Option<&Term> {
self.terms.first()
}
/// Get the leading coefficient.
pub fn leading_coeff(&self) -> BigRational {
self.terms
.first()
.map(|t| t.coeff.clone())
.unwrap_or_else(BigRational::zero)
}
/// Get the leading monomial.
pub fn leading_monomial(&self) -> Option<&Monomial> {
self.terms.first().map(|t| &t.monomial)
}
/// Get the constant term.
pub fn constant_term(&self) -> BigRational {
self.terms
.iter()
.find(|t| t.monomial.is_unit())
.map(|t| t.coeff.clone())
.unwrap_or_else(BigRational::zero)
}
/// Get the coefficient of x^k for a univariate polynomial.
pub fn univ_coeff(&self, var: Var, k: u32) -> BigRational {
for term in &self.terms {
if term.monomial.degree(var) == k && term.monomial.num_vars() <= 1 {
return term.coeff.clone();
}
}
BigRational::zero()
}
/// Get the coefficient polynomial for x^k.
/// For polynomial p(y_1, ..., y_n, x), returns coefficient of x^k.
pub fn coeff(&self, var: Var, k: u32) -> super::Polynomial {
let terms: Vec<Term> = self
.terms
.iter()
.filter(|t| t.monomial.degree(var) == k)
.map(|t| {
let new_mon = if let Some(m) = t.monomial.div(&Monomial::from_var_power(var, k)) {
m
} else {
Monomial::unit()
};
Term::new(t.coeff.clone(), new_mon)
})
.collect();
super::Polynomial::from_terms(terms, self.order)
}
/// Get the leading coefficient with respect to variable x.
pub fn leading_coeff_wrt(&self, var: Var) -> super::Polynomial {
let d = self.degree(var);
self.coeff(var, d)
}
/// Normalize the polynomial (sort terms and combine like terms).
pub(crate) fn normalize(&mut self) {
if self.terms.is_empty() {
return;
}
// Sort by monomial order (descending)
let order = self.order;
self.terms
.sort_by(|a, b| order.compare(&b.monomial, &a.monomial));
// Combine like terms
let mut i = 0;
while i < self.terms.len() {
let mut j = i + 1;
while j < self.terms.len() && self.terms[j].monomial == self.terms[i].monomial {
let coeff = self.terms[j].coeff.clone();
self.terms[i].coeff += coeff;
j += 1;
}
// Remove combined terms
if j > i + 1 {
self.terms.drain((i + 1)..j);
}
i += 1;
}
// Remove zero terms
self.terms.retain(|t| !t.coeff.is_zero());
}
}