/* nag_sparse_nsym_sol (f11dec) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; double *a = 0, *b = 0, *x = 0; double omega; double rnorm; double tol; Integer exit_status = 0; Integer *icol = 0, *irow = 0; Integer i, m, n; Integer maxitn, itn; Integer nnz; char nag_enum_arg[40]; Nag_SparseNsym_Method method; Nag_SparseNsym_PrecType precon; Nag_Sparse_Comm comm; NagError fail; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_sparse_nsym_sol (f11dec) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld%*[^\n]", &n); fscanf(fpin, "%ld%*[^\n]", &nnz); fscanf(fpin, "%s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ method = (Nag_SparseNsym_Method) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%s%*[^\n]", nag_enum_arg); precon = (Nag_SparseNsym_PrecType) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%lf%*[^\n]", &omega); fscanf(fpin, "%ld%lf%ld%*[^\n]", &m, &tol, &maxitn); x = NAG_ALLOC(n, double); b = NAG_ALLOC(n, double); a = NAG_ALLOC(nnz, double); irow = NAG_ALLOC(nnz, Integer); icol = NAG_ALLOC(nnz, Integer); if (!irow || !icol || !a || !x || !b) { fprintf(fpout, "Allocation failure\n"); exit_status = 1; goto END; } /* Read the matrix a */ for (i = 1; i <= nnz; ++i) fscanf(fpin, "%lf%ld%ld%*[^\n]", &a[i-1], &irow[i-1], &icol[i-1]); /* Read right-hand side vector b and initial approximate solution x */ for (i = 1; i <= n; ++i) fscanf(fpin, "%lf", &b[i-1]); fscanf(fpin, "%*[^\n]"); for (i = 1; i <= n; ++i) fscanf(fpin, "%lf", &x[i-1]); fscanf(fpin, "%*[^\n]"); /* Solve Ax = b using nag_sparse_nsym_sol (f11dec) */ /* nag_sparse_nsym_sol (f11dec). * Solver with no Jacobi/SSOR preconditioning (nonsymmetric) */ nag_sparse_nsym_sol(method, precon, n, nnz, a, irow, icol, omega, b, m, tol, maxitn, x, &rnorm, &itn, &comm, &fail); fprintf(fpout, "%s%10ld%s\n", "Converged in", itn, " iterations"); fprintf(fpout, "%s%16.3e\n", "Final residual norm =", rnorm); /* Output x */ fprintf(fpout, " x\n"); for (i = 1; i <= n; ++i) fprintf(fpout, " %16.6e\n", x[i-1]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (irow) NAG_FREE(irow); if (icol) NAG_FREE(icol); if (a) NAG_FREE(a); if (x) NAG_FREE(x); if (b) NAG_FREE(b); return exit_status; }