Skip to content

Polynomials ​

Four classes cover polynomial expressions:

ClassRepresentsExample
MonomA term: a coefficient and powers of variables3x^2y
PolynomA sum of termsx^2-5x+6
FactorA polynom raised to a power(x+1)^2
PolyFactorA product of factors, with a denominator if needed(x-2)(x-3)/(x+1)
js
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.

js
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 // 2

A 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.

js
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.

js
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 // 60

evaluate needs a value for every variable and throws a MathError otherwise. primitive throws for x^-1, whose primitive is a logarithm.

Tests ​

js
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 ​

js
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 // 5

With 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.

js
new Polynom('x^2+') // throws ParseError

Polynom.fromThreePoints builds the polynom of degree at most 2 through three points. The result is exact, and aligned points give a line.

js
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 MathError

Display ​

js
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.00

Operations ​

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.

js
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+1

divide is exact: it throws a MathError when the division leaves a remainder. euclidean gives the quotient and the remainder instead.

js
const {quotient, reminder} = new Polynom('x^2+1').euclidean(new Polynom('x-1'))

quotient.display // x+1
reminder.display // 2

inverse, root and sqrt always throw a MathError: the inverse or the root of a polynom is not a polynom.

Study a polynom ​

js
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 // +oo

evaluate returns an exact Fraction, or a number when the second argument is true.

Factor and roots ​

js
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 ​

js
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() // false

isEqual 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.

js
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.

js
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 // 2

asPower (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.

js
new Factor('(x+1)^(-2)').asRoot.display // 1/((x+1)^(2))
new Factor('(x+1)^(1/2)').asRoot.display // sqrt(x+1)
js
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+1

reduce 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.

js
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 MathError

Members ​

Monom ​

Constructors

  • new Monom(value?: InputValue<Fraction>)
  • new Monom(value?: Monom)

Static members

MemberDescription
fromNumber(value: number): MonomBuild a constant monom (empty literal part) from a number.
fromFraction(value: InputValue<Fraction>): MonomBuild a constant monom (empty literal part).
fromLiteral(coefficient: InputValue<Fraction>, literal: LiteralType<Fraction>): MonomBuild a monom from an explicit coefficient and literal part. The literal Fractions are cloned to avoid aliasing the caller's dictionary.
fromString(value: string): MonomBuild 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[]): MonomGreatest 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[]): MonomMultiply two monoms and return a NEW monom.

Properties and getters

MemberDescription
tex: stringLaTeX rendering of the current state (terminal output, not chainable).
display: stringThis display getter is to be used in the polynom display getter
coefficient: FractionGet 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: stringSet the literal part of the monom from a string
plotFunction: stringExpression to give to a plotting tool: explicit * between the coefficient and the variables.
variables: string[]Get the variables variables

Methods

MemberDescription
parse(inputStr: InputAlgebra<Monom>): MonomRoute an input to the matching from*** builder. Same-type inputs are copied; every other shape delegates without inlining construction logic.
copy(value: Monom): MonomCopy 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(): MonomFull duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy.
withDigits(digits: number | null): MonomShow 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>[]): MonomAdd 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(): booleanTrue when an exponent is not an integer (x^(1/2)).
degree(variable?: string): FractionGet the degree of a monom. If no variable is given, the result will be the global degree.
derivative(variable?: string): MonomDerivative the monom
divide(...M: InputAlgebra<Fraction>[]): MonomDivide the current monoms by multiple monoms
evaluate(values: LiteralType<number | Fraction> | InputValue<Fraction>, asNumeric: true): number
evaluate(values: LiteralType<number | Fraction> | InputValue<Fraction>, asNumeric?: false): Fraction
evaluate(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): booleanDetermine if a monom contains a variable in its literal part
integrate(a: InputValue<Fraction>, b: InputValue<Fraction>, variable?: string): FractionDefinite 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(): MonomReplace 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): booleanTrue 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): booleanDetermine if two monoms are equals
isLiteralSquare(): booleanTrue when every exponent is an even integer (x^4y^2).
isOne(): booleanDetermine if the monom is one
isSameAs(M: Monom): booleanDetermine if two monoms are similar
isSquare(): booleanTrue when the coefficient is a perfect square and every exponent is an even integer.
isZero(): booleanDetermine if the monom is null
multiply(...M: InputAlgebra<Fraction>[]): MonomMultiply multiple monoms into the current monom
one(): MonomCreate a one value monom
opposite(): MonomGet the opposite Returns a monom.
pow(nb: number | Fraction): MonomRaise the monom to a power.
primitive(variable?: string): MonomPrimitive 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(): MonomReduce the coefficient and remove the variables whose exponent is zero.
removeVariable(variable: string): voidRemove a variable from the literal part. Nothing happens when the variable is absent.
root(): MonomGet 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>): MonomSet the exponent of a particular variable.
sqrt(): MonomReturn 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>[]): MonomSubtract 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(): MonomCreate 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

