RE: Checking for non-complex numerics
- To: mathgroup at smc.vnet.net
- Subject: [mg20994] RE: [mg20983] Checking for non-complex numerics
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Thu, 2 Dec 1999 21:41:09 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
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. a=-1.5; 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. In[1]:= ZeroImagQ[x_] := Check[ Block[{$Messages}, Im[ComplexExpand[x]] === 0], Im[ComplexExpand[FullSimplify[x]]] === 0, $MaxExtraPrecision::meprec ] In[2]:= NonComplexQ[x_] := NumericQ[x] && ZeroImagQ[x] ------------------------ Below I use NonComplexQ on several examples. In[3]:= Clear[t]; 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} Out[5]= {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. In[6]:= {f[49/10], f[51/10]} // N Out[6]= {-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. In[7]:= NonComplexQ[ArcCos[1 + Sqrt[3] + Sqrt[2] - Sqrt[5 + 2Sqrt[6]]] ] Out[7]= True 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. -------------------- Regards, Ted Ersek For Mathematica tips, tricks see http://www.dot.net.au/~elisha/ersek/Tricks.html