MathGroup Archive 2004

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

Search the Archive

Re: argMax

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51328] Re: argMax
  • From: "Peltio" <peltio at twilight.zone>
  • Date: Thu, 14 Oct 2004 06:37:10 -0400 (EDT)
  • References: <ckaj3i$m3r$1@smc.vnet.net> <200410110525.BAA05047@smc.vnet.net> <ckfrv9$ise$1@smc.vnet.net>
  • Reply-to: "Peltio" <peltioNOSP at Mdespammed.com.invalid>
  • Sender: owner-wri-mathgroup at wolfram.com

"DrBob"  wrote

>The dichotomy isn't really one-dimensional versus multi-dimensional, I
think; it's values versus positions.

Since the OP was looking for a fast procedure I thought it would have been
better to use 'dedicated' functions for each special case in order to keep
the overhead to a minimum. I got my second procedure totally wrong though.
Yours is fine, instead, but...

>    positions[f_,arglist_List]:=
>        Module[
>            {spectrum=Map[f,arglist,-1]},
>            Position[spectrum,Max[spectrum]]
>    ]
>    values[f_,arglist_List]:=Extract[arglist,positions[f,arglist]]

I think you had a 'lapsus digitae' after reading my mistake. : ) In fact
    Map[f,arglist,-1]
should read
    Map[f,arglist,{-1}]
(with onedimensional lists and Identity there are no differences since
either there is no nesting or when there is nesting, nesting Identity won't
change anything).

I was thinking about a way to make the procedure a little faster, but I fear
there is little room for improvement (at least without a real breakthrough -
such as an undocumented function that does just that : )))).
For a start: if the function f to be applied is rather complicated, it could
be advisable to use N.
Also, when the procedure ArgMax is called many times. using With instead of
Module could reduce its overhead (or at least I think so [1]).
I am not sure if, when the original data structure is very complex, there
could be any improvement in processing the flattened structure (but I guess
it's unlikely to have data in a form that is not a neat list, matrix or
tensor...)

Should that be true, this proc should have a ( theoretical : ) ) edge in
case ArgMax is called very many times feeding it complex data structures and
timeconsuming functions:

    argMax[f_, arglist_List] :=
      With[ {newarg = Flatten[N[arglist]]},
        spectrum = Map[f, newarg];
        Extract[newarg, Position[spectrum, Max[spectrum]] ]
    ]

But it's very likely it won't show any sensible -practical- improvement over
the original (corrected) multidimensional version. Moreover, Flatten is
useless when the list is onedimensional, so if the other tricks have any
effect whatsoever, it'd be better to use a dedicated proc for onedimensional
lists.

[1] I recall a discussion on Bahder's book (Mathematica for Scientist and
Engineers [2]) about different versions of a procedure called trapIntegrate.
[2] Wouldn't it be nice to have that book updated?

cheers,
Peltio
Invalid address in reply-to. Crafty demunging required to mail me.




  • References:
    • Re: argMax
      • From: "Peltio" <peltio@twilight.zone>
  • Prev by Date: Re: Eigenvalues and eigenvectors of a matrix with nonpolynomial elements.
  • Next by Date: Re: Re: Re: normal distribution random number generation
  • Previous by thread: Re: Re: argMax
  • Next by thread: Re: Re: argMax