Polynomial evaluation

From HandWiki
Revision as of 20:12, 6 February 2024 by Wincert (talk | contribs) (over-write)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Short description: Algorithms for polynomial evaluation

In mathematics and computer science, polynomial evaluation refers to computation of the value of a polynomial when its indeterminates are substituted for some values. In other words, evaluating the polynomial [math]\displaystyle{ P(x_1, x_2) = 2x_1x_2 + x_1^3 + 4 }[/math] at [math]\displaystyle{ x_1=2, x_2=3 }[/math] consists of computing [math]\displaystyle{ P(2,3)= 2\cdot 2\cdot 3 + 2^3+4=24. }[/math] See also Polynomial ring § Polynomial evaluation

For evaluating the univariate polynomial [math]\displaystyle{ a_nx^n+a_{n-1}x^{n-1}+\cdots +a_0, }[/math] the most naive method would use [math]\displaystyle{ n }[/math] multiplications to compute [math]\displaystyle{ a_n x^n }[/math], use [math]\displaystyle{ n-1 }[/math] multiplications to compute [math]\displaystyle{ a_{n-1} x^{n-1} }[/math] and so on for a total of [math]\displaystyle{ \tfrac{n(n+1)}{2} }[/math] multiplications and [math]\displaystyle{ n }[/math] additions. Using better methods, such as Horner's rule, this can be reduced to [math]\displaystyle{ n }[/math] multiplications and [math]\displaystyle{ n }[/math] additions. If some preprocessing is allowed, even more savings are possible.

Background

This problem arises frequently in practice. In computational geometry, polynomials are used to compute function approximations using Taylor polynomials. In cryptography and hash tables, polynomials are used to compute k-independent hashing.

In the former case, polynomials are evaluated using floating-point arithmetic, which is not exact. Thus different schemes for the evaluation will, in general, give slightly different answers. In the latter case, the polynomials are usually evaluated in a finite field, in which case the answers are always exact.

General methods

Horner's rule

Horner's method evaluates a polynomial using repeated bracketing: [math]\displaystyle{ \begin{align} a_0 + &a_1x + a_2x^2 + a_3x^3 + \cdots + a_nx^n \\ &= a_0 + x \bigg(a_1 + x \Big(a_2 + x \big(a_3 + \cdots + x(a_{n-1} + x\,a_n) \cdots \big) \Big) \bigg). \end{align} }[/math] This method reduces the number of multiplications and additions to just [math]\displaystyle{ n }[/math]

Horner's method is so common that a computer instruction "multiply–accumulate operation" has been added to many computer processors, which allow doing the addition and multiplication operations in one combined step.

Multivariate

If the polynomial is multivariate, Horner's rule can be applied recursively over some ordering of the variables. E.g.

[math]\displaystyle{ P(x, y) = 4 + x + 2 x y + 2 x^2 y + x^2 y^2 }[/math]

can be written as

[math]\displaystyle{ \begin{align} P(x, y) &= 4 + x (1 + y(2) + x (y(2 + y))) \quad\text{or}\\ P(x, y) &= 4 + x + y(x(2 + x(2)) + y(x^2)). \end{align} }[/math]

An efficient version of this approach was described by Carnicer and Gasca.[1]

Estrin's scheme

While it's not possible to do less computation than Horner's rule (without preprocessing), on modern computers the order of evaluation can matter a lot for the computational efficiency. A method known as Estrin's scheme computes a (single variate) polynomial in a tree like pattern:

[math]\displaystyle{ \begin{align} P(x) = (a_0 + a_1 x) + (a_2 + a_3 x) x^2 + ((a_4 + a_5 x) + (a_6 + a_7 x) x^2)x^4. \end{align} }[/math]

Combined by Exponentiation by squaring, this allows parallelizing the computation.

Evaluation with preprocessing

Arbitrary polynomials can be evaluated with fewer operations than Horner's rule requires if we first "preprocess" the coefficients [math]\displaystyle{ a_n, \dots, a_0 }[/math].

An example was first given by Motzkin[2] who noted that

[math]\displaystyle{ P(x)=x^4 + a_3 x^3 + a_2 x^2 + a_1 x + a_0 }[/math]

