Message Boards Message Boards

7 Replies
3 Total Likes
View groups...
Share this post:

Replace[] malfunction?

Posted 10 years ago

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

enter image description here


POSTED BY: Sandu Ursu
7 Replies

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

POSTED BY: David Reiss
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:, 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 BY: Sandu Ursu

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 BY: David Reiss

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



which does not match s^2

POSTED BY: David Reiss

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*)]

(* 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 BY: Isaac Abraham
Posted 10 years ago


POSTED BY: Sandu Ursu

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]
POSTED BY: Isaac Abraham
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract