1
$\begingroup$

I have

formular = a[0]*e[0] + a[0]*e[1] + b[1]*e[0] + c[0]*e[1]

and I want to replace every e[_] into some unique symbol:

formularNew = UniqueReplacement[formular]
formularNew = a[0]*uu[1092873] + a[0]*uu[76341]  + b[1]*uu[3451] + c[0]*uu[97415]

I have tried:

formularNew = formular/. {e[_] -> uu[], f[_] -> ll[RandomInteger[1000000]]}

but it applies the random number directly, thus it leads to

formularNew = 2 a[0]*uu[885866] + b[1]*uu[885866] + c[0]*uu[885866]

I would be grateful for any hint.

$\endgroup$
2
  • 3
    $\begingroup$ Use :> instead of -> for the replacement rule. This is analogous to = vs :=. $\endgroup$ – Szabolcs Nov 4 '16 at 15:30
  • $\begingroup$ I would do something like Module[{i = 1}, formularNew = formular/. {e[_] :> ll[i++]}] $\endgroup$ – march Nov 4 '16 at 15:31
4
$\begingroup$

Here is one possibility. Gather the variables of interest e.g. using Cases, then do a replacement using Unique to create the new symbols.

formular = a[0]*e[0] + a[0]*e[1] + b[1]*e[0] + c[0]*e[1];
evars = Union[Cases[formular, e[_], Infinity]]
replaced = 
 formular /. Thread[evars -> Table[Unique[uu], Length[evars]]]

(* Out[92]= {e[0], e[1]}

If you really want the uu[randominteger] style of replacement variable, could do as below.

replacedB = 
 formular /. 
  Thread[evars -> 
    Map[uu, RandomSample[Range[10^6, 2*10^6], Length[evars]]]]

(* Out[95]= a[0] uu[1443544] + b[1] uu[1443544] + a[0] uu[1743614] + 
 c[0] uu[1743614] *)

Out[93]= uu$8369 a[0] + uu$8370 a[0] + uu$8369 b[1] + uu$8370 c[0] *)
$\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.