code: private static double[] balance(DenseMatrix A) {
          double sqrdx = Math.RADIX * Math.RADIX;
  
          int n = A.nrows();
  
          double[] scale = new double[n];
          for (int i = 0; i < n; i++) {
              scale[i] = 1.0;
          }
  
          boolean done = false;
          while (!done) {
              done = true;
              for (int i = 0; i < n; i++) {
                  double r = 0.0, c = 0.0;
                  for (int j = 0; j < n; j++) {
                      if (j != i) {
                          c += Math.abs(A.get(j, i));
                          r += Math.abs(A.get(i, j));
                      }
                  }
                  if (c != 0.0 && r != 0.0) {
                      double g = r / Math.RADIX;
                      double f = 1.0;
                      double s = c + r;
                      while (c < g) {
                          f *= Math.RADIX;
                          c *= sqrdx;
                      }
                      g = r * Math.RADIX;
                      while (c > g) {
                          f /= Math.RADIX;
                          c /= sqrdx;
                      }
                      if ((c + r) / f < 0.95 * s) {
                          done = false;
                          g = 1.0 / f;
                          scale[i] *= f;
                          for (int j = 0; j < n; j++) {
                              A.mul(i, j, g);
                          }
                          for (int j = 0; j < n; j++) {
                              A.mul(j, i, f);
                          }
                      }
                  }
              }
          }
  
          return scale;
      }
docs: Given a square matrix, this routine replaces it by a balanced matrix with
  identical eigenvalues. A symmetric matrix is already balanced and is
  unaffected by this procedure.