55 cout <<
"\n" << string(70,
'=') <<
"\n";
56 cout <<
" " << title <<
"\n";
57 cout << string(70,
'=') <<
"\n";
62 cout <<
"\n" << label <<
": " << poly.
to_str() <<
"\n";
66 cout <<
"\n" << string(70,
'*') <<
"\n";
67 cout <<
"* Gen_MultiPolynomial Examples\n";
68 cout <<
"* Demonstrating multivariate polynomial operations\n";
69 cout << string(70,
'*') <<
"\n";
77 const size_t nvars = 2;
81 cout <<
"Variable x: " << x.
to_str() <<
"\n";
82 cout <<
"Variable y: " <<
y.to_str() <<
"\n";
108 print_section(
"Example 3: Algebraic Identity - (a² - b²) = (a+b)(a-b)");
137 cout <<
"\nAre they equal? " << (
expanded ==
manual ?
"YES" :
"NO") <<
"\n";
152 cout <<
"\nVerification: (x + y)³ expands correctly\n";
168 double val1 = f(
pt1);
169 double val2 = f(
pt2);
172 cout <<
"f(1, 2) = " << val1 <<
"\n";
173 cout <<
"f(2, 3) = " << val2 <<
"\n";
174 cout <<
"f(0, 0) = " <<
val3 <<
"\n";
179 print_section(
"Example 7: Algebraic Application - Distance Metric");
218 cout <<
"Polynomial: x²y + xy² + y³\n\n";
219 cout <<
"Lex ordering (x before y): "
221 cout <<
"Grlex ordering (total deg): "
223 cout <<
"Grevlex ordering (std CAS): "
225 cout <<
"\nNote: The orderings determine term precedence in Gröbner basis\n"
226 <<
"algorithms and polynomial division. Lex is used for elimination,\n"
227 <<
"Grevlex is the standard CAS choice.\n";
248 cout <<
"Evaluation at (0.6, 0.8, 0): " <<
sphere_val <<
"\n";
249 cout <<
"(Expected ~0 for points on sphere)\n";
260 cout <<
"\nPolynomial properties:\n";
261 cout <<
" Number of variables: " << p.
num_vars() <<
"\n";
262 cout <<
" Number of terms: " << p.
num_terms() <<
"\n";
263 cout <<
" Total degree: " << p.
degree() <<
"\n";
264 cout <<
" Degree in x: " << p.
degree_in(0) <<
"\n";
265 cout <<
" Degree in y: " << p.
degree_in(1) <<
"\n";
266 cout <<
" Is zero? " << (p.
is_zero() ?
"YES" :
"NO") <<
"\n";
267 cout <<
" Is constant? " << (p.
is_constant() ?
"YES" :
"NO") <<
"\n";
268 cout <<
" Leading coefficient: " << p.
leading_coeff() <<
"\n";
280 cout <<
"\nResulting polynomial is zero? "
286 print_section(
"Example 12: Scalar Multiplication and Division");
297 cout <<
"\nAre they equal? "
298 << (
divided == base_poly ?
"YES" :
"NO") <<
"\n";
308 cout <<
"\nTerms in descending order (by monomial ordering):\n";
313 cout <<
"exponents=[" << idx[0] <<
", " << idx[1] <<
"]\n";
333 cout <<
"\nEvaluation at optimum (1, 1): " <<
val_at_optimum <<
"\n";
341 const size_t n_uni = 1;
351 cout <<
"Evaluation at x=3: " <<
uni_val <<
"\n";
352 cout <<
"(Expected: 9 + 6 + 1 = 16)\n";
367 cout <<
"\nzero_poly.is_zero() = " << (
zero_poly.is_zero() ?
"true" :
"false") <<
"\n";
368 cout <<
"const_poly.is_constant() = " << (
const_poly.is_constant() ?
"true" :
"false") <<
"\n";
369 cout <<
"one_poly.is_constant() = " << (
one_poly.is_constant() ?
"true" :
"false") <<
"\n";
374 print_section(
"Example 17: Groebner Basis and Ideal Membership");
386 cout <<
"Generators:\n";
387 cout <<
" g1 = " <<
gens(0).to_str() <<
"\n";
388 cout <<
" g2 = " <<
gens(1).to_str() <<
"\n";
389 cout <<
"\nAutoreduced Groebner basis (Lex):\n";
390 for (
size_t i = 0; i <
gb_raw.size(); ++i)
391 cout <<
" H[" << i <<
"] = " <<
gb_raw(i).to_str() <<
"\n";
392 cout <<
"\nReduced Groebner basis (Lex):\n";
393 for (
size_t i = 0; i <
gb.size(); ++i)
394 cout <<
" G[" << i <<
"] = " <<
gb(i).to_str() <<
"\n";
400 cout <<
"ideal_member(x^2*y - x) = "
402 cout <<
"ideal_member(x + y) = "
408 print_section(
"Example 18: Integer Factorization with Inactive Variables");
417 cout <<
"Factorization of x^2 - 1 in Z[x,y]:\n";
418 for (
auto it =
split_factors.get_it(); it.has_curr(); it.next_ne())
419 cout <<
" factor = " << it.get_curr().factor.to_str()
420 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
425 cout <<
"\nFactorization of (x + 1)^2 in Z[x,y]:\n";
427 cout <<
" factor = " << it.get_curr().factor.to_str()
428 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
433 cout <<
"\nFactorization of x^2 - y^2 in Z[x,y]:\n";
435 cout <<
" factor = " << it.get_curr().factor.to_str()
436 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
440 cout <<
"\nFactorization of -6*(x^2 - y^2) in Z[x,y]:\n";
442 cout <<
" factor = " << it.get_curr().factor.to_str()
443 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
448 cout <<
"\nFactorization of 6*(x^2 - 1) viewed in Z[x,y]:\n";
450 cout <<
" factor = " << it.get_curr().factor.to_str()
451 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
457 cout <<
"\nFactorization of (2*x + 3*y + 1)(4*x + 5*y + 2) in Z[x,y]:\n";
459 cout <<
" factor = " << it.get_curr().factor.to_str()
460 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
467 cout <<
"\nFactorization of (2*x^2 + 3*y + 1)(3*x^2 + 5*y + 2) in Z[x,y]:\n";
469 cout <<
" factor = " << it.get_curr().factor.to_str()
470 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
476 cout <<
"\nFactorization of (x^2 + y + 1)(x + y^2 + 2) in Z[x,y]:\n";
478 cout <<
" factor = " << it.get_curr().factor.to_str()
479 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
485 cout <<
"\nFactorization of (x^2 + 2*y + 1)(x^2 + 2*y + 3) in Z[x,y]:\n";
487 cout <<
" factor = " << it.get_curr().factor.to_str()
488 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
494 cout <<
"\nFactorization of (x^3 + x + 2*y + 1)(x^3 + x + 2*y + 3) in Z[x,y]:\n";
496 cout <<
" factor = " << it.get_curr().factor.to_str()
497 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
504 cout <<
"\nGen_MultiPolynomial supports:\n"
505 <<
" • Construction via variables, monomials, and initializer lists\n"
506 <<
" • Full arithmetic: +, -, *, scalar /, powers\n"
507 <<
" • Evaluation at points (Array<Coefficient>)\n"
508 <<
" • Degree queries and term inspection\n"
509 <<
" • Three monomial orderings (Lex, Grlex, Grevlex)\n"
510 <<
" • Promotion to higher variable counts\n"
511 <<
" • Groebner bases, ideal membership, and normal forms\n"
512 <<
" • Pretty-printing with automatic variable names\n"
514 <<
" • Algebraic computation and symbolic manipulation\n"
515 <<
" • Polynomial interpolation and least-squares fitting\n"
516 <<
" • Optimization (Rosenbrock, quadratic forms)\n"
517 <<
" • Foundation for Gröbner bases and factorization\n";
519 cout <<
"\n" << string(70,
'*') <<
"\n";
520 cout <<
"* End of Examples\n";
521 cout << string(70,
'*') <<
"\n\n";
Simple dynamic array with automatic resizing and functional operations.
Sparse multivariate polynomial.
static Array< Gen_MultiPolynomial > reduced_groebner_basis(const Array< Gen_MultiPolynomial > &generators)
Reduced Gröbner basis (minimized and normalized).
Gen_MultiPolynomial reduce_modulo(const Array< Gen_MultiPolynomial > &divisors) const
Polynomial reduction modulo an ideal (one-liner).
size_t degree_in(size_t var) const noexcept
Degree in a specific variable.
DynList< FactorTerm > factorize() const
Main multivariate factorization over the integers.
bool is_constant() const noexcept
True if constant or zero (total degree 0).
std::string to_str(const DynList< std::string > &names=DynList< std::string >()) const
Human-readable string.
static Gen_MultiPolynomial monomial(size_t nvars, const Array< size_t > &idx, const Coefficient &c=Coefficient(1))
A single monomial .
size_t num_terms() const noexcept
Number of non-zero terms.
bool is_zero() const noexcept
True if this is the zero polynomial.
Coefficient leading_coeff() const
Leading coefficient.
static Gen_MultiPolynomial variable(size_t nvars, const size_t var)
The polynomial (a single variable).
static Array< Gen_MultiPolynomial > groebner_basis(const Array< Gen_MultiPolynomial > &generators)
Gröbner basis computation via Buchberger's algorithm.
size_t num_vars() const noexcept
Number of variables.
Gen_MultiPolynomial pow(size_t n) const
Exponentiation by repeated squaring.
static bool ideal_member(const Gen_MultiPolynomial &f, const Array< Gen_MultiPolynomial > &generators)
Test membership in an ideal via Gröbner basis.
size_t degree() const noexcept
Total degree (maximum sum of exponents over all terms).
Gen_MultiPolynomial< long long > IntMultipoly
void print_result(const string &label, const Multipoly &poly)
void print_section(const string &title)
Gen_MultiPolynomial< double, Lex_Order > Multipoly_Lex
Gen_MultiPolynomial< double > Multipoly
Main namespace for Aleph-w library functions.
DynList< T > repeated(const Container< T > &c)
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
double pow2(const double x)
Return x^2.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
double pow3(const double x)
Return x^3.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Dynamic array container with automatic resizing.
Sparse multivariate polynomial over an arbitrary coefficient ring.