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); }
poly_t polyMult(poly_t p1,poly_t p2) { poly_t p; int i,j; int exp = p1->degr + p2->degr - 2; p=polyTerm(0,exp); for(i=0;i < p1->degr;i++) for(j=0;j < p2->degr;j++) p->coef[i+j] += p1->coef[i] * p2->coef[j]; return(p); }
void Weights::calculateWeights() { mCoefficientNumber = (mTwoDim ? ((size_t)mPolynomeOrder + 1) * ((size_t)mPolynomeOrder + 1) : (size_t)mPolynomeOrder + 1); size_t ix, iy, i, j; int x, y; // Determine coordinates of pixels to be sampled if (mTwoDim) { int iPolynomeOrder = (int) mPolynomeOrder; //lets avoid signed/unsigned comparison warnings int iHeight = (int) height(); int iWidth = (int) width(); for (y = -iPolynomeOrder; y < iHeight + iPolynomeOrder; ++y) { for (x = -iPolynomeOrder; x < iWidth + iPolynomeOrder; ++x) { if ((x < 0 && y < 0 && -x - y < iPolynomeOrder + 2) || (x < 0 && y >= iHeight && -x + y - iHeight < iPolynomeOrder + 1) || (x >= iWidth && y < 0 && x - y - iWidth < iPolynomeOrder + 1) || (x >= iWidth && y >= iHeight && x + y - iWidth - iHeight < iPolynomeOrder) || (x < 0 && y >= 0 && y < iHeight) || (x >= iWidth && y >= 0 && y < iHeight) || (y < 0 && x >= 0 && x < iWidth ) || (y >= iHeight && x >= 0 && x < iWidth)) { QPoint position(x,y); mPositions.append(position); } } } } else { // In the one-dimensional case, only the y coordinate and y size is used. */ for (y = (-1)*mPolynomeOrder; y < 0; ++y) { QPoint position(0,y); mPositions.append(position); } for (y = (int) height(); y < (int) height() + (int) mPolynomeOrder; ++y) { QPoint position(0,y); mPositions.append(position); } } // Allocate memory. QScopedArrayPointer<double> matrix (new double[mCoefficientNumber * mCoefficientNumber]); QScopedArrayPointer<double> vector0(new double[mPositions.count() * mCoefficientNumber]); QScopedArrayPointer<double> vector1(new double[mPositions.count() * mCoefficientNumber]); // Calculate coefficient matrix and vectors for (iy = 0; iy < mCoefficientNumber; ++iy) { for (ix = 0; ix < mCoefficientNumber; ++ix) { matrix [iy* mCoefficientNumber+ix] = 0.0; } for (j = 0; j < (size_t)mPositions.count(); ++j) { vector0 [iy * mPositions.count() + j] = polyTerm (iy, mPositions.at(j).x(), mPositions.at(j).y(), mPolynomeOrder); for (ix = 0; ix < mCoefficientNumber; ++ix) { matrix [iy* mCoefficientNumber + ix] += (vector0 [iy * mPositions.count() + j] * polyTerm (ix, mPositions.at(j).x(), mPositions.at(j).y(), mPolynomeOrder)); } } } // Invert matrix. matrixInv (matrix.data(), mCoefficientNumber); // Multiply inverse matrix with vector. for (iy = 0; iy < mCoefficientNumber; ++iy) { for (j = 0; j < (size_t)mPositions.count(); ++j) { vector1 [iy * mPositions.count() + j] = 0.0; for (ix = 0; ix < mCoefficientNumber; ++ix) { vector1 [iy * mPositions.count() + j] += matrix [iy * mCoefficientNumber + ix] * vector0 [ix * mPositions.count() + j]; } } } // Store weights // Allocate mPositions.count() matrices. mWeightMatrices = new double** [mPositions.count()]; for (i=0 ; i < (size_t)mPositions.count() ; ++i) { // Allocate mHeight rows on each position mWeightMatrices[i] = new double*[mHeight]; for (j=0 ; j < mHeight ; ++j) { // Allocate mWidth columns on each row mWeightMatrices[i][j] = new double[mWidth]; } } for (y = 0; y < (int) mHeight; ++y) { for (x = 0; x < (int) mWidth; ++x) { for (j = 0; j < (size_t)mPositions.count(); ++j) { mWeightMatrices [j][y][x] = 0.0; for (iy = 0; iy < mCoefficientNumber; ++iy) { mWeightMatrices [j][y][x] += vector1 [iy * mPositions.count() + j] * polyTerm (iy, x, y, mPolynomeOrder); } mWeightMatrices [j][y][x] *= (double) mPositions.count(); } } } }