/************************************************************************ * EVALUATE A COMPLEX POLYNOMIAL BY HORNER'S METHOD * * --------------------------------------------------------------------- * * SAMPLE RUN: * * * * EVALUATE A CPMPLEX POLYNOMIAL BY HORNER'S METHOD * * * * Example: P(Z) = (1+i) Z^4 + (2-i) Z^3 +(3+0.5i) Z^2 + (4-2i) Z * * + (5-0.75i) * * * * For Z = (1.500000, 2.000000) * * * * P(Z) = (-28.937500, -6.937500) * * * * * * C++ Release 1.0 By J-P Moreau, Paris * * (www.jpmoreau.fr) * ************************************************************************/ #include #include #define NMAX 25 //complex number typedef struct { double x,y; //algebraic form } COMPLEX; double AR[NMAX], AI[NMAX], RR[NMAX], RI[NMAX]; COMPLEX Z; double Y0R, Y0I; int N; // multiply two complex numbers} void ZMult(COMPLEX z1,COMPLEX z2, COMPLEX *z) { z->x=(z1.x*z2.x)-(z1.y*z2.y); z->y=(z1.x*z2.y)+(z1.y*z2.x); } void CHorner(double *AR, double *AI, int N, COMPLEX X0, double *YR, double *YI) { int i; COMPLEX tmp,tmp0; *YR = AR[N]; *YI = AI[N]; for (i=N-1; i>=0; i--) { tmp0.x=*YR; tmp0.y=*YI; ZMult(tmp0,X0,&tmp); *YR = tmp.x + AR[i]; *YI = tmp.y + AI[i]; } } void main() { N = 4; // ORDER OF POLYNOMIAL // define complex coefficients AR[4] = 1.0; AI[4] = 1.0; // power 4 AR[3] = 2.0; AI[3] = -1.0; // power 3 AR[2] = 3.0; AI[2] = 0.5; // power 2 AR[1] = 4.0; AI[1] = -2.0; // power 1 AR[0] = 5.0; AI[0] = -0.75; // power 0 Z.x = 1.5; // complex argument Z.y = 2.0; printf("\n EVALUATE A COMPLEX POLYNOMIAL BY HORNER'S METHOD\n\n"); CHorner(AR, AI, N, Z, &Y0R, &Y0I); printf(" For Z = (%f, %f)\n\n", Z.x, Z.y); printf(" P(Z) = (%f, %f)\n\n", Y0R, Y0I); } // end of file chorner.cpp