Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!udel!haven!ni.umd.edu!uc780.umd.edu!cs450a03 From: cs450a03@uc780.umd.edu Newsgroups: comp.lang.apl Subject: RE: WANTED: APL maximization function Message-ID: <1APR91.21504259@uc780.umd.edu> Date: 1 Apr 91 21:50:42 GMT References: <20114@brahms.udel.edu> <31MAR91.00533434@uc780.umd.edu> <16840@chopin.udel.edu> Sender: usenet@ni.umd.edu (USENET News System) Organization: The University of Maryland University College Lines: 69 Nntp-Posting-Host: uc780.umd.edu Fred Masterson writes: >>>I am seeking an APL function that will maximize a function of >>>one variable, i.e., find the value of x that maximizes f(x). >x is indeed a floating point scalar >a local maximum would be just fine >an interactive (no returned value) procedure would be fine Well, like the guy said, a couple postings back, plotting a graph works wonders. Other than that, it looks like some variant on Newton's method should work fine... Lets say I assume that f is a scalar function. (so I can give it a list of arguments and it will give me back a corresponding list of results). I'll also going to freely mix J and APL puns... _ V x =. max_x seed [1] 'seed contains the lower bound and upper bound for the interval' [2] 'you wish to test. If you like, just use i. 0' [3] n =. 30 [4] lower_bound =. <./ seed [5] upper_bound =. >./ seed [6] loop) [7] arg =. lower_bound + (upper_bound-lower_bound) * (i. n+1) % n [8] result =. f arg [9] k =. result i. >./result [10] seed =. arg {~ 1>. n<. _1 0 1 + k [11] lower_bound =. <./ seed [12] upper_bound =. >./ seed [13] $. =. ((lower_bound = upper_bound) # loop), end [14] end) [15] _ (+/seed) % 3 V Now, I just threw this thing together, totally untested and so on. But this is such a sleepy group anyways... Tradeoffs: I am guessing that "f" isn't going to be incredibly expensive, if it is, you'll want to do something a lot more concerned about the number of times f gets evaluated. The loop structure is so I could come up with something that looks almost decent in either J or APL. I don't expect this thing to be very fast, in any event (inverting functions by trial and error is never particularly efficient). Quicky J to APL reference [1] and [2] are comments <. is min (floor) >. is max (ceiling) / is reduce # is compress (in APL they are both / ) =. is assign to local variable * is multiply loop) and end) are line labels $. =. is the equivalent of -> {~ is indexing: list[indices] _1 is negative 1 max_x would be maxHx (maxDELTAx) if I did this in real APL ---------------------------------------------------------------------- I just use built in comparison tolerance to decide when it's done. Also, you could shorten variable names and squash the thing onto fewer lines, if that pleases you. If you're really ambitious, you could even test it :-) Raul Rockwell