---
title: "Integer sequences from noncommuting multivariate polynomials"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Noncommutative multivariate polynomials

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


```{r kickoff}
library("freealg")
p <- as.freealg("1+x+y")
p^2
```

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`):

```{r haiman}
f <- function(p){constant(as.freealg("x+y+X+Y")^p)}
sapply(0:9,f)
```
It's clear in hindsight that only even $p$ will have nonzero constant:

```{r haiman_even}
sapply(2*(0:5),f)
```

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

```{r general}
g <- function(p,string){constant(as.freealg(string)^p)}
sapply(0:7,g,"1+x+y+X+Y")
```

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

```{r duh}
sapply(0:7,g,"x+y+XY")
```

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

```{r new1}
sapply(3*(0:4),g,"x+y+XY")
```

again not in OEIS.  Or even:

```{r new2}
sapply(3*(0:4),g,"x+y+XY+YX")
```

another new sequence.
