0
$\begingroup$

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

mse

How can I get it to produce:

enter image description here

(which equals to Out[97])

I have tried ReplaceRepeated but got no luck:

enter image description here

$\endgroup$
  • $\begingroup$ to get halfway there you need to have the unevaluated forms of the expressions on the rhs of ruleh and ruleyhat. That is, use HoldForm[h[μ, r]] and HoldForm[yhat[μ, r]]. $\endgroup$ – kglr Jan 20 '18 at 5:55
1
$\begingroup$

Here is the result of the operation D[e, w[i, j]]:

Clear[yhat];
expr=(1/(2 m) (\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(\[Mu] = 1\), \(m\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(r = 1\), \(p\)]\((\(-2\)\ \((
\*UnderoverscriptBox[\(\[Sum]\), \(s = 1\), \(n\)]\*
TemplateBox[{RowBox[{"i", ",", "s"}]},
"KroneckerDeltaSeq"]\ \*
TemplateBox[{RowBox[{"j", ",", "r"}]},
"KroneckerDeltaSeq"]\ x[\[Mu], s])\)\ y[\[Mu], r]\ \*
SuperscriptBox["\[Sigma]", "\[Prime]",
MultilineFunction->None][
\*UnderoverscriptBox[\(\[Sum]\), \(s = 1\), \(n\)]w[s, r]\ x[\[Mu], 
              s]] + 2\ \((
\*UnderoverscriptBox[\(\[Sum]\), \(s = 1\), \(n\)]\*
TemplateBox[{RowBox[{"i", ",", "s"}]},
"KroneckerDeltaSeq"]\ \*
TemplateBox[{RowBox[{"j", ",", "r"}]},
"KroneckerDeltaSeq"]\ x[\[Mu], s])\)\ \[Sigma][
\*UnderoverscriptBox[\(\[Sum]\), \(s = 1\), \(n\)]w[s, r]\ x[\[Mu], 
              s]]\ \*
SuperscriptBox["\[Sigma]", "\[Prime]",
MultilineFunction->None][
\*UnderoverscriptBox[\(\[Sum]\), \(s = 1\), \(n\)]w[s, r]\ x[\[Mu], 
              s]])\)\)\))) /. \[Sigma][a_] -> yhat[\[Mu], r] /. 
 Derivative[1][\[Sigma]][a_] -> yhat'[\[Mu], r]

Try this:

expr /. \[Sigma][a_] -> yhat[\[Mu], r] /.Derivative[1][\[Sigma]][a_] -> yhat'[\[Mu], r]

yielding thew following:

enter image description here

Have fun!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.