0
$\begingroup$

I'm learning to simplify expressions and make substitutions. So I made up this example to check my understanding:

Exp[I 5] /. Exp[I x_Real] :> Cos[x] + I Sin[x]

I expected to get

Cos[5] + I Sin[5]

However, Mathematica returns

Exp[I 5]

What did I do wrong?

$\endgroup$
  • 1
    $\begingroup$ Look at the FullForm. Also, MatchQ[5, _Real] is False. $\endgroup$ – Carl Woll Apr 26 '17 at 6:13
  • 1
    $\begingroup$ Your rule considers Exp with a Times expression, whereas the exponent in Exp[I 5] is Complex[0, 5]. So it does not work. Use Exp[Complex[a_, b_] ]:>Exp[a](Cos[b]+I Sin[b]) . $\endgroup$ – Fred Simons Apr 26 '17 at 6:13
2
$\begingroup$

Two important things to understand are:

  • Pattern matching is purely structural. It is unaware of the mathematical meaning of expressions.

  • Real is a datatype. It is a programming concept, not a mathematical one. None of 5, 5I or 5.0 I are Real.

Head /@ {5, 5.0, 5 I, 5.0 I}
(* {Integer, Real, Complex, Complex} *)

FullForm /@ {5, 5.0, 5 I, 5.0 I}
(* {5,5.`,Complex[0,5],Complex[0.`,5.`]} *)

You can use

Exp[5 I] // ExpToTrig
(* Cos[5] + I Sin[5] *)

or

Exp[5 I] /. Exp[z_] :> Exp[Re[z]] (Cos[Im[z]] + I Sin[Im[z]])
(* Cos[5] + I Sin[5] *)
$\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.