code: private static int[] elmhes(DenseMatrix A) {
          int n = A.nrows();
          int[] perm = new int[n];
  
          for (int m = 1; m < n - 1; m++) {
              double x = 0.0;
              int i = m;
              for (int j = m; j < n; j++) {
                  if (Math.abs(A.get(j, m - 1)) > Math.abs(x)) {
                      x = A.get(j, m - 1);
                      i = j;
                  }
              }
              perm[m] = i;
              if (i != m) {
                  for (int j = m - 1; j < n; j++) {
                      double swap = A.get(i, j);
                      A.set(i, j, A.get(m, j));
                      A.set(m, j, swap);
                  }
                  for (int j = 0; j < n; j++) {
                      double swap = A.get(j, i);
                      A.set(j, i, A.get(j, m));
                      A.set(j, m, swap);
                  }
              }
              if (x != 0.0) {
                  for (i = m + 1; i < n; i++) {
                      double y = A.get(i, m - 1);
                      if (y != 0.0) {
                          y /= x;
                          A.set(i, m - 1, y);
                          for (int j = m; j < n; j++) {
                              A.sub(i, j, y * A.get(m, j));
                          }
                          for (int j = 0; j < n; j++) {
                              A.add(j, m, y * A.get(j, i));
                          }
                      }
                  }
              }
          }
  
          return perm;
      }
docs: Reduce a real nonsymmetric matrix to upper Hessenberg form.