code: public static EntropyStats entropy(Matrix m) {
          if (m instanceof SparseMatrix)
              return entropy((SparseMatrix) m);
  
          double sum = 0;
          double[] colSums = new double[m.columns()];
          double[] rowSums = new double[m.rows()];
          for (int r = 0; r < m.rows(); ++r) {
              for (int c = 0; c < m.columns(); ++c) {
                  double v = m.get(r, c);
                  sum += v;
                  colSums[c] += v;
                  rowSums[r] += v;
              }
          }
  
          double entropy = 0;
          double[] colEntropy = new double[m.columns()];
          double[] rowEntropy = new double[m.rows()];
          for (int r = 0; r < m.rows(); ++r) {
              for (int c = 0; c < m.columns(); ++c) {
                  double v = m.get(r, c);
                  if (v != 0d) {
                      entropy -= entropy(v, sum);
                      colEntropy[c] -= entropy(v, colSums[c]);
                      rowEntropy[r] -= entropy(v, rowSums[r]);
                  }
              }
          }
          return new EntropyStats(entropy, colEntropy, rowEntropy);
      }
docs: Returns a {@link EntropyStats} covering the entropies of all joint events,
  rows, and columns in {@code m}.