MathGroup Archive 2001

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

Search the Archive

Re: Greatest element in list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg30594] Re: Greatest element in list
  • From: rknapp at wolfram.com
  • Date: Thu, 30 Aug 2001 03:51:44 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <9m27in$gqd$1@smc.vnet.net> <9m5267$qe0$1@smc.vnet.net> <9m6tvk$l99$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ordering is certainly the fastest solution and it appears to have the
additional benefit of always giving a result, but care should be taken
since it DOES NOT necessarily order along the real line.

First, consider 
 
In[1]:= list = {1, Pi, 4, 3 + 4 I};

In[2]:=Position[#, Max[#]]&[list]

Max::nord: Invalid comparison with 3 + 4 I attempted.

Max::nord: Invalid comparison with 3 + 4 I attempted.

Out[2]= {}

returns empty because, as the message has indicated, the complexes do
not have a clearly defined order, so ,...

In[3]:= Max[list]

Max::nord: Invalid comparison with 3 + 4 I attempted.

Max::nord: Invalid comparison with 3 + 4 I attempted.

Out[3]= Max[3 + 4 I, 4]

Max cannot return a number.

What is more interesting is when  we try this example with Ordering

In[4]:= First[Ordering[list, -1]]          

Out[4]= 2

Which is the position of Pi.

The trick is that Ordering uses Mathematica's default Sort ordering,
which ranks symbols (whether they have numeric values or not)
differently from numbers:

In[5]:= Sort[list]

Out[5]= {1, 3 + 4 I, 4, Pi}

Allan Hayes wrote:
> 
> Jens,
> Your function MaxPosition seems to be nearly twice as fast as
> Position[lst,{1},1] unless a maximum entry comes very early in the list
> (much of the speed seems to be due to the compiling).
> Howevver the new function Ordering is over ten times faster.
> 
> MaxPosition=  (*Jens-Peer Kuska*)
>     Compile[{{lst,_Real,1}},
>       Module[{max=First[lst],pos=1,i=2},
>         Scan[If[#>max,max=#;pos=i++,i++]&,Rest[lst]];
>         pos]];
> 
> MaxPosition2[lst_]:= (*uncompiled version of above*)
>     Module[{max=First[lst],pos=1,i=2},
>       Scan[If[#>max,max=#;pos=i++,i++]&,Rest[lst]];
>       pos];
> 
> TIMINGS
> 
> lst=Insert[Table[Random[],{100000}],2.0,50000];
> 
> Timing[Position[#,Max[#]]&[lst]]
> 
>         {1.43 Second,{{50000}}}
> 
> Timing[Position[#,Max[#],{1},1]&[lst]]
> 
>         {1.15 Second,{{50000}}}
> 
> MaxPosition[lst]//Timing
> 
>         {0.66 Second,50000}
> 
> Timing[MaxPosition[lst]]
> 
>         {7.2 Second,714995}
> 
> Timing[MaxPosition2[lst]]
> 
>         {24.45 Second,50000}
> 
> Ordering[lst,-1]//Timing
> 
>         {0.11 Second,{50000}}
> 
> --
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
> 
> "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de> wrote in message
> news:9m5267$qe0$1 at smc.vnet.net...
> > Hi,
> >
> > without your data I can't reproduce the problem.
> > But
> >
> > MaxPosition = Compile[{{lst, _Real, 1}}, Module[{max = First[lst], pos =
> > 1, i = 2},
> >       Scan[
> >         If[# > max, max = #; pos = i++, i++] & ,
> >         Rest[lst]
> >         ];
> >       pos
> >       ]
> >     ]
> >
> > is atleast faster than Position[#, Max[#]] &[lst]
> >
> > In[]:=lst = Table[Random[], {1000000}];
> >
> > In[]:=Timing[Position[#, Max[#]] &[lst]]
> > Out[]={6.85 Second, {{602020}}}
> >
> > In[]:=Timing[MaxPosition[lst]]
> > Out[]={4.42 Second, 602020}
> >
> > Because it scans the list only once.
> >
> > Regards
> >   Jens
> >
> >
> > Oliver Friedrich wrote:
> > >
> > > Hi,
> > >
> > > what's the best way to get the position of the greatest number in list
> of
> > > reals? I've tried
> > >
> > > Position[#,Max[#]]&list
> > >
> > > but surprisingly, it doesn't work all the time, sometimes it returns an
> > > empty list. How is that, because a theorem says that a non empty set of
> real
> > > numbers must have at least one biggest element. So Max[#] can't be
> empty.
> > >
> > > Any solutions ?
> > >
> > > Oliver Friedrich
> >


  • Prev by Date: Re: Delete All Output (bug)
  • Next by Date: Re: Eulerian numbers
  • Previous by thread: Re: Greatest element in list
  • Next by thread: Optica-GratingSurface