Re: IsIntegerOrFloat
- To: mathgroup at smc.vnet.net
- Subject: [mg87098] Re: IsIntegerOrFloat
- From: Albert Retey <awnl at arcor.net>
- Date: Mon, 31 Mar 2008 04:52:15 -0500 (EST)
- References: <200803290921.EAA16390@smc.vnet.net> <fsnbfs$dcg$1@smc.vnet.net> <fsq2m6$h8g$1@smc.vnet.net>
Hi,
> (* Albert Retey's submission not tested since it wasnt
> wrapped as function *)
I just wanted to mention that it is just as good wrapped as a function
as the others, it just makes definitions for more than one kind of
arguments:
IIOF5[n_Real] := True
IIOF5[n_Integer] := True
IIOF5[lst_List] := And @@ (IIOF5/@ lst)
IIOF5[___] := False
You can use IIOF5 just as any function with just one definition.
I think this i a very common and useful technique in mathematica,
especially for functions like this, which shall return either True or
False for any kind of argument given (in mathematica, these usually end
with Q for Question).
If speed matters - as you have indicated with your comparisons - IIOF5
comes out as second best only, so you might also consider this:
IIOF6[expr_List] :=
Union[Union[data[[All, 0]]], {Real, Integer}] == Union[{Integer, Real}]
timings:
{#, Timing[#[data];][[1]]} & /@ {IIOF1, IIOF2, IIOF3, IIOF4, IIOF5,
IIOF6}
{
{IIOF1, 0.641},
{IIOF2, 3.593},
{IIOF3, 4.079},
{IIOF4, 4.156},
{IIOF5, 1.094},
{IIOF6, 0.265}
}
> I have no idea why IIOF1 is about 6 times faster than the others.
In general things like this are not always easy to explain. In this case
it looks like the use of pattern matching vs. extraction of heads seams
to be crucial to improve speed. This was also what made me come up with
IIOF6...
hth,
albert
- References:
- IsIntegerOrFloat
- From: carlos@Colorado.EDU
- IsIntegerOrFloat