/* nag_real_cholesky (f03aec) 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] int main(void) { Integer dete, exit_status = 0, i, j, n, tda; NagError fail; double *a = 0, determ, detf, *p = 0; INIT_FAIL(fail); printf("nag_real_cholesky (f03aec) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%" NAG_IFMT "\n", &n); if (n >= 1) { if (!(a = NAG_ALLOC(n*n, double)) || !(p = NAG_ALLOC(n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } tda = n; } 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)); /* nag_real_cholesky (f03aec). * LL^T factorization and determinant of real symmetric * positive-definite matrix */ nag_real_cholesky(n, a, tda, p, &detf, &dete, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_real_cholesky (f03aec).\n%s\n", fail.message); exit_status = 1; goto END; } printf("Array A after factorization\n"); for (i = 0; i < n; i++) for (j = 0; j < n; j++) printf("%9.4f%s", A(i, j), (j%8 == 7 || j == n-1)?"\n":" "); printf("\nArray p\n"); for (i = 0; i < n; i++) printf("%9.4f%s", p[i], (i%8 == 7 || i == n-1)?"\n":" "); printf("\ndetf = %9.4f dete = %2" NAG_IFMT "\n\n", detf, dete); determ = detf*pow(2.0, (double) dete); printf("Value of determinant = %9.4f\n", determ); END: if (a) NAG_FREE(a); if (p) NAG_FREE(p); return exit_status; }