/* Test harness routine, which contains test quadratic data, invokes mpfit() */ int testquadfit() { double x[] = {-1.7237128E+00,1.8712276E+00,-9.6608055E-01, -2.8394297E-01,1.3416969E+00,1.3757038E+00, -1.3703436E+00,4.2581975E-02,-1.4970151E-01, 8.2065094E-01}; double y[] = {2.3095947E+01,2.6449392E+01,1.0204468E+01, 5.40507,1.5787588E+01,1.6520903E+01, 1.5971818E+01,4.7668524E+00,4.9337711E+00, 8.7348375E+00}; double ey[10]; double p[] = {1.0, 1.0, 1.0}; /* Initial conditions */ double pactual[] = {4.7, 0.0, 6.2}; /* Actual values used to make data */ double perror[3]; /* Returned parameter errors */ int i; struct vars_struct v; int status; mp_result result; memset(&result,0,sizeof(result)); /* Zero results structure */ result.xerror = perror; for (i=0; i<10; i++) ey[i] = 0.2; /* Data errors */ v.x = x; v.y = y; v.ey = ey; /* Call fitting function for 10 data points and 3 parameters */ status = mpfit(quadfunc, 10, 3, p, 0, 0, (void *) &v, &result); printf("*** testquadfit status = %d\n", status); printresult(p, pactual, &result); return 0; }
int main() { int *p1, *p2; printf("initial allocation\n"); initmemory(56); printallocation(); printf("malloc 20\n"); p1 = (int *)myalloc(20); printresult(p1); printf("malloc 10\n"); p2 = (int *)myalloc(10); printresult(p2); printf("free (malloc 20)\n"); myfree(p1); p1 = NULL; printallocation(); printf("malloc 4\n"); p1 = (int *)myalloc(4); printresult(p1); printf("free (malloc 10)\n"); myfree(p2); p2 = NULL; printallocation(); printf("malloc 30: should fail\n"); // this will fail because we need a block of at least 40 p2 = (int *)myalloc(30); printresult(p2); printf("coalesce\n"); coalesce(); printallocation(); printf("malloc 30 \n"); // now this succeeds p2 = (int *) myalloc(30); printresult(p2); printf("free everything\n"); myfree(p1); p1 = NULL; myfree(p2); p2 = NULL; printallocation(); printf("malloc 56: should fail\n"); // this will fail p1 = (int *)myalloc(56); printresult(p1); printf("coalesce\n"); coalesce(); printallocation(); printf("malloc 56\n"); p1 = (int *)myalloc(56); printresult(p1); }
/* Test harness routine, which contains test gaussian-peak data Example of fixing two parameter Commented example of how to put boundary constraints */ int testgaussfix() { double x[] = {-1.7237128E+00,1.8712276E+00,-9.6608055E-01, -2.8394297E-01,1.3416969E+00,1.3757038E+00, -1.3703436E+00,4.2581975E-02,-1.4970151E-01, 8.2065094E-01}; double y[] = {-4.4494256E-02,8.7324673E-01,7.4443483E-01, 4.7631559E+00,1.7187297E-01,1.1639182E-01, 1.5646480E+00,5.2322268E+00,4.2543168E+00, 6.2792623E-01}; double ey[10]; double p[] = {0.0, 1.0, 0.0, 0.1}; /* Initial conditions */ double pactual[] = {0.0, 4.70, 0.0, 0.5};/* Actual values used to make data*/ double perror[4]; /* Returned parameter errors */ mp_par pars[4]; /* Parameter constraints */ int i; struct vars_struct v; int status; mp_result result; memset(&result,0,sizeof(result)); /* Zero results structure */ result.xerror = perror; memset(pars,0,sizeof(pars)); /* Initialize constraint structure */ pars[0].fixed = 1; /* Fix parameters 0 and 2 */ pars[2].fixed = 1; /* How to put limits on a parameter. In this case, parameter 3 is limited to be between -0.3 and +0.2. pars[3].limited[0] = 0; pars[3].limited[1] = 1; pars[3].limits[0] = -0.3; pars[3].limits[1] = +0.2; */ for (i=0; i<10; i++) ey[i] = 0.5; v.x = x; v.y = y; v.ey = ey; /* Call fitting function for 10 data points and 4 parameters (2 parameters fixed) */ status = mpfit(gaussfunc, 10, 4, p, pars, 0, (void *) &v, &result); printf("*** testgaussfix status = %d\n", status); printresult(p, pactual, &result); return 0; }
int main(int argc, const char * argv[]) { int begin1,begin2; int number; Node nodes[100000]; scanf("%d%d%d",&begin1,&begin2,&number); for(int i=0;i<number;i++){ int flag; scanf("%d",&flag); scanf("%s",&nodes[flag].data); scanf("%d",&nodes[flag].next); nodes[flag].count = 0; } int address = 0; int i=begin1; while(i!=-1){ nodes[i].count++; i = nodes[i].next; } i=begin2; while(i!=-1){ nodes[i].count++; i = nodes[i].next; } i=begin2; while(i!=-1){ if(nodes[i].count>1){ address = i; break; } i = nodes[i].next; } if(address!=0) printresult(address); else printf("-1"); return 0; }
/* Test harness routine, which contains test data, invokes mpfit() */ int testlinfit() { double x[] = {-1.7237128E+00,1.8712276E+00,-9.6608055E-01, -2.8394297E-01,1.3416969E+00,1.3757038E+00, -1.3703436E+00,4.2581975E-02,-1.4970151E-01, 8.2065094E-01}; double y[] = {1.9000429E-01,6.5807428E+00,1.4582725E+00, 2.7270851E+00,5.5969253E+00,5.6249280E+00, 0.787615,3.2599759E+00,2.9771762E+00, 4.5936475E+00}; double ey[10]; /* y = a - b*x */ /* a b */ double p[2] = {1.0, 1.0}; /* Parameter initial conditions */ double pactual[2] = {3.20, 1.78}; /* Actual values used to make data */ double perror[2]; /* Returned parameter errors */ int i; struct vars_struct v; int status; mp_result result; memset(&result,0,sizeof(result)); /* Zero results structure */ result.xerror = perror; for (i=0; i<10; i++) ey[i] = 0.07; /* Data errors */ v.x = x; v.y = y; v.ey = ey; /* Call fitting function for 10 data points and 2 parameters */ status = mpfit(linfunc, 10, 2, p, 0, 0, (void *) &v, &result); printf("*** testlinfit status = %d\n", status); printresult(p, pactual, &result); return 0; }
void run(double nbMesures, double moyArith, double moyHarmo, double ecTy) { bool _end = false; t_variable _var; t_result _res; double nbAdd = 0; int n; char p[SIZE_BUFFER]; memset(p, '\0', SIZE_BUFFER); init_struct(&_var, &_res, nbMesures, moyArith, moyHarmo, ecTy); while (!_end) { nbAdd = write(1, "indtast din vaerdi : ", 21); n = read(0, p, SIZE_BUFFER - 1); p[n - 1] = '\0'; if (n > 1 && n < SIZE_BUFFER) { if (strncmp("ende", p, n) == 0) _end = true; else if (verif_number(p)) { nbAdd = atof(p); moy_arith(&_var, &_res, nbAdd); moy_quadra(&_var, &_res, nbAdd); moy_harmo(&_var, &_res, nbAdd); ec_type(&_var, &_res, nbAdd); printresult(&_res); usleep(500); } } memset(p, '\0', SIZE_BUFFER); init_struct(&_var, &_res, _res.newNbMesures, _res.newMoyArith, _res.newMoyHarmo, _res.newEcTy); } }
int CNLopt::run(double (*error_func)(unsigned int nParams, const double* params, double* grad, void* misc)) { // Create a member function pointer unsigned int n_data = mWorkerThread->GetDataSize(); mOpt = nlopt_create(mAlgorithm, mNParams); // xopt = new double [mNParams]; lb = new double [mNParams]; ub = new double [mNParams]; // Copy out the initial values for the parameters: CModelListPtr model_list = mWorkerThread->GetModelList(); model_list->GetFreeParameters(mParams, mNParams, false); vector<string> names = model_list->GetFreeParamNames(); vector< pair<double, double> > min_max = model_list->GetFreeParamMinMaxes(); // Init parameter values for(int i = 0; i < mNParams; i++) { lb[i] = min_max[i].first; ub[i] = min_max[i].second; // xopt = mParams[i]; } nlopt_set_lower_bounds(mOpt,lb); nlopt_set_upper_bounds(mOpt,ub); nlopt_set_ftol_rel(mOpt, 1e-4); // stopping criterion = when chi2 changes by less than 0.1% nlopt_set_min_objective(mOpt, error_func, (void*)this); int major=0, minor=0, bugfix=0; nlopt_version(&major, &minor, &bugfix); printf("Starting NLopt version %d.%d.%d\n", major, minor, bugfix); printf("Algorithm = %s \n", nlopt_algorithm_name(mAlgorithm)); if(mAlgorithm == NLOPT_G_MLSL_LDS) // set local optimizer to be used with a global search { printf("Secondary Algorithm = %s \n", nlopt_algorithm_name(mAlgorithmSecondary)); mOptSecondary = nlopt_create(mAlgorithmSecondary, mNParams); nlopt_set_lower_bounds(mOptSecondary,lb); nlopt_set_upper_bounds(mOptSecondary,ub); nlopt_set_ftol_rel(mOptSecondary, 1e-4); // stopping criterion = when chi2 changes by less than 0.1% nlopt_set_min_objective(mOptSecondary, error_func, (void*)this); nlopt_set_local_optimizer(mOpt, mOptSecondary); } mIsRunning = true; mEvals = 0; double minf; // Call NLopt. Note, the results are saved in mParams upon completion. nlopt_result result = nlopt_optimize(mOpt, mParams, &minf); mIsRunning = false; printresult(mParams, mNParams, n_data, names, minf, result); delete(lb); delete(ub); nlopt_destroy(mOpt); if(mAlgorithm == NLOPT_G_MLSL_LDS) // also destroy local optimizer { nlopt_destroy(mOptSecondary); } return mEvals; }