public void MakeStep(out int iter, out double residual) { if (!init) { throw new InvalidOperationException("Решатель не инициализирован, выполнение операции невозможно"); } if (currentIter % 20 == 0) { r_prev = A.LSolve(b.Add(A.Multiply(xk), -1), true); r0 = r_prev.Clone(); z = A.USolve(r_prev.Clone(), true); dotproduct_rr = r_prev.DotProduct(r_prev); dotproduct_rprevr0 = dotproduct_rr; } currentIter++; iter = currentIter; LAUz = A.LSolve(A.Multiply(A.USolve(z, true)), true); var alfa = dotproduct_rprevr0 / (LAUz.DotProduct(r0)); var p = r_prev.Add(LAUz, -alfa); LAUp = A.LSolve(A.Multiply(A.USolve(p, true)), true); var gamma = (LAUp.DotProduct(p)) / (LAUp.DotProduct(LAUp)); xk = xk.Add(z, alfa).Add(p, gamma); r = p.Add(LAUp, -gamma); dotproduct_rkr0 = r.DotProduct(r0); var betta = alfa * dotproduct_rkr0 / (gamma * dotproduct_rprevr0); z = r.Add(z, betta).Add(LAUz, -betta * gamma); dotproduct_rprevr0 = dotproduct_rkr0; r_prev = r; dotproduct_rr = r.DotProduct(r); if (Double.IsNaN(alfa) || Double.IsNaN(betta) || Double.IsNaN(gamma)) { residual = -1; return; } x = A.USolve(xk, true); residual = Math.Sqrt(dotproduct_rr) / norm_b; }
public void MakeStep(out int iter, out double residual) { if (!init) { throw new InvalidOperationException("Решатель не инициализирован, выполнение операции невозможно"); } currentIter++; iter = currentIter; if (Factorizer != null) { if (currentIter % 5 == 0) { r_prev = Factorizer.LSolve(b.Add(A.Multiply(xk), -1)); r0 = r_prev.Clone(); z = Factorizer.USolve(r_prev.Clone()); dotproduct_rr = r_prev.DotProduct(r_prev); dotproduct_rprevr0 = dotproduct_rr; } LAUz = Factorizer.LSolve(A.Multiply(Factorizer.USolve(z))); var alpha = dotproduct_rprevr0 / (LAUz.DotProduct(r0)); p = r_prev.Add(LAUz, -alpha); LAUp = Factorizer.LSolve(A.Multiply(Factorizer.USolve(p))); var gamma = (LAUp.DotProduct(p)) / (LAUp.DotProduct(LAUp)); xk = xk.Add(z, alpha).Add(p, gamma); r = p.Add(LAUp, -gamma); dotproduct_rkr0 = r.DotProduct(r0); var beta = alpha * dotproduct_rkr0 / (gamma * dotproduct_rprevr0); z = r.Add(z, beta).Add(LAUz, -beta * gamma); dotproduct_rprevr0 = dotproduct_rkr0; r_prev = r; if (Double.IsNaN(beta) || Double.IsInfinity(beta)) { residual = -1; return; } } else { if (currentIter % 5 == 0) { r_prev = b.Add(A.Multiply(xk), -1); r0 = r_prev.Clone(); z = r_prev.Clone(); dotproduct_rr = r_prev.DotProduct(r_prev); dotproduct_rprevr0 = dotproduct_rr; } Az = A.Multiply(z); var alpha = dotproduct_rprevr0 / (Az.DotProduct(r0)); p = r_prev.Add(Az, -alpha); Ap = A.Multiply(p); var gamma = (Ap.DotProduct(p)) / (Ap.DotProduct(Ap)); xk = xk.Add(z, alpha).Add(p, gamma); r = p.Add(Ap, -gamma); dotproduct_rkr0 = r.DotProduct(r0); var beta = alpha * dotproduct_rkr0 / (gamma * dotproduct_rprevr0); z = r.Add(z, beta).Add(Az, -beta * gamma); dotproduct_rprevr0 = dotproduct_rkr0; r_prev = r; if (Double.IsNaN(beta) || Double.IsInfinity(beta)) { residual = -1; return; } } dotproduct_rr = r.DotProduct(r); residual = Math.Sqrt(dotproduct_rr) / norm_b; }