I wanted to ask if there was a way to quickly replace multiples of the same thing, instead of writing it so many times, for example:

expr //. {4*i*sigma*t1 - i*B -> i*gamma, -4*i*sigma*t1 - -i*B -> -i*gamma, 8*i*sigma*t1 - 2*i*B -> 2*i*gamma, -8*i*sigma*t1 - -2*i*B -> -2*i*gamma, and so on}


expr2 //.{E^(i*gamma) -> Cos[gamma] + i*Sin[gamma], E^(2*i*gamma) -> Cos[2*gamma] + i*Sin[2*gamma] , and so on }

I tried stuff like

xpr2 //. {[E^([#*i*gamma]) -> 
 Cos[#*gamma] + I*Sin[#*gamma] &,4 }

but to be honest, I am not really sure what I am doing.

Thank you

Edit: This really does work, and it is absolutely wonderful. But I would like to add the conditions as Bill mentioned.

I have managed to reduce my expression to something along the lines of

expr2 = E^(ww)+E^(2ww)+E^(-ww)

So at the moment, I believe I have 2 routes: Route 1: I can try to change only the -ww to wa instead, and do the exponent thing separately. or Route 2:

{E^(ww), E^(2*ww), etc}/.{E^(ww) -> Cos[ww] + i*Sin[ww],E^(n_*ww) -> Cos[n*ww] + i*Sin[n*ww], only for positive ww, E^(-ww) -> Cos[-ww] + i*Sin[-ww], E^(n_*-ww) -> Cos[n*-ww] + i*Sin[n*-ww], only for negative}

1 Answer 1


Perhaps this will handle your first problem.

Simplify[{4*i*sigma*t1-i*B,-4*i*sigma*t1- -i*B, 8*i*sigma*t1-2*i*B,-8*i*sigma*t1--2*i*B,etc},

instantly returns

{gamma*i, -(gamma*i), 2*gamma*i, -2*gamma*i, etc}

Carefully read the documentation for Simplify and in particular how you can add an extra argument specifying assumptions that Simplify is to consider.

This method doesn't work for your second problem because Mathematica thinks what you already have is simpler than what you want.

Instead you can try

{E^(i*gamma), E^(2*i*gamma), etc}/.{
 E^(i*gamma) -> Cos[gamma] + i*Sin[gamma],
 E^(n_*i*gamma) -> Cos[n*gamma] + i*Sin[n*gamma]}

which instantly gives

{Cos[gamma] + i*Sin[gamma], Cos[2*gamma] + i*Sin[2*gamma], etc}

You need to give two substitution rules because Mathematica pattern matching is very literal and won't assume that i*gamma is the same as 1*i*gamma.

Test these ideas very carefully in all the ways you are going to use this before you trust it. It is easy for pattern matching and replacement to sometimes behave in ways that you do not expect or are incorrect.

  • $\begingroup$ Thank you very much, you have given me some ideas, specifically Expand[simplify[expr , ww==4*isigmat1-iB]]. Which works for all cases. However, I wanted to ask about the second part. Is it possible to this for both E^(igamma) -> Cos[gamma] + iSin[gamma] only for positive values, while having E^(-igamma) -> Cos[-gamma] + i*Sin[-gamma] for the negatives? $\endgroup$ Sep 7, 2019 at 20:17
  • 1
    $\begingroup$ A few notes for the second example: (1) i is not the imaginary unit, I is. But then it won't work anymore, since 2 I is Complex[0,2], which does not match the pattern. (2) You can use n_. to indicate that n_ may be omitted. (3) ExpToTrig does essentially what your second pattern does (assuming you use I and not i) $\endgroup$
    – Lukas Lang
    Sep 7, 2019 at 20:19
  • $\begingroup$ I think you are saying you want to put conditions on your replacements. You can look up /; in the help system and see if you can make sense of only having patterns only match negative numbers or only match positive numbers. If you need more help you might edit your original post to show exactly the results you want. $\endgroup$
    – Bill
    Sep 7, 2019 at 20:23
  • $\begingroup$ Thank you Bill, conditions are exactly what I was looking for, I am still looking up examples though, though I edited the the post as well just in case. And thank you Lukas, that does seem to work, however, as I am also trying to get rid of using ComplexExpand later on the code, I am thinking that it might be beneficial to me to change the variables to ww instead. Its definitely going to be a mess this way, but it might be faster as the expressions get huge. $\endgroup$ Sep 7, 2019 at 20:52

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.