/* nag_2d_spline_fit_grid (e02dcc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * * Mark 6 revised, 2000. * Mark 8 revised, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, mx, my, npx, npy, nx, ny; Nag_2dSpline spline; Nag_Comm warmstartinf; Nag_Start start; double delta, *f = 0, *fg = 0, fp, *px = 0, *py = 0, s, *x = 0, xhi, xlo, *y = 0, yhi; double ylo; NagError fail; INIT_FAIL(fail); /* Initialise spline */ spline.lamda = 0; spline.mu = 0; spline.c = 0; /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); warmstartinf.nag_w = 0; warmstartinf.nag_iw = 0; fprintf(fpout, "nag_2d_spline_fit_grid (e02dcc) Example Program Results\n"); fscanf(fpin, "%*[^\n]"); /* Skip heading in data file */ /* Input the number of x, y co-ordinates mx, my. */ fscanf(fpin, "%ld%ld", &mx, &my); if (mx >= 4 && my >= 4) { if (!(f = NAG_ALLOC(mx*my, double)) || !(x = NAG_ALLOC(mx, double)) || !(y = NAG_ALLOC(my, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid mx or my.\n"); exit_status = 1; return exit_status; } /* Input the x co-ordinates followed by the y co-ordinates. */ for (i = 0; i < mx; i++) fscanf(fpin, "%lf", &x[i]); for (i = 0; i < my; i++) fscanf(fpin, "%lf", &y[i]); /* Input the mx*my function values f at the grid points. */ for (i = 0; i < mx*my; i++) fscanf(fpin, "%lf", &f[i]); start = Nag_Cold; fscanf(fpin, "%lf", &s); /* Determine the spline approximation. */ /* nag_2d_spline_fit_grid (e02dcc). * Least-squares bicubic spline fit with automatic knot * placement, two variables (rectangular grid) */ nag_2d_spline_fit_grid(start, mx, x, my, y, f, s, mx+4, my+4, &fp, &warmstartinf, &spline, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_2d_spline_fit_grid (e02dcc).\n%s\n", fail.message); exit_status = 1; goto END; } nx = spline.nx; ny = spline.ny; fprintf(fpout, "\nCalling with smoothing factor s = %13.4e:" " spline.nx = %2ld, spline.ny = %2ld.\n\n", s, nx, ny); /* Print the knot sets, lamda and mu. */ fprintf(fpout, "Distinct knots in x direction located at\n"); for (j = 3; j < spline.nx-3; j++) fprintf(fpout, "%12.4f%s", spline.lamda[j], ((j-3)%5 == 4 || j == spline.nx-4)?"\n":" "); fprintf(fpout, "\nDistinct knots in y direction located at\n"); for (j = 3; j < spline.ny-3; j++) fprintf(fpout, "%12.4f%s", spline.mu[j], ((j-3)%5 == 4 || j == spline.ny-4) ?"\n":" "); fprintf(fpout, "\nThe B-spline coefficients:\n\n"); for (i = 0; i < ny-4; i++) { for (j = 0; j < nx-4; j++) fprintf(fpout, "%9.4f", spline.c[i+j*(ny-4)]); fprintf(fpout, "\n"); } fprintf(fpout, "\nSum of squared residuals fp = %13.4e\n", fp); if (fp == 0.0) fprintf(fpout, "\nThe spline is an interpolating spline\n"); else if (nx == 8 && ny == 8) fprintf(fpout, "\nThe spline is the least-squares bi-cubic polynomial\n"); /* Evaluate the spline on a rectangular grid at npx*npy points * over the domain (xlo to xhi) x (ylo to yhi). */ fscanf(fpin, "%ld%lf%lf", &npx, &xlo, &xhi); fscanf(fpin, "%ld%lf%lf", &npy, &ylo, &yhi); if (npx >= 1 && npy >= 1) { if (!(fg = NAG_ALLOC(npx*npy, double)) || !(px = NAG_ALLOC(npx, double)) || !(py = NAG_ALLOC(npy, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid npx or npy.\n"); exit_status = 1; return exit_status; } delta = (xhi-xlo) / (npx-1); for (i = 0; i < npx; i++) px[i] = MIN(xlo+i*delta, xhi); for (i = 0; i < npy; i++) py[i] = MIN(ylo+i*delta, yhi); /* nag_2d_spline_eval_rect (e02dfc). * Evaluation of bicubic spline, at a mesh of points */ nag_2d_spline_eval_rect(npx, npy, px, py, fg, &spline, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_2d_spline_eval_rect (e02dfc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nValues of computed spline:\n"); fprintf(fpout, "\n x"); for (i = 0; i < npx; i++) fprintf(fpout, "%7.2f ", px[i]); fprintf(fpout, "\n y\n"); for (i = npy-1; i >= 0; i--) { fprintf(fpout, "%7.2f ", py[i]); for (j = 0; j < npx; ++j) fprintf(fpout, "%8.2f ", fg[npy*j+i]); fprintf(fpout, "\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); NAG_FREE(spline.lamda); NAG_FREE(spline.mu); NAG_FREE(spline.c); NAG_FREE(warmstartinf.nag_w); NAG_FREE(warmstartinf.nag_iw); if (f) NAG_FREE(f); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (fg) NAG_FREE(fg); if (px) NAG_FREE(px); if (py) NAG_FREE(py); return exit_status; }