Noncommutative multivariate polynomials

We consider multivariate polynomials with non-commuting indeterminates, as in the freealg package. For example:

library("freealg")
p <- as.freealg("1+x+y")
p^2
## free algebra element algebraically equal to
##  + 1 + 2*x + 1*xx + 1*xy + 2*y + 1*yx + 1*yy

See how terms xy and yx are retained: variables are not assumed to commute. We can follow Haiman (1993) and consider the expression \[ E(p)=\left(x+y+x^{-1}+y^{-1}\right)^p,\qquad{p\geqslant 0} \]

On the understanding that the variables do not commute, Haiman asks what the constant term of \(E(p)\) is. The package answers that easily (package idiom for \(x^{-1}\) is uppercase X):

f <- function(p){constant(as.freealg("x+y+X+Y")^p)}
sapply(0:9,f)
##  [1]    1    0    4    0   28    0  232    0 2092    0

It’s clear in hindsight that only even \(p\) will have nonzero constant:

sapply(2*(0:5),f)
## [1]     1     4    28   232  2092 19864

This is Sloane’s sequence A035610, http://oeis.org/A035610. We can ask the same question but for different expressions.

g <- function(p,string){constant(as.freealg(string)^p)}
sapply(0:7,g,"1+x+y+X+Y")
## [1]    1    1    5   13   53  181  713 2689

This sequence is not recorded on OEIS. We might also wonder about other expressions:

sapply(0:7,g,"x+y+XY")
## [1]  1  0  0  3  0  0 21  0

this is only nonzero when \(p=0\mod 3\), duh:

sapply(3*(0:4),g,"x+y+XY")
## [1]    1    3   21  183 1773

again not in OEIS. Or even:

sapply(3*(0:4),g,"x+y+XY+YX")
## [1]     1     6    84  1464 28368

another new sequence.