MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Checking for non-complex numerics

  • To: mathgroup at
  • Subject: [mg20994] RE: [mg20983] Checking for non-complex numerics
  • From: "Ersek, Ted R" <ErsekTR at>
  • Date: Thu, 2 Dec 1999 21:41:09 -0500 (EST)
  • Sender: owner-wri-mathgroup at

Mark Diamond wrote:
I would like a function that returns True for non-complex numerics only. To
be more specific, anything that results in NumericQ returning True is OK,
such as Pi, E, 1, -2.6.

followed by NonComplexQ[a] should return True. But
NonComplexQ[b] and NonComplexQ[Pi I] should return False as should
NonComplexQ[1.1 + 3 I].

I have been able to a function that satisfies various subsets of these
conditions, but not the whole lot ... yet it seems the kind of problem for
which there should be a simple and obvious solution.


A fullproof version isn't simple.
I give my solution below.

ZeroImagQ[x_] := Check[
  Block[{$Messages}, Im[ComplexExpand[x]] === 0],
  Im[ComplexExpand[FullSimplify[x]]] === 0,

NonComplexQ[x_] := NumericQ[x] && ZeroImagQ[x]

Below I use NonComplexQ on several examples.

f[x_] := ArcSin[x Cos[x] + Sin[x]];
NonComplexQ /@{Pi,2/3,f[49/10],f[51/10],Pi*I,2.3+t,t+3I,2+0.0I}

{True, True, True, False, False, False, False, False}

Above (t+3I) is not numeric.
Should NonComplexQ[t+3I] return True or False?
Should NonComplexQ[2+0.0 I] return True of False?

f[49/10] is NonComplex, but f[51/10] has a non-zero imaginary part.
We see that in the numeric approximation below.

{f[49/10], f[51/10]} // N

{-0.0685958, 1.5708-0.0611755 I}

For most problems the following would work:
  NonComplexQ[x_] := NumericQ[x] && (Im[ComplexExpand[x]] === 0)

But the more complicated definition is needed for the next example.

NonComplexQ[ArcCos[1 + Sqrt[3] + Sqrt[2] - Sqrt[5 + 2Sqrt[6]]]  ]


Here FullSimplify is needed to see that 
  ArcCos[1 + Sqrt[3] + Sqrt[2] - Sqrt[5 + 2Sqrt[6]]]
simplifies to ArcCos[1].

Notice my solution only calls on FullSimplify in the rare case where an
answer can't be found otherwise.

Ted Ersek

For Mathematica tips, tricks see

  • Prev by Date: Re: How to: Read Notebook and auto-evaluate ?
  • Next by Date: Re: Q: Dickman function
  • Previous by thread: Re: Checking for non-complex numerics
  • Next by thread: Re: Checking for non-complex numerics