can be written as

[math]\displaystyle{ y = (x+\beta_0)x+\beta_1,\quad P(x)=(y+x+\beta_2)y+\beta_3, }[/math]

where the values [math]\displaystyle{ \beta_0, \dots, \beta_3 }[/math] are computed in advanced, based on [math]\displaystyle{ a_0, \dots, a_3 }[/math]. Motzkin's method uses just 3 multiplications compared to Horner's 4.

The values for each [math]\displaystyle{ \beta_i }[/math] can be easily computed by expanding [math]\displaystyle{ P(x) }[/math] and equating the coefficients:

[math]\displaystyle{ \begin{align} \beta_0&=\tfrac12(a_3-1),\quad &z&=a_2-\beta_0(\beta_0+1),\quad &\beta_1&=a_1-\beta_0 z,\\ \beta_2&=z-2\beta_1, \quad &\beta_3&=a_0-\beta_1(\beta_1+\beta_2).\end{align} }[/math]

Example

To compute the Taylor expansion [math]\displaystyle{ \exp(x) \approx 1+x+x^2/2+x^3/6+x^4/24 }[/math], we can upscale by a factor 24, apply the above steps, and scale back down. That gives us the three multiplication computation

[math]\displaystyle{ y = (x+1.5)x+11.625,\quad P(x)=(y+x-15)y/24+2.63477. }[/math]

Improving over the equivalent Horner form (that is [math]\displaystyle{ P(x) = 1 + x (1 + x (1/2 + x(1/6 + x/24))) }[/math]) by 1 multiplication.

Some general methods include the Knuth–Eve algorithm and the Rabin–Winograd algorithm.

Multipoint evaluation

Evaluate of a [math]\displaystyle{ n }[/math]-degree polynomial [math]\displaystyle{ P(x) }[/math] in multiple points [math]\displaystyle{ x_1, \dots, x_m }[/math] can be done with [math]\displaystyle{ mn }[/math] multiplications by using Horner's method [math]\displaystyle{ m }[/math] times. Using above preprocessing approach, this can be reduced that by a factor of two, that is, to [math]\displaystyle{ mn/2 }[/math] multiplications. However, it is possible to do better.

It is possible to reduce the time requirement to just [math]\displaystyle{ O\big((n + m) \log^2(n + m)\big) }[/math].[3] The idea is to define two polynomials that are zero in respectively the first and second half of the points: [math]\displaystyle{ m_0(x)=(x-x_1)\cdots(x-x_{n/2}) }[/math] and [math]\displaystyle{ m_1(x)=(x-x_{n/2+1})\cdots(x-x_{n}) }[/math]. We then compute [math]\displaystyle{ R_0 = P \bmod m_0 }[/math] and [math]\displaystyle{ R_1 = P \bmod m_1 }[/math] using the Polynomial remainder theorem, which can be done in [math]\displaystyle{ O(n\log n) }[/math] time using a fast Fourier transform. This means [math]\displaystyle{ P(x) = Q(x)m_0(x) + R_0(x) }[/math] and [math]\displaystyle{ P(x) = Q(x)m_1(x) + R_1(x) }[/math] by construction, where [math]\displaystyle{ R_0 }[/math] and [math]\displaystyle{ R_1 }[/math] are polynomials of degree at most [math]\displaystyle{ n/2 }[/math]. Because of how [math]\displaystyle{ m_0 }[/math] and [math]\displaystyle{ m_1 }[/math] were defined, we have

[math]\displaystyle{ \begin{align} R_0(x_i) &= P(x_i) \quad\text{for } i \le n/2 \quad\text{and}\\ R_1(x_i) &= P(x_i) \quad\text{for } i \gt n/2. \end{align} }[/math]

Thus to compute [math]\displaystyle{ P }[/math] on all [math]\displaystyle{ n }[/math] of the [math]\displaystyle{ x_i }[/math], it suffices to compute the smaller polynomials [math]\displaystyle{ R_0 }[/math] and [math]\displaystyle{ R_1 }[/math] on each half of the points. This gives us a divide-and-conquer algorithm with [math]\displaystyle{ T(n) = 2T(n/2) + n\log n }[/math], which implies [math]\displaystyle{ T(n)=O(n(\log n)^2) }[/math] by the master theorem.


