code: public void solve(DenseMatrix B) {
          if (B.nrows() != qr.nrows()) {
              throw new IllegalArgumentException(String.format("Row dimensions do not agree: A is %d x %d, but B is %d x %d", qr.nrows(), qr.nrows(), B.nrows(), B.ncols()));
          }
  
          if (singular) {
              throw new RuntimeException("Matrix is rank deficient.");
          }
  
          int m = qr.nrows();
          int n = qr.ncols();
          int nrhs = B.ncols();
  
          // Compute Y = Q' * B
          for (int k = 0; k < n; k++) {
              for (int j = 0; j < nrhs; j++) {
                  double s = 0.0;
                  for (int i = k; i < m; i++) {
                      s += qr.get(i, k) * B.get(i, j);
                  }
                  s = -s / qr.get(k, k);
                  for (int i = k; i < m; i++) {
                      B.add(i, j, s * qr.get(i, k));
                  }
              }
          }
  
          // Solve R*X = Y;
          for (int k = n - 1; k >= 0; k--) {
              for (int j = 0; j < nrhs; j++) {
                  B.set(k, j, B.get(k, j) / tau[k]);
              }
              
              for (int i = 0; i < k; i++) {
                  for (int j = 0; j < nrhs; j++) {
                      B.sub(i, j, B.get(k, j) * qr.get(i, k));
                  }
              }
          }
      }
docs: Solve the least squares A * X = B. B will be overwritten with the solution
  matrix on output.
  @param B    right hand side of linear system. B will be overwritten with
  the solution matrix on output.
  @exception  RuntimeException  Matrix is rank deficient.