/* calculate exponent of polynomial */ Poly polyExp( Poly p, int e ){ if ( 0 == e) return polyOne(); if ( 1 == e ) return p; if ( 0 == e % 2) //todo isEven() { return polyExp( polyMultiply( p, p) , e/2 ); }else { return polyMultiply( polyExp( polyMultiply( p, p) , e/2 ), p ); } }
SNode SNode::multiplyParenthesesInPoly() const { ENTERMETHOD(); CHECKNODETYPE(*this,NT_POLY); const CoefArray &coefArray = getCoefArray(); SNode newArg = getArgument().multiplyParentheses(); CoefArray newCoefArray(coefArray.getTree(),coefArray.size()); for(size_t i = 0; i < coefArray.size(); i++) { newCoefArray.add(coefArray[i].multiplyParentheses()); } SNode result = polyExp(newCoefArray, newArg); RETURNNODE( result ); }
int main(int argc, char const *argv[]){ int i,a,b; int tmp; Poly li, lp, mulp, sump; li = newPoly(); lp = newPoly(); if ( NULL == li || NULL == lp) return 1; li->exp = 0; lp->exp = 0; srand( time(NULL) ); for(i = 0 ; i< LSIZE; i++){ a = rand()/(RAND_MAX/LSIZE); b = rand()/(RAND_MAX/LSIZE); addItem( a, b, li); addItem( b, a, lp); } //removeDup( li); //removeDup( lp); printPoly( li ); printPoly( lp ); mulp = polyMultiply2( li, lp); printf("\nmultiply : " ); printPoly( mulp ); deletePoly( mulp); sump = polyAdd( li, lp); printf("\nadd : " ); printPoly( sump ); deletePoly( sump ); printf("\n li ^2: "); printPoly( polyExp(li,2) ); //swapNext( first(li), li ); deletePoly( li ); deletePoly( lp ); return 0; }