# Simplifying expressions with ReplaceAll: _Real not matching a number

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 + I Sin


However, Mathematica returns

Exp[I 5]


What did I do wrong?

• Look at the FullForm. Also, MatchQ[5, _Real] is False. – Carl Woll Apr 26 '17 at 6:13
• 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]) . – Fred Simons Apr 26 '17 at 6:13

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 + I Sin *)


or

Exp[5 I] /. Exp[z_] :> Exp[Re[z]] (Cos[Im[z]] + I Sin[Im[z]])
(* Cos + I Sin *)
`