/* nag_real_lu_solve_mult_rhs (f04ajc) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 1, 1990. * Mark 8 revised, 2004. */ #include #include #include #include #include #define A(I, J) a[(I) *tda + J] #define B(I, J) b[(I) *tdb + J] int main(void) { Integer dete, exit_status = 0, i, j, n, nrhs, *pivot = 0, tda, tdb; NagError fail; double *a = 0, *b = 0, detf; INIT_FAIL(fail); printf( "nag_real_lu_solve_mult_rhs (f04ajc) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%ld", &n); nrhs = 1; if (n >= 1) { if (!(a = NAG_ALLOC(n*n, double)) || !(b = NAG_ALLOC(n*nrhs, double)) || !(pivot = NAG_ALLOC(n, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdb = nrhs; } else { printf("Invalid n.\n"); exit_status = 1; goto END; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%lf", &A(i, j)); /* Crout decomposition */ /* 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) { printf("Error from nag_real_lu (f03afc).\n%s\n", fail.message); exit_status = 1; goto END; } for (i = 0; i < n; i++) for (j = 0; j < nrhs; j++) scanf("%lf", &B(i, j)); /* * Approximate solution of linear equations */ /* nag_real_lu_solve_mult_rhs (f04ajc). * Approximate solution of real simultaneous linear * equations (coefficient matrix already factorized by * nag_real_lu (f03afc)) */ nag_real_lu_solve_mult_rhs(n, nrhs, a, tda, pivot, b, nrhs, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_real_lu_solve_mult_rhs (f04ajc).\n%s\n", fail.message); exit_status = 1; goto END; } printf("Solution\n"); for (i = 0; i < n; i++) for (j = 0; j < nrhs; j++) printf("%9.4f\n", B(i, j)); END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (pivot) NAG_FREE(pivot); return exit_status; }