double ErrorTolerance = 0.000001; UINT MaxConvergenceIter = 10000; void LinearSolver::Init(UINT n) { r.Allocate(n); q.Allocate(n); p.Allocate(n); z.Allocate(n); r2.Allocate(n); q2.Allocate(n); p2.Allocate(n); z2.Allocate(n); x.Allocate(n); b.Allocate(n); Temp.Allocate(n); DiagTerms.Allocate(n); M.Init(n); for(UINT i=0;iEntry; if(DiagTerms[i] == 0.0) DiagTerms[i] = 1.0; } else DiagTerms[i] = 1.0; } } void LinearSolver::CGradSolve() { bool Converged = false; double Result, Numerator, Denominator, Alpha, Beta, Error; UINT Iter=0; //p = d Multiply(Temp, M, x); Subtract(r, b, Temp); for(UINT i=0;i " << Error << endl; if(Iter > MaxConvergenceIter || Error < ErrorTolerance) { Converged = true; } } if(Iter > MaxConvergenceIter) cout << "Convergenced not reached; algorithm halted." << endl; else cout << "Convergence, " << Iter << " iterations." << endl; } void LinearSolver::BiCGradSolve(double _BMag) { bool Converged = false; bool First = true; double Beta, Alpha, Row = 0.0, PrevRow, Numerator, Denominator, Error, BMag; UINT Iter=0, N = M.Rows.Length(); LoadDiagTerms(); BMag = Multiply(b, b); if(_BMag != 0.0) BMag = _BMag; if(BMag == 0.0) { cout << "b's zero?" << endl; return; } SparseMatrix M_T; M.Transpose(M_T); //p = d Multiply(Temp, M, x); Subtract(r, b, Temp); for(UINT i=0;i MaxConvergenceIter || Error < ErrorTolerance) Converged = true; } if(Iter > MaxConvergenceIter) { cout << "Convergenced not reached; algorithm halted." << endl; } else { cout << "Convergence, " << Iter << " iterations." << endl; } }