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.
//Converts an expanded polynomial into a nested polynomial for efficient evaluation at points
ex nested_polynomial( ex polynomial, realsymbol x ){
//result is returned to the user, highest order is the peeled off highest order term of the polynomial
ex result=0, highest_order;
//While the polynomial is not empty continue peeling off the highest degree
while( polynomial.degree( x ) != 0 ){
//get the highest order term
highest_order = polynomial.lcoeff(x) * pow( x, polynomial.degree(x) );
//add to nest
result+= highest_order.lcoeff( x );
//remove the highest order term from the expanded polynomial
polynomial-=highest_order;
//multiply the result by the proper amount of x's
result*=pow(x, highest_order.degree(x)-