/* nag_2d_spline_eval_rect (e02dfc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 8 revised, 2004. */ #include #include #include #include #define FF(I, J) ff[my*(I)+(J)] int main(void) { 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; printf("nag_2d_spline_eval_rect (e02dfc) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n]"); /* Read mx and my, the number of grid points in the x and y * directions respectively. */ scanf("%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))) { printf("Allocation failure\n"); exit_status = -1; goto END; } } else { printf("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. */ scanf("%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))) { printf("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++) scanf("%lf", &(spline.lamda[i])); for (i = 0; i < spline.ny; i++) scanf("%lf", &(spline.mu[i])); /* Read spline.c, the bicubic spline coefficients. */ for (i = 0; i < (spline.nx-4)*(spline.ny-4); i++) scanf("%lf", &(spline.c[i])); /* Read the x and y co-ordinates defining the evaluation grid. */ for (i = 0; i < mx; i++) scanf("%lf", &x[i]); for (i = 0; i < my; i++) scanf("%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) { printf("Error from nag_2d_spline_eval_rect (e02dfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the result array. */ printf("Spline evaluated on x-y grid (x across, y down):\n "); for (i = 0; i < mx; i++) printf("%2.1f ", x[i]); printf("\n"); for (j = 0; j < my; j++) { printf("%2.1f", y[j]); for (i = 0; i < mx; i++) printf("%8.3f%s", FF(i, j), (i%7 == 6 || i == mx-1)?"\n":" "); } END: 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; }