From octave-maintainers-request at bevo dot che dot wisc dot edu Sun Dec 17 05:34:29 2000 Subject: patch : [...] = leval(name, list) From: Etienne Grossmann To: octave-sources at bevo dot che dot wisc dot edu, octave-maintainers@bevo.che.wisc.edu CC: etienne at isr dot ist dot utl dot pt Date: Sun, 17 Dec 2000 11:40:14 +0000 --Multipart_Sun_Dec_17_11:40:13_2000-1 Content-Type: text/plain; charset=US-ASCII Hello, I got convinced that a function [...] = leval(name, list) offers more flexibility than [...] = feval(name, arg1,...,argn). For example, such a function is useful for minimization of a function that takes many arguments. I first implemented it as a leval.m which works fine, but puts an overhead of ~4ms (PII,350MHz), which I think is too much (there may be hundreds of calls to it). It turns out that "leval" is very simple to implement as a built-in: see the patch (against 2.1.32) below. It works fine : I append a test script too. Run it after setting "verbose=1". Does that patch meet the coding standards? Etienne ====================================================================== --Multipart_Sun_Dec_17_11:40:13_2000-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="parse.leval.patch" Content-Transfer-Encoding: 7bit --- parse.cc.orig Sun Dec 17 10:45:13 2000 +++ parse.cc Sun Dec 17 10:47:15 2000 at @ -4836,6 +4836,50 @@ return retval; } + +DEFUN (leval, args, nargout, + "-*- texinfo -*-\n\ + at deftypefn {Built-in Function} {} leval (@var{name}, @var{list})\n\ +Evaluate the function named at var{name} dot All the elements in @var{list}\n\ +are passed on to the named function. For example,\n\ +\n\ + at example\n\ +leval (\"acos\", list (-1))\n\ + at result{} 3.1416\n\ + at end example\n\ +\n\ + at noindent\n\ +calls the function at code{acos} with the argument @samp{-1}.\n\ +\n\ +The function at code{leval} provides provides more flexibility than\n\ + at code{feval} since arguments need not be hard-wired in the calling \n\ +code. at seealso{feval and eval}\n\ + at end deftypefn") +{ + octave_value_list retval; + + int nargin = args.length (); + + if (nargin == 2) + { + std::string name = args(0).string_value (); + if (error_state) + error ("leval: first argument must be a string"); + + octave_value_list lst = args(1).list_value (); + if (error_state) + error ("leval: second argument must be a list"); + + retval = feval (name, lst, nargout); + + } + else + print_usage ("leval"); + + return retval; +} + + octave_value_list eval_string (const std::string& s, bool silent, int& parse_status, int nargout) { --Multipart_Sun_Dec_17_11:40:13_2000-1 Content-Type: text/plain; charset=US-ASCII ====================================================================== --Multipart_Sun_Dec_17_11:40:13_2000-1 Content-Type: application/octet-stream Content-Disposition: attachment; filename="test_leval.m" Content-Transfer-Encoding: 7bit ok = 1; if !exist ("verbose"), verbose = 0; end function [a,b,c] = f(x,y), a=1; b=x; c=y; end l = list (2,3); [a,b,c] = leval ("f",l); if a!=1 || b!=2 || c!=3, if verbose, printf ("test_leval : not ok\n"); end ok = 0; else if verbose, printf ("test_leval : ok\n"); end end [a,b] = leval ("f",l); if a!=1 || b!=2, if verbose, printf ("test_leval : not ok\n"); end ok = 0; else if verbose, printf ("test_leval : ok\n"); end end a = leval ("f",l); if a!=1, if verbose, printf ("test_leval : not ok\n"); end ok = 0; else if verbose, printf ("test_leval : ok\n"); end end if ok && verbose, ntest = 1000; printf ("For %i function calls\n",ntest); mytic (); for i = 1:ntest, [a,b,c] = f(2,3); end t1 = mytic (); mytic (); for i = 1:ntest, [a,b,c] = leval ("f",l); end t2 = mytic (); mytic (); for i = 1:ntest, [a,b,c] = feval ("f",2,3); end t3 = mytic (); printf ("time : plain : %8.3g, leval : %8.3g, feval : %8.3g (sec)\n",\ t1,t2,t3); printf ("overhead for leval : %8.3g seconds per call\n",(t2-t1)/ntest); printf ("overhead for feval : %8.3g seconds per call\n",(t3-t1)/ntest); end --Multipart_Sun_Dec_17_11:40:13_2000-1 Content-Type: text/plain; charset=US-ASCII --Multipart_Sun_Dec_17_11:40:13_2000-1--