I am constructing polynomial expressions in the variables s[0],...,s[n]
with (coefficients that are other variables) and I'm trying to define a new operation to simplify the expression:
$Assumptions = B \[Element] Complexes
vars = Table[s[j], {j, 0, 20}];
newTimes[] := 1
newTimes[a_] := a
newTimes[s[0], b_] := b
newTimes[a_, s[0]] := a
newTimes[a___, newTimes[b_, c__], d___] := newTimes[a, b, c, d];
newTimes[a___, s[i_Integer]*s[j_Integer], d___] := newTimes[a, newTimes[s[i], s[j]], d];
newTimes[a___, x_ + y_, b___] := newTimes[a, x, b] + newTimes[a, y, b];
newTimes[a___, (K_ /; (! MemberQ[vars, K]))*s[i_Integer], b___] :=
K*ncTimes[a, s[i], b];
newTimes[a___, (K_ /; (! MemberQ[vars, K])), b___] := K*ncTimes[a, b];
(*B a complex valued variable*)
newTimes[a___, s[i_Integer], s[i_Integer], b___] := B*newTimes[a, s[i], b];
newTimes[a___, K_Complex*s[i_Integer], b___] := K*newTimes[a, s[i], b];
newTimes[a___, s[i_Integer], s[i_Integer + 1], s[i_Integer], b___] :=
newTimes[a, s[i], b];
newTimes[a___, s[i_Integer], s[i_Integer - 1], s[i_Integer], b___] :=
newTimes[a, s[i], b];
newTimes[a___, s[i_Integer], s[j_Integer], b___] /;
TrueQ[Abs[i - j] > 1] := newTimes[a, s[j]*s[i], b];
newTimes[a___, s[i_Integer], s[j_Integer], b___] /;
TrueQ[Abs[i - j] < 2] := newTimes[a, s[i]*s[j], b];
How can I apply this to an already built polynomial? For example s[1]+5s[4]s[0]s[2]-s[4]s[2]
and then I want to apply newTimes anywhere their is a multiplication of the variables.
How do I make sure Mathematica uses my multiplication on s[i] variables and does not simplify (e.g. Commuting s[i] ) using the normal rules for *? So far it seems to build expressions with squares and not simplify them fully