MemberDescription
xMultiply(...polynoms: InputValue<Polynom>[]): PolynomProduct of the given values, as a new polynom.
fromCoefficients(variable: string, ...values: InputValue<Fraction>[]): PolynomBuild 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>): PolynomBuild a constant polynom.
fromLinear(variables: string, ...coefficients: InputValue<Fraction>[]): PolynomBuild 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>[]): PolynomBuild 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): PolynomBuild 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): PolynomBuild 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

MemberDescription
tex: stringLaTeX rendering of the current state (terminal output, not chainable).
display: stringASCIIMath 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: numberNumber 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: numberNumber of distinct variables.
plotFunction: stringExpression 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 | undefinedNumeric value of a constant polynom, or undefined when the degree is not zero.
variables: string[]The variables of the polynom, sorted alphabetically.

Methods

MemberDescription
parse(inputStr: PolynomParsingType, ...values: InputAlgebra<Monom>[]): PolynomRoute 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): PolynomCopy the mathematical state (monoms and default variable) of another polynom into this one. Display state is not transferred — see clone.
clone(): PolynomFull duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy.
withDigits(digits: number | null): PolynomShow 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>[]): PolynomAdd 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(): MonomGreatest 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): FractionDegree 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): PolynomDerivative with respect to variable (default x), as a new reduced polynom.
divide(...values: InputAlgebra<Polynom>[]): PolynomDivide 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(): PolynomRemove every monom. Unlike zero, which holds the monom 0, the polynom then has no term at all.
euclidean(P: Polynom): IEuclideanEuclidean 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): number
evaluate(values: LiteralType<Fraction | number> | InputValue<Fraction>, asNumeric?: false): Fraction
evaluate(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(): numberGreatest common divisor of the denominators of the coefficients.
gcdNumerator(): numberGreatest 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): booleanTrue when the variable appears in a term with a non-zero exponent.
integrate(a: InputValue<Fraction>, b: InputValue<Fraction>, variable?: string): FractionDefinite 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(): Polynom1/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): booleanWhether the given string is the fully developed (expanded) form of this polynom. Throws: {ParseError} when polynomString is malformed.
isDivisibleBy(div: Polynom): booleanTrue when the division by div leaves no remainder. A degree-1 divisor is tested through its root.
isEqual(P: Polynom): booleanTrue when both polynoms are equal once reduced and sorted: same terms, same coefficients.
hasCrossTerm(): booleanWhether the polynom contains a cross-term monom (a single monom carrying more than one variable, such as xy).
isOne(): booleanTrue for the constant polynom 1.
isOppositeAt(P: Polynom): booleanTrue when P is the opposite polynom (x+1 and -x-1).
isSameAs(P: Polynom): booleanTrue when both polynoms have the same terms, whatever their coefficients (x^2+x and 3x^2-5x).
isZero(): booleanTrue for the zero polynom (no term, or the single term 0).
lcmDenominator(): numberLeast common multiple of the denominators of the coefficients.
lcmNumerator(): numberLeast common multiple of the numerators of the coefficients.
limitTo(value: InputValue<Fraction>, variable?: string): FractionLimit 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): MonomMonom 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): MonomFirst 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>[]): PolynomMultiply 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(): PolynomSet the polynom to the constant 1.
opposite(): PolynomChange the sign of every term.
pow(nb: number): PolynomRaise 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): PolynomA primitive with respect to variable (default x), without integration constant. Throws: {MathError} if a term has a power of -1 of the variable.
reduce(): PolynomAdd the similar terms, remove the zero terms, reduce the coefficients and sort by decreasing degree.
reorder(variable?: string, revert?: boolean): PolynomSort 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): PolynomReplace a variable by a polynom (in place). Throws: {MathError} when the replaced variable appears with a negative power.
root(): PolynomThe n-th root of a polynom is not a polynom: the operation is not available. Throws: {MathError} always.
setVariable(value: string): PolynomSet 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(): PolynomThe square root of a polynom is not a polynom: the operation is not available. Throws: {MathError} always.
subtract(...values: InputAlgebra<Polynom>[]): PolynomSubtract 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(): PolynomSet the polynom to zero.

