88 cout <<
"\n" << string(65,
'=') <<
"\n"
89 <<
" " << title <<
"\n"
90 << string(65,
'=') <<
"\n\n";
104 section(
"1. Basic Polynomial Arithmetic");
109 cout <<
" p(x) = " << p <<
"\n";
113 cout <<
" q(x) = " << q <<
"\n\n";
116 cout <<
" p + q = " << (p + q) <<
"\n";
117 cout <<
" p - q = " << (p - q) <<
"\n";
118 cout <<
" p * q = " << (p * q) <<
"\n";
121 cout <<
" 3 * p = " << (p * 3.0) <<
"\n";
122 cout <<
" p / 2 = " << (p / 2.0) <<
"\n\n";
128 cout <<
" (" << num <<
") / (" <<
den <<
")\n";
129 cout <<
" quotient = " <<
quotient <<
"\n";
130 cout <<
" remainder = " <<
remainder <<
"\n\n";
135 cout << (
reconstructed == num ?
" [OK]" :
" [FAIL]") <<
"\n";
150 section(
"2. Symbolic Calculus");
154 cout <<
" f(x) = " << f <<
"\n\n";
158 cout <<
" f'(x) = " << fp <<
"\n";
162 cout <<
" f''(x) = " <<
fpp <<
"\n\n";
166 cout <<
" Integral of f(x) = " <<
F <<
"\n";
170 cout <<
" d/dx[integral(f)] = " <<
roundtrip <<
"\n";
171 cout <<
" Matches original? " << (
roundtrip == f ?
"Yes" :
"No") <<
"\n\n";
175 cout <<
" f(" << x <<
") = " << f(x) <<
"\n";
176 cout <<
" f'(" << x <<
") = " << fp(x) <<
"\n";
192 section(
"3. Roots and Interpolation");
202 cout <<
" Polynomial with roots {1, 2, 3}:\n";
203 cout <<
" p(x) = " << p <<
"\n\n";
206 cout <<
" Evaluation at roots:\n";
209 cout <<
" p(" <<
r <<
") = " << p(
r) <<
"\n";
215 cout <<
"\n Polynomial interpolation through (0,1), (1,0), (2,1)"
216 <<
" using Newton divided differences:\n";
219 points.
append(std::make_pair(0.0, 1.0));
220 points.
append(std::make_pair(1.0, 0.0));
221 points.
append(std::make_pair(2.0, 1.0));
224 cout <<
" L(x) = " <<
interp <<
"\n\n";
227 cout <<
" Verification:\n";
230 cout <<
" L(" << pt.first <<
") = " <<
interp(pt.first)
231 <<
" (expected " << pt.second <<
")\n";
247 section(
"4. Transfer Function (Signal Processing)");
252 cout <<
" H(s) = (" << num <<
") / (" <<
den <<
")\n\n";
254 cout <<
" Frequency response |H(jw)|^2 at sample points:\n";
255 cout <<
" " << setw(10) <<
"omega" << setw(15) <<
"|N(jw)|^2"
256 << setw(15) <<
"|D(jw)|^2" << setw(15) <<
"|H(jw)|^2" <<
"\n";
257 cout <<
" " << string(55,
'-') <<
"\n";
297 freqs.for_each([&](
double w)
302 cout <<
" " << setw(10) << fixed << setprecision(2) <<
w
303 << setw(15) << setprecision(6) << n2
305 << setw(15) <<
h2 <<
"\n";
311 cout <<
"\n At w=1 (cutoff), |H(j)|^2 should be ~0.5 (-3dB).\n";
325 section(
"5. Sparse High-Degree Polynomials");
330 terms_p.append(std::make_pair(
size_t(500), 1.0));
331 terms_p.append(std::make_pair(
size_t(1000), 1.0));
334 cout <<
" p(x) = " << p <<
"\n";
335 cout <<
" degree = " << p.
degree() <<
", terms stored = "
341 terms_q.append(std::make_pair(
size_t(1000), 1.0));
344 cout <<
" q(x) = " << q <<
"\n";
345 cout <<
" degree = " << q.
degree() <<
", terms stored = "
350 cout <<
" p - q = " <<
diff <<
"\n";
351 cout <<
" degree = " <<
diff.
degree() <<
", terms = "
352 <<
diff.num_terms() <<
"\n\n";
356 cout <<
" p'(x) = " << dp <<
"\n";
357 cout <<
" degree = " << dp.
degree() <<
", terms = "
361 cout <<
" p(1) = " << p(1.0) <<
"\n";
362 cout <<
" p(0) = " << p(0.0) <<
"\n";
368 <<
" with " <<
composed.num_terms() <<
" terms\n";
382 section(
"6. Root Analysis and Finding");
386 cout <<
" p(x) = " << p <<
"\n";
387 cout <<
" degree = " << p.
degree() <<
"\n\n";
391 cout <<
" Cauchy root bound: |roots| <= " << bound <<
"\n";
392 cout <<
" (actual roots: 1, 2, -3; max |root| = 3)\n\n";
396 <<
" (upper bound on positive roots)\n";
398 <<
" (upper bound on negative roots)\n\n";
401 cout <<
" Real roots in [-10, 10]: "
403 cout <<
" Real roots in [0, 5]: "
405 cout <<
" Real roots in [-5, 0]: "
407 cout <<
" Total real roots: "
411 cout <<
" Root finding (bisection):\n";
417 cout <<
" root in [0.5, 1.5]: " << fixed << setprecision(10)
419 cout <<
" root in [1.5, 3.0]: " << r2 <<
"\n";
420 cout <<
" root in [-5, -1]: " <<
r3 <<
"\n\n";
423 cout <<
" Root finding (Newton-Raphson):\n";
427 cout <<
" from x0=0.5: " <<
r1 <<
"\n";
428 cout <<
" from x0=2.5: " << r2 <<
"\n";
429 cout <<
" from x0=-2.0: " <<
r3 <<
"\n";
445 section(
"7. Algebraic Transformations");
453 cout <<
" p(x) = (x-1)^3*(x-2) = " << p <<
"\n";
454 cout <<
" degree = " << p.
degree()
455 <<
", roots: 1 (mult 3), 2 (mult 1)\n\n";
458 cout <<
" Square-free part: " <<
sf <<
"\n";
459 cout <<
" degree = " <<
sf.
degree() <<
" (repeated roots removed)\n\n";
467 cout <<
" Integer factorization:\n";
468 cout <<
" z(x) = " << z <<
"\n";
469 cout <<
" factors:\n";
474 cout <<
" (" <<
term.factor <<
")^" <<
term.multiplicity <<
"\n";
477 for (
size_t i = 0; i <
term.multiplicity; ++i)
478 block *=
term.factor;
483 << (
z_rebuilt == z ?
" [factorization OK]" :
" [FAIL]") <<
"\n\n";
490 cout <<
" quartic z2(x) = " <<
z_quartic <<
"\n";
491 cout <<
" quadratic factors:\n";
496 cout <<
" (" <<
term.factor <<
")^" <<
term.multiplicity <<
"\n";
499 for (
size_t i = 0; i <
term.multiplicity; ++i)
500 block *=
term.factor;
512 cout <<
" sextic z3(x) = " <<
z_sextic <<
"\n";
513 cout <<
" cubic factors:\n";
518 cout <<
" (" <<
term.factor <<
")^" <<
term.multiplicity <<
"\n";
521 for (
size_t i = 0; i <
term.multiplicity; ++i)
522 block *=
term.factor;
534 cout <<
" Extended GCD:\n";
535 cout <<
" a(x) = " << a <<
"\n";
536 cout <<
" b(x) = " << b <<
"\n";
537 cout <<
" gcd = " << g <<
"\n";
538 cout <<
" s(x) = " << s <<
"\n";
539 cout <<
" t(x) = " << t <<
"\n";
542 cout <<
" s*a + t*b = " <<
bezout
543 << (
bezout == g ?
" [Bezout OK]" :
" [FAIL]") <<
"\n\n";
547 cout <<
" Reversal:\n";
548 cout <<
" p(x) = " << q <<
"\n";
549 cout <<
" rev(x) = " << q.
reverse() <<
"\n";
550 cout <<
" (reverses coefficient order: a_i -> a_{d-i})\n\n";
554 cout <<
" Taylor shift:\n";
555 cout <<
" f(x) = " << f <<
"\n";
556 cout <<
" f(x+2) = " << f.
shift(2.0) <<
"\n";
557 cout <<
" f(x-1) = " << f.
shift(-1.0) <<
"\n";
567 cout << string(65,
'*') <<
"\n";
568 cout <<
" Aleph-w Polynomial Example\n";
569 cout <<
" Gen_Polynomial<Coefficient> — Sparse Polynomial Arithmetic\n";
570 cout << string(65,
'*') <<
"\n";
580 cout <<
"\n" << string(65,
'*') <<
"\n";
581 cout <<
" All examples completed successfully.\n";
582 cout << string(65,
'*') <<
"\n";
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Univariate polynomial over a generic coefficient ring.
Gen_Polynomial nth_derivative(size_t n) const
-th derivative .
Gen_Polynomial negate_arg() const
Argument negation: .
void for_each_term(Op &&op) const
Iterate over non-zero terms in ascending exponent order.
size_t num_terms() const noexcept
Number of non-zero terms.
Coefficient cauchy_bound() const
Cauchy upper bound on absolute value of roots.
static Gen_Polynomial from_roots(const DynList< Coefficient > &roots)
Build polynomial from its roots: .
static Gen_Polynomial interpolate(const DynList< std::pair< Coefficient, Coefficient > > &points)
Polynomial interpolation through a set of points.
size_t count_real_roots(const Coefficient &a, const Coefficient &b) const
Count distinct real roots in via Sturm's theorem.
Gen_Polynomial pow(size_t n) const
Exponentiation by repeated squaring.
size_t sign_variations() const
Count sign changes in the coefficient sequence.
static Xgcd_Result xgcd(Gen_Polynomial a, Gen_Polynomial b)
Gen_Polynomial compose(const Gen_Polynomial &q) const
Composition .
size_t count_all_real_roots() const
Total number of distinct real roots.
Coefficient bisect_root(Coefficient a, Coefficient b, Coefficient tol=Coefficient(1e-12), size_t max_iter=200) const
Find a root by bisection in .
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).
Gen_Polynomial shift(const Coefficient &k) const
Taylor shift: .
Gen_Polynomial square_free() const
Square-free part: .
Gen_Polynomial derivative() const
Formal derivative .
Gen_Polynomial integral(const Coefficient &C=Coefficient{}) const
Formal indefinite integral with constant of integration.
Coefficient newton_root(Coefficient x0, Coefficient tol=Coefficient(1e-12), size_t max_iter=100) const
Find a root by Newton-Raphson iteration.
Gen_Polynomial reverse() const
Reciprocal polynomial: .
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_remainder_function > > remainder(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_pow_function > > pow(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
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.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
static void roots_and_interpolation()
Two ways to build polynomials from known data: (a) from_roots: given roots r1, r2,...
static void algebraic_transformations()
Demonstrates square-free factorization, extended GCD (Bezout identity), polynomial reversal,...
static void calculus_example()
Symbolic differentiation and integration, verifying the Fundamental Theorem of Calculus: the derivati...
static void root_analysis()
Demonstrates Sturm sequences, root counting, Cauchy bounds, Descartes' rule of signs,...
static void sparse_polynomials()
Demonstrates efficiency of sparse representation by constructing polynomials with very high degree bu...
static void basic_arithmetic()
Demonstrates polynomial construction, printing, and the four basic arithmetic operations.
static void section(const string &title)
static void transfer_function()
A simple low-pass filter transfer function H(s) = N(s) / D(s).
Univariate polynomial ring arithmetic over generic coefficients.