There are two main problems with your code.
- You want to match something of a form
FiniteField[q_?PrimeQ][s_]. But you will never encounter such a form in your result, because it always resolves to a FiniteFieldElement. See it for yourself by observing the InputForm:
FiniteField[3][2] // InputForm (* FiniteFieldElement[FiniteField[3, 1 + #1 & , "Polynomial"], {2}] *)
- Nevertheless, you will still encounter the same error message when you try to fix your pattern:
MatchQ[FiniteField[3][2], FiniteFieldElement[FiniteField[___], _]] (* FiniteField::fmod: The modulus ___ should be a prime number. *) (* FiniteFieldElement::bfld: FiniteField[___] is not a valid FiniteField object. *) (* False *)
This is because FiniteFieldElement is an atomic expression, which you can confirm by using AtomQ:
AtomQ[FiniteField[3][2]] (* True *)
You cannot do direct pattern matching on atomic expressions, so you have to construct your pattern differently:
patt = x_?NumericQ ffe_FiniteFieldElement /; PrimeQ[Information[ffe["Field"], "Characteristic"]] :> FiniteField[Information[ffe["Field"], "Characteristic"]][x] * FiniteField[Information[ffe["Field"], "Characteristic"]][ffe["Index"]];
This will now correctly match and replace your expression. But, once again, you will actually never encounter a term with the form x * FiniteFieldElement[...], because this always automatically evaluates to another FiniteFieldElement:
5 FiniteField[3][2] (* FiniteFieldElement[FiniteField[3, 1 + #& , "Polynomial"], {1}] *)
(Unless you are giving it a real number in front of it, but in this case, you will also get an error message.)
This means that you probably need to use $Pre instead of $Post. But at this point, I believe you should specify and clarify what is your actual goal that you want to achieve ...
exp/.FiniteField[x_]:>FiniteField[z]; exp/.FiniteField[x_][y_]:>FiniteField[z][y]; exp/.FiniteField[__]:>FiniteField[z]; exp/.FiniteField[x_]->FiniteField[z];$\endgroup$FFvector[{n_, FiniteField[q][n_]}]:>n$\endgroup$FullFormof someFiniteField- this is the pattern that should be matched. $\endgroup$vec=Table[FiniteField[3][k],{k,0,2}]; Through[vec["Index"]]? $\endgroup$