I thought that Mathematica would be smart enough to know that f[t]^2
should match a_ * b_
with a
and b
both taking the value f[t]
, but it doesn't.
I'm writing some rules for identities involving products. It's mainly finding different ways of doing integration by parts. Am I forced to write separate rules for handling powers, or can Mathematica be persuaded to matchf[t]^2
as a_ * b_
?
I would like to handle arbitrarily large natural powers, so f[t]^3
should match a_ * b_
with a
as f[t]
and b
as f[t]^2
, or vice versa. f[t]^4
should match as both f[t] * f[t]^3
and f[t]^2 * f[t]^2
. I'm using ReplaceList
, so the order is unimportant. I want the behaviour to be identical to if the powers were represented as f[t] * f[t] * f[t] ...
.
If not, is there a nice single pattern I can use to match all cases? I've been looking at Alternatives
, but I don't know how to capture the a
and b
values properly when dealing with the powers forms.
Power
while the latterTimes
. Seen from this single fact, they two do not match of course. It is not a matter of being smart or not. Pattern matching is performed based on the form of an expression, not its (mathematical) meaning. Using jargon, pattern matching is performed syntactically, not semantically. $\endgroup$Flat
that allow semantic matching that deviates from those strict rules and I assumed that there would be similar special handling forPower
, although I realise now that there isn't. That's quite inconvenient, because when I have an expression featuringa * b
, anda
happens to equalb
at some point, MMA automatically transforms it into a power, then all of my rules stop working. Do you know of any concise pattern that is able to match either form interchangably while giving thea
andb
values? $\endgroup$Hold
,Unevaluated
, etc. to stop MMA from evaluations before matchings; because MMA "thinks"Power
is simpler thanTimes
when multiplying factors are the same. Or why do you not just usingPower
to construct the matching rules? $\endgroup$Power
, but they would be duplicates of ones I've already written that useTimes
. Although I'm not sure how to get the exact functionality I need. I'm usingReplaceList
to get every possible match witha_ * b_
, but I don't know how to write a rule forPower
that gives all of the different forms off[t]^4
likef[t] * f[t]^3
andf[t]^2 * f[t]^2
. The best idea I have is to do this in two stages, first transforming allTimes
andPower
into a new headmyTimes
then doing my matching against that. That feels like a hack. $\endgroup$Hold
etc, because I'm passing these expressions through other functions that transform them too. I'll have another read of the docs in case I'm missing something that I could do there. $\endgroup$