41# include <type_traits>
54 template <
typename Coeff>
68 template <
typename Poly>
70 print_term_array(
const Poly &p)
74 p.for_each_term([&first](
const Array<size_t> &idx,
const typename Poly::Coeff &coeff)
79 std::cout <<
"{\"coeff\": " << coeff <<
", \"exponents\": [";
80 for (
size_t i = 0; i < idx.
size(); ++i)
83 if (i + 1 < idx.
size())
91 template <
typename TermList>
93 print_factor_terms(
const TermList &terms)
97 for (
auto it = terms.get_it(); it.has_curr(); it.next_ne())
102 const auto &term = it.get_curr();
103 std::cout <<
"{\"multiplicity\": " << term.multiplicity <<
", \"factor\": ";
104 using FactorType = std::remove_cvref_t<
decltype(term.factor)>;
105 if constexpr (std::is_same_v<FactorType, IntPoly>)
106 print_coeff_array(term.factor);
108 print_term_array(term.factor);
115 print_univariate_section()
122 IntPoly outer({1, -2, 0, 0, 1});
129 IntPoly factorable = f3 * f2 * f1 * f1;
130 auto uni_factors = factorable.
factorize();
132 std::cout <<
" \"univariate\": [\n"
134 <<
" \"name\": \"sum_mul_divmod\",\n"
136 print_coeff_array(a);
139 print_coeff_array(b);
142 print_coeff_array(a + b);
145 print_coeff_array(prod);
147 <<
" \"quotient\": ";
148 print_coeff_array(q);
150 <<
" \"remainder\": ";
151 print_coeff_array(
r);
155 <<
" \"name\": \"compose_sparse\",\n"
157 print_coeff_array(outer);
160 print_coeff_array(inner);
162 <<
" \"composed\": ";
163 print_coeff_array(composed);
167 <<
" \"name\": \"factorize_integer\",\n"
169 print_coeff_array(factorable);
172 print_factor_terms(uni_factors);
179 print_multivariate_section()
181 auto x = IntMultiPoly::variable(2, 0);
182 auto y = IntMultiPoly::variable(2, 1);
184 IntMultiPoly diff_sq = x * x -
y *
y;
185 auto diff_sq_factors = diff_sq.factorize();
187 IntMultiPoly q1 = 2LL * x * x + 3LL *
y + IntMultiPoly(2, 1LL);
188 IntMultiPoly q2 = 3LL * x * x + 5LL *
y + IntMultiPoly(2, 2LL);
189 IntMultiPoly same_degree = q1 * q2;
190 auto same_degree_factors = same_degree.factorize();
192 auto xlex = LexMultiPoly::variable(2, 0);
193 auto ylex = LexMultiPoly::variable(2, 1);
195 gens(0) = xlex * xlex - ylex;
196 gens(1) = xlex * ylex - LexMultiPoly(2, 1.0);
197 auto gb = LexMultiPoly::reduced_groebner_basis(gens);
200 <<
" \"multivariate\": [\n"
202 <<
" \"name\": \"factorize_difference_of_squares\",\n"
203 <<
" \"nvars\": 2,\n"
205 print_term_array(diff_sq);
208 print_factor_terms(diff_sq_factors);
212 <<
" \"name\": \"factorize_same_degree_non_monic\",\n"
213 <<
" \"nvars\": 2,\n"
215 print_term_array(same_degree);
218 print_factor_terms(same_degree_factors);
222 <<
" \"name\": \"groebner_reduced_lex\",\n"
223 <<
" \"nvars\": 2,\n"
224 <<
" \"generators\": [";
225 for (
size_t i = 0; i < gens.size(); ++i)
227 print_term_array(gens(i));
228 if (i + 1 < gens.size())
233 for (
size_t i = 0; i < gb.size(); ++i)
235 print_term_array(gb(i));
236 if (i + 1 < gb.size())
248 for (
int i = 1; i < argc; ++i)
250 const std::string arg = argv[i];
253 std::cout <<
"Usage: polynomial_reference_probe [--json]\n";
258 std::cerr <<
"Unknown option: " << arg <<
"\n";
264 <<
" \"mode\": \"reference\",\n";
265 print_univariate_section();
266 print_multivariate_section();
267 std::cout <<
"\n}\n";
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Sparse multivariate polynomial.
Univariate polynomial over a generic coefficient ring.
Gen_Polynomial compose(const Gen_Polynomial &q) const
Composition .
size_t degree() const noexcept
Degree of the polynomial.
DynList< SfdTerm > factorize() const
Main factorization over integers.
std::pair< Gen_Polynomial, Gen_Polynomial > divmod(const Gen_Polynomial &d) const
Polynomial long division: returns (quotient, remainder).
Coefficient get_coeff(size_t exp) const noexcept
Coefficient accessor (read-only at exponent).
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
Sparse multivariate polynomial over an arbitrary coefficient ring.
Univariate polynomial ring arithmetic over generic coefficients.