In the case where the points in which we wish to evaluate the polynomials have some structure, simpler methods exist. For example, Knuth[4] section 4.6.4 gives a method for tabulating polynomial values of the type

[math]\displaystyle{ P(x_0 + h), P(x_0 + 2h), \dots. }[/math]

Dynamic evaluation

In the case where [math]\displaystyle{ x_1, \dots, x_m }[/math] are not known in advance, Kedlaya and Umans[5] gave a data structure for evaluating polynomials over a finite field of size [math]\displaystyle{ F_q }[/math] in time [math]\displaystyle{ (\log n)^{O(1)}(\log_2 q)^{1+o(1)} }[/math] per evaluation after some initial preprocessing. This was shown by Larsen[6] to be essentially optimal.

The idea is to transform [math]\displaystyle{ P(x) }[/math] of degree [math]\displaystyle{ n }[/math] into a multivariate polynomial [math]\displaystyle{ f(x_1, x_2, \dots, x_m) }[/math], such that [math]\displaystyle{ P(x) = f(x, x^d, x^{d^2}, \dots, x^{d^m}) }[/math] and the individual degrees of [math]\displaystyle{ f }[/math] is at most [math]\displaystyle{ d }[/math]. Since this is over [math]\displaystyle{ \bmod q }[/math], the largest value [math]\displaystyle{ f }[/math] can take (over [math]\displaystyle{ \mathbb Z }[/math]) is [math]\displaystyle{ M = d^m (q-1)^{dm} }[/math]. Using the Chinese remainder theorem, it suffices to evaluate [math]\displaystyle{ f }[/math] modulo different primes [math]\displaystyle{ p_1, \dots, p_\ell }[/math] with a product at least [math]\displaystyle{ M }[/math]. Each prime can be taken to be roughly [math]\displaystyle{ \log M = O(dm\log q) }[/math], and the number of primes needed, [math]\displaystyle{ \ell }[/math], is roughly the same. Doing this process recursively, we can get the primes as small as [math]\displaystyle{ \log\log q }[/math]. That means we can compute and store [math]\displaystyle{ f }[/math] on all the possible values in [math]\displaystyle{ T = (\log\log q)^m }[/math] time and space. If we take [math]\displaystyle{ d = \log q }[/math], we get [math]\displaystyle{ m = \tfrac{\log n}{\log\log q} }[/math], so the time/space requirement is just [math]\displaystyle{ n^\frac{\log\log q}{\log\log\log q}. }[/math]

Kedlaya and Umans further show how to combine this preprocessing with fast (FFT) multipoint evaluation. This allows optimal algorithms for many important algebraic problems, such as polynomial modular composition.

Specific polynomials

While general polynomials require [math]\displaystyle{ \Omega(n) }[/math] operations to evaluate, some polynomials can be computed much faster. For example, the polynomial [math]\displaystyle{ P(x)=x^2+2x+1 }[/math] can be computed using just one multiplication and one addition since [math]\displaystyle{ P(x)=(x+1)^2 }[/math]

Evaluation of powers

Main pages: Exponentiation by squaring and Addition-chain exponentiation

A particularly interesting type of polynomial is powers like [math]\displaystyle{ x^n }[/math]. Such polynomials can always be computed in [math]\displaystyle{ O(\log n) }[/math] operations. Suppose, for example, that we need to compute [math]\displaystyle{ x^{16} }[/math]; we could simply start with [math]\displaystyle{ x }[/math] and multiply by [math]\displaystyle{ x }[/math] to get [math]\displaystyle{ x^2 }[/math]. We can then multiply that by itself to get [math]\displaystyle{ x^4 }[/math] and so on to get [math]\displaystyle{ x^8 }[/math] and [math]\displaystyle{ x^{16} }[/math] in just four multiplications. Other powers like [math]\displaystyle{ x^5 }[/math] can similarly be computed efficiently by first computing [math]\displaystyle{ x^4 }[/math] by 2 multiplications and then multiplying by [math]\displaystyle{ x }[/math].

