Change of basis of polynomials

Suppose I have a favourite basis for polynomials in $x_1,\dotsc,x_n$, say non-symmetric Macdonald polynomials to be specific. I can easily compute these, and thus the change-of-basis matrix that takes me from the monomial basis to this basis.

I store this change-of-basis as a memoized, dynamically generated list of substitution rules, one list for each $(n,d)$ where $d$ is the degree.

The Macdonald polynomials also depend on two additional variables, $q$ and $t$. My code is something as follows (bas is an additional parameter that determines the $n$ among other things) and CompositionIndexedBasisRule is a method that actually computes the change-of-basis rule, given a function indexed by a weak compositions that generate the polynomial basis (the coefficients are rational functions in $q,,t$).

MacdonaldERule[size_, bas_, x_, q_, t_, aa_] :=
MacdonaldERule[size, bas, x, aa] =
CompositionIndexedBasisRule[size, Length@bas, x, aa,
MacdonaldEPolynomial[#, bas, x, q, t] &];

ToMacdonaldBasis[bas_List, x_, q_, t_, aa_][polIn_] :=
Module[{vars, z, deg, monomList},
If[polIn === 0,
0,
vars = Cases[Variables[polIn], x[_]];
monomList = MonomialList[polIn, vars];
Sum[
deg = Exponent[mon /. x[i_] :> z, z];
If[deg <= 0,
mon,
mon /. MacdonaldERule[deg, bas, x, q, t, aa]
]
, {mon, monomList}]
]
];


There are several issues with this code: What if I use another symbol instead of $x$ as variable? This would be inefficient if I already computed the actual polynomial in $x$. Same thing for $q$ and $t$.

Also, it is inconvenient to always send the parameters $q$ and $t$, it becomes a long parameter list. If this is defined in a notebook, I can just have these as global symbols and omit them from the parameter lists, but then I cannot put this code in a package.

Truly, there must be a "right" way to do what I am trying to do, as this should be a very common task.

• Hard to show anything testable without full code, but offhand I'd say you probably want to use GroebnerBasis and PolynomialReduce to do this type of substitution. – Daniel Lichtblau Mar 8 '16 at 0:35
• @DanielLichtblau: The problem I have is not really doing the change of basis, I know how to do that, the problem I have is how to package it in a user-friendly manner that also allows for efficient memoization. – Per Alexandersson Mar 8 '16 at 1:10