When working with finite fields, I often want to apply some transformation rules to the integers that appear either as coefficients in expressions. This becomes a problem when the transformation also gets applied to integral exponents.
As a simple example, suppose that I want to transform all the integer coefficients using the rule
n_Integer -> Mod[n + 2, 5] - 2
If I apply this rule to 13 x^3
, both the coefficient and the exponent get transformed, so the result I get is
-(2/x^2)
...instead of the desired
-2*x^3
I've tried so many approaches, I can't list them all. Here's one example. This works for simple cases:
13 x^3
/. {Power[x, n_Integer] :> Power[x, ToString[n]]}
/. {n_Integer -> Mod[n + 2, 5] - 2}
/. {x_String -> ToExpression[x]}
-2*x^3
In general, however, I'd end up with expressions like
-2 x^(1 + "3")
...that would be impervious to the x_String -> ToExpression[x]
transformation. (WHY?????)
Therefore I need a way to protect the exponents from transformation that can be more reliably reversed than converting them to strings.
Either that, or some other approach altogether, to transform the coefficients without transforming the exponents.
(FWIW, the "coefficient" corresponding to the constant term in a polynomial expression often does not belong to a Times
expression.)
EDIT: OK, I found that changing the last transformation to {x_String :> ToExpression[x]}
solves the problem (though I don't really understand why this delay is needed). Still, this solution strikes me as too-desperate a hack; there has to be better ways to solve this problem, so I'll let the question stand.
PolynomialMod
for this. $\endgroup$