public static boolean isOrthonormal(double[][] qt, boolean insufficientRank, double epsilon) {
    int n = qt.length;
    int rank = 0;
    for (int i = 0; i < n; i++) {
      Vector ei = new DenseVector(qt[i], true);

      double norm = ei.norm(2);

      if (Math.abs(1.0 - norm) < epsilon) {
        rank++;
      } else if (Math.abs(norm) > epsilon) {
        return false; // not a rank deficiency, either
      }

      for (int j = 0; j <= i; j++) {
        Vector ej = new DenseVector(qt[j], true);
        double dot = ei.dot(ej);
        if (!(Math.abs((i == j && rank > j ? 1.0 : 0.0) - dot) < epsilon)) {
          return false;
        }
      }
    }
    return insufficientRank ? rank < n : rank == n;
  }