int _nrrdHestKernelSpecParse(void *ptr, char *str, char err[AIR_STRLEN_HUGE]) { NrrdKernelSpec **ksP; char me[]="_nrrdHestKernelSpecParse", *nerr; if (!(ptr && str)) { sprintf(err, "%s: got NULL pointer", me); return 1; } ksP = (NrrdKernelSpec **)ptr; *ksP = nrrdKernelSpecNew(); if (nrrdKernelParse(&((*ksP)->kernel), (*ksP)->parm, str)) { nerr = biffGetDone(NRRD); airStrcpy(err, AIR_STRLEN_HUGE, nerr); free(nerr); return 1; } return 0; }
int main(int argc, char *argv[]) { char *me, *kS, *minS, *stepS, *maxS, *outS, *err, kstr[AIR_STRLEN_LARGE]; const NrrdKernel *k; NrrdKernelSpec *ksp; double parm[NRRD_KERNEL_PARMS_NUM], min, step, max, integral, *dom_d, *ran_d; float *dom_f, *ran_f, v, r_f, r_d; FILE *fout; int i, len; airArray *mop; me = argv[0]; if (6 != argc) { usage(me); } kS = argv[1]; minS = argv[2]; stepS = argv[3]; maxS = argv[4]; outS = argv[5]; mop = airMopNew(); if (nrrdKernelParse(&k, parm, kS)) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble:\n%s\n", me, err); airMopError(mop); exit(1); } ksp = nrrdKernelSpecNew(); airMopAdd(mop, ksp, (airMopper)nrrdKernelSpecNix, airMopAlways); nrrdKernelSpecSet(ksp, k, parm); if (nrrdKernelSpecSprint(kstr, ksp)) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble:\n%s\n", me, err); airMopError(mop); exit(1); } printf("%s: printed kernel as \"%s\"\n", me, kstr); if (3 != (sscanf(minS, "%lf", &min) + sscanf(stepS, "%lf", &step) + sscanf(maxS, "%lf", &max))) { fprintf(stderr, "%s: couldn't parse \"%s\", \"%s\", \"%s\" as 3 doubles\n", me, minS, stepS, maxS); exit(1); } if (!( min <= -k->support(parm) && max >= k->support(parm) )) { fprintf(stderr, "%s: WARNING: support=%g => lower min (%g) or raise max (%g)\n", me, k->support(parm), min, max); } /* see how many values are in the interval */ len = 0; for (v=min; v<=max; v+=step) { len++; } /* allocate domain and range for both float and double */ if (!( (dom_d = (double *)calloc(len, sizeof(double))) && (ran_d = (double *)calloc(len, sizeof(double))) && (dom_f = (float *)calloc(len, sizeof(float))) && (ran_f = (float *)calloc(len, sizeof(float))) )) { fprintf(stderr, "%s: PANIC: couldn't allocate buffers\n", me); exit(1); } airMopAdd(mop, dom_d, airFree, airMopAlways); airMopAdd(mop, ran_d, airFree, airMopAlways); airMopAdd(mop, dom_f, airFree, airMopAlways); airMopAdd(mop, ran_f, airFree, airMopAlways); /* set values in both domains */ i=0; for (v=min; v<=max; v+=step) { /* note that the value stored in dom_d[i] is only a single-precision float, so that it is really equal to dom_f[i] */ dom_d[i] = v; dom_f[i] = v; i++; } /* do the vector evaluations */ k->evalN_f(ran_f, dom_f, len, parm); k->evalN_d(ran_d, dom_d, len, parm); /* do the single evaluations, and make sure everything agrees */ i = 0; integral = 0; for (v=min; v<=max; v+=step) { /* compare two single evaluations */ r_f = k->eval1_f(v, parm); r_d = k->eval1_d(v, parm); if (!CLOSE(r_f,r_d)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (eval1_d(%g)== %f)\n", me, v, r_f, v, r_d); } /* compare single float with vector float */ if (!CLOSE(r_f,ran_f[i])) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_f[%d]== %f)\n", me, v, r_f, i, ran_f[i]); } /* compare single float with vector double */ r_d = ran_d[i]; if (!CLOSE(r_f,r_d)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_d[%d]== %f)\n", me, v, r_f, i, r_d); } integral += step*ran_d[i]; i++; } if (!KINDACLOSE(integral, k->integral(parm))) { fprintf(stderr, "discrete integral %f != %f\n", integral, k->integral(parm)); /* not a fatal error */ } /* it all checks out; write the file */ if (!(fout = fopen(outS, "w"))) { fprintf(stderr, "%s: couldn't open \"%s\" for writing\n", me, outS); exit(1); } for (i=0; i<=len-1; i++) { fprintf(fout, "%g %g\n", dom_f[i], ran_f[i]); } fclose(fout); fprintf(stderr, "(for matlab:)\n"); fprintf(stderr, "x = dlmread(\'%s\', \' \'); plot(x(:,1), x(:,2));\n", outS); airMopOkay(mop); exit(0); }
int main(int argc, char *argv[]) { char *me, *kernS[2], *minS, *stepS, *maxS, *outS, *err, kstr[AIR_STRLEN_LARGE]; const NrrdKernel *kern[2]; NrrdKernelSpec *ksp[2]; double parm[NRRD_KERNEL_PARMS_NUM], min, step, max, integral, *dom_d, *ran_d; float *dom_f, *ran_f, val, r_f, r_d; FILE *fout; int i, len; airArray *mop; unsigned int kii; me = argv[0]; if (!( 6 == argc || 7 == argc )) { usage(me); } kernS[0] = argv[1]; minS = argv[2]; stepS = argv[3]; maxS = argv[4]; outS = argv[5]; if (7 == argc) { kernS[1] = argv[6]; } else { kernS[1] = NULL; } if (3 != (sscanf(minS, "%lf", &min) + sscanf(stepS, "%lf", &step) + sscanf(maxS, "%lf", &max))) { fprintf(stderr, "%s: couldn't parse \"%s\", \"%s\", \"%s\" as 3 doubles\n", me, minS, stepS, maxS); exit(1); } mop = airMopNew(); for (kii=0; kii<=(kernS[1] ? 1 : 0); kii++) { if (nrrdKernelParse(&(kern[kii]), parm, kernS[kii])) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: (kii %u) trouble:\n%s\n", me, kii, err); airMopError(mop); exit(1); } ksp[kii] = nrrdKernelSpecNew(); airMopAdd(mop, ksp[kii], (airMopper)nrrdKernelSpecNix, airMopAlways); nrrdKernelSpecSet(ksp[kii], kern[kii], parm); if (nrrdKernelSpecSprint(kstr, ksp[kii])) { airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble:\n%s\n", me, err); airMopError(mop); exit(1); } fprintf(stderr, "%s: printed kernel as \"%s\"\n", me, kstr); if (!( min <= -kern[kii]->support(parm) && max >= kern[kii]->support(parm) )) { fprintf(stderr, "%s: WARNING: support=%g => lower min (%g) or raise max (%g)\n", me, kern[kii]->support(parm), min, max); } fprintf(stderr, "%s: support(%s) = %g\n", me, kstr, kern[kii]->support(parm)); } /* see how many values are in the interval */ len = 0; for (val=min; val<=max; val+=step) { len++; } /* allocate domain and range for both float and double */ if (!( (dom_d = (double *)calloc(len, sizeof(double))) && (ran_d = (double *)calloc(len, sizeof(double))) && (dom_f = (float *)calloc(len, sizeof(float))) && (ran_f = (float *)calloc(len, sizeof(float))) )) { fprintf(stderr, "%s: PANIC: couldn't allocate buffers\n", me); exit(1); } airMopAdd(mop, dom_d, airFree, airMopAlways); airMopAdd(mop, ran_d, airFree, airMopAlways); airMopAdd(mop, dom_f, airFree, airMopAlways); airMopAdd(mop, ran_f, airFree, airMopAlways); /* set values in both domains */ i=0; for (val=min; val<=max; val+=step) { /* note that the value stored in dom_d[i] is only a single-precision float, so that it is really equal to dom_f[i] */ dom_d[i] = val; dom_f[i] = val; i++; } /* do the vector evaluations */ kern[0]->evalN_f(ran_f, dom_f, len, parm); kern[0]->evalN_d(ran_d, dom_d, len, parm); /* do the single evaluations, and make sure everything agrees */ i = 0; integral = 0; for (val=min; val<=max; val+=step) { /* compare two single evaluations */ r_f = kern[0]->eval1_f(val, parm); r_d = kern[0]->eval1_d(val, parm); if (!CLOSE(r_f,r_d, 0.00001)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (eval1_d(%g)== %f)\n", me, val, r_f, val, r_d); } /* compare single float with vector float */ if (!CLOSE(r_f,ran_f[i], 0.00001)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_f[%d]== %f)\n", me, val, r_f, i, ran_f[i]); } /* compare single float with vector double */ r_d = ran_d[i]; if (!CLOSE(r_f,r_d, 0.00001)) { fprintf(stderr, "%s: (eval1_f(%g)== %f) != (evalN_d[%d]== %f)\n", me, val, r_f, i, r_d); } integral += step*ran_d[i]; /* possibly check on given derivatives */ if (kern[1]) { double numd; numd = (kern[0]->eval1_d(val+step/2, parm) - kern[0]->eval1_d(val-step/2, parm))/step; if (!CLOSE(numd, kern[1]->eval1_d(val+step, parm), 0.005)) { fprintf(stderr, "%s: |numerical f'(%g) %g - true %g| = %g > 0.005\n", me, val, numd, kern[1]->eval1_d(val+step, parm), fabs(numd - kern[1]->eval1_d(val+step, parm))); /* exit(1); */ } } i++; } if (!CLOSE(integral, kern[0]->integral(parm), 0.0005)) { fprintf(stderr, "%s: HEY HEY HEY HEY HEY HEY!\n", me); fprintf(stderr, "%s: discrete integral %f != %f\n", me, integral, kern[0]->integral(parm)); /* exit(1); */ } /* it all checks out; write the file */ if (!(fout = airFopen(outS, stdout, "w"))) { fprintf(stderr, "%s: couldn't open \"%s\" for writing\n", me, outS); exit(1); } for (i=0; i<=len-1; i++) { fprintf(fout, "%g %g\n", dom_f[i], ran_f[i]); } fclose(fout); airMopOkay(mop); exit(0); }