The following algorithm creates a nested polynomial to minimize the
number of multiplications and additions in evaluating a polynomial at
some given point, x. I use it to reduce my Finite Element Method basis
functions.<br>
<br>//Converts an <i>expanded</i> polynomial into a nested polynomial for efficient evaluation at points<br>ex nested_polynomial( ex polynomial, realsymbol x ){<br> //result is returned to the user, highest order is the peeled off highest order term of the polynomial<br>
ex result=0, highest_order;<br> //While the polynomial is not empty continue peeling off the highest degree<br>
while( polynomial.degree( x ) != 0 ){<br> //get the highest order term<br>
highest_order = polynomial.lcoeff(x) * pow( x, polynomial.degree(x) );<br> //add to nest<br>
result+= highest_order.lcoeff( x );<br> //remove the highest order term from the expanded polynomial<br>
polynomial-=highest_order;<br> //multiply the result by the proper amount of x's<br>
result*=pow(x, highest_order.degree(x)-<div id=":6o">polynomial.degree(x) );<br> }<br> //correct for forgotten 0th order terms<br> result+=polynomial.lcoeff(x);<br> return result;<br>}<br><br><br>Example:<br> polynomial = 10*x^5 + 3*x^3 + 1;<br>
the function would return (with polynomial and x as inputs):<br> 1+x^3*(3+x^2*10)<br> which minimizes the number of multiplications and additions we need to do in evaluation.<br>The
first polynomial form needs 5+3=8 multiplications and 2 additions,
while the second form needs 2+3=5 multiplications and 2 additions.</div>