Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • $\begingroup$ Does Distribute[grad[f]-grad[g]+grad[h]-grad[p] /. a_ * grad[x_] :> grad[ a * x ], grad] solve your problem? $\endgroup$ Commented Jun 28, 2022 at 12:50
  • $\begingroup$ Thanks @BenIzd. Your code works if I decide to write an individual method or function called CollectGradient[]. However, what I want is a rule which can be used by ReplaceAll[] once, your code seems not quite applicable in this case for the reasons: 1. The pattern of expression needs recognizing before the expression can be manipulated. 2. If your code is used as the r.h.s of the rule, there might appear nested replacement rules, I am not sure if that works. $\endgroup$ Commented Jun 28, 2022 at 13:40