Factor ​

Constructors

  • new Factor(value?: InputAlgebra<Polynom> \| Factor, power?: InputValue<Fraction>)

Static members

MemberDescription
fromString(value: string): FactorBuild 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>): FactorBuild a factor of power 1 from a polynom, or from anything a polynom accepts.
checkParseAsFactors(str: string): booleanTrue 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

MemberDescription
tex: stringLaTeX rendering of the current state (terminal output, not chainable).
display: stringASCIIMath rendering of the current state (terminal output, not chainable).
asSingle: FactorDisplay 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: PolynomBase polynom. The getter returns a copy: modify the factor through its methods or the setter.
power: FractionPower 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: FactorDisplay option (default): a power is written (P)^{n}, a negative one with a negative exponent. Chainable, kept by clone.
asRoot: FactorDisplay 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

MemberDescription
parse(value: InputAlgebra<Polynom> | Factor, power?: InputValue<Fraction>): FactorReplace 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): FactorCopy 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(): FactorFull duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy.
withDigits(digits: number | null): FactorShow every numeric value as a decimal with digits digits after the point (exact values included: 3/2 becomes 1.50 with 2 digits).
add(): FactorAdding two factors is not defined. Throws: {MathError} always.
degree(variable?: string): FractionDegree of the polynom multiplied by the power.
derivative(): FactorNot 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(): PolynomExpand 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>): FactorDivide 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): number
evaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: false): Fraction
evaluate(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): booleanTrue when the base polynom contains the variable.
inverse(): FactorNegate the power (P^n becomes P^-n).
isEqual(value: Factor): booleanTrue when both factors have the same base polynom and the same power.
isOne(): booleanTrue when the base polynom is 1, or the power is zero.
isSameAs(value: InputAlgebra<Factor | Polynom>): booleanTrue when the base polynom is the same; the power is ignored.
isZero(): booleanTrue when the base polynom is zero.
multiply(value: InputAlgebra<Factor | Polynom>): FactorMultiply 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(): FactorSet the factor to 1.
opposite(): FactorA factor has no sign: use PolyFactor, which adds a factor -1. Throws: {UnsupportedOperationError} always.
pow(value: number | Fraction): FactorMultiply the power by value.
primitive(): FactorNot available on a single factor. Throws: {UnsupportedOperationError} always.
reduce(): FactorNot available on a single factor: use PolyFactor.reduce. Throws: {UnsupportedOperationError} always.
root(value: number): FactorDivide the power by value (the square root of P^2 is P^1).
sqrt(): FactorSquare root: divide the power by 2.
subtract(): FactorSubtracting two factors is not defined. Throws: {MathError} always.
zero(): FactorSet the factor to 0.

