Re: argMax
- To: mathgroup at smc.vnet.net
- Subject: [mg51290] Re: argMax
- From: "Peltio" <peltio at twilight.zone>
- Date: Tue, 12 Oct 2004 01:57:58 -0400 (EDT)
- References: <ckaj3i$m3r$1@smc.vnet.net> <ckd63b$52f$1@smc.vnet.net>
- Reply-to: "Peltio" <peltioNOSP at Mdespammed.com.invalid>
- Sender: owner-wri-mathgroup at wolfram.com
"Peltio" wrote >I've written this without any testing, so I can't tell whether it's faster >or not than your solution (if it happens to be so, it won't be much faster, >though). > ArgMax[f_, arglist_List] := > Module[ > {spectrum}, > spectrum = f /@ arglist; > Flatten[Take[arglist, #] & /@ Position[spectrum, Max[spectrum]]] > ] It is indeed a little bit faster, and this version is just a bit more so (please note the numericization of the list, remove N[ ] if that is a problem): ArgMax[f_, arglist_List] := With[ {spectrum = N[f /@ arglist]}, Apply[arglist[[#]] & , Position[spectrum, Max[spectrum]], 1] ] f[x_] := -x^2 + 7x arglist = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ArgMax[f, arglist] >Or a multidimensional one, that can give you a list of coordinates of the >points giving you a max. > > ArgMax[f_, arglist_List] := > Module[ > {spectrum}, > spectrum = Map[f, arglist, -1]; > Take[arglist, #] & /@ Position[spectrum, Max[spectrum]] > ] > Sorry, this won't work. Try this instead for functions of several variables: ArgMax[f_, arglist_List] := Module[ {newarg = Flatten[arglist, Depth[arglist] - 3], spectrum}, spectrum = N[Apply[f, newarg, {-2}]]; Apply[newarg[[#]] & , Position[spectrum, Max[spectrum]], 1] ] f[x_, y_] := Sin[x] - Cos[3y] {arglist = Flatten[Table[{x, y}, {x, 0, 3}, {y, 2, 4}], 1], ArgMax[f, arglist]} {arglist = Table[{x, y}, {x, 0, 3}, {y, 2, 4}], ArgMax[f, arglist]} f[x_, y_, z_] := Sin[x] - Cos[3y] + z {arglist = Table[{x, y, z}, {x, 0, 3}, {y, 2, 4}, {z, -1, 1}], ArgMax[f, arglist]} cheers, Peltio Invalid address in reply-to. Crafty demunging required to mail me.