@Override public Matrix run(Matrix coef, Matrix free) { int n = coef.getHeight(); double[][] alpha = new double[n][n]; double[][] beta = new double[n][1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { alpha[i][j] = (i == j ? 0. : -coef.get(i, j) / coef.get(i, i)); } beta[i][0] = free.get(i, 0) / coef.get(i, i); } Matrix alphaMatrix = new Matrix(alpha); Matrix betaMatrix = new Matrix(beta); alphaNorm = alphaMatrix.getNorm(); Matrix cur = new Matrix(approximate); Matrix prev; iteration = 0; do { iteration++; prev = new Matrix(cur); cur = alphaMatrix.multiply(prev).add(betaMatrix); } while (check(cur, prev) && iteration < allowedIterations); return new Matrix(cur); }
private boolean check(Matrix cur, Matrix prev) { return cur.subtract(prev).getVectorNorm() > (1 - alphaNorm) * eps / alphaNorm; }