public override Vector Solve(IPreconditioner matrix, Vector rightPart, Vector initialSolution, ILogger logger, ISolverLogger solverLogger, ISolverParametrs solverParametrs) { JacobiParametrs JacParametrs = solverParametrs as JacobiParametrs; if (JacParametrs != null) { Vector x, r, diagonal; int size, k; double relativeResidual, w, rightPartNorm; Vector Db, Dx, Lx, Ux, DULx; size = initialSolution.Size; x = new Vector(size); r = new Vector(size); diagonal = new Vector(size); x = initialSolution; w = JacParametrs.Relaxation; diagonal = matrix.SourceMatrix.Diagonal; rightPartNorm = rightPart.Norm(); Dx = Vector.Mult(diagonal, x); Lx = matrix.SourceMatrix.LMult(x, false); Ux = matrix.SourceMatrix.UMult(x, false); r = Lx + Dx + Ux - rightPart; relativeResidual = r.Norm() / rightPartNorm; if (System.Double.IsInfinity(relativeResidual)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by Jacobi."); return x; } if (System.Double.IsNaN(relativeResidual)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by Jacobi."); return x; } Db = Vector.Division(rightPart, diagonal); solverLogger.AddIterationInfo(0, relativeResidual); for (k = 1; k <= solverParametrs.MaxIterations && relativeResidual > solverParametrs.Epsilon; k++) { DULx = Vector.Division(Ux + Lx, diagonal); x = (Db - DULx) * w + x * (1 - w); Dx = Vector.Mult(diagonal, x); Lx = matrix.SourceMatrix.LMult(x, false); Ux = matrix.SourceMatrix.UMult(x, false); r = Lx + Dx + Ux - rightPart; relativeResidual = r.Norm() / rightPartNorm; if (System.Double.IsInfinity(relativeResidual)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by Jacobi."); return x; } if (System.Double.IsNaN(relativeResidual)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by Jacobi."); return x; } solverLogger.AddIterationInfo(k, relativeResidual); } return x; } else { logger.Error("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in Jacobi"); throw new Exception("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in Jacobi"); } }
public override Vector Solve(IPreconditioner matrix, Vector rightPart, Vector initialSolution, ILogger logger, ISolverLogger solverLogger, ISolverParametrs solverParametrs) { MSGParametrs ConGradParametrs = solverParametrs as MSGParametrs; if (ConGradParametrs == null) { logger.Error("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in MSG"); throw new Exception("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in MSG"); } else { //prestart int oIter = 0; double alpha, beta, oNev, bNev, scalRO, scaleRN; Vector x = initialSolution, rNew, rOld, z, ap, p; rOld = rightPart - matrix.SourceMatrix.Multiply(x); z = matrix.QSolve(matrix.SSolve(rOld)); p = z; ap = matrix.SourceMatrix.Multiply(p); bNev = rightPart.Norm(); oNev = rOld.Norm() / bNev; scalRO = z * rOld; // x = matrix.QSolve(x); solverLogger.AddIterationInfo(oIter, oNev);//logger if (System.Double.IsInfinity(oNev)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by MSG."); return x; } if (System.Double.IsNaN(oNev)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by MSG."); return x; } while (oIter < ConGradParametrs.MaxIterations && oNev > ConGradParametrs.Epsilon) { alpha = scalRO / (ap * p); x = x + p * alpha; //if (oIter % 100 == 0) //rNew = matrix.QMultiply(rightPart - matrix.SMultiply(matrix.SourceMatrix.Multiply(x))); //else rNew = rOld - ap * alpha; z = matrix.QSolve(matrix.SSolve(rNew)); scaleRN = z * rNew; beta = scaleRN / scalRO; scalRO = scaleRN; p = z + p * beta; ap = matrix.SourceMatrix.Multiply(p); rOld = rNew; oIter++; oNev = rNew.Norm() / bNev; if (System.Double.IsInfinity(oNev)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by MSG."); return x; } if (System.Double.IsNaN(oNev)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by MSG."); return x; } solverLogger.AddIterationInfo(oIter, oNev);//logger } return x; } }
public override Vector Solve(IPreconditioner matrix, Vector rightPart, Vector initialSolution, ILogger logger, ISolverLogger solverLogger, ISolverParametrs solverParametrs) { BCGStabParametrs ConGradParametrs = solverParametrs as BCGStabParametrs; if (ConGradParametrs == null) { logger.Error("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in BSGStab"); throw new Exception("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in BSGSTab"); } else { int oIter = 0; double nPi, oPi, alpha, w, oNev, bNev, betta; oPi = alpha = w = 1; int size = rightPart.Size; Vector x, r, rTab, s, t, z, sqt, y; Vector v = new Vector(size); Vector p = new Vector(size); v.Nullify(); p.Nullify(); x = initialSolution; r = rightPart - matrix.SourceMatrix.Multiply(x); rTab = r; bNev = rightPart.Norm(); oNev = r.Norm() / bNev; solverLogger.AddIterationInfo(oIter, oNev);//logger if (System.Double.IsInfinity(oNev)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by BSG Stab."); return x; } if (System.Double.IsNaN(oNev)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by BSG Stab."); return x; } while (oIter < ConGradParametrs.MaxIterations && oNev > ConGradParametrs.Epsilon) { nPi = rTab * r; betta = (nPi / oPi) * (alpha / w); p = r + (p - v * w) * betta; y= matrix.QSolve(matrix.SSolve(p)); v = matrix.SourceMatrix.Multiply(y); alpha = nPi / (rTab * v); x = x + y * alpha;//УТОЧНИТЬ!!!!!!!!!!!!!!! s = r - v * alpha; if (s.Norm() / bNev < ConGradParametrs.Epsilon) return x; z = matrix.QSolve(matrix.SSolve(s)); t = matrix.SourceMatrix.Multiply(z); sqt = matrix.QSolve(matrix.SSolve(t)); w = (z * sqt) / (sqt * sqt); x = x + z * w;//УТОЧНИТЬ!!!!!!!!!!!!!!!!!!! r = s - t * w; oPi = nPi; oIter++; oNev = r.Norm() / bNev; if (System.Double.IsInfinity(oNev)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by BSG Stab."); return x; } if (System.Double.IsNaN(oNev)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by BSG Stab."); return x; } solverLogger.AddIterationInfo(oIter, oNev);//logger } return x; } }
public override Vector Solve(IPreconditioner matrix, Vector rightPart, Vector initialSolution, ILogger logger, ISolverLogger solverLogger, ISolverParametrs solverParametrs) { if (solverParametrs is GaussSeidelParametrs) { GaussSeidelParametrs GZParametrs = solverParametrs as GaussSeidelParametrs; Vector x,xnext, r, di; int size, k; double Residual, w; Vector DEx,DEb, Dx, Fx, Ex; size = initialSolution.Size; x = new Vector(size); xnext = new Vector(size); r = new Vector(size); di = new Vector(size); x = initialSolution; w = GZParametrs.Relaxation; di = matrix.SourceMatrix.Diagonal; Dx = Vector.Mult(di, x); Fx = matrix.SourceMatrix.LMult(x, false); Ex = matrix.SourceMatrix.UMult(x, false); r = Dx+Fx+ Ex - rightPart; var rpnorm = rightPart.Norm(); Residual = r.Norm() / rpnorm; DEb = Vector.Division(rightPart,di); solverLogger.AddIterationInfo(0, Residual); for (k = 1; k <= GZParametrs.MaxIterations && Residual > GZParametrs.Epsilon; k++) { DEx = Vector.Division(Ex+Fx,di); xnext = (DEb - DEx) * w + x * (1 - w); Ex = matrix.SourceMatrix.UMult(xnext, false); Dx = Vector.Mult(di, xnext); DEx = Vector.Division(Ex + Fx, di); x = xnext; xnext = (DEb - DEx) * w + x * (1 - w); Dx = Vector.Mult(di, xnext); Fx = matrix.SourceMatrix.LMult(xnext, false); r = Dx + Fx + Ex - rightPart; Residual = r.Norm() / rpnorm; if (System.Double.IsInfinity(Residual)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by GaussSeidel ."); return xnext; } if (System.Double.IsNaN(Residual)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by GaussSeidel."); return xnext; } solverLogger.AddIterationInfo(k, Residual); } return xnext; } else { logger.Error("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in GaussSeidel"); throw new Exception("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in GaussSeidel"); } }
public override Vector Solve(IPreconditioner matrix, Vector rightPart, Vector initialSolution, ILogger logger, ISolverLogger solverLogger, ISolverParametrs solverParametrs) { if (solverParametrs is LOSParametrs) { LOSParametrs LOSParametrs = solverParametrs as LOSParametrs; Vector x, r, z, p; double alpha, betta; int size, k; double Residual,pp,rightnorm; Vector Ax; Vector LAU, Ur; size = initialSolution.Size; x = new Vector(size); r = new Vector(size); z = new Vector(size); p = new Vector(size); Ax = new Vector(size); LAU = new Vector(size); Ur = new Vector(size); x = initialSolution; Ax = matrix.SourceMatrix.Multiply(x); r = matrix.SSolve(rightPart - Ax); z = matrix.QSolve(r); p = matrix.SSolve(matrix.SourceMatrix.Multiply(z)); rightnorm = matrix.SSolve(rightPart).Norm(); Residual = r.Norm()/rightnorm; solverLogger.AddIterationInfo(0, Residual); for (k = 1; k <= LOSParametrs.MaxIterations && Residual > LOSParametrs.Epsilon; k++) { pp = p*p; alpha = (p * r) / pp; x = x + z * alpha; r = r - p * alpha; Ur = matrix.QSolve(r); LAU = matrix.SourceMatrix.Multiply(Ur); LAU = matrix.SSolve(LAU); betta = - (p * LAU) / pp; z = Ur + z * betta; p = LAU + p * betta; Residual = r.Norm() / rightnorm; if (System.Double.IsInfinity(Residual)) { logger.Error("Residual is infinity. It is impossible to solve this SLAE by LOS."); return x; } if (System.Double.IsNaN(Residual)) { logger.Error("Residual is NaN. It is impossible to solve this SLAE by LOS."); return x; } solverLogger.AddIterationInfo(k, Residual); } return x; } else { logger.Error("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in GaussSeidel"); throw new Exception("Incorrect " + solverParametrs.GetType().Name.ToString() + " as a SolverParametrs in GaussSeidel"); } }