/* nag_sparse_nsym_sol (f11dec) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. */ #include #include #include #include #include int main(void) { double *a=0, *b=0, *x=0; double omega; double rnorm; double tol; Integer *icol=0, *irow=0; Integer i, m, n; Integer maxitn, itn; Integer nnz; Nag_SparseNsym_Method method; Nag_SparseNsym_PrecType precon; Nag_Sparse_Comm comm; char char_enum[20]; Vprintf("nag_sparse_nsym_sol (f11dec) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld%*[^\n]",&n); Vscanf("%ld%*[^\n]",&nnz); Vscanf("%s",char_enum); if (!strcmp(char_enum, "RGMRES")) method = Nag_SparseNsym_RGMRES; else if (!strcmp(char_enum, "CGS")) method = Nag_SparseNsym_CGS; else if (!strcmp(char_enum, "BiCGSTAB")) method = Nag_SparseNsym_BiCGSTAB; else { Vprintf("Unrecognised string for method enum representation.\n"); return EXIT_FAILURE; } Vscanf("%s%*[^\n]",char_enum); if (!strcmp(char_enum, "NoPrec")) precon = Nag_SparseNsym_NoPrec; else if (!strcmp(char_enum, "SSORPrec")) precon = Nag_SparseNsym_SSORPrec; else if (!strcmp(char_enum, "JacPrec")) precon = Nag_SparseNsym_JacPrec; else { Vprintf("Unrecognised string for precon enum representation.\n"); return EXIT_FAILURE; } Vscanf("%lf%*[^\n]",&omega); Vscanf("%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) { Vprintf("Allocation failure\n"); return EXIT_FAILURE; } /* Read the matrix a */ for (i = 1; i <= nnz; ++i) Vscanf("%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) Vscanf("%lf",&b[i-1]); Vscanf("%*[^\n]"); for (i = 1; i <= n; ++i) Vscanf("%lf",&x[i-1]); Vscanf("%*[^\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, NAGERR_DEFAULT); Vprintf("%s%10ld%s\n","Converged in",itn," iterations"); Vprintf("%s%16.3e\n","Final residual norm =",rnorm); /* Output x */ Vprintf(" x\n"); for (i = 1; i <= n; ++i) Vprintf(" %16.6e\n",x[i-1]); NAG_FREE(irow); NAG_FREE(icol); NAG_FREE(a); NAG_FREE(x); NAG_FREE(b); return EXIT_SUCCESS; }