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.

3
  • $\begingroup$ Thanks Michael. I am really impressed by your ingenious method. However, like I said to Ben in the comment under my original post, your code is very nice if I want to write an individual method or function doing this, but what I want is a patterned rule which can be used by ReplaceAll[] once, and your code might be too complicated for that purpose. $\endgroup$ Commented Jun 29, 2022 at 9:49
  • $\begingroup$ @AlbertLew lcRule = DownValues@linearCombine is such a rule. constantQ may be replaced by NumericQ if you don't want it to deal with symbolic constants. $\endgroup$ Commented Jun 29, 2022 at 13:26
  • $\begingroup$ Thanks Michael, I have tested the rule generated by DownValues and found that the expr has to be enclosed in linearCombine[ ,grad] then ReplaceAll[] to make it work. Without the wrapper linearCombine[ ,grad] and letting ReplaceAll[] directly handle the expr like grad[f] - grad[g] + grad[h] with the rules won't work, which certainly leaves the way you suggested somewhat inconvenient since there are a lot of other rules which can directly be applied to the expr by /.. This specific rule regarding collecting grad [] should be able to work together with them in a big rule List. $\endgroup$ Commented Jun 30, 2022 at 4:21