1 | initial version |

Here's a naive answer, as I am somewhat naive (I just posted a question and, while waiting breathlessly for someone to answer it, I thought I'd just have a look at what other people are asking). No idea how efficient it is, but maybe it's a start.

You can construct the ring that you want to work in as a quotient ring of a multivariate polynomial ring. You can then construct the matrix B that you want by multiplying by a diagonal matrix whose entries are the variables you want.

```
n = 4
R = PolynomialRing(QQ, n, 'a')
squared_vars = [t**2 for t in R.gens()]
I = R.ideal(squared_vars)
S = R.quotient_ring(I, 'b')
def nil(A,k):
B = A * diagonal_matrix(S, n, R.gens())
return (B**k).trace()
```

2 | No.2 Revision |

Here's a naive answer, as I am somewhat naive (I just posted a question and, while waiting breathlessly for someone to answer it, I thought I'd just have a look at what other people are asking). No idea how efficient it is, but maybe it's a start.

You can construct the ring that you want to work in as a quotient ring of a multivariate polynomial ring. You can then construct the matrix B that you want by multiplying by a diagonal matrix whose entries are the variables you want.

```
n = 4
R = PolynomialRing(QQ,
```~~n, ~~n+1, 'a')
squared_vars = [t**2 for t in R.gens()]
I = R.ideal(squared_vars)
S = R.quotient_ring(I, 'b')
def nil(A,k):
B = A * diagonal_matrix(S, n, ~~R.gens())
~~list(R.gens())[1:])
return (B**k).trace()

3 | No.3 Revision |

Here's a naive answer, as I am somewhat naive (I just posted a question and, while waiting breathlessly for someone to answer it, I thought I'd just have a look at what other people are asking). No idea how efficient it is, but maybe it's a start.

You can construct the ring that you want to work in as a quotient ring of a multivariate polynomial ring. You can then construct the matrix B that you want by multiplying by a diagonal matrix whose entries are the variables you want.

```
n = 4
R = PolynomialRing(QQ, n+1, 'a')
squared_vars = [t**2 for t in R.gens()]
I = R.ideal(squared_vars)
S = R.quotient_ring(I, 'b')
def nil(A,k):
B = A * diagonal_matrix(S, n, list(R.gens())[1:])
return (B**k).trace()
```

Edit: It turns out that OP also wants to compute the sum of the coefficients. I found it kind of tricky to compute do, largely because the quotient ring classes seem not to have a lot of the methods you'd expect. However it's possible to lift an element of the quotient ring to the obvious preimage in the original ring, and since the original ring is a plain old multivariate polynomial ring, you can do all the things you'd normally do in it, like "subs". Naturally substituting 1 for all the variables computes the sum of the coefficients:

```
def sum_of_coeffs(x):
return x.lift().subs({t:1 for t in R.gens()})
```

so, for instance, you could do

```
sum_of_coeffs(nil(A,2))
```

which hopefully should produce the answer 10 when you run it with OP's example, for instance. This was a great question - at least, I feel like I learned a lot... :)

4 | No.4 Revision |

```
n = 4
R = PolynomialRing(QQ, n+1, 'a')
squared_vars = [t**2 for t in R.gens()]
I = R.ideal(squared_vars)
S = R.quotient_ring(I, 'b')
def nil(A,k):
B = A * diagonal_matrix(S, n, list(R.gens())[1:])
return (B**k).trace()
```

Edit: It turns out that OP also wants to compute the sum of the coefficients. I found it kind of tricky to compute ~~do, ~~this, largely because the quotient ring classes seem not to have a lot of the methods you'd expect. However it's possible to lift an element of the quotient ring to the obvious preimage in the original ring, and since the original ring is a plain old multivariate polynomial ring, you can do all the things you'd normally do in it, like "subs". Naturally substituting 1 for all the variables computes the sum of the coefficients:

```
def sum_of_coeffs(x):
return x.lift().subs({t:1 for t in R.gens()})
```

so, for instance, you could do

```
sum_of_coeffs(nil(A,2))
```

which hopefully should produce the answer 10 when you run it with OP's example, for instance. This was a great question - at least, I feel like I learned a lot... :)

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.