static void R_cpolyroot(double *opr, double *opi, int *degree, double *zeror, double *zeroi, Rboolean *fail) { static const double smalno = DBL_MIN; static const double base = (double)FLT_RADIX; static int d_n, i, i1, i2; static double zi, zr, xx, yy; static double bnd, xxx; Rboolean conv; int d1; double *tmp; static const double cosr =/* cos 94 */ -0.06975647374412529990; static const double sinr =/* sin 94 */ 0.99756405025982424767; xx = M_SQRT1_2;/* 1/sqrt(2) = 0.707.... */ yy = -xx; *fail = FALSE; nn = *degree; d1 = nn - 1; /* algorithm fails if the leading coefficient is zero. */ if (opr[0] == 0. && opi[0] == 0.) { *fail = TRUE; return; } /* remove the zeros at the origin if any. */ while (opr[nn] == 0. && opi[nn] == 0.) { d_n = d1-nn+1; zeror[d_n] = 0.; zeroi[d_n] = 0.; nn--; } nn++; /*-- Now, global var. nn := #{coefficients} = (relevant degree)+1 */ if (nn == 1) return; /* Use a single allocation as these as small */ const void *vmax = vmaxget(); tmp = (double *) R_alloc((size_t) (10*nn), sizeof(double)); pr = tmp; pi = tmp + nn; hr = tmp + 2*nn; hi = tmp + 3*nn; qpr = tmp + 4*nn; qpi = tmp + 5*nn; qhr = tmp + 6*nn; qhi = tmp + 7*nn; shr = tmp + 8*nn; shi = tmp + 9*nn; /* make a copy of the coefficients and shr[] = | p[] | */ for (i = 0; i < nn; i++) { pr[i] = opr[i]; pi[i] = opi[i]; shr[i] = hypot(pr[i], pi[i]); } /* scale the polynomial with factor 'bnd'. */ bnd = cpoly_scale(nn, shr, eta, infin, smalno, base); if (bnd != 1.) { for (i=0; i < nn; i++) { pr[i] *= bnd; pi[i] *= bnd; } } /* start the algorithm for one zero */ while (nn > 2) { /* calculate bnd, a lower bound on the modulus of the zeros. */ for (i=0 ; i < nn ; i++) shr[i] = hypot(pr[i], pi[i]); bnd = cpoly_cauchy(nn, shr, shi); /* outer loop to control 2 major passes */ /* with different sequences of shifts */ for (i1 = 1; i1 <= 2; i1++) { /* first stage calculation, no shift */ noshft(5); /* inner loop to select a shift */ for (i2 = 1; i2 <= 9; i2++) { /* shift is chosen with modulus bnd */ /* and amplitude rotated by 94 degrees */ /* from the previous shift */ xxx= cosr * xx - sinr * yy; yy = sinr * xx + cosr * yy; xx = xxx; sr = bnd * xx; si = bnd * yy; /* second stage calculation, fixed shift */ conv = fxshft(i2 * 10, &zr, &zi); if (conv) goto L10; } } /* the zerofinder has failed on two major passes */ /* return empty handed */ *fail = TRUE; vmaxset(vmax); return; /* the second stage jumps directly to the third stage iteration. * if successful, the zero is stored and the polynomial deflated. */ L10: d_n = d1+2 - nn; zeror[d_n] = zr; zeroi[d_n] = zi; --nn; for (i=0; i < nn ; i++) { pr[i] = qpr[i]; pi[i] = qpi[i]; } }/*while*/ /* calculate the final zero and return */ cdivid(-pr[1], -pi[1], pr[0], pi[0], &zeror[d1], &zeroi[d1]); vmaxset(vmax); return; }
int cpoly(double opr[], double opi[], int degree, double zeror[], double zeroi[]) { /* Finds the zeros of a complex polynomial. opr, opi - double precision vectors of real and imaginary parts of the coefficients in order of decreasing powers. degree - integer degree of polynomial zeror, zeroi - output double precision vectors of real and imaginary parts of the zeros. fail - output logical parameter, TRUE if leading coefficient is zero, if cpoly has found fewer than degree zeros, or if there is another internal error. The program has been written to reduce the chance of overflow occurring. If it does occur, there is still a possibility that the zerofinder will work provided the overflowed quantity is replaced by a large number. */ double xx,yy,xxx,zr,zi,bnd; int fail,conv; int cnt1,cnt2,i,idnn2; /* initialization of constants */ nn = degree+1; if (!init(nn)) { fail = TRUE; return fail; } xx = .70710678L; yy = -xx; fail = FALSE; /* algorithm fails if the leading coefficient is zero. */ if (opr[0] == 0.0 && opi[0] == 0.0) { fail = TRUE; return fail; } /* Remove the zeros at the origin if any */ while (opr[nn-1] == 0.0 && opi[nn-1] == 0.0) { idnn2 = degree+1-nn; zeror[idnn2] = 0.0; zeroi[idnn2] = 0.0; nn--; } /* Make a copy of the coefficients */ for (i=0;i<nn;i++) { pr[i] = opr[i]; pi[i] = opi[i]; shr[i] = cmod(pr[i],pi[i]); } /* Scale the polynomial */ bnd = scale(nn,shr); if (bnd != 1.0) { for (i=0;i<nn;i++) { pr[i] *= bnd; pi[i] *= bnd; } } while (!fail) { /* Start the algorithm for one zero */ if (nn < 3) { /* Calculate the final zero and return */ cdivid(-pr[1],-pi[1],pr[0],pi[0],&(zeror[degree-1]),&(zeroi[degree-1])); return fail; } /* Calculate bnd, a lower bound on the modulus of the zeros */ for (i=0;i<nn;i++) { shr[i] = cmod(pr[i],pi[i]); } bnd = cauchy(nn,shr,shi); /* Outer loop to control 2 major passes with different sequences of shifts */ fail = TRUE; for(cnt1=1;fail && (cnt1<=2);cnt1++) { /* First stage calculation, no shift */ noshft(5); /* Inner loop to select a shift. */ for (cnt2=1;fail && (cnt2<10);cnt2++) { /* Shift is chosen with modulus bnd and amplitude rotated by 94 degrees from the previous shift */ xxx = COSR*xx-SINR*yy; yy = SINR*xx+COSR*yy; xx = xxx; sr = bnd*xx; si = bnd*yy; /* Second stage calculation, fixed shift */ conv = fxshft(10*cnt2,&zr,&zi); if (conv) { /* The second stage jumps directly to the third stage iteration If successful the zero is stored and the polynomial deflated */ idnn2 = degree+1-nn; zeror[idnn2] = zr; zeroi[idnn2] = zi; nn--; for(i=0;i<nn;i++) { pr[i] = qpr[i]; pi[i] = qpi[i]; } fail = FALSE; } /* If the iteration is unsuccessful another shift is chosen */ } /* If 9 shifts fail, the outer loop is repeated with another sequence of shifts */ } } /* The zerofinder has failed on two major passes Return empty handed */ return fail; }
int CPoly<T>::findRoots( const T *opr, const T *opi, int degree, T *zeror, T *zeroi ) { int cnt1, cnt2, idnn2, i, conv; T xx, yy, cosr, sinr, smalno, base, xxx, zr, zi, bnd; mcon( &eta, &infin, &smalno, &base ); are = eta; mre = (T) (2.0 * sqrt( 2.0 ) * eta); xx = (T) 0.70710678; yy = -xx; cosr = (T) -0.060756474; sinr = (T) -0.99756405; nn = degree; // Algorithm fails if the leading coefficient is zero, or degree is zero. if( nn < 1 || (opr[ 0 ] == 0 && opi[ 0 ] == 0) ) return -1; // Remove the zeros at the origin if any while( opr[ nn ] == 0 && opi[ nn ] == 0 ) { idnn2 = degree - nn; zeror[ idnn2 ] = 0; zeroi[ idnn2 ] = 0; nn--; } // sherm 20130410: If all coefficients but the leading one were zero, then // all solutions are zero; should be a successful (if boring) return. if (nn == 0) return degree; // Allocate arrays pr = new T [ degree+1 ]; pi = new T [ degree+1 ]; hr = new T [ degree+1 ]; hi = new T [ degree+1 ]; qpr= new T [ degree+1 ]; qpi= new T [ degree+1 ]; qhr= new T [ degree+1 ]; qhi= new T [ degree+1 ]; shr= new T [ degree+1 ]; shi= new T [ degree+1 ]; // Make a copy of the coefficients for( i = 0; i <= nn; i++ ) { pr[ i ] = opr[ i ]; pi[ i ] = opi[ i ]; shr[ i ] = cmod( pr[ i ], pi[ i ] ); } // Scale the polynomial bnd = scale( nn, shr, eta, infin, smalno, base ); if( bnd != 1 ) for( i = 0; i <= nn; i++ ) { pr[ i ] *= bnd; pi[ i ] *= bnd; } search: if( nn <= 1 ) { cdivid( -pr[ 1 ], -pi[ 1 ], pr[ 0 ], pi[ 0 ], &zeror[ degree-1 ], &zeroi[ degree-1 ] ); goto finish; } // Calculate bnd, alower bound on the modulus of the zeros for( i = 0; i<= nn; i++ ) shr[ i ] = cmod( pr[ i ], pi[ i ] ); cauchy( nn, shr, shi, &bnd ); // Outer loop to control 2 Major passes with different sequences of shifts for( cnt1 = 1; cnt1 <= 2; cnt1++ ) { // First stage calculation , no shift noshft( 5 ); // Inner loop to select a shift for( cnt2 = 1; cnt2 <= 9; cnt2++ ) { // Shift is chosen with modulus bnd and amplitude rotated by 94 degree from the previous shif xxx = cosr * xx - sinr * yy; yy = sinr * xx + cosr * yy; xx = xxx; sr = bnd * xx; si = bnd * yy; // Second stage calculation, fixed shift fxshft( 10 * cnt2, &zr, &zi, &conv ); if( conv ) { // The second stage jumps directly to the third stage ieration // If successful the zero is stored and the polynomial deflated idnn2 = degree - nn; zeror[ idnn2 ] = zr; zeroi[ idnn2 ] = zi; nn--; for( i = 0; i <= nn; i++ ) { pr[ i ] = qpr[ i ]; pi[ i ] = qpi[ i ]; } goto search; } // If the iteration is unsuccessful another shift is chosen } // if 9 shifts fail, the outer loop is repeated with another sequence of shifts } // The zerofinder has failed on two major passes // return empty handed with the number of roots found (less than the original degree) degree -= nn; finish: // Deallocate arrays delete [] pr; delete [] pi; delete [] hr; delete [] hi; delete [] qpr; delete [] qpi; delete [] qhr; delete [] qhi; delete [] shr; delete [] shi; return degree; }
// Main function // int cpoly(int degree, const xcomplex poly[], xcomplex Roots[]) { xcomplex PhiDiff = -0.069756473 + 0.99756405i; xcomplex PhiRand = (1.0-1.0i) /sqrt(2.0); xcomplex P[degree+1], H[degree+1], h[degree+1], p[degree+1], zero, s, bnd; unsigned int conv = 0; for (int i = 0; i <= degree; i++) if (isnan(poly[i])) return -1; // otherwise we may get stuck in infinite loops while(poly[0] == xdata.ZERO) { poly++; degree--; if (degree < 0) return -1; }; int deg = degree; // Remove the zeros at the origin if any while(poly[deg] == xdata.ZERO){ Roots[degree - deg] = xdata.ZERO; deg--; } if (deg == 0) return degree; // Make a copy of the coefficients for(int i = 0; i <= deg; i++) { P[i] = poly[i]; } scale(deg, P); search: if(deg <= 1){ Roots[degree-1] = - P[1] / P[0]; return degree; } // compute a bound of the moduli of the roots (Newton-Raphson) bnd = cauchy(deg, P); // Outer loop to control 2 Major passes with different sequences of shifts for(int cnt1 = 1; cnt1 <= 2; cnt1++) { // First stage calculation , no shift noshft(5, deg, P, H); // Inner loop to select a shift for(int cnt2 = 1; cnt2 <= 9; cnt2++) { // Shift is chosen with modulus bnd and amplitude rotated by 94 degree from the previous shif PhiRand = PhiDiff * PhiRand; s = bnd * PhiRand; // Second stage calculation, fixed shift conv = fxshft(10 * cnt2, deg, P, p, H, h, &zero, &s); if(conv) { // The second stage jumps directly to the third stage iteration // If successful the zero is stored and the polynomial deflated Roots[degree - deg] = zero; // continue with the remaining polynomial deg--; for(int i = 0; i <= deg; i++){ P[i] = p[i]; }; goto search; } // if the iteration is unsuccessful another shift is chosen } // if 9 shifts fail, the outer loop is repeated with another sequence of shifts } // The zerofinder has failed on two major passes // return empty handed with the number of roots found (less than the original degree) return degree - deg; }