## Introduction

We know that there is no general sub-exponential algorithm for computing the permanent of a square matrix. But we may very reasonably ask – might there be a faster, possibly even polynomial-time algorithm, for some specific classes of matrices? For example, a sparse matrix will have most terms of the permanent zero – can this be somehow leveraged for a better algorithm?

The answer seems to be a qualified “yes”. In particular, if a matrix is banded, so that most diagonals are zero, then a very fast algorithm can be applied. This algorithm is described in an online article by Paolo Butera and Mario Pernici called Sums of permanental minors using Grassmann algebra. Accompanying software (Python programs) is available at github. This software has been rewritten for the SageMath system, and you can read about it in the documentation. The algorithm as described by Butera and Pernici, and as implemented in Sage, actually produces a generating function.

Our intention here is to investigate a simpler version, which computes the permanent only.

## Basic outline

Let \(M\) be an \(n\times n\) square matrix, and consider the polynomial ring on \(n\) variables \(x_1,x_2,\ldots,x_n\). Each row of the matrix will correspond to an element of this ring; in particular row \(i\) will correspond to

\[ \sum_{j=1}^nm_{ij}a_j=m_{i1}a_1+m_{i2}a_2+\cdots+m_{in}a_n. \]

Suppose further that all the generating elements \(x_i\) are nilpotent of order two, so that \(x_i^2=0\).

Now if we take all the row polynomials and multiply them, each term of the product will have order \(n\). But by nilpotency, all terms which contain a repeated element will vanish. The result will be only those terms which contain each generator exactly once, of which there will be \(n!\). To obtain the permanent all that is required is to set \(x_i=1\) for each generator.

Here’s an example in Sage.

```
sage: R.<a,b,c,d,e,f,g,h,i,x1,x2,x3> = PolynomialRing(QQbar)
sage: M = matrix([[a,b,c],[d,e,f],[g,h,i]])
sage: X = matrix([[x1],[x2],[x3]])
sage: MX = M*X
[a*x1 + b*x2 + c*x3]
[d*x1 + e*x2 + f*x3]
[g*x1 + h*x2 + i*x3]
```

To implement nilpotency, it’s easiest to reduce modulo the ideal defined by \(x_i^2=0\) for all \(i\). So we take the product of those row elements, and reduce:

```
sage: I = R.ideal([x1^2, x2^2, x3^2])
sage: pr = MX[0,0]*MX[1,0]*MX[2,0]
sage: pr.reduce(I)
c*e*g*x1*x2*x3 + b*f*g*x1*x2*x3 + c*d*h*x1*x2*x3 + a*f*h*x1*x2*x3 + b*d*i*x1*x2*x3 + a*e*i*x1*x2*x3
```

Finally, set each generator equal to 1:

```
sage: pr.reduce(I).subs({x1:1, x2:1, x3:1})
c*e*g + b*f*g + c*d*h + a*f*h + b*d*i + a*e*i
```

and this is indeed the permanent for a general \(3\times 3\) matrix.

## Some experiments

Let’s experiment now with the matrices we’ve seen in a previous post, which contain three consecutive super-diagonals of ones, and the rest zero.

Such a matrix is easy to set up in Sage:

```
sage: n = 10
sage: v = n*[0]; v[1:4] = [1,1,1]
sage: M = matrix.circulant(v)
sage: M
[0 1 1 1 0 0 0 0 0 0]
[0 0 1 1 1 0 0 0 0 0]
[0 0 0 1 1 1 0 0 0 0]
[0 0 0 0 1 1 1 0 0 0]
[0 0 0 0 0 1 1 1 0 0]
[0 0 0 0 0 0 1 1 1 0]
[0 0 0 0 0 0 0 1 1 1]
[1 0 0 0 0 0 0 0 1 1]
[1 1 0 0 0 0 0 0 0 1]
[1 1 1 0 0 0 0 0 0 0]
```

Similarly we can define the polynomial ring:

```
sage: R = PolynomialRing(QQbar,x,n)
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4, x5, x6, x7, x8, x9
```

And now the polynomials corresponding to the rows:

```
sage: MX = M*matrix(R.gens()).transpose()
sage: MX
[x1 + x2 + x3]
[x2 + x3 + x4]
[x3 + x4 + x5]
[x4 + x5 + x6]
[x5 + x6 + x7]
[x6 + x7 + x8]
[x7 + x8 + x9]
[x0 + x8 + x9]
[x0 + x1 + x9]
[x0 + x1 + x2]
```

If we multiply them, we will end up with a huge expression, far too long to display:

```
sage: pr = prod(MX[i,0] for i in range(n))
sage: len(pr.monomials)
14103
```

We *could* reduce this by the ideal, but that would be slow. Far better to
reduce after each separate multiplication:

```
sage: I = R.ideal([v^2 for v in R.gens()])
sage: p = R.one()
sage: for i in range(n):
p = p*MX[i,0]
p = p.reduce(I)
sage: p.subs({v:1 for v in R.gens())
125
```

The answer is almost instantaneous. We can repeat the above list of commands
starting with different values of `n`

; for example with n=20 the result is 15129,
as we expect.

This is not yet optimal; for n=20 on my machine the final loop takes about 7.8
seconds. Butera and Pernici show that the multiplication and setting the
variables to one can sometimes be done in the opposite order; that is, some
variables can be identified to be set to one *before* the multiplication. This
can speed the entire loop dramatically, and this optimization has been included
in the Sage implementation. For details, see their paper.