MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / resultantRec

Function resultantRec

src/compute-engine/boxed-expression/polynomials.ts:571–622  ·  view source on GitHub ↗

Recursive core of polynomialResultant. Returns `null` (not a * polynomial) so the public wrapper can map it to `undefined`.

(
  a: Expression,
  b: Expression,
  variable: string
)

Source from the content-addressed store, hash-verified

569 * `polynomialGCD` divides by symbolic coefficients and gives up with `1` when
570 * a remainder has a coefficient that it cannot read, which happens for most
571 * operands with two variables or more. When it gives a constant and the
572 * operands have another unknown, this function asks the multivariate GCD
573 * (Brown's algorithm, verified by exact division). A result of degree 1 or
574 * more in `variable` is a common factor, made monic in `variable`; a result
575 * that does not hold `variable` is a unit of that field, so the GCD stays `1`.
576 *
577 * Examples:
578 * - `(x + y + 1)³(x − y)` and `(x + y + 1)²(x + 2y)` → `(x + y + 1)²`, expanded
579 * - `x·y` and `y²` → `1` (`y` is a unit when the variable is `x`)
580 */
581export function polynomialGCDWithParameters(
582 a: Expression,
583 b: Expression,
584 variable: string
585): Expression {
586 const gcd = polynomialGCD(a, b, variable);
587 if (polynomialDegree(gcd, variable) !== 0) return gcd;
588 // An operand that is not a polynomial in `variable` has no GCD to find.
589 if (polynomialDegree(a, variable) < 0 || polynomialDegree(b, variable) < 0)
590 return gcd;
591 const unknowns = new Set([...a.unknowns, ...b.unknowns]);
592 if (unknowns.size < 2 || !unknowns.has(variable)) return gcd;
593 const g = multivariateBinaryGCD(a, b);
594 if (g === undefined || polynomialDegree(g, variable) < 1) return gcd;
595 return makeMonic(g, variable);
596}
597
598/** Index of the leading (highest-degree) non-zero coefficient, or -1 if the
599 * coefficient array represents the zero polynomial. */
600function leadingIndex(coeffs: Expression[]): number {
601 for (let i = coeffs.length - 1; i >= 0; i--)
602 if (!coeffs[i].isSame(0)) return i;
603 return -1;
604}
605
606/**
607 * Compute the resultant of two univariate polynomials `a` and `b` in
608 * `variable`. The resultant is the determinant of the Sylvester matrix; it is
609 * zero iff the polynomials share a common (non-constant) factor.
610 *
611 * Computed by the Euclidean recursion over the coefficient field (exact
612 * rationals / radicals — no floating point), avoiding an explicit Sylvester
613 * determinant. With `m = deg a`, `n = deg b`, and `R = a mod b` of degree `r`:
614 *
615 * Res(a, b) = (-1)^(m·n) · lc(b)^(m - r) · Res(b, R)
616 *
617 * with base cases Res(a, const c) = c^deg(a) and Res(const, const) = 1.
618 *
619 * Returns `undefined` if either argument is not a polynomial in `variable`.
620 *
621 * Examples:
622 * - `polynomialResultant(x² - 1, x - 1, 'x')` → 0 (common factor x - 1)
623 * - `polynomialResultant(x² + 1, x² - 1, 'x')` → 4
624 * - `polynomialResultant(x² + a, x + b, 'x')` → a + b²
625 */

Callers 1

polynomialResultantFunction · 0.85

Calls 7

checkDeadlineFunction · 0.90
leadingIndexFunction · 0.85
polynomialDivideFunction · 0.85
powMethod · 0.65
negMethod · 0.65
mulMethod · 0.65

Tested by

no test coverage detected