Long time listener, first time caller here. I am experimenting with matrices and want to know the number of valid combinations for a binary {0,1} $n \times n$ matrix based on density\sparsity. Combinatorics seems like it might provide a calculation method, but the inclusion-exclusion calculations are new to me. I have a thorough understanding of set theory and basics in combination/permutation calculations.
This post talks about a {0,1} $m \times n$ matrix that has no empty rows or columns. (Moreover, every row and column has at least one 1) Brian M. Scott concludes with an inclusion-exclusion calculation for a sum of possible valid combinations for an $m \times n$ matrix. Note: Because my matrix is square, I've replace $m$ with $n$.
I confirmed the first few cases with my own inspection and this works for $n=1, 2, 3$. $$ \sum_{k=0}^n\binom{n}k(-1)^k\left(2^{n-k}-1\right)^n\tag{1} $$
Another post talks about a similar {0,1} $m \times n$ matrix but calculates with a restriction number $k$ of 1s contained within. It also has the same condition that every row and column have at least one 1. 'bof' gives an inclusion-exclusion calculation, I have substituted $n$ for $m$ (this might be invalid?), and have confirmed upon inspection that the calculation is correct for $n=1,2,3$.
$$ \sum_{u=0}^n\sum_{v=0}^n(-1)^{u+v}\binom nu\binom nv\binom{(n-u)(n-v)}k\tag{2} $$
and 'bof' concludes with $$ \sum_{r=0}^n\sum_{s=0}^n(-1)^{2n+r+s}\binom nr\binom ns\binom{rs}k.\tag{3} $$
Here is the interesting part, summing $(3)$ from $k=0$ to $n$ should reproduce the calculation found in $(1)$.
And it does for $n=1,2,3,4$.
But once $n\geq5$, the calculations differ!
The following are tables of my results:
$n=1$ is trivial
$n=2$
| k | 0 | 1 | 2 | 3 | 4 | sum | $(1)$ | difference |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 2 | 4 | 1 | 7 | 7 | 0 |
$n=3$
| k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | sum | $(1)$ | difference |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 6 | 45 | 90 | 78 | 36 | 9 | 1 | 265 | 265 | 0 |
$n=4$
| k | 0 | 1 | 2 | 3 | 4 | 5 | ~ | 12 | 13 | 14 | 15 | 16 | sum | $(1)$ | difference |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 24 | 432 | ~ | 1812 | 560 | 120 | 16 | 1 | 41503 | 41503 | 0 |
$n=5$
| k | 0 | 1 | 2 | 3 | 4 | 5 | ~ | 21 | 22 | 23 | 24 | 25 | sum | $(1)$ | difference |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 120 | ~ | 12650 | 2300 | 300 | 25 | 1 | 24997921 | 24997916 | 5 |
$n=6$
| k | ~ | 5 | 6 | 7 | 8 | 9 | ~ | 32 | 33 | 34 | 35 | 36 | sum | $(1)$ | difference |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ~ | 0 | 720 | 43200 | 755100 | 6700500 | ~ | 58905 | 7140 | 630 | 36 | 1 | 57366997447 | 57366986518 | 10929 |
There is some discrepancy between $(1)$ and summing the parts of $(3)$ when $n>=5$.
Any insight on if I've made a mistaken substitution, I've used the calculations incorrectly, or something else missed would be immensely helpful.
$n$=1,2,3yields a very ugly "$n$=1,2,3", while$n=1,2,3$yields a much nicer "$n=1,2,3$". You might also be interested in the MathJax tutorial – 5xum Apr 13 '22 at 06:46int's have infinite precision. If you only use double precision, you can get overflow, and if you use floats, you can get rounding error. This Python program computes the $n=5$ case correctly. I can't comment on what is wrong with your program, since you haven't said what program you used or shared your code. – Mike Earnest Apr 13 '22 at 19:04