/* nag_sparse_sym_sol (f11jec) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * */ #include #include #include #include #include /* f11jec Example Program Text */ int main(void) { double *a=0, *b=0, *x=0; double omega; double rnorm; double tol; Integer *icol, *irow; Integer i, n, maxitn, itn, nnz; Nag_SparseSym_Method method; Nag_SparseSym_PrecType precon; Nag_Sparse_Comm comm; char char_enum[20]; Vprintf("f11jec Example Program Results\n"); /* Skip heading in data file */ Vscanf(" %*[^\n]"); /* Read algorithmic parameters */ Vscanf("%ld%*[^\n]",&n); Vscanf("%ld%*[^\n]",&nnz); Vscanf("%s",char_enum); if (!strcmp(char_enum, "CG")) method = Nag_SparseSym_CG; else if (!strcmp(char_enum, "Lanczos")) method = Nag_SparseSym_Lanczos; else { Vprintf("Unrecognised string for method enum representation.\n"); return EXIT_FAILURE; } Vscanf("%s%*[^\n]",char_enum); if (!strcmp(char_enum, "Prec")) precon = Nag_SparseSym_Prec; else if (!strcmp(char_enum, "NoPrec")) precon = Nag_SparseSym_NoPrec; else if (!strcmp(char_enum, "SSORPrec")) precon = Nag_SparseSym_SSORPrec; else if (!strcmp(char_enum, "JacPrec")) precon = Nag_SparseSym_JacPrec; else { Vprintf("Unrecognised string for precon enum representation.\n"); return EXIT_FAILURE; } Vscanf("%lf%*[^\n]",&omega); Vscanf("%lf%ld%*[^\n]",&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 */ f11jec(method, precon, n, nnz, a, irow, icol, omega, b, 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 */ for (i = 1; i <= n; ++i) Vprintf(" %16.4e\n",x[i-1]); NAG_FREE(irow); NAG_FREE(icol); NAG_FREE(a); NAG_FREE(x); NAG_FREE(b); return EXIT_SUCCESS; }