class LUDecomposition implements AutoCloseable {

    public Vector solve(Vector b) {
        final int m = pivot.size();

        if (b.size() != m)
            throw new CardinalityException(b.size(), m);

        if (singular)
            throw new SingularMatrixException();

        final double[] bp = new double[m];

        // Apply permutations to b
        for (int row = 0; row < m; row++)
            bp[row] = b.get((int)pivot.get(row));

        // Solve LY = b
        for (int col = 0; col < m; col++) {
            final double bpCol = bp[col];

            for (int i = col + 1; i < m; i++)
                bp[i] -= bpCol * lu.get(i, col);
        }

        // Solve UX = Y
        for (int col = m - 1; col >= 0; col--) {
            bp[col] /= lu.get(col, col);
            final double bpCol = bp[col];

            for (int i = 0; i < col; i++)
                bp[i] -= bpCol * lu.get(i, col);
        }

        return b.like(m).assign(bp);
    }

    public  LUDecomposition(Matrix matrix);
    public  LUDecomposition(Matrix matrix, double singularityThreshold);

}