I have a horrid expression, which is basically a sum of exponents with coefficients. Here's a taste:

enter image description here

What I want to do is to replace ks with another expression, but to do so only when ks appears outside an exponent. Alternatively, I want to preform this replacement, but keep the arguments of all exponents unchanged.

For example I want

SpecialReplace[(ks-foo*bar) Exp[ks^foo-bar],ks->Y]

to yield

(Y-foo*bar) Exp[ks^foo-bar]

How would I do that?


Use two rules in the replacement with a dummy rule to replace exponents with themselves.

Along the lines of.

(ks-foo*bar) Exp[ks^foo-bar] /. {x:Exp[___]->x, ks->Y}

So anything that is an exponent is 'caught' by the first rule and the kx replacement rule doesn't get a look in.

| improve this answer | |
  • $\begingroup$ Thanks. But lists of replacement rules are executed consecutively, so this will not work. $\endgroup$ – yohbs Jan 14 '15 at 8:56
  • $\begingroup$ You're right it doesn't work, I have corrected it. Not because the rules execute in sequence but because my first rule was incorrect. $\endgroup$ – Ymareth Jan 14 '15 at 9:07

Try the following. Let this:

expr = (ks - foo1*bar1) Exp[ks^foo1 - bar1] + (ks - foo2*bar2) Exp[
 ks*foo2 - bar2];


pos = Position[expr, Exp[__]];

returns the position of exponents in it. Then this:

 expr2 = MapAt[ReplaceAll[#, ks -> kg] &, expr, pos] /. ks -> Y /. 
  kg -> ks

(*   E^(-bar1 + ks^foo1) (-bar1 foo1 + Y) + 
 E^(-bar2 + foo2 ks) (-bar2 foo2 + Y)       *)

makes the job.

Have fun!

| improve this answer | |

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