void saetrain(NN** saes, int numsaes, int numdata, const double** train_x, int numepochs, int batchsize) { int i, j; double **train_data = (double**)train_x; double **new_train_data = NULL; for(i=0;i<numsaes; i++){ printf("Training AE %d / %d\n", i+1, numsaes); if( i==0) { nntrain(saes[i], numdata, (const double**)train_data, (const double**)train_data, numepochs, batchsize); }else{ // generate data for next SAE new_train_data = create2Darray( numdata, saes[i-1]->layer[0].units); for(j=0; j<numdata; j++){ nnff(saes[i-1], j, (const double**)train_data) ; memcpy( new_train_data[j], saes[i-1]->layer[0].a, saes[i-1]->layer[0].units); } nntrain(saes[i], numdata, (const double**)new_train_data, (const double**)new_train_data, numepochs, batchsize); if( i > 1) free2Darray( train_data, numdata); if( i== numsaes -1) free2Darray( new_train_data, numdata); train_data = new_train_data; } } }
int main(int arg, char* argv[]) { int p = 2; // our basis order double U[] = {0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0,5.0, 5.0}; // knot vector int m = sizeof(U)/sizeof(double) - 1; // number of knot points int n = m - p - 1; // number of basis functions double u; int c, span; /*printf("Number of knot points: %d\n", m); // // Test our knot span index code // for(c = 0; c < m+1; ++c) { span = FindSpan(n, p, U[c], U); printf("For Xi=%2.1f, the knot span index is i=%d\n\n",U[c], span); }*/ // // Now let's test our routines which calculate the basis functions and derivatives // int numNonZeroBsFns = p + 1; /* double *N = (double*)malloc(sizeof(double) * numNonZeroBsFns); double **ders = init2DArray(n+1, p+1); for(c = 0; c < numNonZeroBsFns; ++c) N[c] = 0.0; double point[] = {0.0, 1.0, 2,0, 3.0, 4.0, 5.0}; // some sample points to evaluate at int k = sizeof(point) / sizeof(double); // number of points (for our loop) int j,d; for(j = 0; j < k; ++j) { span = FindSpan(n, p, point[j], U); // get the span BasisFuns( span, point[j], p, U, N); // get the non-zero basis functions dersBasisFuns(span, point[j], p, n, U, ders); // get the derivatives ders[k][j] // print out basis functions printf("The non-zero basis functions in the range %2.2f and %2.2f and u=%2.2f are:\n",U[span], U[span+1], point[j]); for(c = 0; c < numNonZeroBsFns; ++c) printf("\n %2.2f\n", N[c]); // print out derivatives for(d = 0; d < (n+1); d++) { printf("For k=%d, derivatives are: \n", d); for(c = 0; c < numNonZeroBsFns; c++) printf("%2.2f\t", ders[d][c]); printf("\n"); } printf("\n\n"); } free(N); free2Darray(ders, n+1); */ double *dN = (double*)malloc(sizeof(double) * (n+1)); double **ders = init2DArray(n+1, p+1); double xi, Nip; int i=2; printf("xi\tN\tdNdxi\n"); for(xi=0.0; xi <= 5.0; xi+=0.1) { span = FindSpan(n, p, xi, U); Nip = OneBasisFun(p, m, U, i, xi); dersOneBasisFuns(p, m, U, i, xi, n, dN); } xi = 2.1; span = FindSpan(n, p, xi, U); dersBasisFuns(span, xi, p, n, U, ders); // get the derivatives ders[k][j] printf("\n\n%2.2f\t%2.20f\t%2.20f\t%2.20f\n",xi, ders[1][0], ders[1][1], ders[1][2]); free(dN); free2Darray(ders, n+1); return 0; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Return the NURBS basis function to matlab // // We expect the function to be called as [Nip] = NURBSBasis(i, p, xi, knot, weights) // // p = order of basis (0,1,2 etc) // knot = knot vector // i = basis function we want (1,2,3 ie. Hughes' notation) // xi = the coordinate we wish to evaluate at // weights = the weightings for the NURNS function (length(weights) = m - p -1) */ if(nrhs != 5) mexErrMsgTxt("You fool! You haven't passed in 5 arguments to the function." "We expect it to be in the form [Nip] = NURBSBasis(i, p, xi, knot, weights)\n"); int c, k, p, m, i, numKnot, numWeights; double *p_in, *m_in, *i_in; double *knot, *xi, *weight; double tol=100*DBL_EPSILON; for(c = 0; c < nrhs; c++) { switch(c) { case 0: i_in = mxGetPr(prhs[c]); i = (int)*i_in -1; /*mexPrintf("\n\ni=%d\n", i);*/ case 1: p_in = mxGetPr(prhs[c]); p = (int)*p_in; /*mexPrintf("\n\np=%d\n", p);*/ case 2: xi = mxGetPr(prhs[c]); /*mexPrintf("\nxi=%2.20f\n", *xi); */ case 3: knot = mxGetPr(prhs[c]); numKnot = mxGetN(prhs[c]); m = numKnot - 1; /*mexPrintf("\nWe have a knot vector with %d components\n", numKnot); for(k = 0; k < numKnot; k++) mexPrintf("%2.2f\t", *(knot+k)); mexPrintf("\n");*/ case 4: weight = mxGetPr(prhs[c]); numWeights = mxGetM(prhs[c]); } } if(fabs(*xi-knot[numKnot-1]) < tol) *xi = knot[numKnot-1] - tol; /* and call the basis function routine*/ double Rip, Nip, dRip_dxi; double w_interp, dw_interp_dxi ; int n = m - p -1; double *N = (double *)malloc(sizeof(double)*(p+1)); double *dN = (double *)malloc(sizeof(double)*(p+1)); double **ders = init2DArray(p+1, p+1); int span = FindSpan(n, p, *xi, knot); BasisFuns(span, *xi, p, knot, N); dersBasisFuns(span, *xi, p, p, knot, ders); w_interp = 0.0; dw_interp_dxi = 0.0; for(c = 0; c <= p; c++) { w_interp += N[c] * weight[span-p+c]; dw_interp_dxi += ders[1][c] * weight[span-p+c]; } dersOneBasisFuns(p, m, knot, i, *xi, p, dN); Nip = OneBasisFun(p, m, knot, i, *xi); Rip = Nip * weight[i] / w_interp; dRip_dxi = weight[i] * ( w_interp * dN[1] - dw_interp_dxi * Nip ) / (w_interp * w_interp); free2Darray(ders, (p+1)); free(N); free(dN); plhs[0] = mxCreateDoubleScalar(Rip); plhs[1] = mxCreateDoubleScalar(dRip_dxi); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Return the NURBS basis function to matlab // // We expect the function to be called as [interp interp_deriv] = NURBSinterpolation(xi, p, knot, points, weights) // // xi = point where we want to interpolate // knot = knot vector // vector of points in format [pt1 pt2 pt3 pt4 .... ptn] */ if(nrhs != 5) mexErrMsgTxt("You fool! You haven't passed in 3 arguments to the function." "We expect it to be in the form [interp interp_deriv] = NURBSinterpolation(xi, knot, points)\n"); /* First get the inputs */ double *xi = mxGetPr(prhs[0]); double *p_in = (mxGetPr(prhs[1])); int p = (int) *p_in; double *knot = mxGetPr(prhs[2]); int numKnot = mxGetN(prhs[2]); int m = numKnot - 1; int n = m - p -1; double *points = mxGetPr(prhs[3]); int numPoints = mxGetN(prhs[3]); double *weight = mxGetPr(prhs[4]); int numWeights = mxGetN(prhs[4]); double tol=100*DBL_EPSILON; if(fabs(*xi-knot[numKnot-1]) < tol) *xi = knot[numKnot-1] - tol; /* and evaluate the non-zero basis functions*/ double *N = (double *)malloc(sizeof(double)*(p+1)); double *NURBS = (double *)malloc(sizeof(double)*(p+1)); double *NURBS_deriv = (double *)malloc(sizeof(double)*(p+1)); double **ders = init2DArray(n+1, p+1); int span = FindSpan(n, p, *xi, knot); BasisFuns(span, *xi, p, knot, N); dersBasisFuns(span, *xi, p, n, knot, ders); /* and create NURBS approximation */ int k, c; for(k = 0; k <=p; k++) { double w_interp = 0.0, dw_interp_dxi= 0.0; for(c = 0; c <= p; c++) { w_interp += N[c] * weight[span-p+c]; dw_interp_dxi += ders[1][c] * weight[span-p+c]; } NURBS[k] = N[k] * weight[span-p+k] / w_interp; NURBS_deriv[k] = weight[span-p+k] * ( w_interp * ders[1][k] - dw_interp_dxi * N[k] ) / (w_interp * w_interp); } double interp = 0.0; double interp_deriv = 0.0; for(c = 0; c <= p; c++) { interp += NURBS[c] * points[span-p+c]; interp_deriv += NURBS_deriv[c] * points[span-p+c]; } free(N); free(NURBS); free(NURBS_deriv); free2Darray(ders, (n+1)); plhs[0] = mxCreateDoubleScalar(interp); plhs[1] = mxCreateDoubleScalar(interp_deriv); }