Question: How can I tell Mathematica to replace a sub-expression (in a simplified expression) with a symbol if the symbol is defined to be that sub-expression?
Say you have defined:
f[x_, y_] := some_complicated_expr_using_x_and_y_1 g[x_, y_] := some_complicated_expr_using_x_and_y_2 h := some_complicated_expr_using_f_and_g then you do D[h,x] // Simplify, you would get an expression that has sub-expressions some_complicated_expr_using_x_and_y_1 and some_complicated_expr_using_x_and_y_2.
How can I tell Mathematica to replace some_complicated_expr_using_x_and_y_1 with f[x,y]?
For a concrete example, consider:
h[μ_, r_] := Sum[x[μ, s]*w[s, r], {s, 1, n}] yhat[μ_, r_] := σ[h[μ, r]] e := (1/(2 m)) Sum[Sum[(y[μ, r] - yhat[μ, r])^2, {r, 1, p}], {μ, 1, m}] h[μ, r] yhat[μ, r] e D[e, w[i, j]] I you evaluate the above you will get
How can I get it to produce:
(which equals to Out[97])
I have tried ReplaceRepeated but got no luck:




rulehandruleyhat. That is, useHoldForm[h[μ, r]]andHoldForm[yhat[μ, r]]. $\endgroup$