class MatrixUtils {

  public static double computeOrthogonality(DoubleMatrix2D A) {
    double orthogonality = 0;

    // Compute pairwise inner products
    DoubleMatrix2D cosines = A.zMult(A, null, 1, 0, true, false);

    for (int r = 0; r < cosines.rows(); r++) {
      for (int c = r + 1; c < cosines.columns(); c++) {
        orthogonality += cosines.getQuick(r, c);
      }
    }

    return orthogonality / ((cosines.rows() - 1) * cosines.rows() / 2.0);
  }

}