/* nag_real_cholesky_solve_mult_rhs (f04agc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * 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] #define X(I, J) x[(I) *tdx + J] int main(void) { Integer exit_status = 0, i, id, j, n, nrhs, tda, tdb, tdx; NagError fail; double *a = 0, *b = 0, d1, *p = 0, *x = 0; INIT_FAIL(fail); printf("nag_real_cholesky_solve_mult_rhs (f04agc) 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)) || !(p = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n*nrhs, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdb = nrhs; tdx = nrhs; } else { printf("Invalid n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) scanf("%lf", &A(i, j)); for (i = 0; i < n; ++i) for (j = 0; j < nrhs; ++j) scanf("%lf", &B(i, j)); /* Cholesky decomposition */ /* nag_real_cholesky (f03aec). * LL^T factorization and determinant of real symmetric * positive-definite matrix */ nag_real_cholesky(n, a, tda, p, &d1, &id, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_real_cholesky (f03aec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Approximate solution of linear equations */ /* nag_real_cholesky_solve_mult_rhs (f04agc). * Approximate solution of real symmetric positive-definite * simultaneous linear equations (coefficient matrix already * factorized by nag_real_cholesky (f03aec)) */ nag_real_cholesky_solve_mult_rhs(n, nrhs, a, tda, p, b, tdb, x, tdx, &fail); if (fail.code != NE_NOERROR) { printf( "Error from nag_real_cholesky_solve_mult_rhs (f04agc).\n%s\n", fail.message); exit_status = 1; goto END; } printf("\n Solution\n"); for (i = 0; i < n; ++i) { for (j = 0; j < nrhs; ++j) printf("%9.4f", X(i, j)); printf("\n"); } END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (p) NAG_FREE(p); if (x) NAG_FREE(x); return exit_status; }