# Translating syntax of a pure function

I have a matrix in complex exponential form. I want to factor out the complex exponential form and then express the inside in the a+i*b form.

For example:

\begin{bmatrix} e^{i \frac{\pi}{4}} & 0 \\ 0 & e^{-i \frac{\pi}{4}} \end{bmatrix}

becomes $\begin{bmatrix} e^{i \frac{\pi}{4}} & 0 \\ 0 & e^{-i \frac{\pi}{4}} \end{bmatrix} \rightarrow e^{i \frac{\pi}{4}} \begin{bmatrix} 1 & 0 \\ 0 & e^{-i \frac{\pi}{2}} \end{bmatrix} \rightarrow e^{i \frac{\pi}{4}} \begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}$

I found a helpful similar question here, but the most upvoted choice involves notation I don't know. And because the symbols are just question marks and underscores it's impossible to google. Here is code that turns a number into complex form (the inverse of part of what I want to do):

polarForm = Expand[# /. z_?NumericQ :> Abs[z] Exp[I Arg[z]]] &;


I know about pure functions (using # and &) and replacement (using /.), but I have no idea a what "z_?NumericQ" is, and only half-understand what :> does.

I understand that using this fancy notation is not necessary, and that I could just look at the other upvoted posts and learn from their code - but I would like to learn to be a better Mathematica coder, and I'm thinking learning this notation would help me.

In summary, I'm looking for help in understanding what the syntax in this "polarform" is doing (and maybe how I can write something similar for my code).

• Both ? and :> have documentation pages which can be searched using just those symbols. The former is a pattern test; here it is applying the function NumericQ to the pattern z_, which is useful for pattern matching to a number. The latter is a delayed version of ->, the difference is that the RHS of :> is not evaluated until occurrences of z have been replaced. – IPoiler Nov 30 '15 at 20:16
• Thanks for the responses. I know about the documentation for NumericQ and RuleDelayed. I think my issue is with "patterns." I've never heard that term until now, and now see that there are tutorials on "patterns." I saw the documentation on "PatternTest" but assumed that it was unrelated to what this was doing. – Steven Sagona Nov 30 '15 at 20:38

(m = {{E^(I*Pi/4), 0}, {0, E^(-I*Pi/4)}}) //


(m2 = m[[1, 1]]*f[m/m[[1, 1]]] /. f -> Defer) // TraditionalForm


m == m2 /. Defer -> Identity

(*  True  *)

• Using your suggestion and reading up on "patterns" - I was able to understand how to make what I want. Additionally, I generalized your code to work for any expression and input I want to factor: FactorOutConstant[constant_, expression_] := TraditionalForm[constant*f[expression/constant]] /. f[x_] -> Defer[x] // TraditionalForm The input now no longer needs to be a matrix and I can factor out a constant from any expression: FactorOutConstant[2, m] And it can reproduce your code by inputting: FactorOutConstant[m[[1, 1]], m] – Steven Sagona Dec 1 '15 at 0:02
• @StevenSagona - For a generalized solution, recommend that you use Module to localize f, i.e., FactorOutConstant[constant_, expression_] := Module[{f}, TraditionalForm[constant * f[expression/constant]] /. f -> Defer // TraditionalForm] – Bob Hanlon Dec 1 '15 at 0:29

Could you be looking for something like this?

DisplayForm[Row[{Re/@m, " + ", I, Im/@m}]]