MathGroup Archive 2002

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

Search the Archive

RE: Test of a pure function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34068] RE: [mg34010] Test of a pure function
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 1 May 2002 08:00:42 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: Shawn O'Connor [mailto:soconnor at ccs.nrl.navy.mil]
To: mathgroup at smc.vnet.net
> Sent: Friday, April 26, 2002 9:28 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg34068] [mg34010] Test of a pure function
> 
> 
> Hello,
> 
>     I hope someone can help me with this I know the solution 
> should be easy
> but I am still learning how pure functions work.
> 
> I am trying to pick out a position in a list were a number falls i.e.
> 
> Position[xBins, _?(#1<=x <= #2 &)].  I keep getting errors Like these
> 
> 
> Function::slotn: Slot number 2 in #1¾x¾#2& cannot be filled
> from \
> (#1¾x¾#2&)[List].
> 
> Function::slotn: Slot number 2 in
> #1¾x¾#2& cannot be filled from \
> (#1¾x¾#2&)[-1].
> 
> From
> In[83]:=
> Function::slotn: Slot number 2 in #1¾x¾#2& cannot be filled from
> \
> (#1¾x¾#2&)[-0.995].
> 
> General::stop: Further output of
> Function::slotn will be suppressed during \
> this calculation.
> 
> 
> Can I make a comparison with the nth and nth+1 element in a 
> pure function.
> Thank you,
> 
> 
> 
> 
Shawn,

it was only after a couple of replies, as I began to understand your
question.

If you want to classify single events to predefined bins - especially if you
have got many bins - you may use binary search, instead of linearly scanning
the list of values delimiting the bins:

In[2]:= Attributes[binarySearch] = {HoldFirst};
In[3]:=
binarySearch[xBins_, x_] := 
  Module[{lo = 1, mid, hi = Length[xBins], BS}, 
    BS[lo_, hi_] :=
      If[lo == hi - 1, lo,
        If[
          x < xBins[[ mid = Floor[(lo + hi)/2] ]],
          BS[lo, mid],
          BS[mid, hi]]
        ];
    Which[x < xBins[[lo]], 0,
          x < xBins[[hi]], BS[lo, hi],
          True, hi] /; hi >= 1]

But if you have all your data together, it might be preferable to sort all
your data first, and then bin your data in a single linear scan (merge) over
the data and the (sorted) list describing the bins. This has no better
asymptotic complexity but uses Mathematicas built-in Sort which cannot be
beaten by a program like this one above (as long as we have no kernel
version of binary search).


Just to show how your - as I surmise - first idea might get working, look at
this: 

In[13]:= lst=Sort[Table[Random[],{10}]]
Out[13]= {0.0501495,0.155844,0.306824,0.395722,0.400529,
          0.44935,0.631482,0.632848,0.77239,0.816055}

In[14]:= x = Random[]
         Position[Partition[lst, 2, 1], bin_ /; (#1 <= x < #2 & @@ bin)]

Out[14]= 0.76333
Out[15]= {{8}}

This also works if your bins are not contiguous. Then just replace
Partition[lst,2,1] by a list of pairs of lower and upper bounds for each of
the bins (then they also need not be sorted). 

--
Hartmut





binarySearch[xBins_, x_] := Module[{lo = 1, mid, hi = Length[xBins]}, 
   Which[x < xBins[[lo]], 0, 
         x < xBins[[hi]], 
             (If[#1 == #2 - 1, #1, 
                If[x < xBins[[mid = Floor[(#1 + #2)/2]]], 
                   #0[#1, mid], 
                   #0[mid, #2]]] &) [lo,hi],
         True, hi] /; hi >= 1]



  • Prev by Date: Re: Redefing a Numberedfigure-Style in a MyReport Style Sheet
  • Next by Date: Re: Question about pattern matching
  • Previous by thread: RE: RE: Re: Test of a pure function
  • Next by thread: Re: Test of a pure function