RobinHankin / mvp

https://robinhankin.github.io/mvp/
8 stars 3 forks source link

qfactorial #65

Open RobinHankin opened 1 year ago

RobinHankin commented 1 year ago

The q-factorial function is defined as follows;

$$\left[n\right]_q=1\cdot(1+q)\cdot(1+q+q^2)\cdots(1+q+\cdots+q^{n-1})$$

It would be nice to implement this in mvp idiom but the best I can come up with is:

 f <- function(n){mvp(rep("q",n),seq_len(n)-1,rep(1,n))}
 Reduce("*",sapply(seq_len(5),f,simplify=FALSE))
mvp object algebraically equal to
1 + 4 q + 9 q^2 + 15 q^3 + 20 q^4 + 22 q^5 + 20 q^6 + 15 q^7 + 9 q^8 + 4 q^9 +q^10

I'm sure there is slicker idiom somewhere for this.