Replace[] malfunction?

The replacement operation below doesn’t work properly, i.e. not all the the sigmas are changed.

![](/c/portal/getImageAttachment?filename=#2.png&userId=61922)

Why?

It seems that the image that you tried to upload did not appear. Perhaps post the code?

There is just the likelihood function of the normal distribution with a replacement rule.

I like to work with greeks in Mathematica :), so if you copy the LaTeX code below in the online editor: Equation Editor for online mathematics - create, integrate and download, you will get the picture.

\left(2 \pi  \sigma ^2\right)^{-\frac{n}{2}} \exp \left(-\frac{\sum _{i=1}^n \left(x_i-\theta \right){}^2}{2 \sigma ^2}\right)\text{/.}\, \sigma ^2\to \kappa

I am having a hard time posting what I want to. However my suggestion is to see what the FullForm of the expression is. your sigma^2 may not appear in that form in both places, but your replacement rule only covers the specific pattern that you use

E.g.,

 (-(a/(2 s^2))) // FullForm

returns

Times[Rational[-1,2],a,Power[s,-2]]

which does not match s^2

A given power might exist as Power[x,2] (which becomes k) or Power[x, -2] (which we want to become k^-1). Therefore we might try the following on the FullForm, since FullForm makes expressions look like lists. The replacements are done for all levels down to Infinity.

(* Mathematica 7 *)    
FullForm[(*given equation*)]
    
Replace[ 
(* First replacement for all [Sigma]^2 *) 
   Replace[%, #, Infinity(*Close inner Replace[]*)] &[Power[[Sigma], 2] -> [Kappa]],
(* Second replacement for all [Sigma]^-2 *) #, Infinity] &[
   Power[[Sigma], -2] -> [Kappa]^-1 (*Close outer Replace[]*)]

interesting

I wanted to try and find a less nested expression. Here is another (visually cleaner) attempt where f[x] is the original expression on which the operation needs to be implemented.

((FullForm[f[x]] /. Power[#1, #2] -> #3) /. 
    Power[#1, -#2] -> #3^-1) &[(*Variable to be replaced*)x,(*power \
of variable to be replaced*)2,(*The replacing variable*)k]