Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I apologize if similar questions have already been asked, but I have some difficulties with replacements in Mathematica and I would like to make a substitution like

Exp[I m phiA] --> f[m,phiA]
Exp[I m phiB] --> f[m,phiB]

where m is a number and phiA,phiB are variables, in expressions that contain sums in the exponent, e.g.

2 + Exp[-2 I (phiA + phiB)] + Exp[2 I phiA + 2 I phiB]

which should give

1/4 + f[-2,phiA] f[-2,phiB] + f[2,phiA] f[2,phiB]

Is this possible? How can it be done?

share|improve this question
Lookk at a related question: HornerForm of polynomials in terms of E^(i x). – Artes Dec 3 '13 at 17:11
I managed to replace the form Exp[m I phi] using Complex. How can I transform Exp[-2 I (phiA + phiB)] to a product of elements like Exp[m I phi]? – Pincopallino Dec 3 '13 at 18:03
E.g. Exp[-2 I (phiA + phiB)] /. Exp[a_ (b_ + c_)] :> HoldForm[Exp[a b] Exp[a c]]. – Artes Dec 3 '13 at 18:08
Oh thanks! Can it be generalized to sums with an arbitrary number of addends? – Pincopallino Dec 3 '13 at 18:45

1 Answer 1

When attempting to deal with replacements a good general rule is to examine the FullForm of the components.

Expression 1

FullForm[Exp[I m phiA]]

Mathematica graphics

Expression 2

FullForm[Exp[-2 I (phiA + phiB)]]

Mathematica graphics

Expression 3

FullForm[Exp[2 I phiA + 2 I phiB]]

Mathematica graphics

Note carefully that even though Exp[-2 I (phiA + phiB)] is algebraically equivalent to Exp[-2 I phiA -2 I phiB] the two FullForms are not the same.

Pattern matching uses the syntax to find matches.

We can make replacement rules as follows.

Take the FullForm of the expression and insert named patterns.

Note: You can stay with the exact FullForm syntax but it doesn't print well on Stack Exchange so I will use the expanded FullForm.

Expression 1

Exp[I m phiA] /. E^(I m_ phiA_) -> f[m, phiA]

f[m, phiA]

We will come back to Expression 2 in a moment.

Expression 3

Exp[2 I phiA + 2 I phiB] /. E^(Complex[0, m_] phiA_ + Complex[0, n_] phiB_) -> 
  f[m, phiA] f[n, phiB]

f[2, phiA] f[2, phiB]

Expression 2

The question was asked in a comment if it would be possible to generalize expression 2 to an arbitrary number of components. This can be done as follows:

FullForm[Exp[-2 I (phiA + phiB + phiC)]]

Mathematica graphics

Replace the Plus with List and use Map.

Exp[-2 I (phiA + phiB + phiC)] /. E^(Complex[0, m_] (first_ + rest__)) :> 
  Times@@Map[f[m, #] &, List[first, rest]]


f[-2, phiA] f[-2, phiB] f[-2, phiC]


All of the rules would be placed in a list to transform expressions containing exponents with arguments that match the forms in expressions one, two and three.

expr /. {
  E^(I m_ phiA_) -> f[m, phiA],
  E^(Complex[0, m_] (phiA_ + phiB_)) -> f[m, phiA] f[m, phiB],
  E^(Complex[0, m_] (first_ + rest__)) :> 
   Times @@ Map[f[m, #] &, List[first, rest]]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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