/* nag_real_lin_eqn (f04arc) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 2 revised, 1992. * Mark 8 revised, 2004. */ #include #include #include #include #include #define A(I, J) a[(I) *tda + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, n, tda; NagError fail; double *a = 0, *b = 0, *x = 0; INIT_FAIL(fail); /* 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_real_lin_eqn (f04arc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); if (n >= 1) { if (!(a = NAG_ALLOC(n*n, double)) || !(b = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tda = n; } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) fscanf(fpin, "%lf", &A(i, j)); for (i = 0; i < n; i++) fscanf(fpin, "%lf", &b[i]); /* nag_real_lin_eqn (f04arc). * Approximate solution of real simultaneous linear * equations, one right-hand side */ nag_real_lin_eqn(n, a, tda, b, x, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_lin_eqn (f04arc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Solution\n"); for (i = 0; i < n; i++) fprintf(fpout, "%9.4f\n", x[i]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (x) NAG_FREE(x); return exit_status; }