Somewhat related to a previous question, I'm experimenting with ReplaceList to get the list of all possible transformations we can obtain by applying a rule to an expression. But I don't manage to make it work as expected:
expr := 1+Cos[ω t - π/4 + 3π/4]
rule := Cos[a_ + b_] -> X[a,b]
ReplaceList[expr,{ rule }]
I expected the result:
> { 1+X[ω t - π/4, 3π/4], 1+X[ω t, -π/4 + 3π/4] }
But instead, I obtain:
{}
Why is Mathematica unable to apply rule on expr? How should I fix my code to obtain the expected result?
As an extra requirement, I missed the fact that ReplaceAll
like Replace
"does not map down to subparts".
I'm probably looking for a mix between ReplaceList
and Cases
(?)
As per comment, to avoid Mathematica to rewrite the expression as a sinus, I tried wrapping it inside various Hold*
functions--in no case, the rule is applied.
expr := Hold[1+Cos[ω t - π/4 + 3π/4]]
expr := HoldForm[1+Cos[ω t - π/4 + 3π/4]]
expr := HoldComplete[1+Cos[ω t - π/4 + 3π/4]]
Sin[t \[Omega]]
$\endgroup$expr
evaluates to-Sin[t ω]
, which does not contain any expression withCos
head, so nothing in it matches the left-hand side of your rule? I also find yourrule
definition usingSetDelayed
somewhat odd, so I wonder whether there might not be a misunderstanding there. Why should it matter thatrule
is evaluated every time it is called? $\endgroup$Hold
,HoldForm
andHoldComplete
. It prevents rewriting the expression as as sinus--but the rule still doesn't apply. $\endgroup$SetDelayed
somewhat odd, so I wonder whether there might not be a misunderstanding there." Indeed. What would be the consequences of usingSetDelayed
instead ofSet
? $\endgroup$RuleDelayed
(:>
) rather thanSetDelayed
, i.e. you would want to delay the evaluation of the right-hand side of the rule untila
andb
have been given values. I have put the rest in an answer cause it was getting too long for comments. $\endgroup$