Package polexpr documentation

0.6 (2018/11/20)

Contents

Basic Examples

The syntax is:

\poldef polname(x):= expression in variable x;

where:

Attention!

The ' is authorized since 0.5.1. As a result some constructs recognized by the \xintexpr parser, such as var1 'and' var2 will get misinterpreted and cause errors. However these constructs are unlikely to be frequently needed in polynomial expressions, and the \xintexpr syntax offers alternatives, so it was deemed a small evil. Of course the \xintexpr parser is modified only temporarily during execution of \poldef.

One can also issue:

\PolDef{polname}{expression in variable x}

which admits an optional first argument to modify the variable letter from its default x.

\poldef f(x):= 1-x+x^2;
defines polynomial f. Polynomial names must start with a letter and may contain letters, digits, underscores and the right tick character. The variable must be a single letter. The colon character is optional. The semi-colon at end of expression is mandatory.
\PolDef{f}{1-x+x^2}
does the same as \poldef f(x):= 1-x+x^2; To use another letter than x in the expression, one must pass it as an extra optional argument to \PolDef. Useful if the semi-colon has been assigned some non-standard catcode by some package.
\PolLet{g}={f}
saves a copy of f under name g. Also usable without =.
\poldef f(z):= f(z)^2;
redefines f in terms of itself.
\poldef f(T):= f(f(T));
again redefines f in terms of its (new) self.
\poldef k(z):= f(z)-g(g(z)^2)^2;
should now define the zero polynomial... Let's check: \[ k(z) = \PolTypeset[z]{k} \]
\PolDiff{f}{f'}
sets f' to the derivative of f. The name doesn't have to be f' (in fact the ' is licit only since 0.5.1).

Important

This is not done automatically. If some new definition needs to use the derivative of some available polynomial, that derivative polynomial must have been defined via \PolDiff: something like T'(x)^2 will not work without a prior \PolDiff{T}{T'}.

