int main (void) { struct poly *p0 = polySetCoefficient (polySetCoefficient (polySetCoefficient ( polyCreate() , 0, 4.0), 1, -1.0), 10, 2.0); struct poly *p1 = polyCopy (p0); struct poly *p2, *p3, *p4; printf ("%g\n", polyGetCoefficient (p0, 10)); printf ("%g\n", polyGetCoefficient (p0, 100)); printf ("%d\n", polyDegree (p0)); polyPrint (p0); polyPrint (p1); polySetCoefficient (p1, 2, 1.0/2.0); polyPrint (p1); p2 = polyAdd (p0, p1); polyPrint (p2); p3 = polyMultiply (p0, p1); polyPrint (p3); p4 = polyPrime (p0); polyPrint (p4); printf ("%g\n", polyEval (p0, 0.0)); printf ("%g\n", polyEval (p0, 1.0)); printf ("%g\n", polyEval (p0, 2.0)); p0 = polyDelete (p0); p1 = polyDelete (p1); p2 = polyDelete (p2); p3 = polyDelete (p3); p4 = polyDelete (p4); return 0; }
/* * *************************************************************************** * Routine: simplexBasisForm * * Purpose: Evaluate the bases for the trial or the test space, for a * particular component of the system, at all quadrature points * on the master simplex element. * * Author: Michael Holst * *************************************************************************** */ VPUBLIC void simplexBasisForm(int key, int dim, int comp, int pdkey, double xq[], double basis[]) { if (pdkey == 0) { polyEval(numP, basis, c, xq); } else if (pdkey == 1) { polyEval(numP, basis, cx, xq); } else if (pdkey == 2) { polyEval(numP, basis, cy, xq); } else if (pdkey == 3) { polyEval(numP, basis, cz, xq); } else { VASSERT(0); } }
void extractDigits(vector<Ctxt>& digits, const Ctxt& c, long r) { const FHEcontext& context = c.getContext(); long rr = c.effectiveR(); if (r<=0 || r>rr) r = rr; // how many digits to extract long p = context.zMStar.getP(); ZZX x2p; if (p>3) { buildDigitPolynomial(x2p, p, r); } Ctxt tmp(c.getPubKey(), c.getPtxtSpace()); digits.resize(r, tmp); // allocate space #ifdef DEBUG_PRINTOUT fprintf(stderr, "***\n"); #endif for (long i=0; i<r; i++) { tmp = c; for (long j=0; j<i; j++) { if (p==2) digits[j].square(); else if (p==3) digits[j].cube(); else polyEval(digits[j], x2p, digits[j]); // "in spirit" digits[j] = digits[j]^p #ifdef DEBUG_PRINTOUT fprintf(stderr, "%5ld", digits[j].bitCapacity()); #endif tmp -= digits[j]; tmp.divideByP(); } digits[i] = tmp; // needed in the next round #ifdef DEBUG_PRINTOUT if (dbgKey) { double ratio = log(embeddingLargestCoeff(digits[i], *dbgKey)/digits[i].getNoiseBound())/log(2.0); fprintf(stderr, "%5ld [%f]", digits[i].bitCapacity(), ratio); if (ratio > 0) fprintf(stderr, " BAD-BOUND"); fprintf(stderr, "\n"); } else { fprintf(stderr, "%5ld\n", digits[i].bitCapacity()); } #endif } #ifdef DEBUG_PRINTOUT fprintf(stderr, "***\n"); #endif }
// Newton's method to calculate roots bigfloat AlgRemez::rtnewt(bigfloat *poly, long i, bigfloat x1, bigfloat x2, bigfloat xacc) { int j; bigfloat df, dx, f, rtn; rtn=(bigfloat)0.5*(x1+x2); for (j=1; j<=JMAX;j++) { f = polyEval(rtn, poly, i); df = polyDiff(rtn, poly, i); dx = f/df; rtn -= dx; //if ((x1-rtn)*(rtn-x2) < (bigfloat)0.0) //printf("Jumped out of brackets in rtnewt\n"); if (abs_bf(dx) < xacc) return rtn; } printf("Maximum number of iterations exceeded in rtnewt\n"); return 0.0; }
// Newton's method to calculate roots bigfloat AlgRemez::rtnewt(bigfloat *poly, long i, bigfloat x1, bigfloat x2, bigfloat xacc) { char *fname = "rtnnewt(bigfloat *, long, bigfloatm bigfloat, bigfloat)"; VRB.Func(cname,fname); int j; bigfloat df, dx, f, rtn; rtn=(bigfloat)0.5*(x1+x2); for (j=1; j<=JMAX;j++) { f = polyEval(rtn, poly, i); df = polyDiff(rtn, poly, i); dx = f/df; rtn -= dx; if ((x1-rtn)*(rtn-x2) < (bigfloat)0.0) VRB.Warn(cname,fname,"Jumped out of brackets in rtnewt\n"); if (abs_bf(dx) < xacc) return rtn; } VRB.Warn(cname,fname,"Maximum number of iterations exceeded in rtnewt\n"); return 0.0; }
int main(int argc,char *argv[]) { int i,N=atoi(argv[1]); double x=atof(argv[2]); poly_t p,y; p=polyAdd(polyTerm(1,1),polyTerm(1,0)); for(y=p,i=1;i<N;i++){ y=polyMult(p,y); } puts("binomial coefficients:"); polyShow(y); putchar('\n'); putchar('\n'); printf("(%.3f+1)^%d: %.3f\n",x,N,polyEval(y,x)); return(0); }
/** * @name fluxMatrix * @brief This is the main working function of the file. Every work of the file is done in this function. * @param[in] vector<double> Points * This would denote the inteprolating points for which the Lagrange polynomials have to be written. * * Example usage * @code * double integral = lobattoIntegration(unsigned n);//Storing the coefficient of the nth degree Legendre roots. * @endcode */ vector< vector<double> > fluxMatrix(vector<double> Points) { unsigned n = Points.size(); sort(Points.begin(),Points.end()); double start = Points[0];///The first element. double end = Points[n-1];///The last element. vector< vector<double> > FluxMatrix; FluxMatrix=zeros(n,n); vector< vector<double> > LagrangePolynomials = lagrangePolynomials(Points); unsigned i,j;///Counters for the loop. function<double(double)> eval; for(i=0;i<n;i++) { for(j=0;j<n;j++) { eval = [&LagrangePolynomials,&i,&j](double x){ return ((polyEval(LagrangePolynomials[i],x)*polyEval(LagrangePolynomials[j],x)));}; FluxMatrix[i][j] =( eval(end)-eval(start) ); } } return FluxMatrix; }
/** * @name derivativeMatrix * @brief This is the main working function of the file. Every work of the file is done in this function. * @param[in] vector<double> Points * This would denote the inteprolating points for which the Lagrange polynomials have to be written. * * Example usage * @code * double integral = lobattoIntegration(unsigned n);//Storing the coefficient of the nth degree Legendre roots. * @endcode */ vector< vector<double> > derivativeMatrix(vector<double> Points) { unsigned n = Points.size(); sort(Points.begin(),Points.end()); double start = Points[0];///The first element. double end = Points[n-1];///The last element. vector< vector<double> > DerivativeMatrix; DerivativeMatrix=zeros(n,n); vector< vector<double> > LagrangePolynomials = lagrangePolynomials(Points); unsigned i,j;///Counters for the loop. function<double(double)> eval; for(i=0;i<n;i++) { for(j=0;j<n;j++) { eval = [&LagrangePolynomials,&i,&j](double x){ return ((polyEval(LagrangePolynomials[i],x)*polyEval(polyDeriv(LagrangePolynomials[j]),x)));}; DerivativeMatrix[i][j] = lobattoIntegration(start,end,n,eval); } } return DerivativeMatrix; }
void extendExtractDigits(vector<Ctxt>& digits, const Ctxt& c, long r, long e) { const FHEcontext& context = c.getContext(); long p = context.zMStar.getP(); ZZX x2p; if (p>3) { buildDigitPolynomial(x2p, p, r); } // we should pre-compute this table // for i = 0..r-1, entry i is G_{e+r-i} in Chen and Han Vec<ZZX> G; G.SetLength(r); for (long i: range(r)) { compute_magic_poly(G[i], p, e+r-i); } vector<Ctxt> digits0; Ctxt tmp(c.getPubKey(), c.getPtxtSpace()); digits.resize(r, tmp); // allocate space digits0.resize(r, tmp); #ifdef DEBUG_PRINTOUT fprintf(stderr, "***\n"); #endif for (long i: range(r)) { tmp = c; for (long j: range(i)) { if (digits[j].capacity() >= digits0[j].capacity()) { // optimization: digits[j] is better than digits0[j], // so just use it tmp -= digits[j]; #ifdef DEBUG_PRINTOUT fprintf(stderr, "%5ld*", digits[j].bitCapacity()); #endif } else { if (p==2) digits0[j].square(); else if (p==3) digits0[j].cube(); else polyEval(digits0[j], x2p, digits0[j]); // "in spirit" digits0[j] = digits0[j]^p tmp -= digits0[j]; #ifdef DEBUG_PRINTOUT fprintf(stderr, "%5ld ", digits0[j].bitCapacity()); #endif } tmp.divideByP(); } digits0[i] = tmp; // needed in the next round polyEval(digits[i], G[i], tmp); #ifdef DEBUG_PRINTOUT if (dbgKey) { double ratio = log(embeddingLargestCoeff(digits[i], *dbgKey)/digits[i].getNoiseBound())/log(2.0); fprintf(stderr, "%5ld --- %5ld", digits0[i].bitCapacity(), digits[i].bitCapacity()); fprintf(stderr, " [%f]", ratio); if (ratio > 0) fprintf(stderr, " BAD-BOUND"); fprintf(stderr, "\n"); } else { fprintf(stderr, "%5ld --- %5ld\n", digits0[i].bitCapacity(), digits[i].bitCapacity()); } #endif } }