Polynomials
Four classes cover polynomial expressions:
| Class | Represents | Example |
|---|---|---|
Monom | A term: a coefficient and powers of variables | 3x^2y |
Polynom | A sum of terms | x^2-5x+6 |
Factor | A polynom raised to a power | (x+1)^2 |
PolyFactor | A product of factors, with a denominator if needed | (x-2)(x-3)/(x+1) |
import {Monom, Polynom, Factor, PolyFactor} from 'pimath'As everywhere in PiMath, operations modify the object and return it. Use clone() to keep the original, and a failing operation leaves the object unchanged.
Monom
A Monom has a coefficient (a Fraction) and a literal part, a dictionary from each variable to its exponent. Exponents are fractions too, so x^(1/2) is allowed.
const m = new Monom('3x^2y')
m.tex // 3x^{ 2 }y
m.coefficient.display // 3
m.variables // [ 'x', 'y' ]
m.degree().display // 3
m.degree('x').display // 2A string must be a single term. Spaces are ignored, and a power applies to the whole base: (3x)^2 is 9x^2. A malformed string, a sum such as 3x+2 (use a Polynom), or a negative exponent without parentheses (x^-1, write x^(-1)) throws a ParseError.
new Monom('3x((') // throws ParseError
new Monom('3x+2') // throws ParseError
new Monom('(3x)^2').display // 9x^(2)
new Monom('x^(-1)').display // x^(-1)Operations
add and subtract only work between similar monoms (same variables, same exponents), otherwise they throw a MathError. multiply and divide always work.
new Monom('3x').add('2x').display // 5x
new Monom('3x').add('2y') // throws MathError
new Monom('3x^2').multiply('2xy').display // 6x^(3)y
new Monom('6x^2').divide('2x').display // 3x
new Monom('3x^2').derivative('x').display // 6x
new Monom('6x^2').primitive('x').display // 2x^(3)
new Monom('3x^2y').evaluate({x: 2, y: 5}).display // 60evaluate needs a value for every variable and throws a MathError otherwise. primitive throws for x^-1, whose primitive is a logarithm.
Tests
new Monom('3x').isSameAs(new Monom('5x')) // true
new Monom('6x^2').isDivisibleBy(new Monom('2x')) // true
new Monom('4x^2').isSquare() // true
Monom.gcd(new Monom('6x^2y'), new Monom('4x^3')).display // 2x^(2)Polynom
A Polynom is a list of monoms, always kept reduced and sorted by decreasing degree.
Build a polynom
new Polynom('x^2-5x+6').display // x^(2)-5x+6
new Polynom('(x+1)^2').display // x^(2)+2x+1
new Polynom('x', 1, -5, 6).display // x^(2)-5x+6
new Polynom('xyz', 2, 3, 5, 7).display // 2x+3y+5z+7
new Polynom(5).display // 5With a single letter and some numbers, the numbers are the coefficients from the highest degree down. With several letters, the polynom is linear: 'xyz', a, b, c, d is ax + by + cz + d. Strings accept sums, products, integer powers and a division by a constant.
new Polynom('x^2+') // throws ParseErrorPolynom.fromThreePoints builds the polynom of degree at most 2 through three points. The result is exact, and aligned points give a line.
Polynom.fromThreePoints({x: 1, y: 2}, {x: 2, y: 3}, {x: 4, y: 11}).display // x^(2)-2x+3
Polynom.fromThreePoints({x: 0, y: 1}, {x: 1, y: 3}, {x: 2, y: 5}).display // 2x+1
Polynom.fromThreePoints({x: 1, y: 2}, {x: 1, y: 3}, {x: 2, y: 3}) // throws MathErrorDisplay
const P = new Polynom('x^2-5x+6')
P.tex // x^{ 2 }-5x+6
P.display // x^(2)-5x+6
P.withDigits(2).display // x^(2)-5.00x+6.00Operations
add, subtract, multiply and divide accept polynoms, monoms, numbers and strings, and several operands at once. A string is read as a polynom, and a malformed one throws a ParseError without changing the polynom.
new Polynom('x+1').add('2x').display // 3x+1
new Polynom('x+1').add('x^2+3').display // x^(2)+x+4
new Polynom('x-2').multiply('x+3').display // x^(2)+x-6
new Polynom('x^2-1').subtract('x-1').display // x^(2)-x
new Polynom('x+1').pow(2).display // x^(2)+2x+1
new Polynom('x^2-1').divide('x-1').display // x+1divide is exact: it throws a MathError when the division leaves a remainder. euclidean gives the quotient and the remainder instead.
const {quotient, reminder} = new Polynom('x^2+1').euclidean(new Polynom('x-1'))
quotient.display // x+1
reminder.display // 2inverse, root and sqrt always throw a MathError: the inverse or the root of a polynom is not a polynom.
Study a polynom
const P = new Polynom('x^2-5x+6')
P.degree().display // 2
P.variables // [ 'x' ]
P.evaluate({x: 3}).display // 0
P.evaluate(3, true) // 0
P.derivative().display // 2x-5
P.primitive().display // 1/3x^(3)-5/2x^(2)+6x
P.integrate(0, 3).display // 9/2
P.limitTo('+oo').display // +ooevaluate returns an exact Fraction, or a number when the second argument is true.
Factor and roots
const P = new Polynom('x^2-5x+6')
P.factorize().map(f => f.display) // [ 'x-2', 'x-3' ]
P.roots.map(s => s.display) // [ '2', '3' ]
P.isDivisibleBy(new Polynom('x-2')) // true
new Polynom('2x^3+6x^2').commonMonom().display // 2x^(2)roots returns Solution objects, sorted by value. A constant polynom has none.
Compare
new Polynom('x+1').isEqual(new Polynom('1+x')) // true
new Polynom('x+1').isOppositeAt(new Polynom('-x-1')) // true
new Polynom('x^2+x').isSameAs(new Polynom('3x^2-5x')) // true
new Polynom('x+1').isOne() // falseisEqual compares the terms and their coefficients. isSameAs compares the terms only.
Factor and PolyFactor
A PolyFactor writes an expression as a product of factors. It is useful to display a factored form, or to check an answer of a student.
const F = new PolyFactor('(x-1)(x+2)^2')
F.display // (x-1)(x+2)^(2)
F.tex // \left( x-1 \right)\left( x+2 \right)^{ 2 }
F.develop().display // x^(3)+3x^(2)-4
F.degree().display // 3
F.roots.map(s => s.display) // [ '-2', '1' ]A factor with a negative power belongs to the denominator.
const R = new PolyFactor('(x+1)/(x-2)')
R.numerator.display // x+1
R.denominator.display // x-2
R.asRoot.display // (x+1)/(x-2)
R.evaluate(5).display // 2asPower (default) writes the factors one after the other with their exponent, asRoot writes a fraction.
On a Factor, asRoot writes a fractional power as a radical and a negative power as a fraction.
new Factor('(x+1)^(-2)').asRoot.display // 1/((x+1)^(2))
new Factor('(x+1)^(1/2)').asRoot.display // sqrt(x+1)new PolyFactor('x^2-5x+6').factorize().display // (x-2)(x-3)
new PolyFactor('(x+1)(x+1)(x-2)').reduce().display // (x+1)^(2)(x-2)
new PolyFactor('(x+1)(x-2)').isEqual(new PolyFactor('(x-2)(x+1)')) // true
PolyFactor.gcd(new PolyFactor('(x+1)(x-2)'), new PolyFactor('(x+1)(x+3)')).display // x+1reduce groups the factors with the same base. isEqual compares two expressions independently of the order of their factors.
Factor is the building block of a PolyFactor: a polynom and a power. It is rarely used alone, and several operations are not defined on it (add, subtract, opposite, primitive, derivative): they throw a MathError or an UnsupportedOperationError.
const f = new Factor('(x+1)^2')
f.polynom.display // x+1
f.power.display // 2
f.develop().display // x^(2)+2x+1
f.add() // throws MathErrorMembers
Monom
Constructors
new Monom(value?: InputValue<Fraction>)new Monom(value?: Monom)
Static members
| Member | Description |
|---|---|
fromNumber(value: number): Monom | Build a constant monom (empty literal part) from a number. |
fromFraction(value: InputValue<Fraction>): Monom | Build a constant monom (empty literal part). |
fromLiteral(coefficient: InputValue<Fraction>, literal: LiteralType<Fraction>): Monom | Build a monom from an explicit coefficient and literal part. The literal Fractions are cloned to avoid aliasing the caller's dictionary. |
fromString(value: string): Monom | Build a monom from a string. A purely numeric string is a constant monom; any other string is parsed as an algebraic expression. Throws: {ParseError} If the string cannot be parsed as a monom. |
gcd(...monoms: Monom[]): Monom | Greatest common divisor of monoms: the gcd of the numerators over the lcm of the denominators, times each variable shared by all the monoms at its lowest exponent. Throws: {MathError} if a monom has a non-integer exponent. |
xMultiply(...monoms: Monom[]): Monom | Multiply two monoms and return a NEW monom. |
Properties and getters
| Member | Description |
|---|---|
tex: string | LaTeX rendering of the current state (terminal output, not chainable). |
display: string | This display getter is to be used in the polynom display getter |
coefficient: Fraction | Get the coefficient k of the monom k·x^n Set the coefficient k of the monom |
dividers: Monom[] | Every divisor of the monom: each divisor of the absolute value of the coefficient, combined with every exponent from 0 up to the exponent of each variable. A monom with a non-integer coefficient or exponent, or a numerator above 1 000 000, is returned alone. |
literal: LiteralType<Fraction> | Get the literal part of x^a·y^b as a dictionary from each variable to its exponent: {x: a, y: b} Set the literal part of the monom. Must be a dictionary {x: Fraction, y: Fraction, ...} |
literalStr: string | Set the literal part of the monom from a string |
plotFunction: string | Expression to give to a plotting tool: explicit * between the coefficient and the variables. |
variables: string[] | Get the variables variables |
Methods
| Member | Description |
|---|---|
parse(inputStr: InputAlgebra<Monom>): Monom | Route an input to the matching from*** builder. Same-type inputs are copied; every other shape delegates without inlining construction logic. |
copy(value: Monom): Monom | Copy the mathematical state (coefficient and literal part) of another monom into this. Display state is not involved (Monom has none). Entry point used to commit the result of an atomic operation. |
clone(): Monom | Full duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy. |
withDigits(digits: number | null): Monom | Show every numeric value as a decimal with digits digits after the point (exact values included: 3/2 becomes 1.50 with 2 digits). |
add(...M: InputAlgebra<Fraction>[]): Monom | Add multiple similar monoms to the current one. Throws: {MathError} If any operand is not similar to the current monom. The receiver is left unchanged (atomic). |
containsRationalPower(): boolean | True when an exponent is not an integer (x^(1/2)). |
degree(variable?: string): Fraction | Get the degree of a monom. If no variable is given, the result will be the global degree. |
derivative(variable?: string): Monom | Derivative the monom |
divide(...M: InputAlgebra<Fraction>[]): Monom | Divide the current monoms by multiple monoms |
evaluate(values: LiteralType<number | Fraction> | InputValue<Fraction>, asNumeric: true): numberevaluate(values: LiteralType<number | Fraction> | InputValue<Fraction>, asNumeric?: false): Fractionevaluate(values: LiteralType<number | Fraction> | InputValue<Fraction>, asNumeric?: boolean): Fraction | number | Evaluate a monom. Every variable of the monom must be assigned in values; extra entries in values (variables not present in the monom) are ignored. Throws: {MathError} If a variable of the monom is missing from values. |
hasVariable(variable: string): boolean | Determine if a monom contains a variable in its literal part |
integrate(a: InputValue<Fraction>, b: InputValue<Fraction>, variable?: string): Fraction | Definite integral between a and b, as a fraction: primitive(b) - primitive(a). Throws: {MathError} for a power of -1 of the variable (see primitive). |
inverse(): Monom | Replace the monom by its inverse: inverse the coefficient and negate every exponent. The inverse of zero has an infinite coefficient; it does not throw. |
isDivisibleBy(div: Monom): boolean | True when div divides the monom: each variable of div has at least the same exponent here, and the quotient of the coefficients is an integer (a coefficient with a fractional part is not checked). |
isEqual(M: Monom): boolean | Determine if two monoms are equals |
isLiteralSquare(): boolean | True when every exponent is an even integer (x^4y^2). |
isOne(): boolean | Determine if the monom is one |
isSameAs(M: Monom): boolean | Determine if two monoms are similar |
isSquare(): boolean | True when the coefficient is a perfect square and every exponent is an even integer. |
isZero(): boolean | Determine if the monom is null |
multiply(...M: InputAlgebra<Fraction>[]): Monom | Multiply multiple monoms into the current monom |
one(): Monom | Create a one value monom |
opposite(): Monom | Get the opposite Returns a monom. |
pow(nb: number | Fraction): Monom | Raise the monom to a power. |
primitive(variable?: string): Monom | Primitive with respect to a variable (integration constant omitted). The primitive of the zero monom is the zero monom. Throws: {MathError} for a power of -1 of the variable: its primitive is a logarithm, not a monom. |
reduce(): Monom | Reduce the coefficient and remove the variables whose exponent is zero. |
removeVariable(variable: string): void | Remove a variable from the literal part. Nothing happens when the variable is absent. |
root(): Monom | Get the nth-root of the monom Throws: {NotImplementedError} Always: the nth-root of a monom is not yet implemented. |
setExponent(variable: string, exponent: InputValue<Fraction>): Monom | Set the exponent of a particular variable. |
sqrt(): Monom | Return the square root of a monom. The literal part always admits a square root (halving each power, rational powers allowed), so only the coefficient constrains the result. Throws: {MathError} If the coefficient is not a perfect square. The receiver is left unchanged (atomic: the guard precedes any mutation). |
subtract(...M: InputAlgebra<Fraction>[]): Monom | Subtract multiple similar monoms from the current one. Throws: {MathError} If any operand is not similar to the current monom. The receiver is left unchanged (atomic). |
zero(): Monom | Create a zero value monom |
Polynom
Constructors
new Polynom(value: InputValue<Fraction>)new Polynom(value: string)new Polynom(value: Monom)new Polynom(value: Polynom)new Polynom(...values: InputValue<Fraction>[])new Polynom(...values: InputAlgebra<Polynom>[])
Static members
| Member | Description |
|---|---|
xMultiply(...polynoms: InputValue<Polynom>[]): Polynom | Product of the given values, as a new polynom. |
fromCoefficients(variable: string, ...values: InputValue<Fraction>[]): Polynom | Build a polynom from coefficients ordered highest→lowest degree. The first argument is the variable (e.g. 'x', 't'). Throws: {InvalidArgumentError} when variable is not a single lowercase letter. |
fromConstant(value: InputValue<Fraction>): Polynom | Build a constant polynom. |
fromLinear(variables: string, ...coefficients: InputValue<Fraction>[]): Polynom | Build a multivariate linear polynom: 'xyz', a, b, c, d → ax + by + cz + d. Throws: {ParseError} when variables is not two or more lowercase letters.; {InvalidArgumentError} when more coefficients than variables+1 are given. |
fromMonoms(...monoms: InputAlgebra<Fraction>[]): Polynom | Build a polynom from monoms, or from anything a monom accepts (numbers, strings, fractions). The result is neither reduced nor sorted: call reduce. |
fromThreePoints(point1: XY<InputValue<Fraction>>, point2: XY<InputValue<Fraction>>, point3: XY<InputValue<Fraction>>, variable?: string): Polynom | Build the polynom of degree at most 2 passing through three points, using Newton divided differences. The result is exact (fractions). When the three points are aligned, the result is a line (degree 1), or a constant when the three y are equal. Throws: {InvalidArgumentError} when a coordinate is not finite, or variable is not a single lowercase letter.; {MathError} when two points share the same x (no polynom is a function of that shape). |
fromString(value: string): Polynom | Build a polynom from an expression: sums, products, integer powers, and division by a constant. Throws: {ParseError} if the string is malformed.; {MathError} for a division by a polynom of degree 1 or more, or a fractional power of a sum. |
Properties and getters
| Member | Description |
|---|---|
tex: string | LaTeX rendering of the current state (terminal output, not chainable). |
display: string | ASCIIMath rendering of the current state (terminal output, not chainable). |
factors: Polynom[] | Factors of the polynom, computed by factorize on first access and kept until the polynom changes. The zero polynom has none. |
length: number | Number of monoms. |
monoms: Monom[] | A detached copy of the monoms (each one cloned). Mutating the returned array or its monoms does NOT affect the polynom — go through add / subtract / multiply / the monoms setter to modify it. |
numberOfVars: number | Number of distinct variables. |
plotFunction: string | Expression to give to a plotting tool: explicit * between a coefficient and its variables. |
roots: Solution[] | Roots of the polynom, as Solutions sorted by value. A constant polynom has none. The result is kept until the polynom changes. Throws: {MathError} if a degree is not a natural number. |
value: number | undefined | Numeric value of a constant polynom, or undefined when the degree is not zero. |
variables: string[] | The variables of the polynom, sorted alphabetically. |
Methods
| Member | Description |
|---|---|
parse(inputStr: PolynomParsingType, ...values: InputAlgebra<Monom>[]): Polynom | Route an input to the matching from*** builder. This method only dispatches on the input's type/format; it never inlines construction. Throws: {ParseError} when the input matches no known form. |
copy(value: Polynom): Polynom | Copy the mathematical state (monoms and default variable) of another polynom into this one. Display state is not transferred — see clone. |
clone(): Polynom | Full duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy. |
withDigits(digits: number | null): Polynom | Show every numeric value as a decimal with digits digits after the point (exact values included: 3/2 becomes 1.50 with 2 digits). |
add(...values: InputAlgebra<Polynom>[]): Polynom | Add polynoms, monoms, numbers or strings, then reduce. Modifies and returns the receiver. A string is read as a polynom ("2x+1"). Every operand is built before the receiver is touched: an invalid value throws and leaves it unchanged. Throws: {ParseError} if a string operand is malformed.; {InvalidArgumentError} if an operand is undefined. |
commonMonom(): Monom | Greatest monom dividing every term: gcd of the numerators over gcd of the denominators, and the lowest exponent of each variable (2x^3+6x^2 gives 2x^2). |
degree(variable?: string): Fraction | Degree of the polynom as a fraction. Without variable, the highest total degree of a term; with one, the highest exponent of that variable. |
derivative(variable?: string): Polynom | Derivative with respect to variable (default x), as a new reduced polynom. |
divide(...values: InputAlgebra<Polynom>[]): Polynom | Divide in place. Polynom division is only defined when it is exact. Throws: {MathError} when the divisor leaves a non-zero remainder, or when the value cannot be interpreted as a divisor. The receiver is unchanged. |
empty(): Polynom | Remove every monom. Unlike zero, which holds the monom 0, the polynom then has no term at all. |
euclidean(P: Polynom): IEuclidean | Euclidean division of this polynom by P. Throws: {MathError} when either polynom has a non-integer degree. |
evaluate(values: LiteralType<Fraction | number> | InputValue<Fraction>, asNumeric: true): numberevaluate(values: LiteralType<Fraction | number> | InputValue<Fraction>, asNumeric?: false): Fractionevaluate(values: LiteralType<Fraction | number> | InputValue<Fraction>, asNumeric?: boolean): Fraction | number | Value of the polynom for given values of its variables. Throws: {MathError} if a variable of the polynom has no value. |
factorize(variable?: string): Polynom[] | Factorize a polynom with respect to a single variable and store the best results in factors. The variable is the explicit variable, else the sole variable of a single-variable polynom, else the default variable. Multi-variable factorization (treating other variables as symbolic constants) is not supported — the numeric solver cannot represent roots that depend on other variables. |
gcdDenominator(): number | Greatest common divisor of the denominators of the coefficients. |
gcdNumerator(): number | Greatest common divisor of the numerators of the coefficients. |
getCoefficients(): Fraction[] | Coefficients from the highest degree down to the constant term (zeros included). Assumes a single variable. Throws: {MathError} when the polynom has a non-integer degree. |
getDenominators(): number[] | Denominator of the coefficient of each monom, in the order of the monoms. |
getNumerators(): number[] | Numerator of the coefficient of each monom, in the order of the monoms. |
hasVariable(variable: string): boolean | True when the variable appears in a term with a non-zero exponent. |
integrate(a: InputValue<Fraction>, b: InputValue<Fraction>, variable?: string): Fraction | Definite integral between a and b, as a fraction. The variable is resolved like in limitTo: the one passed, else the only variable of the polynom, else the default variable. Throws: {MathError} if a term has a power of -1 of the variable. |
inverse(): Polynom | 1/P is not a polynom: the operation is not available. Throws: {MathError} always. The IExpression contract returns a polynom, never null, so impossibility is a throw. |
isDeveloped(polynomString: string): boolean | Whether the given string is the fully developed (expanded) form of this polynom. Throws: {ParseError} when polynomString is malformed. |
isDivisibleBy(div: Polynom): boolean | True when the division by div leaves no remainder. A degree-1 divisor is tested through its root. |
isEqual(P: Polynom): boolean | True when both polynoms are equal once reduced and sorted: same terms, same coefficients. |
hasCrossTerm(): boolean | Whether the polynom contains a cross-term monom (a single monom carrying more than one variable, such as xy). |
isOne(): boolean | True for the constant polynom 1. |
isOppositeAt(P: Polynom): boolean | True when P is the opposite polynom (x+1 and -x-1). |
isSameAs(P: Polynom): boolean | True when both polynoms have the same terms, whatever their coefficients (x^2+x and 3x^2-5x). |
isZero(): boolean | True for the zero polynom (no term, or the single term 0). |
lcmDenominator(): number | Least common multiple of the denominators of the coefficients. |
lcmNumerator(): number | Least common multiple of the numerators of the coefficients. |
limitTo(value: InputValue<Fraction>, variable?: string): Fraction | Limit of the polynom as its variable approaches value. An infinite value returns a signed infinite Fraction, determined by the leading term (and the degree parity at -∞). |
monomByDegree(degree?: Fraction | number, variable?: string): Monom | Monom of the given degree for variable, or the monom of highest degree when degree is omitted. Returns the zero monom when none matches. |
monomByVariable(variable: string): Monom | First monom that contains variable, or the zero monom when none does. |
monomsByDegree(degree?: number | Fraction, variable?: string): Monom[] | Every monom of the given degree for variable, or of the highest degree when degree is omitted. |
multiply(...values: InputAlgebra<Polynom>[]): Polynom | Multiply by polynoms, monoms, fractions, numbers or strings, then reduce. Modifies and returns the receiver. A string is read as a polynom ("x+3"). Every operand is built before the receiver is touched: an invalid value throws and leaves it unchanged. Throws: {ParseError} if a string operand is malformed.; {InvalidArgumentError} if an operand is undefined. |
one(): Polynom | Set the polynom to the constant 1. |
opposite(): Polynom | Change the sign of every term. |
pow(nb: number): Polynom | Raise to a non-negative integer power; the power 0 gives 1. Throws: {InvalidArgumentError} if nb is negative or not a safe integer. |
primitive(variable?: string): Polynom | A primitive with respect to variable (default x), without integration constant. Throws: {MathError} if a term has a power of -1 of the variable. |
reduce(): Polynom | Add the similar terms, remove the zero terms, reduce the coefficients and sort by decreasing degree. |
reorder(variable?: string, revert?: boolean): Polynom | Sort the monoms by decreasing degree of variable (default: the default variable of the polynom), or by increasing degree when revert is true. The other variables break the ties. |
replaceBy(variable: string, P: Polynom): Polynom | Replace a variable by a polynom (in place). Throws: {MathError} when the replaced variable appears with a negative power. |
root(): Polynom | The n-th root of a polynom is not a polynom: the operation is not available. Throws: {MathError} always. |
setVariable(value: string): Polynom | Set the default variable, used when a method needs one and the polynom does not decide it. Throws: {InvalidArgumentError} if value is not a single lowercase letter. |
sqrt(): Polynom | The square root of a polynom is not a polynom: the operation is not available. Throws: {MathError} always. |
subtract(...values: InputAlgebra<Polynom>[]): Polynom | Subtract polynoms, monoms, numbers or strings, then reduce. Modifies and returns the receiver. A string is read as a polynom ("x-1"). Every operand is built before the receiver is touched: an invalid value throws and leaves it unchanged. Throws: {ParseError} if a string operand is malformed.; {InvalidArgumentError} if an operand is undefined. |
zero(): Polynom | Set the polynom to zero. |
Factor
Constructors
new Factor(value?: InputAlgebra<Polynom> \| Factor, power?: InputValue<Fraction>)
Static members
| Member | Description |
|---|---|
fromString(value: string): Factor | Build a factor from a string such as "(x+1)^2". Only the first factor of a product is kept: use factorsFromString to get all of them. |
fromPolynom(polynom: InputAlgebra<Polynom>): Factor | Build a factor of power 1 from a polynom, or from anything a polynom accepts. |
checkParseAsFactors(str: string): boolean | True when the string has no + or - outside the parentheses, so it reads as a product of factors. |
factorsFromString(value: string, isNumerator?: boolean): Factor[] | Parse a product string into an array of Factor instances. Supported format: parenthesised terms with optional powers and implicit multiplication, e.g. "(3x+2)^2*(x-1)" or "(x+1)(x-1)^(1/2)". Non-parenthesised tokens (e.g. "3x") are treated as a single factor with implicit power ±1. |
Properties and getters
| Member | Description |
|---|---|
tex: string | LaTeX rendering of the current state (terminal output, not chainable). |
display: string | ASCIIMath rendering of the current state (terminal output, not chainable). |
asSingle: Factor | Display option: do not wrap the polynom in parentheses when the power is 1. PolyFactor sets it on a lone factor. Chainable, kept by clone. |
polynom: Polynom | Base polynom. The getter returns a copy: modify the factor through its methods or the setter. |
power: Fraction | Power of the factor, as a fraction (a copy). A negative power is a denominator in a PolyFactor. |
variables: string[] | Variables of the base polynom, sorted. |
asPower: Factor | Display option (default): a power is written (P)^{n}, a negative one with a negative exponent. Chainable, kept by clone. |
asRoot: Factor | Display option: a fractional power is written as a radical (sqrt(P)) and a negative power as a fraction (1/(P)). Chainable, kept by clone. |
Methods
| Member | Description |
|---|---|
parse(value: InputAlgebra<Polynom> | Factor, power?: InputValue<Fraction>): Factor | Replace the factor by a factor, a polynom or a string. When power is given, the power of the result is multiplied by it. |
copy(value: Factor): Factor | Copy the mathematical state of another instance into this (display state is NOT transferred — the receiver keeps its own formatting). Entry point used when constructing/parsing from a same-type input. Distinct from clone. Classes without a real implementation throw NotImplementedError. |
clone(): Factor | Full duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy. |
withDigits(digits: number | null): Factor | Show every numeric value as a decimal with digits digits after the point (exact values included: 3/2 becomes 1.50 with 2 digits). |
add(): Factor | Adding two factors is not defined. Throws: {MathError} always. |
degree(variable?: string): Fraction | Degree of the polynom multiplied by the power. |
derivative(): Factor | Not available on a single factor: use getDerivativeFactors and compose through PolyFactor. Throws: {UnsupportedOperationError} always. |
getDerivativeFactors(): Factor[] | Chain-rule decomposition of P(x)^n: returns the factors whose product equals d/dx[P^n]. - n = 0 → [0] (constant) - n = 1 → [P'(x)] - otherwise → [n, P'(x), P(x)^(n−1)] Intended for use by PolyFactor.derivative, not for standalone consumption. |
develop(): Polynom | Expand the factor into a polynom (P^n with n a natural number). Throws: {MathError} if the power is not a natural number. |
divide(value: InputAlgebra<Factor | Polynom>): Factor | Divide by a factor or a polynom: multiply by its inverse. Throws: {MathError} when the two factors do not share the same base polynomial. |
evaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric: true): numberevaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: false): Fractionevaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: boolean): number | Fraction | Value of the factor for given values of its variables: the polynom evaluated, then raised to the power. |
hasVariable(variable: string): boolean | True when the base polynom contains the variable. |
inverse(): Factor | Negate the power (P^n becomes P^-n). |
isEqual(value: Factor): boolean | True when both factors have the same base polynom and the same power. |
isOne(): boolean | True when the base polynom is 1, or the power is zero. |
isSameAs(value: InputAlgebra<Factor | Polynom>): boolean | True when the base polynom is the same; the power is ignored. |
isZero(): boolean | True when the base polynom is zero. |
multiply(value: InputAlgebra<Factor | Polynom>): Factor | Multiply by another factor or polynom. Succeeds only when both share the same base polynomial; powers are added. Two single-monom factors with integer power 1 are multiplied directly as polynomials. Throws: {MathError} when the base polynomials differ. |
one(): Factor | Set the factor to 1. |
opposite(): Factor | A factor has no sign: use PolyFactor, which adds a factor -1. Throws: {UnsupportedOperationError} always. |
pow(value: number | Fraction): Factor | Multiply the power by value. |
primitive(): Factor | Not available on a single factor. Throws: {UnsupportedOperationError} always. |
reduce(): Factor | Not available on a single factor: use PolyFactor.reduce. Throws: {UnsupportedOperationError} always. |
root(value: number): Factor | Divide the power by value (the square root of P^2 is P^1). |
sqrt(): Factor | Square root: divide the power by 2. |
subtract(): Factor | Subtracting two factors is not defined. Throws: {MathError} always. |
zero(): Factor | Set the factor to 0. |
PolyFactor
Constructors
new PolyFactor(...values: (InputAlgebra<Polynom> \| Factor \| PolyFactor)[])
Static members
| Member | Description |
|---|---|
gcd(...values: PolyFactor[]): PolyFactor | Greatest common divisor: every base shared by all the polyfactors, at its lowest power. |
lcm(...values: PolyFactor[]): PolyFactor | Least common multiple: every base found in one of the polyfactors, at its highest power. |
fromPolynom(numerator: InputAlgebra<Polynom>, denominator?: InputAlgebra<Polynom>): PolyFactor | Build a polyfactor with one factor for the numerator and, when given, one for the denominator. Throws: {MathError} if the denominator is zero. |
fromFactors(...values: (Factor | InputAlgebra<Polynom>)[]): PolyFactor | Build a polyfactor from factors, or from anything a factor accepts. |
fromString(value: string): PolyFactor | Build a polyfactor from a product of parenthesised terms, with at most one / for the denominator. Throws: {ParseError} if the numerator is empty or the string has more than one /. |
Properties and getters
| Member | Description |
|---|---|
tex: string | LaTeX rendering of the current state (terminal output, not chainable). |
display: string | ASCIIMath rendering of the current state (terminal output, not chainable). |
asPower: PolyFactor | Display option (default): the factors are written one after the other, with their exponent. Chainable, kept by clone. |
asRoot: PolyFactor | Display option: the factors with a negative power are written in a fraction under the numerator. Chainable, kept by clone. |
denominator: PolyFactor | The factors with a negative power, as a new polyfactor with positive powers. |
factors: Factor[] | The factors, as copies. Setting the property replaces them. |
roots: Solution[] | Roots of every factor, sorted by value without duplicates. The roots of the denominator are included: they are the values where the expression is undefined. |
numerator: PolyFactor | The factors with a positive power, as a new polyfactor. |
variables: string[] | Variables of all the factors. |
Methods
| Member | Description |
|---|---|
parse(value: InputAlgebra<Polynom> | Factor | PolyFactor | string): PolyFactor | Replace the content by a string, a polynom, a factor or another polyfactor. Throws: {ParseError} if a string is malformed. |
copy(value: PolyFactor): PolyFactor | Copy the mathematical state of another instance into this (display state is NOT transferred — the receiver keeps its own formatting). Entry point used when constructing/parsing from a same-type input. Distinct from clone. Classes without a real implementation throw NotImplementedError. |
clone(): PolyFactor | Full duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy. |
withDigits(digits: number | null): PolyFactor | Show every numeric value as a decimal with digits digits after the point (exact values included: 3/2 becomes 1.50 with 2 digits). |
add(...values: PolyFactor[]): PolyFactor | Add one or more PolyFactors to this one. Algorithm: compute the LCM of all denominators, scale each numerator by its complementary factor, then extract the GCD of the scaled numerators and collect the residuals into a single polynomial factor. |
degree(variable?: string): Fraction | Sum of the degrees of the factors; a denominator counts negatively. |
derivative(): PolyFactor | Derivative, by the product rule. The result is added with add, so it is factored again. |
develop(): PolyFactor | Expand the numerator and the denominator into polynoms. Returns a new polyfactor (numerator over denominator). Throws: {MathError} if a power is not a natural number. |
divide(...values: PolyFactor[]): PolyFactor | Divide by polyfactors: append their inverted factors. The result is not reduced. |
evaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric: true): numberevaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: false): Fractionevaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: boolean): number | Fraction | Value of the expression for given values of its variables. |
factorize(variable?: string): PolyFactor | Factor the polynom of each factor, and return the result as a new polyfactor. |
hasVariable(variable: string): boolean | True when a factor contains the variable. |
inverse(): PolyFactor | Negate every power: numerator and denominator are swapped. |
isEqual(value: PolyFactor): boolean | True when both expressions are equal: their quotient, once the common factors are removed, reduces to one. |
isOne(): boolean | True when every factor is one. |
isZero(): boolean | True when a factor is zero. |
multiply(...values: (InputAlgebra<Polynom> | Factor | PolyFactor)[]): PolyFactor | Multiply by polyfactors, factors, polynoms or strings: append their factors. The result is not reduced. |
one(): PolyFactor | Set the expression to 1. |
opposite(): PolyFactor | Change the sign: add a factor -1, or remove it when it is already there. |
pow(value: number | Fraction): PolyFactor | Raise to a power: multiply the power of every factor. |
primitive(): PolyFactor | Not implemented. Throws: {NotImplementedError} always. |
reduce(): PolyFactor | Group the factors with the same base by adding their powers, remove the factors of power zero, and merge the monomial factors with an integer power. |
root(value: number): PolyFactor | N-th root: divide every power by value. |
sort(variable?: string): PolyFactor | Reoarder the factors using : 1. number of monoms 2. degree of polynom 3. power of polyfactor |
sqrt(): PolyFactor | Square root: divide every power by 2. |
subtract(...values: PolyFactor[]): PolyFactor | Subtract polyfactors. The operands are negated, so they are modified: pass clones to keep them. |
zero(): PolyFactor | Set the expression to 0. |