@Override public MySparseVector iterationStep(MySparseVector x, MySparseMatrix m, MySparseVector b) { int n = x.getSize(); List<MatrixItem> data = m.getData(); MySparseVector xnew = new MySparseVector(n); int row = 0; double rowsum = 0; double diag = 0; double value = 0; for (MatrixItem i : data) { if (i.getRow() != row) { value = (b.getValue(row) - rowsum) * (1 / diag); xnew.setValue(row, value); row = i.getRow(); rowsum = 0; } if (i.getCol() == row) diag = i.getValue(); else rowsum += i.getValue() * x.getValue(i.getCol()); } value = (b.getValue(row) - rowsum) * (1 / diag); xnew.setValue(row, value); return xnew; }
public MySparseVector solve(MySparseMatrix a, MySparseVector b, MySparseVector x0) { residuumNorms = new ArrayList<Double>(); qs = new ArrayList<Double>(); List<MySparseVector> xns = new ArrayList<MySparseVector>(); MySparseVector x = x0.clone(); MySparseVector resVector = b.substract(a.multiple(x)); residuumNorms.add(resVector.norm()); xns.add(x); int i = 0; boolean needMoreStep = true; while (i < numberOfSteps && needMoreStep) { x = iterationStep(x, a, b); xns.add(x); resVector = b.substract(a.multiple(x)); residuumNorms.add(resVector.norm()); if (i > 2) { xns.remove(0); double szamlalo = x.substract(xns.get(1)).norm(); double nevezo = xns.get(1).substract(xns.get(0)).norm(); if (nevezo == 0.0) { needMoreStep = false; } else { double q = szamlalo / nevezo; qs.add(q); if (i > 5 && q > 1.5) { needMoreStep = false; } } } ++i; } lastStepNumber = i; return x; }