0
|
4699 Views
|
7 Replies
|
3 Total Likes
View groups...
Share

# Replace[] malfunction?

Posted 10 years ago
 The replacement operation below doesn't work properly, i.e. not all the the sigmas are changed. Why?
7 Replies
Sort By:
Posted 10 years ago
 It seems that the image that you tried to upload did not appear. Perhaps post the code?
Posted 10 years ago
 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: http://www.codecogs.com/latex/eqneditor.php, 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 
Posted 10 years ago
 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
Posted 10 years ago
 E.g., (-(a/(2 s^2))) // FullForm returns Times[Rational[-1,2],a,Power[s,-2]] which does not match s^2
Posted 10 years ago
 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[]*)] 
Posted 10 years ago
 interesting
Posted 10 years ago
 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]