\PolDiff{f'}{f''}
obtains second derivative.
\PolDiff[3]{f}{f'''}
computes the third derivative.
$f(z)   = \PolTypeset[z]{f}    $\newline
$f'(z)  = \PolTypeset[z]{f'}   $\newline
$f''(z) = \PolTypeset[z]{f''}  $\newline
$f'''(z)= \PolTypeset[z]{f'''} $\par

Important

The package does not currently know rational functions: / in a parsed polynomial expression does the Euclidean quotient:

(1-x^2)/(1-x)

does give 1+x but

(1/(1-x))*(1-x^2)

evaluates to zero. This will work as expected:

\poldef k(x):= (x-1)(x-2)(x-3)(x-4)/(x^2-5x+4);

Attention!

1/2 x^2 skips the space and is treated like 1/(2*x^2) because of the tacit multiplication rules of xintexpr. But this means it gives zero! Thus one must use (1/2)x^2 or 1/2*x^2 or (1/2)*x^2 for disambiguation: x - 1/2*x^2 + 1/3*x^3.... It is even simpler to move the denominator to the right: x - x^2/2 + x^3/3 - ....

It is worth noting that 1/2(x-1)(x-2) suffers the same issue: xint tacit multiplication always "ties more", hence this gets interpreted as 1/(2*(x-1)*(x-2)) which gives zero by polynomial division. Thus, use one of (1/2)(x-1)(x-2), 1/2*(x-1)(x-2) or (x-1)(x-2)/2.

After:

\poldef f_1(x):= 25(x-1)(x^2-2)(x-3)(x-4)(x-5);%
\poldef f_2(x):= 37(x-1)(x^2-2)(x-6)(x-7)(x-8);%

the macro call \PolGCD{f_1}{f_2}{k} sets k to the (unitary) GCD of f_1 and f_2 (hence to the expansion of (x-1)(x^2-2).)

\PolToExpr{k}
will (expandably) give in this case x^3-x^2-2*x+2. This is useful for console or file output (the syntax is Maple- and PSTricks-compatible; the letter used in output can be (non-expandably) changed via a redefinition of \PolToExprVar.)
\PolToExpr*{k}
gives ascending powers: 2-2*x-x^2+x^3.

Examples of localization of roots

A typical example

In this example the polynomial is square-free; we can make sure of that by comparing the degree of the first element of the Sturm chain with the degree of the original polynomial. In such case the second element of the Sturm chain is still the polynomial first derivative, because there was no further reduction.

\poldef f(x) := x^7 - x^6 - 2x + 1;

\PolToSturm{f}{f}
\PolSturmIsolateZeros{f}
The \PolTypeset{f} polynomial has \PolSturmNbOfIsolatedZeros{f} distinct real
roots which are located in the following intervals:
\PolPrintIntervals{f}
Here is the second root with ten more decimal digits:
\PolRefineInterval[10]{f}{2}
\[\PolSturmIsolatedZeroLeft{f}{2}<Z_2<\PolSturmIsolatedZeroRight{f}{2}\]
And here is the first root with twenty digits after decimal mark:
\PolEnsureIntervalLength{f}{1}{-20}
\[\PolSturmIsolatedZeroLeft{f}{1}<Z_1<\PolSturmIsolatedZeroRight{f}{1}\]
The first element of the Sturm chain has degree $\PolDegree{f_0}$. As
this same as $\PolDegree{f}$ we know that the latter was square free.
So the derivative is up to a constant \PolTypeset{f_1} (in fact here
it is identical with it).
\PolToSturm{f_1}{f_1}\PolSturmIsolateZeros{f_1}%
It has \PolSturmNbOfIsolatedZeros{f_1} distinct real
roots:
\PolPrintIntervals[W]{f_1}
\PolEnsureIntervalLengths{f_1}{-10}%
Here they are with ten digits after decimal mark:
\PolPrintIntervals[W]{f_1}
\PolDiff{f_1}{f''}
\PolToSturm{f''}{f''}
\PolSturmIsolateZeros{f''}
The second derivative is \PolTypeset{f''}.
It has \PolSturmNbOfIsolatedZeros{f''} distinct real
roots:
\PolPrintIntervals[X]{f''}
Here is the positive one with 20 digits after decimal mark:
\PolEnsureIntervalLength{f''}{2}{-20}%
\[X_2 = \PolSturmIsolatedZeroLeft{f''}{2}\dots\]
The more mathematically advanced among our dear readers will be able
to give the exact value for $X_2$!

A degree four polynomial with nearby roots

\PolDef{Q}{(x-1.050001)(x-1.105001)(x-1.110501)(x-1.111051)}
\PolTypeset{Q}
\PolToSturm{Q}{Q} % it is allowed to use same prefix for Sturm chain
\PolSturmIsolateZeros{Q}
\PolPrintIntervals{Q}
% reports 1.0 < Z_1 < 1.1, 1.10 < Z_2 < 1.11, 1.110 < Z_3 < 1.111, and 1.111 < Z_4 < 1.112
% but the above bounds do not allow minimizing separation between roots
% so we refine:
\PolRefineInterval*{Q}{1}
\PolRefineInterval*{Q}{2}
\PolRefineInterval*{Q}{3}
\PolRefineInterval*{Q}{4}
\PolPrintIntervals{Q}
% reports 1.05 < Z_1 < 1.06, 1.105 < Z_2 < 1.106, 1.1105 < Z_3 < 1.1106,
% and 1.11105 < Z_4 < 1.11106.
\PolEnsureIntervalLengths{Q}{-6}
\PolPrintIntervals{Q}
% of course finds here all roots exactly

The degree nine polynomial with 0.99, 0.999, 0.9999 as triple roots

% define a user command (xinttools is loaded automatically by polexpr)
\newcommand\showmultiplicities[1]{% #1 = "sturmname"
\xintFor* ##1 in {\xintSeq{1}{\PolSturmNbOfIsolatedZeros{#1}}}\do{%
    The multiplicity is \PolSturmIsolatedZeroMultiplicity{#1}{##1}
    \PolSturmIfZeroExactlyKnown{#1}{##1}%
    {at the root $x=\PolSturmIsolatedZeroLeft{#1}{##1}$}
    {for the root such that
    $\PolSturmIsolatedZeroLeft{#1}{##1}<x<\PolSturmIsolatedZeroRight{#1}{##1}$}
    \par
}}%
\PolDef{f}{(x-0.99)^3(x-0.999)^3(x-0.9999)^3}
\renewcommand\PolTypesetOne[1]{\PolDecToString{\xintREZ{#1}}}
\PolTypeset{f}\par
\PolToSturm{f}{f}% it is allowed to use "polname" as "sturmname" too
\PolSturmIsolateZerosAndGetMultiplicities{f}% use the "sturmname" here
% or \PolSturmIsolateZeros*{f} which is exactly the same, but shorter..

\showmultiplicities{f}

In this example, the output will look like this (but using math mode):

x^9 − 8.9667x^8 + 35.73400293x^7 − 83.070418400109x^6 + 124.143648875193123x^5
− 123.683070924326075877x^4 + 82.149260397553075617891x^3
− 35.07602992699900159127007x^2 + 8.7364078733314648368671733x
− 0.967100824643585986488103299

The multiplicity is 3 at the root x = 0.99
The multiplicity is 3 at the root x = 0.999
The multiplicity is 3 at the root x = 0.9999

On first pass, these rational roots were found. But multiplicity computation works also with (decimal) roots not yet identified or with non-decimal or irrational roots.

Try it out!

It is fun to modify only a tiny bit the polynomial and see if polexpr survives:

\PolDef{g}{f(x)+1e-27}
\PolTypeset{g}\par
\PolToSturm{g}{g}
\PolSturmIsolateZeros*{g}

\showmultiplicities{g}

This produces:

x^9 − 8.9667x^8 + 35.73400293x^7 − 83.070418400109x^6 + 124.143648875193123x^5
− 123.683070924326075877x^4 + 82.149260397553075617891x^3
− 35.07602992699900159127007x^2 + 8.7364078733314648368671733x
− 0.967100824643585986488103298

The multiplicity is 1 for the root such that 0.98 < x < 0.99
The multiplicity is 1 for the root such that 0.9991 < x < 0.9992
The multiplicity is 1 for the root such that 0.9997 < x < 0.9998

Which means that the multiplicity-3 roots each became a real and a pair of complex ones. Let's see them better:

\PolEnsureIntervalLengths{g}{-10}

\showmultiplicities{g}

which produces:

The multiplicity is 1 for the root such that 0.9899888032 < x < 0.9899888033
The multiplicity is 1 for the root such that 0.9991447980 < x < 0.9991447981
The multiplicity is 1 for the root such that 0.9997663986 < x < 0.9997663987

Try obtaining this with your pocket calculator! (or IEEE-7554 numerics...)

A Mignotte type polynomial

\PolDef{P}{x^10 - (10x-1)^2}%
\PolTypeset{P}              % prints it in expanded form
\PolToSturm{P}{P}           % we can use same prefix for Sturm chain
\PolSturmIsolateZeros{P}    % finds 4 real roots
This polynomial has \PolSturmNbOfIsolatedZeros{P} distinct real roots:
\PolPrintIntervals{P}%
% reports  -2 < Z_1 < -1, 0 < Z_2 < 0.1, 0.1 < Z_3 < 0.2, 1 < Z_4 < 2
Let us refine the second and third intervals to separate the corresponding
roots:
\PolRefineInterval*{P}{2}% will refine to 0.0999990 < Z_2 < 0.0999991
\PolRefineInterval*{P}{3}% will refine to 0.100001 < Z_3 < 0.100002
\PolPrintIntervals{P}%
Let us now get to know all roots with 10 digits after decimal mark:
\PolEnsureIntervalLengths{P}{-10}%
\PolPrintIntervals{P}% now all roots are known 10 decimal digits after mark
Finally, we display 20 digits of the second root:
\PolEnsureIntervalLength{P}{2}{-20}% makes Z_2 known with 20 digits after mark
\[\PolSturmIsolatedZeroLeft{P}{2}<Z_2<\PolSturmIsolatedZeroRight{P}{2}\]

The last line produces:

0.09999900004999650028 < Z_2 < 0.09999900004999650029

The degree 41 polynomial with -2, -1.9, -1.8, ..., 0, 0.1, ..., 1.9, 2 as roots

\PolDef{P}{mul((x-i*1e-1), i=-20..20)}% i/10 is same but less efficient

In the defining expression we could have used i/10 but this gives less efficient internal form for the coefficients (the 10's end up in denominators). Using \PolToExpr{P} after having done

\renewcommand\PolToExprCmd[1]{\PolDecToString{\xintREZ{#1}}}

we get this expanded form:

x^41
-28.7*x^39
+375.7117*x^37
-2975.11006*x^35
+15935.28150578*x^33
-61167.527674162*x^31
+173944.259366417394*x^29
-373686.963560544648*x^27
+613012.0665016658846445*x^25
-771182.31133138163125495*x^23
+743263.86672885754888959569*x^21
-545609.076599482896371978698*x^19
+301748.325708943677229642930528*x^17
-123655.8987669450434698869844544*x^15
+36666.1782054884005855608205864192*x^13
-7607.85821367459445649518380016128*x^11
+1053.15135918687298508885950223794176*x^9
-90.6380005918141132650786081964032*x^7
+4.33701563847327366842552218288128*x^5
-0.0944770968420804735498178265088*x^3
+0.00059190121813899276854174416896*x

which shows coefficients with up to 36 significant digits...

Stress test: not a hard challenge to xint + polexpr, but be a bit patient!

\PolDef{P}{mul((x-i*1e-1), i=-20..20)}%
\PolToSturm{P}{S}           % dutifully computes S_0, ..., S_{41}
\PolSturmIsolateZeros[1]{S} % finds *exactly* (but a bit slowly) all 41 roots!
\PolPrintIntervals{S}       % nice, isn't it?

Note

Release 0.5 has experimental addition of optional argument E to \PolSturmIsolateZeros. It instructs to search roots only in interval (-10^E, 10^E), extremities assumed to not be roots. Thus here:

\PolSturmIsolateZeros[1]{S}

gives some speed gain; without it, it turns out in this case that polexpr would have started with (-10^6, 10^6) interval.

This will probably get replaced in future by the specification of a general interval.

Roots of Chebyshev polynomials

\newcount\mycount
\poldef T_0(x) := 1;
\poldef T_1(x) := x;
\mycount 2
\xintloop
  \poldef T_\the\mycount(x) :=
          2x*T_\the\numexpr\mycount-1(x)
           - T_\the\numexpr\mycount-2(x);
\ifnum\mycount<15
\advance\mycount 1
\repeat

\[T_{15} = \PolTypeset[X]{T_15}\]
\PolToSturm{T_15}{T_15}
\PolSturmIsolateZeros{T_15}
\PolEnsureIntervalLengths{T_15}{-10}
\PolPrintIntervals{T_15}

Non-expandable macros

\poldef polname(letter):= expression in letter;

This evaluates the polynomial expression and stores the coefficients in a private structure accessible later via other package macros, under the user-chosen polname. Of course the expression can use other previously defined polynomials. Names must start with a letter and are constituted of letters, digits, underscores and (since 0.5.1) the right tick '. The whole xintexpr syntax is authorized:

\poldef sin(z) := add((-1)^i z^(2i+1)/(2i+1)!, i = 0..10);

With fractional coefficients, beware the tacit multiplication issue.

As a side effect the function polname() is recognized as a genuine \xintexpr...\relax function for (exact) numerical evaluation (or within an \xintdefvar assignment.) It computes values not according to the original expression but via the Horner scheme corresponding to the polynomial coefficients.

Attention!

Release 0.3 also did the necessary set-up to let the polynomial be known to the \xintfloatexpr (or \xintdeffloatvar) parser.

Since 0.4 this isn't done automatically. Even more, a previously existing floating point variant of the same name will be let undefined again, to avoid hard to debug mismatches between exact and floating point polynomials. This also applies when the polynomial is produced not via \poldef or \PolDef but as a product of the other package macros.

See \PolGenFloatVariant{polname}.

The original expression is lost after parsing, and in particular the package provides no way to typeset it. This has to be done manually, if needed.

\PolDef[letter]{polname}{expression in letter}

Does the same as \poldef in an undelimited macro format (thus avoiding potential problems with the catcode of the semi-colon in presence of some packages.) In absence of the [letter] optional argument, the variable is assumed to be x.

\PolGenFloatVariant{polname}

Makes the polynomial also usable in the \xintfloatexpr parser. It will therein evaluates via an Horner scheme with coefficients already pre-rounded to the float precision.

See also \PolToFloatExpr{polname}.

Attention!

Release 0.3 did this automatically on \PolDef and \poldef but this was removed at 0.4 for optimization.

Any operation, for example generating the derivative polynomial, or dividing two polynomials or using the \PolLet, must be followed by explicit usage of \PolGenFloatVariant{polname} if the new polynomial is to be used in \xintfloatexpr or alike context.

\PolLet{polname_2}={polname_1}

Makes a copy of the already defined polynomial polname_1 to a new one polname_2. Same effect as \PolDef{polname_2}{polname_1(x)} but with less overhead. The = is optional.

\PolAssign{polname}\toarray\macro

Defines a one-argument expandable macro \macro{#1} which expands to the (raw) #1th polynomial coefficient.

  • Attention, coefficients here are indexed starting at 1.
  • With #1=-1, -2, ..., \macro{#1} returns leading coefficients.
  • With #1=0, returns the number of coefficients, i.e. 1 + deg f for non-zero polynomials.
  • Out-of-range #1's return 0/1[0].

See also \PolNthCoeff{polname}{number}. The main difference is that with \PolAssign, \macro is made a prefix to 1 + deg f already defined (hidden to user) macros holding individually the coefficients but \PolNthCoeff{polname}{number} does each time the job to expandably recover the Nth coefficient, and due to expandability can not store it in a macro for future usage (of course, it can be an argument in an \edef.) The other difference is the shift by one in indexing, mentioned above (negative indices act the same in both.)

\PolGet{polname}\fromarray\macro

Does the converse operation to \PolAssign{polname}\toarray\macro. Each individual \macro{number} gets expanded in an \edef and then normalized via xintfrac's macro \xintRaw.

The leading zeros are removed from the polynomial.

(contrived) Example:

\xintAssignArray{1}{-2}{5}{-3}\to\foo
\PolGet{f}\fromarray\foo

This will define f as would have \poldef f(x):=1-2x+5x^2-3x^3;.

Note

Prior to 0.5, coefficients were not normalized via \xintRaw for internal storage.

\PolFromCSV{polname}{<csv>}

Defines a polynomial directly from the comma separated list of values (or a macro expanding to such a list) of its coefficients, the constant term being the first item. No validity checks. Spaces from the list argument are trimmed. List items are each expanded in an \edef and then put into normalized form via xintfrac's macro \xintRaw.

Leading zero coefficients are removed:

\PolFromCSV{f}{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

defines the zero polynomial, which has only one (zero) coefficient.

See also expandable macro \PolToCSV.

Note

Prior to 0.5, coefficients were not normalized via \xintRaw for internal storage.

\PolTypeset{polname}

Typesets in descending powers in math mode. It uses letter x but this can be changed via an optional argument:

\PolTypeset[z]{polname}

By default zero coefficients are skipped (issue \poltypesetalltrue to get all of them in output).

These commands (whose meanings will be found in the package code) can be re-defined for customization. Their default definitions are expandable, but this is not a requirement.

\PolTypesetCmd{raw_coeff}

Checks if the coefficient is 1 or -1 and then skips printing the 1, except for the constant term. Also it sets conditional \PolIfCoeffIsPlusOrMinusOne{A}{B}.

The actual printing of the coefficients, when not equal to plus or minus one is handled by \PolTypesetOne{raw_coeff}.

\PolTypesetOne{raw_coeff}

The default is \xintSignedFrac but this macro is annoying as it insists to use a power of ten, and not decimal notation.

One can do things such as for example: [1]

\renewcommand\PolTypesetOne[1]{\num{\xintPFloat[5]{#1}}}
\renewcommand\PolTypesetOne[1]{\num{\xintRound{4}{#1}}}

where e.g. we used the \num macro of siunitx as it understands floating point notation.

[1]the difference in the syntaxes of \xintPFloat and \xintRound is explained from the fact that \xintPFloat by default uses the prevailing precision hence the extra argument like here 5 is an optional one.

One can also give a try to using \PolDecToString{decimal number} which uses decimal notation (at least for the numerator part).

\PolTypesetMonomialCmd

This decides how a monomial (in variable \PolVar and with exponent \PolIndex) is to be printed. The default does nothing for the constant term, \PolVar for the first degree and \PolVar^{\PolIndex} for higher degrees monomials. Beware that \PolIndex expands to digit tokens and needs termination in \ifnum tests.

\PolTypesetCmdPrefix{raw_coeff}

Expands to a + if the raw_coeff is zero or positive, and to nothing if raw_coeff is negative, as in latter case the \xintSignedFrac used by \PolTypesetCmd{raw_coeff} will put the - sign in front of the fraction (if it is a fraction) and this will thus serve as separator in the typeset formula. Not used for the first term.

\PolTypeset*{polname}

Typesets in ascending powers. Use e.g. [h] optional argument (after the *) to use letter h rather than x.

\PolDiff{polname_1}{polname_2}

This sets polname_2 to the first derivative of polname_1. It is allowed to issue \PolDiff{f}{f}, effectively replacing f by f'.

Coefficients of the result polname_2 are irreducible fractions (see Technicalities for the whole story.)

\PolDiff[N]{polname_1}{polname_2}

This sets polname_2 to the N-th derivative of polname_1. Identical arguments is allowed. With N=0, same effect as \PolLet{polname_2}={polname_1}. With negative N, switches to using \PolAntiDiff.

\PolAntiDiff{polname_1}{polname_2}

This sets polname_2 to the primitive of polname_1 vanishing at zero.

Coefficients of the result polname_2 are irreducible fractions (see Technicalities for the whole story.)

\PolAntiDiff[N]{polname_1}{polname_2}

This sets polname_2 to the result of N successive integrations on polname_1. With negative N, it switches to using \PolDiff.

\PolDivide{polname_1}{polname_2}{polname_Q}{polname_R}

This sets polname_Q and polname_R to be the quotient and remainder in the Euclidean division of polname_1 by polname_2.

\PolQuo{polname_1}{polname_2}{polname_Q}

This sets polname_Q to be the quotient in the Euclidean division of polname_1 by polname_2.

\PolRem{polname_1}{polname_2}{polname_R}

This sets polname_R to be the remainder in the Euclidean division of polname_1 by polname_2.

\PolGCD{polname_1}{polname_2}{polname_GCD}

This sets polname_GCD to be the (monic) GCD of the two first polynomials. It is a unitary polynomial except if both polname_1 and polname_2 vanish, then polname_GCD is the zero polynomial.

\PolToSturm{polname}{sturmname}

With polname being for example P, the macro starts by computing polynomials P and P', then computes the (opposite of the) remainder in euclidean division, iteratively.

The last non-zero remainder P_N_ (where N is obtainable as \PolSturmChainLength{sturmname}) is up to a factor the GCD of P and P' hence it is a constant if and only if P is square-free.

Note

  • Since 0.5 all these polynomials are divided by their rational content, so they have integer coefficients with no common factor, and the last one if a constant is either 1 or -1.
  • After this normalization to primitive polynomials, they are stored internally as sturmname_k_, k=0,1, ....
  • These polynomials are used internally only. To keep them as genuine declared polynomials also after the macro call, use the starred variant PolToSturm*.

Note

It is perfectly allowed to use the polynomial name as Sturm chain name: \PolToSturm{f}(f}.

The macro then declares sturmname_0, sturmname_1, ..., which are the (non-declared) sturmname_k_ divided by the last one. Division is not done if this last one is the constant 1 or -1, i.e. if the original polynomial was square-free. These polynomials are primitive polynomials too, i.e. with integer coefficients having no common factor.

Thus sturmname_0 has exactly the same real and complex roots as polynomial polname, but with each root now of multiplicity one.

Notice that sturmname_1 isn't necessarily the derivative of sturmname_0 due to the various normalizations.

These polynomials sturmname_k (contrarily to the sturmname_k_ ones) are usable after the macro execution but their main utility is for the execution of \PolSturmIsolateZeros{sturmname}.

\PolSturmChainLength{sturmname} gives the index of the last element of the Sturm chain.

\PolToSturm*{polname}{sturmname}

Does the same as un-starred version and additionally it keeps for user usage the memory of the un-normalized Sturm chain polynomials sturmname_k_, k=0,1, ..., N, with N being \PolSturmChainLength{sturmname}.

Note

This behaviour was modified at 0.6, anyhow the macro was broken at 0.5.

\PolSetToSturmChainSignChangesAt{\macro}{sturmname}{fraction}

Sets macro \macro to the number of sign changes in the Sturm chain with name prefix sturmname, at location fraction (which must be in format as acceptable by the xintfrac macros.)

Note

The author was lazy and did not provide rather an expandable variant, where one would do \edef\macro{\PolNbOf...}.

This will presumably get added in a future release.

After some hesitation it was decided the macro would by default act globally. To make the scope of its macro definition local, use [\empty] as extra optional argument.

\PolSetToNbOfZerosWithin{\macro}{sturmname}{value_a}{value_b}

Applies the Sturm Theorem to set \macro to the exact number of distinct roots of sturmname_0 in the interval (value_a, value_b] (the macro first re-orders the value for value_a <= value_b to hold).

Note

The author was lazy and did not provide rather an expandable variant, where one would do \edef\macro{\PolNbOf...}.

This will presumably get added in future.

After some hesitation it was decided the macro would by default act globally. To make the scope of its macro definition local, use [\empty] as extra optional argument.

See also the expandable \PolSturmNbOfRootsOf{sturmname}\LessThanOrEqualTo{value}, from which it is immediate (with \numexpr) to create an expandable variant of this macro. However the difference is that this macro requires only \PolToSturm to have been executed, whereas the expandable variant requires prior execution of \PolSturmIsolateZeros.

See also the expandable \PolSturmNbWithMultOfRootsOf{sturmname}\LessThanOrEqualTo{value} which requires prior execution of \PolSturmIsolateZeros*.

\PolSturmIsolateZeros{sturmname}

First, it evaluates using Sturm theorem the number of distinct real roots of sturmname_0.

Note

The Sturm chain must have been produced by an earlier \PolToSturm{polname}{sturmname}.

Why does this macro ask for argument the name of Sturm chain, rather than the name of a polynomial? well this is mainly for legacy reason, and because it is accompanied by other macros for which it is simpler to assume the argument will be the name of an already computed Sturm chain.

Notice that \PolToSturm{f}{f} is perfectly legal (the sturmname can be same as the polname): it defines polynomials f_0, f_1, ... having f has name prefix.

Such a prior call to \PolToSturm must have been made at any rate for \PolSturmIsolateZeros to be usable.

Then it locates, again using Sturm theorem, as many disjoint intervals as there are roots. Some intervals reduce to singleton which are roots. Non-singleton intervals get refined to make sure none of their two limit points is a root: they contain each a single root, in their respective interiors.

The interval boundaries are decimal numbers, originating in iterated decimal subdivision from initial intervals (-10^E, 0) and (0, 10^E); if zero is a root it is always identified individually. The non-singleton intervals are of the type (a/10^f, (a+1)/10^f) with a an integer, which is neither 0 nor -1. Hence a and a+1 are both positive or both negative.

The interval boundaries (and exactly found roots) are made available for future computations in \xintexpr-essions or polynomial definitions as variables <sturmname>L_1, <sturmname>L_2, etc..., for the left end-points and <sturmname>R_1, <sturmname>R_2, ..., for the right end-points.

Also two macro arrays (in the sense of xinttools's \xintAssignArray) are created for holding the interval end-points written out in standard decimal notation (see \PolDecToString{decimal number}). To access these values, macros \PolSturmIsolatedZeroLeft{sturmname}{index} and \PolSturmIsolatedZeroRight{sturmname}{index} are provided.

Important

Trailing zeroes in these stored decimal numbers are significant: they are also present in the decimal expansion of the exact root.

Note

The actual array macros are \POL_ZeroInt<sturmname>L and \POL_ZeroInt<sturmname>R but as these names use the non-letter character _ and possibly also digits from sturmname, the accessor macros above have been made part of the package.

The start of decimal expansion of a positive k-th root is given by \PolSturmIsolatedZeroLeft{sturmname}{k}, and for a negative root it is given by PolSturmIsolatedZeroRight{sturmname}{k}. These two decimal numbers are either both zero or both of the same sign.

The number of distinct roots is obtainable expandably as \PolSturmNbOfIsolatedZeros{sturmname}.

Furthermore \PolSturmNbOfRootsOf{sturmname}\LessThanOrEqualTo{value} and \PolSturmNbOfRootsOf{sturmname}\LessThanOrEqualToExpr{expression}. will expandably act as their names indicate.

Note

In the current implementation the <sturmname>... xintexpr variables and the \POL_ZeroInt... arrays are globally defined. On the other hand the Sturm sequence polynomials obey the current scope.

Note

When two successive roots are located in adjacent intervals, the separation between them is not lower bounded. See \PolRefineInterval*{sturmname}{index}.

Note

As all computations are done exactly there can be no errors... apart those due to bad coding by author. The results are exact bounds for the mathematically exact real roots.

Future releases will perhaps also provide macros based on Newton or Regula Falsi methods. Exact computations with such methods lead however quickly to very big fractions, and this forces usage of some rounding scheme for the abscissas if computation times are to remain reasonable. This raises issues of its own, which are studied in numerical mathematics.

\PolSturmIsolateZeros*{sturmname}

The macro does the same as \PolSturmIsolateZeros{sturmname} and then in addition it does the extra work to determine all multiplicities (of the real roots): after executing this macro, \PolSturmIsolatedZeroMultiplicity{sturmname}{index} will expand to the multiplicity of the root located in the index-th interval (intervals are enumerated from left to right, with index starting at 1).

Also, the \PolSturmNbWithMultOfRootsOf{sturmname}\LessThanOrEqualTo{value} will be operant.

Note

It is not necessary to have executed the PolToSturm* starred variant, as the non-starred variant keeps internally the memory of the original GCD (and even of the full non-normalized original Sturm chain), even though it does not make the declarations as user-level genuine polynomials.

See The degree nine polynomial with 0.99, 0.999, 0.9999 as triple roots for an example of use.

\PolRefineInterval*{sturmname}{index}

The index-th interval (starting indexing at one) is further subdivided as many times as is necessary in order for the newer interval to have both its end-points distinct from the end-points of the original interval. This means that the kth root is then strictly separated from the other roots.

\PolRefineInterval[N]{sturmname}{index}

The index-th interval (starting count at one) is further subdivided once, reducing its length by a factor of 10. This is done N times if the optional argument [N] is present.

\PolEnsureIntervalLength{sturmname}{index}{E}

The index-th interval is subdivided until its length becomes at most 10^E. This means (for E<0) that the first -E digits after decimal mark of the kth root will then be known exactly.

\PolEnsureIntervalLengths{sturmname}{E}

The intervals as obtained from \PolSturmIsolateZeros are (if necessary) subdivided further by (base 10) dichotomy in order for each of them to have length at most 10^E (length will be shorter than 10^E in output only if it did not change or became zero.)

This means that decimal expansions of all roots will be known with -E digits (for E<0) after decimal mark.

\PolPrintIntervals[varname]{sturmname}

This is a convenience macro which prints the bounds for the roots Z_1, Z_2, ... (the optional argument varname allows to specify a replacement for the default Z). This will be done in a math mode array, one interval per row, and pattern rcccl, where the second and fourth column hold the < sign, except when the interval reduces to a singleton, which means the root is known exactly. The user is invited to renewcommand the macro if some other type of tabular environment for example is wanted.

In each array cell the corresponding interval end-point (which may be an exactly known root) is available as macro \PolPrintIntervalsTheEndPoint (in decimal notation). And the corresponding interval index is available as \PolPrintIntervalsTheIndex.

These values may be tested to decide some on-the-fly customization (color for example), via the following auxiliaries which can be modified by user. Furthermore these auxiliaries can also use the following conditionals: \PolIfEndPointIsPositive{A}{B}, \PolIfEndPointIsNegative{A}{B}, \PolIfEndPointIsZero{A}{B}.

\PolPrintIntervalsPrintExactZero

This is provided to help customize how an exactly known root is printed in the right most column of the array. The package definition is:

\newcommand\PolPrintIntervalsPrintExactZero{\PolPrintIntervalsTheEndPoint}%

Recall that this is expanded in an array cell.

If for example you want to print in red the third root, known exactly, the macro could make a test for the value of \PolPrintIntervalsTheIndex and act accordingly.

\PolPrintIntervalsPrintLeftEndPoint

Package definition is:

\newcommand\PolPrintIntervalsPrintLeftEndPoint{\PolPrintIntervalsTheEndPoint}%

\PolPrintIntervalsPrintRightEndPoint

Package definition is:

\newcommand\PolPrintIntervalsPrintRightEndPoint{\PolPrintIntervalsTheEndPoint}%

\PolMapCoeffs{\macro}{polname}

It modifies ('in-place': original coefficients get lost) each coefficient of the defined polynomial via the expandable macro \macro. The degree is adjusted as necessary if some leading coefficients vanish after the operation. In replacement text of \macro, \index expands to the coefficient index (which is defined to be zero for the constant term).

Notice that \macro will have to handle inputs of the shape A/B[N] (xintfrac internal notation). This means that it probably will have to be expressed in terms of macros from xintfrac package.

Example:

\def\foo#1{\xintMul{#1}{\the\numexpr\index*\index\relax}}

(or with \xintSqr{\index}) to replace n-th coefficient f_n by f_n*n^2.

\PolReduceCoeffs{polname}

About the same as \PolMapCoeffs{\xintIrr}{polname} (but maintaining a [0] postfix for speedier xintfrac parsing when polynomial function is used for computations.) This is a one-argument macro, working 'in-place'.

\PolReduceCoeffs*{polname}

This starred variant leaves un-touched the decimal exponent in the internal representation of the fractional coefficients, i.e. if a coefficient is internally A/B[N], then A/B is reduced to smallest terms, but the 10^N part is kept as is. Note: if the polynomial is freshly defined directly via \PolFromCSV its coefficients might still be internally in some format like 1.5e7; the macro will anyhow always first do the needed conversion to strict format A/B[N].

Evaluations with polynomials treated by this can be much faster than with those handled by the non-starred variant \PolReduceCoeffs{polname}: as the numerators and denominators remain smaller, this proves very beneficial in favorable cases (especially when the coefficients are decimal numbers) to the expansion speed of the xintfrac macros used internally by \PolEval.

\PolMakeMonic{polname}

Divides by the leading coefficient. It is recommended to execute \PolReduceCoeffs*{polname} immediately afterwards. This is not done automatically, due to the case the original polynomial had integer coefficients and we want to keep the leading one as common denominator.

\PolMakePrimitive{polname}

Divides by the integer content see (\PolIContent). This thus produces a polynomial with integer coefficients having no common factor. The sign of the leading coefficient is not modified.

Expandable macros

All these macros expand completely in two steps except \PolToExpr and \PolToFloatExpr (and their auxiliaries) which need a \write, \edef or a \csname...\endcsname context.

\PolEval{polname}\AtExpr{numerical expression}

It boils down to \xinttheexpr polname(numerical expression)\relax.

\PolEval{polname}\At{fraction}

Evaluates the polynomial at value fraction which must be in (or expand to) a format acceptable to the xintfrac macros.

\PolEvalReduced{polname}\AtExpr{numerical expression}

Boils down to \xinttheexpr reduce(polname(numerical expression))\relax.

\PolEvalReduced{polname}\At{fraction}

Evaluates the polynomial at value fraction which must be in (or expand to) a format acceptable to the xintfrac macros, and produce an irreducible fraction.

\PolFloatEval{polname}\AtExpr{numerical expression}

Boils down to \xintthefloatexpr polname(numerical expression)\relax.

This is done via a Horner Scheme (see \poldef and \PolGenFloatVariant{polname}), with already rounded coefficients. [2] To use the exact coefficients with exactly executed additions and multiplications, just insert it in the float expression as in this example: [3]

\xintthefloatexpr 3.27*\xintexpr f(2.53)\relax^2\relax

The f(2.53) is exactly computed then rounded at the time of getting raised to the power 2. Moving the ^2 inside, that operation would also be treated exactly.

[2]Anyway each floating point operation starts by rounding its operands to the floating point precision.
[3]The \xintexpr here could be \xinttheexpr but that would be less efficient. Cf. xintexpr documentation about nested expressions.

\PolFloatEval{polname}\At{fraction}

Evaluates the polynomial at value fraction which must be in (or expand to) a format acceptable to the xintfrac macros, and produces a floating point number.

\PolIfCoeffIsPlusOrMinusOne{A}{B}

This macro is a priori undefined.

It is defined via the default \PolTypesetCmd{raw_coeff} to be used if needed in the execution of \PolTypesetMonomialCmd, e.g. to insert a \cdot in front of \PolVar^{\PolIndex} if the coefficient is not plus or minus one.

The macro will execute A if the coefficient has been found to be plus or minus one, and B if not.

\PolLeadingCoeff{polname}

Expands to the leading coefficient.

\PolNthCoeff{polname}{number}

It expands to the raw N-th coefficient (0/1[0] if the index number is out of range). With N=-1, -2, ... expands to the leading coefficients.

\PolDegree{polname}

It expands to the degree. This is -1 if zero polynomial but this may change in future. Should it then expand to -\infty ?

\PolIContent{polname}

It expands to the contents of the polynomial, i.e. to the positive fraction such that dividing by this fraction produces a polynomial with integer coefficients having no common prime divisor.

See \PolMakePrimitive.

\PolToExpr{polname}

Expands [4] to coeff_N*x^N+... (descending powers.)

[4]in a \write, \edef, or \csname...\endcsname, but not under \romannumeral-`0.

By default zero coefficients are skipped (issue \poltoexpralltrue to get all of them in output).

By default, no + sign before negative coefficients, for compliance with Maple input format (but see \PolToExprTermPrefix{raw_coeff}.) Also, like the default behaviour of \PolTypeset{polname}, does not print (for the non constant terms) coefficients equal to plus or minus one. The degree one monomial is output as x, not x^1. Complete customization is possible, see next macros.

Of course \PolToExpr{f} can be inserted in a \poldef, as the latter expands token by token, hence will force complete expansion of \PolToExpr{f}, but a simple f(x) is more efficient for the identical result.

\PolToExprOneTerm{raw_coeff}{number}

This two argument expandable command takes care of the monomial and its coefficient. The default definition is done in order for coefficients of absolute value 1 not be printed explicitely (except of course for the constant term). Also by default, the monomial of degree one is x not x^1, and x^0 is skipped.

For compatibility with Maple input requirements, by default a * always precedes the x^number, except if the coefficient is a one or a minus one. See \PolToExprTimes.

\PolToExprOneTermStyleB{raw_coeff}{number}

For output in this style:

2*x^11/3+3*x^8/7-x^5−x^4/4−x^3−x^2/2−2*x+1

issue \let\PolToExprOneTerm\PolToExprOneTermStyleB before usage of \PolToExpr. Note that then \PolToExprCmd isn't used at all. To revert to package default, issue \let\PolToExprOneTerm\PolToExprOneTermStyleA.

To suppress the *'s, cf. \PolToExprTimes.

\PolToExprCmd{raw_coeff}

It is the one-argument macro used by the package definition of \PolToExprOneTerm for the coefficients themselves (when not equal to plus or minus one), and it defaults to \xintPRaw{\xintRawWithZeros{#1}}. One will have to redefine it to \xintIrr{#1} or to \xintPRaw{\xintIrr{#1}} to obtain in the output forcefully reduced coefficients.

\PolToExprTermPrefix{raw_coeff}

Defined identically as \PolTypesetCmdPrefix{raw_coeff}. It prefixes with a plus sign for non-negative coefficients, because they don't carry one by themselves.

\PolToExprVar

This expands to the variable to use in output (it does not have to be a single letter, may be an expandable macro.) Initial definition is x.

\PolToExprTimes

This expands to the symbol used for multiplication of an x^{number} by the corresponding coefficient. The default is *. Redefine the macro to expand to nothing to get rid of it (but this will give output incompatible with some professional computer algebra software).

\PolToExpr*{polname}

Expands to coeff_0+coeff_1*x+coeff_2*x^2+... (ascending powers). Customizable like \PolToExpr{polname} via the same macros.

\PolToFloatExpr{polname}

Similar to \PolToExpr{polname} but uses \PolToFloatExprCmd which by default rounds and converts the coefficients to floating point format.

Note

It is not necessary to have issued \PolGenFloatVariant{polname}. The rounded coefficients are not easily recoverable from the \xintfloatexpr polynomial function hence \PolToFloatExprCmd operates from the exact coefficients anew.

Attention that both macros obey the prevailing float precision. If it is changed between those macro calls, then a mismatch exists between the coefficients as used in \xintfloatexpr and those output by \PolToFloatExpr{polname}.

\PolToFloatExprOneTerm{raw_coeff}{number}

Similar to \PolToExprOneTerm. But does not treat especially coefficients equal to plus or minus one.

\PolToFloatExprCmd{raw_coeff}

It is the one-argument macro used by \PolToFloatExprOneTerm. Its package definition is \xintFloat{#1}.

Caution!

Currently (xint 1.3c) \xintFloat{0} outputs 0.e0 which is perfectly acceptable input for Python, but not for Maple. Thus, one should better leave the \poltoexprallfalse toggle to its default \iffalse state, if one intends to use the output in a Maple worksheet.

But even then the zero polynomial will cause a problem. Workaround:

\renewcommand\PolToFloatExprCmd[1]{\xintiiifZero{#1}{0.0}{\xintFloat{#1}}}

Usage of \xintiiifZero and not \xintifZero is only for optimization (I can't help it) because #1 is known to be in xintfrac raw format.

\PolToFloatExpr*{polname}

Typesets in ascending powers.

\PolToList{polname}

Expands to {coeff_0}{coeff_1}...{coeff_N} with N = degree (except zero polynomial which does give {0/1[0]} and not an empty output.)

\PolToCSV{polname}

Expands to coeff_0, coeff_1, coeff_2, ....., coeff_N. Converse to \PolFromCSV.

\PolSturmChainLength{sturmname}

Returns the integer N such that sturmname_N is the last one in the Sturm chain sturmname_0, sturmname_1, ...

See \PolToSturm{polname}{sturmname}.

\PolSturmIfZeroExactlyKnown{sturmname}{index}{A}{B}

Executes A if the index-th interval reduces to a singleton, i.e. the root is known exactly, else B.

Note

index may be a TeX count, or a \value{latexcounter}, or a numerical expression as parsable by \numexpr: it does not have to be given via explicit digits.

This remark applies also to the other package macros with index being the name of the argument in this documentation. There is also an out-of-range check done for some reasonable error message (right before everything goes haywire).

\PolSturmIsolatedZeroLeft{sturmname}{index}

Expands to the left end-point for the index-th interval obtained via \PolSturmIsolateZeros{sturmname} and possibly refined afterwards.

\PolSturmIsolatedZeroRight{sturmname}{index}

Expands to the right end-point for the index-th interval obtained via \PolSturmIsolateZeros{sturmname} and possibly refined afterwards.

\PolSturmIsolatedZeroMultiplicity{sturmname}{index}

Expands to the multiplicity of the unique root contained in the index-th interval as determined by \PolSturmIsolateZeros*{sturmname} and possibly refined afterwards.

Attention!

A prior execution of \PolSturmIsolateZeros*{sturmname} is mandatory.

See The degree nine polynomial with 0.99, 0.999, 0.9999 as triple roots for an example of use.

\PolSturmNbOfIsolatedZeros{sturmname}

Expands to the number of real roots of the polynomial <sturmname>_0 (which is the number of distinct real roots of the polynomial used to create the Sturm chain via \PolToSturm{polname}{sturmname}.

\PolSturmNbOfRootsOf{sturmname}\LessThanOrEqualTo{value}

Expands to the number of distinct roots (of the polynomial used to create the Sturm chain) less than or equal to the value (i.e. a number of fraction recognizable by the xintfrac macros).

Attention!

\PolSturmIsolateZeros{sturmname} must have been executed beforehand.

And the argument is a sturmname, not a polname (this is why the macro contains Sturm in its name), simply to be reminded of the above constraint.

\PolSturmNbOfRootsOf{sturmname}\LessThanOrEqualToExpr{expression}

Expands to the number of distinct roots (of the polynomial used to create the Sturm chain) which are less than or equal to the given expression.

Attention!

\PolSturmIsolateZeros{sturmname} must have been executed beforehand.

\PolSturmNbWithMultOfRootsOf{sturmname}\LessThanOrEqualTo{value}

Expands to the number counted with multiplicities of the roots (of the polynomial used to create the Sturm chain) which are less than or equal to the given value.

Attention!

\PolSturmIsolateZeros*{sturmname} or its alias \PolSturmIsolateZerosAndGetMultiplicities{sturmname} must have been executed beforehand.

\PolSturmNbWithMultOfRootsOf{sturmname}\LessThanOrEqualToExpr{expression}

Expands to the total number of roots (counted with multiplicities) which are less than or equal to the given expression.

Attention!

\PolSturmIsolateZeros*{sturmname} or its alias \PolSturmIsolateZerosAndGetMultiplicities{sturmname} must have been executed beforehand.

\PolIntervalWidth{sturmname}{index}

The 10^E width of the current index-th root localization interval. Output is in xintfrac raw 1/1[E] format (if not zero).

Macros for use within execution of \PolPrintIntervals

More precisely, they can be used within the replacement texts of the \PolPrintIntervalsPrintLeftEndPoint, etc, macros.

\PolPrintIntervalsTheEndPoint

Within a custom \PolPrintIntervalsPrintLeftEndPoint, custom \PolPrintIntervalsPrintRightEndPoint, or custom \PolPrintIntervalsPrintExactZero this macro expands to the left or right end point of the considered interval. Serves as default replacement for \PolPrintIntervalsPrintLeftEndPoint , etc...

\PolPrintIntervalsTheIndex

Within a custom \PolPrintIntervalsPrintLeftEndPoint, custom \PolPrintIntervalsPrintRightEndPoint, or custom \PolPrintIntervalsPrintExactZero this macro expands to the index of the considered interval. For example if user wants to print the corresponding end points in red, the index value can thus be tested in the replacement text of \PolPrintIntervalsPrintLeftEndPoint and the other two similar macros.

\PolIfEndPointIsPositive{A}{B}

Within a custom \PolPrintIntervalsPrintLeftEndPoint, custom \PolPrintIntervalsPrintRightEndPoint, or custom \PolPrintIntervalsPrintExactZero this macro executes A if the considered interval end-point is positive, else B.

\PolIfEndPointIsNegative{A}{B}

Within a custom \PolPrintIntervalsPrintLeftEndPoint, custom \PolPrintIntervalsPrintRightEndPoint, or custom \PolPrintIntervalsPrintExactZero this macro executes A if the considered interval end-point is negative, else B.

\PolIfEndPointIsZero{A}{B}

Within a custom \PolPrintIntervalsPrintLeftEndPoint, custom \PolPrintIntervalsPrintRightEndPoint, or custom \PolPrintIntervalsPrintExactZero this macro executes A if the considered interval end-point is zero, else B.

\PolDecToString{decimal number}

This is a utility macro to print decimal numbers. It has been backported to xintfrac (release 1.3 of 2018/03/01) under the name \xintDecToString, and the polexpr macro is simply now an alias to it.

For example \PolDecToString{123.456e-8} will expand to 0.00000123456 and \PolDecToString{123.450e-8} to 0.00000123450 which illustrates that trailing zeros are not trimmed. To trim trailing zeroes, one can use \PolDecToString{\xintREZ{#1}}.

The exact behaviour of this macro may evolve in future releases of xint.

Booleans (with default setting as indicated)

\xintverbosefalse

This is actually an xintexpr configuration. Setting it to true triggers the writing of information to the log when new polynomials are defined.

Caution!

The macro meanings as written to the log are to be considered unstable and undocumented internal structures.

\poltypesetallfalse

If true, \PolTypeset{polname} will also typeset the vanishing coefficients.

\poltoexprallfalse

If true, \PolToExpr{polname} and \PolToFloatExpr{polname} will also include the vanishing coefficients in their outputs.

Technicalities

CHANGE LOG

Acknowledgments

Thanks to Jürgen Gilg whose question about xint usage for differentiating polynomials was the initial trigger leading to this package, and to Jürgen Gilg and Thomas Söll for testing it on some concrete problems.

Renewed thanks to them on occasion of the 0.6 release for their continued interest.

See README.md for the License.