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)");
122 cout <<
"\nAre they equal? " << (identity1 == identity2 ?
"YES" :
"NO") <<
"\n";
130 Multipoly expanded = binomial * binomial;
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);
170 double val3 = f(pt3);
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");
189 Multipoly distance_from_point = x_centered * x_centered +
190 y_centered * y_centered;
191 print_result(
"Distance² from (1,2): (x-1)² + (y-2)²", distance_from_point);
218 cout <<
"Polynomial: x²y + xy² + y³\n\n";
219 cout <<
"Lex ordering (x before y): "
220 << poly_lex.
to_str() <<
"\n";
221 cout <<
"Grlex ordering (total deg): "
222 << poly_grlex.to_str() <<
"\n";
223 cout <<
"Grevlex ordering (std CAS): "
224 << poly_grevlex.to_str() <<
"\n";
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";
241 Multipoly sphere = x3*x3 + y3*y3 + z3*z3 - 1.0;
247 double sphere_val = sphere(sphere_pt);
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? "
281 << (cancellation.
is_zero() ?
"YES" :
"NO") <<
"\n";
286 print_section(
"Example 12: Scalar Multiplication and Division");
295 print_result(
"Result / 2.5 (back to original)", divided);
297 cout <<
"\nAre they equal? "
298 << (divided == base_poly ?
"YES" :
"NO") <<
"\n";
305 Multipoly iter_poly = 2.0*x*x + 3.0*x*
y + 5.0*
y*
y + 1.0;
308 cout <<
"\nTerms in descending order (by monomial ordering):\n";
309 size_t term_count = 0;
312 cout <<
" Term " << (++term_count) <<
": coeff=" << coeff <<
", ";
313 cout <<
"exponents=[" << idx[0] <<
", " << idx[1] <<
"]\n";
326 Multipoly rosenbrock = one_minus_x * one_minus_x +
327 y_minus_x2 * y_minus_x2;
332 double val_at_optimum = rosenbrock(optimum);
333 cout <<
"\nEvaluation at optimum (1, 1): " << val_at_optimum <<
"\n";
341 const size_t n_uni = 1;
345 Multipoly uni_expr = uni_poly * uni_poly + 2.0 * uni_poly + 1.0;
350 double uni_val = uni_expr(uni_pt);
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");
380 gens(0) = xlex * xlex - ylex;
381 gens(1) = xlex * ylex - 1.0;
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");
415 auto split_factors =
split.factorize();
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";
423 auto repeated_factors =
repeated.factorize();
425 cout <<
"\nFactorization of (x + 1)^2 in Z[x,y]:\n";
426 for (
auto it = repeated_factors.get_it(); it.has_curr(); it.next_ne())
427 cout <<
" factor = " << it.get_curr().factor.to_str()
428 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
431 auto diff_sq_factors = diff_sq.
factorize();
433 cout <<
"\nFactorization of x^2 - y^2 in Z[x,y]:\n";
434 for (
auto it = diff_sq_factors.get_it(); it.has_curr(); it.next_ne())
435 cout <<
" factor = " << it.get_curr().factor.to_str()
436 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
438 auto scaled_diff_sq_factors = (-6LL * diff_sq).factorize();
440 cout <<
"\nFactorization of -6*(x^2 - y^2) in Z[x,y]:\n";
441 for (
auto it = scaled_diff_sq_factors.get_it(); it.has_curr(); it.next_ne())
442 cout <<
" factor = " << it.get_curr().factor.to_str()
443 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
446 auto embedded_univariate_factors = embedded_univariate.
factorize();
448 cout <<
"\nFactorization of 6*(x^2 - 1) viewed in Z[x,y]:\n";
449 for (
auto it = embedded_univariate_factors.get_it(); it.has_curr(); it.next_ne())
450 cout <<
" factor = " << it.get_curr().factor.to_str()
451 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
455 auto non_monic_affine_factors = (non_monic_affine_f1 * non_monic_affine_f2).factorize();
457 cout <<
"\nFactorization of (2*x + 3*y + 1)(4*x + 5*y + 2) in Z[x,y]:\n";
458 for (
auto it = non_monic_affine_factors.get_it(); it.has_curr(); it.next_ne())
459 cout <<
" factor = " << it.get_curr().factor.to_str()
460 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
464 auto non_monic_same_degree_factors =
465 (non_monic_same_degree_f1 * non_monic_same_degree_f2).factorize();
467 cout <<
"\nFactorization of (2*x^2 + 3*y + 1)(3*x^2 + 5*y + 2) in Z[x,y]:\n";
468 for (
auto it = non_monic_same_degree_factors.get_it(); it.has_curr(); it.next_ne())
469 cout <<
" factor = " << it.get_curr().factor.to_str()
470 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
474 auto nonlinear_factors = (nonlinear_f1 * nonlinear_f2).factorize();
476 cout <<
"\nFactorization of (x^2 + y + 1)(x + y^2 + 2) in Z[x,y]:\n";
477 for (
auto it = nonlinear_factors.get_it(); it.has_curr(); it.next_ne())
478 cout <<
" factor = " << it.get_curr().factor.to_str()
479 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
483 auto same_degree_factors = (same_degree_f1 * same_degree_f2).factorize();
485 cout <<
"\nFactorization of (x^2 + 2*y + 1)(x^2 + 2*y + 3) in Z[x,y]:\n";
486 for (
auto it = same_degree_factors.get_it(); it.has_curr(); it.next_ne())
487 cout <<
" factor = " << it.get_curr().factor.to_str()
488 <<
", multiplicity = " << it.get_curr().multiplicity <<
"\n";
492 auto cubic_same_degree_factors = (cubic_same_degree_f1 * cubic_same_degree_f2).factorize();
494 cout <<
"\nFactorization of (x^3 + x + 2*y + 1)(x^3 + x + 2*y + 3) in Z[x,y]:\n";
495 for (
auto it = cubic_same_degree_factors.get_it(); it.has_curr(); it.next_ne())
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";