/* nag_real_lu (f03afc) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 2 revised, 1992. * Mark 8 revised, 2004. */ #include #include #include #include #include #include #define A(I, J) a[(I) *tda + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer dete, exit_status = 0, i, j, n, *pivot = 0, tda; NagError fail; double *a = 0, detf, two = 2.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_lu (f03afc) 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)) || !(pivot = NAG_ALLOC(n, Integer))) { 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)); /* nag_real_lu (f03afc). * LU factorization and determinant of real matrix */ nag_real_lu(n, a, tda, pivot, &detf, &dete, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_real_lu (f03afc).\n%s\n", fail.message); exit_status = 1; goto END; } else { fprintf(fpout, "Array A after factorization\n"); for (i = 0; i < n; i++) for (j = 0; j < n; j++) fprintf(fpout, "%9.4f%s", A(i, j), (j%8 == 7 || j == n-1)?"\n":" "); fprintf(fpout, "\nArray P\n"); for (i = 0; i < n; i++) fprintf(fpout, "%3ld%s", pivot[i], (i%8 == 7 || i == n-1)?"\n":" "); fprintf(fpout, "\ndetf = %9.4f dete = %2ld\n", detf, dete); detf = detf * pow(two, (double) dete); fprintf(fpout, "\nValue of determinant = %9.4f\n", detf); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (pivot) NAG_FREE(pivot); return exit_status; }