/* nag_sparse_sym_precon_ichol_solve (f11jbc) Example Program. * * Copyright 2011, Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include int main(void) { /* Scalars */ Integer exit_status = 0; double dscale, dtol; Integer i, la, lfill, n, nnz, nnzc, npivm; /* Arrays */ double *a = 0, *x = 0, *y = 0; Integer *icol = 0, *ipiv = 0, *irow = 0, *istr = 0; /* NAG types */ Nag_SparseSym_Fact mic; Nag_SparseSym_Piv pstrat; Nag_SparseSym_CheckData check; Nag_Sparse_Comm comm; NagError fail; INIT_FAIL(fail); printf("nag_sparse_sym_precon_ichol_solve (f11jbc) Example Program Results"); printf("\n"); /* Skip heading in data file*/ scanf("%*[^\n]"); /* Read order of matrix and number of non-zero entries*/ scanf("%ld%*[^\n]", &n); scanf("%ld%*[^\n]", &nnz); /* Allocate memory */ la = 3 * nnz; if ( !(a = NAG_ALLOC(la, double)) || !(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(icol = NAG_ALLOC(la, Integer)) || !(ipiv = NAG_ALLOC(n, Integer)) || !(irow = NAG_ALLOC(la, Integer)) || !(istr = NAG_ALLOC(n + 1, Integer)) ) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read the matrix A*/ for (i = 0; i < nnz; i++) scanf("%lf%ld%ld%*[^\n]", &a[i], &irow[i], &icol[i]); /* Read the vector y*/ for (i = 0; i < n ; i++) scanf("%lf", &y[i]); lfill = -1; dtol = 0.0; dscale = 0.0; mic = Nag_SparseSym_UnModFact; pstrat = Nag_SparseSym_MarkPiv; /* Calculate Cholesky factorization using * nag_sparse_sym_chol_fac (f11jac). */ nag_sparse_sym_chol_fac(n, nnz, &a, &la, &irow, &icol, lfill, dtol, mic, dscale, pstrat, ipiv, istr, &nnzc, &npivm, &comm, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_sparse_sym_chol_fac (f11jac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Check the output value of npivm */ if (npivm != 0) printf("Factorization is not complete \n"); else { /* Solve linear system involving incomplete Cholesky factorization * * T T * P L D L P x = y * * using nag_sparse_sym_precon_ichol_solve (f11jbc). */ check = Nag_SparseSym_Check; nag_sparse_sym_precon_ichol_solve(n, a, la, irow, icol, ipiv, istr, check, y, x, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_sparse_sym_precon_ichol_solve (f11jbc).\n%s\n", fail.message); exit_status = 2; goto END; } /* Output results*/ printf(" Solution of linear system \n"); for (i = 0; i < n; i++) printf("%16.4e\n", x[i]); printf("\n"); } END: if (a) NAG_FREE(a); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (icol) NAG_FREE(icol); if (ipiv) NAG_FREE(ipiv); if (irow) NAG_FREE(irow); if (istr) NAG_FREE(istr); return exit_status; }