Re: IsIntegerOrFloat
- To: mathgroup at smc.vnet.net
- Subject: [mg87089] Re: IsIntegerOrFloat
- From: carlos at Colorado.EDU
- Date: Mon, 31 Mar 2008 02:06:16 -0500 (EST)
- References: <200803290921.EAA16390@smc.vnet.net> <fsnbfs$dcg$1@smc.vnet.net>
Thanks to all who replied. I have collected here 4 solutions
for comparison,
IIOF1[list_]:=With[{ll=Flatten@list},
If[Length[ll]==Length[Cases[ll,_Real|_Integer]],
True,False]]; (* Peter Breitfeld *)
IIOF2[expr_]:=VectorQ[expr,Head[#]===Integer||Head[#]===Real&];
(* Szabolcs Horvat *)
IIOF3[expr_]:=Cases[expr,
x_/;Not[Head[x]===Integer||Head[x]===Real]->False,1,1]/.
{False}->False/.{}->True; (* Jean-Marc Gulliet *)
IIOF4[expr_]:=VectorQ[expr,NumberQ[#]&&(Head[#]=!=Rational)&&
(Head[#]=!=Complex)&]; (* Andrzej Kozlowski *)
(* Albert Retey's submission not tested since it wasnt
wrapped as function *)
A timing test on a random-float list of 10^6 items gives
(version 5.2 on MacBook Pro laptop running Mac OS X 10.4.11,
dual 2.3GHz Intel processors)
IIOF1 0.396629 Second
IIOF2 2.47186 Second
IIOF3 2.81298 Second
II0F4 2.9122 Second
I have no idea why IIOF1 is about 6 times faster than the others.
- References:
- IsIntegerOrFloat
- From: carlos@Colorado.EDU
- IsIntegerOrFloat