/**
* Solve linear system for a given right-hand side
*/
@Override
public final Array solveFor(final Array rhs) {
final Array result = new Array(size());
final Array tmp = new Array(size());
double bet = diagonal.first();
if (bet == 0.0) throw new IllegalStateException("division by zero");
result.set(0, rhs.first() / bet);
int j;
for (j = 1; j <= size() - 1; j++) {
tmp.set(j, upperDiagonal.get(j - 1) / bet);
bet = diagonal.get(j) - lowerDiagonal.get(j - 1) * tmp.get(j);
if (bet == 0.0) throw new IllegalStateException("division by zero");
result.set(j, (rhs.get(j) - lowerDiagonal.get(j - 1) * result.get(j - 1)) / bet);
}
// cannot be j>=0 with Size j
for (j = size() - 2; j > 0; --j) {
result.set(j, result.get(j) - (tmp.get(j + 1) * result.get(j + 1)));
}
result.set(0, result.first() - (tmp.get(1) * result.get(1)));
return result;
}