# Pattern-matching complex numbers as a + bi

Mathematica Asked by ppln on October 24, 2020

I just started working with Mathematica and am toying with pattern matching. There may be something obvious I’m missing in this, but I can’t figure it out by myself.

I want to write down a function that takes a complex number as arguments. So f[1 + 2 I] should be a valid input, as well as f[a + b I]. I want, however, to make my function parse this as two numbers of the form a + bi, getting a and b by pattern matching. I made several attempts similar to this:

f[a_ + b_ I] := NSolve[a^2 + b^2 == 1/2 (1 + z), z]
SetAttributes[f, HoldAll]


(I guess the NSolve doesn’t matter in this case, but let it there in case it’s part of the issue.)

This doesn’t work as I planned. Any attempt to call it, like f[1 + 2 I], just echoes itself, but it does work fine when I call it with symbolic arguments, such as f[a + b I].

I guessed this should be due to some difference in the internal representation of symbolic expressions and complex numbers. Indeed, whenever I try to MatchQ[m + n I, a_ + b_ I], it says it’s True. But when I try the sorts of MatchQ[Unevaluated[2 + 3 I], a_ + b_ I], it’s False.

In trying to figure it out, I asked

FullForm[a + b I]
FullForm[Unevaluated[2 + 3 I]]
FullForm[a_ + b_ I]


and got

Plus[a,Times[Complex[0,1],b]]
Unevaluated[Plus[2,Times[3,[ImaginaryI]]]]
Plus[Pattern[a,Blank[]],Times[Complex[0,1],Pattern[b,Blank[]]]]


My questions are:

1. Shouldn’t the Plus[2,Times[3,[ImaginaryI]]] match with Plus[Pattern[a,Blank[]],Times[Complex[0,1],Pattern[b,Blank[]]]]?
2. What is the difference between [ImaginaryI] and Complex[0,1]? I know the first is a symbol as much as [Alpha] is, and I guess me asking for Unevaluated is preventing it from being cast as a Complex[0,1]. Probably this would be needed for the matching, but I don’t know a workaround.
3. Is there a better way to do what I’m attempting with my function?

Thanks!

f[a_. + b_ I | Complex[a_, b_]] := {a, b}


You need to catch both the unevaluated form for symbolic work and the evaluated Complex representation for numerical work. Note that the first a_. allows the real part to be omitted so that purely imaginary parts can be matched as well.

Note that this might break on sufficiently complicated polynomial forms, but it hasn't blown up on the examples I've tried.

Correct answer by eyorble on October 24, 2020

## Related Questions

### What has DiscreteAsymptotic been thinking about for so long?

1  Asked on May 24, 2021

### Is it possible to use different colors for connected portions in Region Plot?

1  Asked on May 24, 2021

### Custom object display in Dataset using MakeBoxes

1  Asked on May 24, 2021 by rfrasier

### ImageSize bug in Rasterize

1  Asked on May 24, 2021 by simon-woods

### How to increase the number of minor ticks in a plot?

4  Asked on May 24, 2021 by tmh

### How to define a version of Table[] which localizes iteration variable by Module[]?

1  Asked on May 23, 2021

### Why MMA refuses to simplify the following simple expression?

4  Asked on May 23, 2021

### How to represent a data list by arrows?

1  Asked on May 23, 2021 by sac

### NDSolve of a 3-D function from interpolated data

1  Asked on May 23, 2021

### Finding an approximate solution to this integral: $alpha(phi,r,p,d)=int_0^infty w(z,r,p,d)Q(z,r,phi)dz$

1  Asked on May 23, 2021

### Strange result with Laplace transform

0  Asked on May 22, 2021

### How to assemble Python abstract syntax trees (AST) in Mathematica?

2  Asked on May 22, 2021 by berniethejet

### Remove extra space from Grid

2  Asked on May 22, 2021

### Reduce the time to find six integer numbers so that the angle of two vectors equal to Pi/6

1  Asked on May 22, 2021

### Speed up NDSolve for a changing equation?

2  Asked on May 22, 2021

### Fourier Transform on a Sound

3  Asked on May 22, 2021 by nxt191

### Can I use Solve to solve for an expression (instead of a variable)?

1  Asked on May 22, 2021

### Is it possible to use Mathematica’s Twitter integration to unfollow a certain list of users?

0  Asked on May 21, 2021

### How do I remove a singularity that appears when I make a fit to my data?

0  Asked on May 21, 2021

### Graph embedding constrained to a surface?

0  Asked on May 21, 2021