Matrix Balancing

Let \(A\) be an \(n \times n\) matrix over the integers. For a permutation \(\sigma \in \mathcal{S}_n\), let \(A^\sigma = P_\sigma^\top A P_\sigma\), where \(P_\sigma\) is the permutation matrix corresponding to \(\sigma\).

The balancing index of \(A\) is the least positive sum of coefficients \(c_\sigma\) such that \[\sum_{\sigma \in \mathcal{S}_n} c_\sigma A^\sigma \in \langle I,J \rangle.\]

To compute the balancing index of \(A\), type "bal(A)" using brackets for the matrix, as shown in the example. An optional second boolean argument (False by default) shows detailed output.