/** * Typed Algebra Functions * * Native TypeScript implementations of polynomial operations, expression * manipulation, and algebraic utilities. Polynomial functions work with * coefficient arrays where index corresponds to power (e.g., [1, 2, 3] * represents 1 + 2x + 3x^2). Expression manipulation functions operate * on string expressions. * * @packageDocumentation */ type f64 = number; /** * Evaluate a polynomial at a given point using Horner's method. * * Coefficients are ordered by ascending power: coeffs[i] is the * coefficient of x^i. E.g., [1, 2, 3] represents 1 + 2x + 3x^2. * * @param coeffs - Coefficient array (index = power) * @param x - Point at which to evaluate * @returns The polynomial value at x * * @example * ```typescript * polyval([1, 2, 3], 2); // 1 + 2*2 + 3*4 = 17 * polyval([1, 0, -1], 3); // 1 + 0 - 9 = -8 * ``` */ export declare function polyval(coeffs: number[], x: f64): f64; /** * Add two polynomials represented as coefficient arrays. * * @param a - First polynomial coefficients * @param b - Second polynomial coefficients * @returns Sum polynomial coefficients * * @example * ```typescript * polyadd([1, 2], [3, 4, 5]); // [4, 6, 5] * ``` */ export declare function polyadd(a: number[], b: number[]): number[]; /** * Multiply two polynomials (convolution of coefficient arrays). * * @param a - First polynomial coefficients * @param b - Second polynomial coefficients * @returns Product polynomial coefficients * * @example * ```typescript * polymul([1, 1], [1, 1]); // [1, 2, 1] => (1+x)^2 * ``` */ export declare function polymul(a: number[], b: number[]): number[]; /** * Compute the n-th derivative of a polynomial. * * @param coeffs - Polynomial coefficients (index = power) * @param n - Number of derivatives to take (default 1) * @returns Derivative polynomial coefficients * * @example * ```typescript * polyder([1, 2, 3]); // [2, 6] => derivative of 1+2x+3x^2 * polyder([1, 2, 3, 4], 2); // [6, 24] => second derivative * ``` */ export declare function polyder(coeffs: number[], n?: number): number[]; /** * Compute the GCD of two polynomials using the Euclidean algorithm. * The result is monic (leading coefficient = 1). * * @param a - First polynomial coefficients * @param b - Second polynomial coefficients * @returns GCD polynomial coefficients (monic) * * @example * ```typescript * // GCD of x^2-1 and x-1 is x-1 * polynomialGCD([-1, 0, 1], [-1, 1]); // [-1, 1] * ``` */ export declare function polynomialGCD(a: number[], b: number[]): number[]; /** * Compute the LCM of two polynomials: LCM(a, b) = (a * b) / GCD(a, b). * * @param a - First polynomial coefficients * @param b - Second polynomial coefficients * @returns LCM polynomial coefficients */ export declare function polynomialLCM(a: number[], b: number[]): number[]; /** * Compute the quotient of polynomial division a / b. * * @param a - Dividend polynomial coefficients * @param b - Divisor polynomial coefficients * @returns Quotient polynomial coefficients * * @example * ```typescript * // (x^2 - 1) / (x - 1) = x + 1 * polynomialQuotient([-1, 0, 1], [-1, 1]); // [1, 1] * ``` */ export declare function polynomialQuotient(a: number[], b: number[]): number[]; /** * Compute the remainder of polynomial division a / b. * * @param a - Dividend polynomial coefficients * @param b - Divisor polynomial coefficients * @returns Remainder polynomial coefficients */ export declare function polynomialRemainder(a: number[], b: number[]): number[]; /** * Return the degree of a polynomial. * * @param coeffs - Polynomial coefficients (index = power) * @returns Degree (highest power with non-zero coefficient) * * @example * ```typescript * degree([1, 2, 3]); // 2 * degree([5]); // 0 * degree([0]); // 0 * ``` */ export declare function degree(coeffs: number[]): number; /** * Extract the coefficient list of a polynomial, trimming leading zeros. * * @param coeffs - Polynomial coefficients (index = power) * @returns Trimmed coefficient array */ export declare function coefficientList(coeffs: number[]): number[]; /** * Compute the discriminant of a polynomial. * * For quadratic ax^2 + bx + c: discriminant = b^2 - 4ac * For cubic ax^3 + bx^2 + cx + d: discriminant = 18abcd - 4b^3d + b^2c^2 - 4ac^3 - 27a^2d^2 * * @param coeffs - Polynomial coefficients (index = power) * @returns Discriminant value */ export declare function discriminant(coeffs: number[]): f64; /** * Compute finite differences of a sequence. * * The k-th finite difference of arr is computed by applying the forward * difference operator k times. Without a second argument, computes * first differences. * * @param arr - Input array * @param n - Number of difference iterations (default 1) * @returns Array of finite differences * * @example * ```typescript * differences([1, 4, 9, 16]); // [3, 5, 7] * differences([1, 4, 9, 16], 2); // [2, 2] * ``` */ export declare function differences(arr: number[], n?: number): number[]; /** * Extract free variable names from an expression string. * Filters out known math function names and constants. * * @param expr - Expression string * @returns Sorted array of variable names * * @example * ```typescript * variables('x^2 + 2*y + sin(z)'); // ['x', 'y', 'z'] * variables('pi * r^2'); // ['r'] * ``` */ export declare function variables(expr: string): string[]; /** * Substitute variables in an expression string with their values. * * @param expr - Expression string * @param vars - Map of variable name to replacement value * @returns Expression string with substitutions applied * * @example * ```typescript * substitute('x^2 + y', { x: '3', y: '1' }); // '3^2 + 1' * substitute('a*b + c', { a: '(x+1)' }); // '(x+1)*b + c' * ``` */ export declare function substitute(expr: string, vars: Record): string; /** * Expand an expression string by distributing multiplication over addition. * * **Polynomials in one OR MORE variables** (integer/non-negative-integer * powers, no function calls, division only by numeric constants) are expanded * EXACTLY via `polyFromExpression` + `polyToString`, collecting like terms: * `expand('(x+1)^3')` → `'1*x^3 + 3*x^2 + 3*x + 1'`; * `expand('(x+y)^2')` → `'1*y^2 + 2*x*y + 1*x^2'`; * `expand('(x+y)*(x-y)')` → `'-1*y^2 + 1*x^2'` (the `x*y` terms cancel). * * Everything else (function calls like `sin`, non-integer exponents, division * by a variable) falls back to the original regex-based distributor below, * which does NOT collect like terms. * * @param expr - Expression string * @returns Expanded expression string * * @example * ```typescript * expand('(a+b)*(c+d)'); // 'a*c + a*d + b*c + b*d' * expand('(x+1)^3'); // '1*x^3 + 3*x^2 + 3*x + 1' * expand('(x+y)^2'); // '1*y^2 + 2*x*y + 1*x^2' * ``` */ export declare function expand(expr: string): string; /** * Factor an expression string. * * **Univariate polynomials** (a single variable, integer coefficients, * degree ≥ 2) are factored **completely over ℤ/ℚ** into irreducible factors * with multiplicity. Rational linear roots are extracted first (rational-root * theorem); any higher-degree remainder — and any polynomial with no rational * root — is routed through the Zassenhaus engine * ({@link factorPolynomialUnivariate}): `factor('x^2-1')` → `'(x - 1)*(x + 1)'`, * `factor('x^4-1')` → `'(x - 1)*(x + 1)*(x^2 + 1)'`, * `factor('x^4+3*x^2+2')` → `'(x^2 + 1)*(x^2 + 2)'`. Polynomials irreducible * over ℚ (`x^4 + 1`, `x^2 + x + 1`) are returned unchanged. * * **Multivariate polynomials** (`n ≥ 2` variables, integer coefficients) are * factored **completely over ℤ/ℚ** into irreducible factors. A fast path (see * {@link factorMultivariate}) handles the common cases byte-for-byte — integer * content, common-monomial extraction, monomial difference-of-squares * (`x^2*y + x*y^2 → 'x*y*(1*y + 1*x)'`, `4*x^2 - 9*y^2 → '(2*x - 3*y)*(2*x + 3*y)'`). * Anything the fast path leaves whole or only partially factored is routed * through the Kronecker-substitution engine ({@link factorMultivariateString}), * which reduces to the univariate ℤ engine and recombines by exact division * (`x^2 + 3*x*y + 4*x + 2*y^2 + 5*y + 3 → (1*x + 1*y + 1)*(1*x + 2*y + 3)`). The * engine declines (and the caller keeps the fast-path/legacy output) beyond a * substituted-degree cap; irreducible multivariate polynomials (`x^2 + y^2`) are * returned unchanged. Wang/EEZ is a future performance upgrade, not a * capability gap. Because the engine confirms every factor by division, it never * emits a wrong factorization. * * Everything else (no rational root, no common factor) falls back to the * original common-integer-factor extraction below. * * @param expr - Expression string * @returns Factored expression string */ export declare function factor(expr: string): string; /** * Collect like terms with respect to a variable. * * @param expr - Expression string * @param variable - Variable to collect terms for * @returns Expression with collected terms */ export declare function collect(expr: string, variable: string): string; /** * Cancel common factors in a rational expression. * * **Univariate integer-coefficient rationals** (a single variable, e.g. * `(x^2-1)/(x-1)`) are cancelled EXACTLY via polynomial GCD: the numerator * and denominator are divided by `polynomialGCD(N, D)`, and any shared * integer content between the resulting numerator/denominator is cancelled * too (e.g. `(2*x^2-2)/(2*x-2) → x + 1`). When the GCD fully divides out the * denominator, the result collapses to a bare polynomial (no `/`); when a * nontrivial denominator remains, the result stays a (lower-degree) * fraction: `(x^3-1)/(x^2-1) → (x^2+x+1)/(x+1)`. This matches `sympy.cancel`. * * **Falls back** to the legacy numeric-only handling below for: purely * numeric fractions `a/b` (including compound `(a/b)/(c/d)`), the identical * numerator/denominator polynomial-string short-circuit (`(p) / (p) → 1`), * multivariate expressions, non-integer coefficients, and expressions whose * numerator/denominator share no non-trivial polynomial factor (returned * unchanged in that case). * * @param expr - Expression string (e.g., "6/4", "(2/3)/(4/9)", "(x^2-1)/(x-1)") * @returns Simplified expression */ export declare function cancel(expr: string): string; /** * Combine a sum of rational terms into a single fraction over a common * (not necessarily lowest) denominator. * * **Univariate rationals** (a single variable, e.g. `1/x + 1/(x+1)`) are * combined EXACTLY: the common denominator is the product of every term's * denominator, and the numerator is the exact polynomial sum * `Σ numᵢ · Π_{j≠i} denⱼ`, simplified via `polyFromExpression`/`polyToString`: * `together('1/x + 1/(x+1)')` → `'(2*x + 1)/((x)*((x+1)))'`. * * Everything else (no variable, i.e. purely numeric) falls back to the * original numeric-fraction addition below. * * @param expr - Expression string * @returns Combined expression */ export declare function together(expr: string): string; /** * Partial fraction decomposition. * * **Univariate rationals with a fully-factorable denominator** (distinct * rational roots only — repeated roots are out of scope) are decomposed via * the cover-up/residue method: `apart('1/(x^2-1)')` → * `'1/(2*(x - 1)) - 1/(2*(x + 1))'`. An improper fraction (numerator degree ≥ * denominator degree) is first split into a polynomial quotient + proper * remainder via `polynomialQuotient`/`polynomialRemainder`. * * Everything else (no variable — i.e. purely numeric — multiple variables, * or a denominator that doesn't factor into distinct rational linear * factors) falls back to the original numeric-only path below. * * @param expr - Expression string * @returns Decomposed expression */ export declare function apart(expr: string): string; /** * Expand trigonometric expressions using angle addition formulas. * * @param expr - Expression string * @returns Expanded expression */ export declare function trigExpand(expr: string): string; /** * Reduce trigonometric expressions using product-to-sum formulas. * * @param expr - Expression string * @returns Reduced expression */ export declare function trigReduce(expr: string): string; /** * Convert trigonometric functions to exponential form using Euler's formula. * * @param expr - Expression string * @returns Expression with trig replaced by exponentials */ export declare function trigToExp(expr: string): string; /** * Convert exponential expressions to trigonometric form. * * @param expr - Expression string * @returns Expression with exponentials replaced by trig */ export declare function expToTrig(expr: string): string; /** * Compute the tangent line to a function at a given point. * * @param f - Function to differentiate * @param x0 - Point at which to compute tangent * @returns Tuple [slope, intercept] * * @example * ```typescript * tangentLine(x => x**2, 3); // [6, -9] => y = 6x - 9 * ``` */ export declare function tangentLine(f: (x: number) => number, x0: f64): [f64, f64]; /** * Reduce an expression to its simplest form. * * @param expr - Expression string * @returns Reduced expression */ export declare function reduce(expr: string): string; /** * Combine two expressions by addition. * * @param a - First expression * @param b - Second expression * @returns Combined expression string */ export declare function combine(a: string, b: string): string; /** * Expand complex-valued expressions using i^2 = -1. * * @param expr - Expression string * @returns Expanded expression */ export declare function complexExpand(expr: string): string; /** * Convert expression to a canonical normal form. * * @param expr - Expression string * @returns Normal form expression */ export declare function normalForm(expr: string): string; /** * Expand powers in an expression. * * @param expr - Expression string * @returns Expanded expression */ export declare function powerExpand(expr: string): string; /** * Full simplification -- more aggressive than basic simplify. * * @param expr - Expression string * @returns Fully simplified expression */ export declare function fullSimplify(expr: string): string; /** * Extract an element from an array at the specified index. * * @param arr - Input array * @param index - Zero-based index * @returns The element at the given index */ export declare function element(arr: T[], index: number): T; /** * Eliminate a variable from a system of polynomial equations (`"lhs = rhs"` * strings) by computing the ELIMINATION IDEAL: a lex Gröbner basis with the * eliminated variable ordered first, keeping the basis elements free of it. * Returns the surviving relations as `" = 0"` strings. * * B-5: the former implementation returned decorative strings * (`"(A) - (B) [x eliminated]"`) — not equations — and echoed non-equation * input unchanged. It now performs real elimination and throws on input it * cannot parse as polynomial equations. * * @param system - Array of equation strings (`"lhs = rhs"` — the `=` is required) * @param variable - Variable to eliminate * @returns The eliminated system as `" = 0"` strings */ export declare function eliminate(system: string[], variable: string): string[]; /** * Compute a partial derivative of an expression string with respect * to a variable using basic symbolic differentiation rules. * * @param expr - Expression string * @param variable - Variable to differentiate with respect to * @returns Derivative expression string */ export declare function symbolicPartialDerivative(expr: string, variable: string): string; /** * Expand special function expressions. * * @param expr - Expression string * @returns Expanded expression */ export declare function functionExpand(expr: string): string; /** * Compute the resultant of two polynomials. * The resultant is the determinant of the Sylvester matrix. * * @param p - First polynomial coefficients (index = power) * @param q - Second polynomial coefficients (index = power) * @returns Resultant value */ export declare function resultant(p: number[], q: number[]): f64; /** * All algebra functions combined. */ export declare const typedAlgebra: { polyval: typeof polyval; polyadd: typeof polyadd; polymul: typeof polymul; polyder: typeof polyder; polynomialGCD: typeof polynomialGCD; polynomialLCM: typeof polynomialLCM; polynomialQuotient: typeof polynomialQuotient; polynomialRemainder: typeof polynomialRemainder; degree: typeof degree; coefficientList: typeof coefficientList; discriminant: typeof discriminant; differences: typeof differences; expand: typeof expand; factor: typeof factor; collect: typeof collect; substitute: typeof substitute; variables: typeof variables; cancel: typeof cancel; together: typeof together; apart: typeof apart; trigExpand: typeof trigExpand; trigReduce: typeof trigReduce; trigToExp: typeof trigToExp; expToTrig: typeof expToTrig; tangentLine: typeof tangentLine; reduce: typeof reduce; combine: typeof combine; complexExpand: typeof complexExpand; normalForm: typeof normalForm; powerExpand: typeof powerExpand; fullSimplify: typeof fullSimplify; element: typeof element; eliminate: typeof eliminate; symbolicPartialDerivative: typeof symbolicPartialDerivative; functionExpand: typeof functionExpand; resultant: typeof resultant; }; export {}; //# sourceMappingURL=algebra.d.ts.map