PolyFactor ​

Constructors

  • new PolyFactor(...values: (InputAlgebra<Polynom> \| Factor \| PolyFactor)[])

Static members

MemberDescription
gcd(...values: PolyFactor[]): PolyFactorGreatest common divisor: every base shared by all the polyfactors, at its lowest power.
lcm(...values: PolyFactor[]): PolyFactorLeast common multiple: every base found in one of the polyfactors, at its highest power.
fromPolynom(numerator: InputAlgebra<Polynom>, denominator?: InputAlgebra<Polynom>): PolyFactorBuild 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>)[]): PolyFactorBuild a polyfactor from factors, or from anything a factor accepts.
fromString(value: string): PolyFactorBuild 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

MemberDescription
tex: stringLaTeX rendering of the current state (terminal output, not chainable).
display: stringASCIIMath rendering of the current state (terminal output, not chainable).
asPower: PolyFactorDisplay option (default): the factors are written one after the other, with their exponent. Chainable, kept by clone.
asRoot: PolyFactorDisplay option: the factors with a negative power are written in a fraction under the numerator. Chainable, kept by clone.
denominator: PolyFactorThe 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: PolyFactorThe factors with a positive power, as a new polyfactor.
variables: string[]Variables of all the factors.

Methods

MemberDescription
parse(value: InputAlgebra<Polynom> | Factor | PolyFactor | string): PolyFactorReplace the content by a string, a polynom, a factor or another polyfactor. Throws: {ParseError} if a string is malformed.
copy(value: PolyFactor): PolyFactorCopy 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(): PolyFactorFull duplicate of this instance, including display state (selected form and options set via as*** / with***). Distinct from copy.
withDigits(digits: number | null): PolyFactorShow 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[]): PolyFactorAdd 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): FractionSum of the degrees of the factors; a denominator counts negatively.
derivative(): PolyFactorDerivative, by the product rule. The result is added with add, so it is factored again.
develop(): PolyFactorExpand 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[]): PolyFactorDivide by polyfactors: append their inverted factors. The result is not reduced.
evaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric: true): number
evaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: false): Fraction
evaluate(values: InputValue<Fraction> | LiteralType<number | Fraction>, asNumeric?: boolean): number | Fraction
Value of the expression for given values of its variables.
factorize(variable?: string): PolyFactorFactor the polynom of each factor, and return the result as a new polyfactor.
hasVariable(variable: string): booleanTrue when a factor contains the variable.
inverse(): PolyFactorNegate every power: numerator and denominator are swapped.
isEqual(value: PolyFactor): booleanTrue when both expressions are equal: their quotient, once the common factors are removed, reduces to one.
isOne(): booleanTrue when every factor is one.
isZero(): booleanTrue when a factor is zero.
multiply(...values: (InputAlgebra<Polynom> | Factor | PolyFactor)[]): PolyFactorMultiply by polyfactors, factors, polynoms or strings: append their factors. The result is not reduced.
one(): PolyFactorSet the expression to 1.
opposite(): PolyFactorChange the sign: add a factor -1, or remove it when it is already there.
pow(value: number | Fraction): PolyFactorRaise to a power: multiply the power of every factor.
primitive(): PolyFactorNot implemented. Throws: {NotImplementedError} always.
reduce(): PolyFactorGroup 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): PolyFactorN-th root: divide every power by value.
sort(variable?: string): PolyFactorReoarder the factors using : 1. number of monoms 2. degree of polynom 3. power of polyfactor
sqrt(): PolyFactorSquare root: divide every power by 2.
subtract(...values: PolyFactor[]): PolyFactorSubtract polyfactors. The operands are negated, so they are modified: pass clones to keep them.
zero(): PolyFactorSet the expression to 0.

Released under the MIT License.