The most efficient way to compute a given power [math]\displaystyle{ x^n }[/math] is provided by addition-chain exponentiation. However, this requires designing a specific algorithm for each exponent, and the computation needed for designing these algorithms are difficult (NP-complete[7]), so exponentiation by squaring is generally preferred for effective computations.

Polynomial families

Often polynomials show up in a different form than the well known [math]\displaystyle{ a_n x^n + \dots + a_1 x + a_0 }[/math]. For polynomials in Chebyshev form we can use Clenshaw algorithm. For polynomials in Bézier form we can use De Casteljau's algorithm, and for B-splines there is De Boor's algorithm.

Hard polynomials

The fact that some polynomials can be computed significantly faster than "general polynomials" suggests the question: Can we give an example of a simple polynomial that cannot be computed in time much smaller than its degree? Volker Strassen has shown[8] that the polynomial

[math]\displaystyle{ P(x)=\sum_{k=0}^n 2^{2^{kn^3}}x^k }[/math]

cannot be evaluated by with less than [math]\displaystyle{ \tfrac12 n - 2 }[/math] multiplications and [math]\displaystyle{ n - 4 }[/math] additions. At least this bound holds if only operations of those types are allowed, giving rise to a so-called "polynomial chain of length [math]\displaystyle{ \lt n^2/\log n }[/math]".

The polynomial given by Strassen has very large coefficients, but by probabilistic methods, one can show there must exist even polynomials with coefficients just 0's and 1's such that the evaluation requires at least [math]\displaystyle{ \Omega(n/\log n) }[/math] multiplications.[9]

For other simple polynomials, the complexity is unknown. The polynomial [math]\displaystyle{ (x+1)(x+2)\cdots(x+n) }[/math] is conjectured to not be computable in time [math]\displaystyle{ (\log n)^{c} }[/math] for any [math]\displaystyle{ c }[/math]. This is supported by the fact, that if it can be computed fast then integer factorization can be computed in polynomial time, breaking the RSA cryptosystem.[10]

Matrix polynomials

Sometimes the computational cost of scalar multiplications (like [math]\displaystyle{ ax }[/math]) is less than the computational cost of "non scalar" multiplications (like [math]\displaystyle{ x^2 }[/math]). The typical example of this is matrices. If [math]\displaystyle{ M }[/math] is an [math]\displaystyle{ m\times m }[/math] matrix, a scalar multiplication [math]\displaystyle{ aM }[/math] takes about [math]\displaystyle{ m^2 }[/math] arithmetic operations, while computing [math]\displaystyle{ M^2 }[/math] takes about [math]\displaystyle{ m^3 }[/math] (or [math]\displaystyle{ m^{2.3} }[/math] using fast matrix multiplication).

Matrix polynomials are important for example for computing the Matrix Exponential.

Paterson and Stockmeyer [11] showed how to compute a degree [math]\displaystyle{ n }[/math] polynomial using only [math]\displaystyle{ O(\sqrt n) }[/math] non scalar multiplications and [math]\displaystyle{ O(n) }[/math] scalar multiplications. Thus a matrix polynomial of degree n can be evaluated in [math]\displaystyle{ O(m^{2.3}\sqrt{n} + m^2n) }[/math] time. If [math]\displaystyle{ m=n }[/math] this is [math]\displaystyle{ O(m^3) }[/math], as fast as one matrix multiplication with the standard algorithm.

This method works as follows: For a polynomial

[math]\displaystyle{ P(M)=a_{n-1} M^{n-1} + \dots + a_{1}M + a_0 I, }[/math]

let k be the least integer not smaller than [math]\displaystyle{ \sqrt{n}. }[/math] The powers [math]\displaystyle{ M, M^2, \dots, M^k }[/math] are computed with [math]\displaystyle{ k }[/math] matrix multiplications, and [math]\displaystyle{ M^{2k}, M^{3k}, \dots, M^{k^2-k} }[/math] are then computed by repeated multiplication by [math]\displaystyle{ M^k. }[/math] Now,

