/**************************************************** * Nth Derivative of a polynomial P(x) * * ------------------------------------------------- * * Ref.: "Mathématiques en Turbo-Pascal By M. Ducamp * * and A. Reverchon (vol 2), Eyrolles, Paris, 1988". * * [BIBLI 05]. * * ------------------------------------------------- * * SAMPLE RUN: * * * * Nth DERIVATIVE OF A POLYNOMIAL: * * * * P(X) = 4x5 -3/5x3 + x2 -7/2x +11 * * Order of derivative: 1 * * * * +20 X4 -9/5 X2 +2 X -7/2 * * * * * * Nth DERIVATIVE OF A POLYNOMIAL: * * * * P(X) = 4x5 -3/5x3 + x2 -7/2x +11 * * Order of derivative: 3 * * * * +240 X2 -18/5 * * * * ------------------------------------------------- * * Functions used (of module Polynoms): * * * * EnterPolynom(), DisplayPolynom(), MultNumber(), * * SetNumber(). * * * * C++ version by J-P Moreau. * * (To be linked with polynoms.cpp).* * (www.jpmoreau.fr) * ****************************************************/ #include #include #include "polynoms.h" ar_polynom *P, *R; // polynomials int n; // order of derivative bool DerivPolynom(ar_polynom *P,int n,ar_polynom *R) { int i,j; ar_number u; R->degree=P->degree-n; if (R->degree<0) { R->degree=0; SetNumber(&R->coeff[0], "0"); } else for (i=0; i<=R->degree; i++) { R->coeff[i]=P->coeff[i+n]; for (j=1; j<=n; j++) { //put i+j in u u.is_real=FALSE; u.p=(long) i+j; u.q=1; u.value=(double) i+j; if (!MultNumber(R->coeff[i], u, &R->coeff[i])) return FALSE; } } return TRUE; } void main() { //dynamic memory allocations for polynomials P = (ar_polynom *) calloc(1,sizeof(ar_polynom)); R = (ar_polynom *) calloc(1,sizeof(ar_polynom)); printf("\n Nth DERIVATIVE OF A POLYNOMIAL:\n\n"); if (!EnterPolynom(" P(X) = ", P)) return; printf(" Order of derivative: "); scanf("%d",&n); if (DerivPolynom(P,n,R)) DisplayPolynom(R); else printf(" Error in calculating %dth derivative.", n); printf("\n\n"); free(P); free(R); } // end of file derivpol.cpp