/* nag_sparse_herm_sol (f11jsc) Example Program. * * Copyright 2011, Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include #include int main(void) { /* Scalars */ Integer exit_status = 0; double omega, rnorm, tol; Integer i, itn, maxitn, n, nnz; /* Arrays */ Complex *a = 0, *b = 0, *x = 0; double *rdiag = 0; Integer *icol = 0, *irow = 0; char nag_enum_arg[40]; /* NAG types */ Nag_SparseSym_Method method; Nag_SparseSym_PrecType precon; NagError fail; INIT_FAIL(fail); printf("nag_sparse_herm_sol (f11jsc) Example Program Results\n"); /* Skip heading in data file*/ scanf("%*[^\n]"); /* Read algorithmic parameters*/ scanf("%ld%*[^\n]", &n); scanf("%ld%*[^\n]", &nnz); scanf("%s%*[^\n]", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ method = (Nag_SparseSym_Method) nag_enum_name_to_value(nag_enum_arg); scanf("%s%*[^\n]", nag_enum_arg); precon = (Nag_SparseSym_PrecType) nag_enum_name_to_value(nag_enum_arg); scanf("%lf%*[^\n]", &omega); scanf("%lf%ld%*[^\n]", &tol, &maxitn); /* Allocate memory */ if ( !(a = NAG_ALLOC((nnz), Complex)) || !(b = NAG_ALLOC((n), Complex)) || !(x = NAG_ALLOC((n), Complex)) || !(rdiag = NAG_ALLOC((n), double)) || !(icol = NAG_ALLOC((nnz), Integer)) || !(irow = NAG_ALLOC((nnz), Integer)) ) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read the matrix a */ for (i = 0; i < nnz; i++) scanf(" ( %lf , %lf ) %ld%ld%*[^\n] ", &a[i].re, &a[i].im, &irow[i], &icol[i]); /* Read rhs vector b and initial approximate solution x*/ for (i = 0; i < n; i++) scanf(" ( %lf , %lf ) ", &b[i].re, &b[i].im); scanf("%*[^\n]"); for (i = 0; i < n; i++) scanf(" ( %lf , %lf ) ", &x[i].re, &x[i].im); scanf("%*[^\n]"); /* nag_sparse_herm_sol (f11jsc). * Solution of complex sparse Hermitian linear system, conjugate * gradient/Lanczos method, Jacobi or SSOR preconditioner */ nag_sparse_herm_sol(method, precon, n, nnz, a, irow, icol, omega, b, tol, maxitn, x, &rnorm, &itn, rdiag, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_sparse_herm_sol (f11jsc)\n%s\n", fail.message); exit_status = 1; goto END; } printf("Converged in %10ld iterations \n", itn); printf("Final residual norm = %10.3e\n", rnorm); /* Output x*/ printf("\n Converged Solution\n"); for (i = 0; i < n; i++) printf("(%11.4e, %11.4e)\n", x[i].re, x[i].im); END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (x) NAG_FREE(x); if (rdiag) NAG_FREE(rdiag); if (icol) NAG_FREE(icol); if (irow) NAG_FREE(irow); return exit_status; }