[math]\displaystyle{ \begin{align}P(M) = &\,(a_0 I + a_1 M + \dots + a_{k-1}M^{k-1}) \\+&\,(a_k I + a_{k+1} M + \dots + a_{2k-1}M^{k-1})M^k \\+&\,\dots \\+&\,(a_{n-k} I + a_{n-k+1} M + \dots + a_{n-1}M^{k-1})M^{k^2-k}, \end{align} }[/math],

where [math]\displaystyle{ a_i=0 }[/math] for in. This requires just [math]\displaystyle{ k }[/math] more non-scalar multiplications.

We can write this succinctly using the Kronecker product:

[math]\displaystyle{ P(M) = \begin{bmatrix}I\\M\\\vdots\\M^{k-1}\end{bmatrix}^T \left(\begin{bmatrix} a_0 & a_1 & a_2 & \dots\\ a_k & a_{k+1} & \ddots \\ a_{2k} & \ddots \\ \vdots\end{bmatrix}\otimes I\right) \begin{bmatrix}I\\M^k\\M^{2k}\\\vdots\end{bmatrix} }[/math].

The direct application of this method uses [math]\displaystyle{ 2\sqrt{n} }[/math] non-scalar multiplications, but combining it with Evaluation with preprocessing, Paterson and Stockmeyer show you can reduce this to [math]\displaystyle{ \sqrt{2n} }[/math].

Methods based on matrix polynomial multiplications and additions have been proposed allowing to save nonscalar matrix multiplications with respect to the Paterson-Stockmeyer method.[12]

See also

References

  1. Carnicer, J.; Gasca, M. (1990). "Evaluation of Multivariate Polynomials and Their Derivatives". Mathematics of Computation 54 (189): 231–243. doi:10.2307/2008692. 
  2. Motzkin, T. S. (1955). "Evaluation of polynomials and evaluation of rational functions". Bulletin of the American Mathematical Society 61 (163): 10. 
  3. Von Zur Gathen, Joachim; Jürgen, Gerhard (2013). Modern computer algebra. Cambridge University Press. Chapter 10. ISBN 9781139856065. 
  4. Knuth, Donald (2005). Art of Computer Programming. 2: Seminumerical Algorithms. Addison-Wesley. ISBN 9780201853926. 
  5. Kedlaya, Kiran S.; Umans, Christopher (2011). "Fast Polynomial Factorization and Modular Composition". SIAM Journal on Computing 40 (6): 1767–1802. doi:10.1137/08073408x. 
  6. Larsen, K. G. (2012). "Higher Cell Probe Lower Bounds for Evaluating Polynomials". 2012 IEEE 53rd Annual Symposium on Foundations of Computer Science. 53. IEEE. pp. 293–301. doi:10.1109/FOCS.2012.21. ISBN 978-0-7695-4874-6. 
  7. Downey, Peter; Leong, Benton; Sethi, Ravi (1981). "Computing Sequences with Addition Chains". SIAM Journal on Computing 10 (3). https://epubs.siam.org/doi/10.1137/0210047. Retrieved 27 January 2024. 
  8. Strassen, Volker (1974). "Polynomials with Rational Coefficients Which are Hard to Compute" (in en). SIAM Journal on Computing 3 (2): 128–149. doi:10.1137/0203010. 
  9. Schnorr, C. P. (1979), "On the additive complexity of polynomials and some new lower bounds", Theoretical Computer Science, Lecture Notes in Computer Science (Springer) 67: pp. 286–297, doi:10.1007/3-540-09118-1_30, ISBN 978-3-540-09118-9 
  10. Chen, Xi, Neeraj Kayal, and Avi Wigderson. Partial derivatives in arithmetic complexity and beyond. Now Publishers Inc, 2011.
  11. Paterson, Michael S.; Stockmeyer, Larry J. (1973). "On the Number of Nonscalar Multiplications Necessary to Evaluate Polynomials" (in en). SIAM Journal on Computing 2 (1): 60–66. doi:10.1137/0202007. 
  12. Fasi, Massimiliano (1 August 2019). "Optimality of the Paterson–Stockmeyer method for evaluating matrix polynomials and rational matrix functions". Linear Algebra and its Applications 574: 185. doi:10.1016/j.laa.2019.04.001. ISSN 0024-3795. http://eprints.maths.manchester.ac.uk/2676/1/fasi18.pdf.