/* nag_2d_spline_eval_rect (e02dfc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 8 revised, 2004. */ #include #include #include #include #include #define FF(I, J) ff[my*(I)+(J)] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, mx, my; NagError fail; Nag_2dSpline spline; double *ff = 0, *x = 0, *y = 0; 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); fprintf(fpout, "nag_2d_spline_eval_rect (e02dfc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); /* Read mx and my, the number of grid points in the x and y * directions respectively. */ fscanf(fpin, "%ld%ld", &mx, &my); if (mx >= 1 && my >= 1) { if (!(x = NAG_ALLOC(mx, double)) || !(y = NAG_ALLOC(my, double)) || !(ff = NAG_ALLOC(mx*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; } /* Read spline.nx and spline.ny, the number of knots * in the x and y directions. */ fscanf(fpin, "%ld%ld", &(spline.nx), &(spline.ny)); if (!(spline.c = NAG_ALLOC((spline.nx-4)*(spline.ny-4), double)) || !(spline.lamda = NAG_ALLOC(spline.nx, double)) || !(spline.mu = NAG_ALLOC(spline.ny, double))) { fprintf(fpout, "Storage allocation failed.\n"); exit_status = -1; goto END; } /* Read the knots spline.lamda[0]...spline.lamda[nx-1] * and spline.mu[0]...spline.mu[ny-1]. */ for (i = 0; i < spline.nx; i++) fscanf(fpin, "%lf", &(spline.lamda[i])); for (i = 0; i < spline.ny; i++) fscanf(fpin, "%lf", &(spline.mu[i])); /* Read spline.c, the bicubic spline coefficients. */ for (i = 0; i < (spline.nx-4)*(spline.ny-4); i++) fscanf(fpin, "%lf", &(spline.c[i])); /* Read the x and y co-ordinates defining the evaluation grid. */ for (i = 0; i < mx; i++) fscanf(fpin, "%lf", &x[i]); for (i = 0; i < my; i++) fscanf(fpin, "%lf", &y[i]); /* Evaluate the spline at the mx by my points. */ /* nag_2d_spline_eval_rect (e02dfc). * Evaluation of bicubic spline, at a mesh of points */ nag_2d_spline_eval_rect(mx, my, x, y, ff, &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; } /* Print the result array. */ fprintf(fpout, "Spline evaluated on x-y grid (x across, y down):\n "); for (i = 0; i < mx; i++) fprintf(fpout, "%2.1f ", x[i]); fprintf(fpout, "\n"); for (j = 0; j < my; j++) { fprintf(fpout, "%2.1f", y[j]); for (i = 0; i < mx; i++) fprintf(fpout, "%8.3f%s", FF(i, j), (i%7 == 6 || i == mx-1)?"\n":" "); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); NAG_FREE(spline.c); NAG_FREE(spline.lamda); NAG_FREE(spline.mu); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (ff) NAG_FREE(ff); return exit_status; }