6226
|
1 ## Copyright (C) 2007 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} optimset () |
|
22 ## @deftypefnx {Function File} {} optimset (@var{par}, @var{val}, @dots{}) |
|
23 ## @deftypefnx {Function File} {} optimset (@var{old}, @var{par}, @var{val}, @dots{}) |
|
24 ## @deftypefnx {Function File} {} optimset (@var{old}, @var{new}) |
|
25 ## Create options struct for optimization functions. |
|
26 ## @end deftypefn |
|
27 |
|
28 function retval = optimset (varargin) |
|
29 |
|
30 nargs = nargin (); |
|
31 |
|
32 ## Add more as needed. |
|
33 persistent opts = { |
|
34 "Display", "\"off\"|\"iter\"|{\"final\"}|\"notify\""; |
|
35 "FunValCheck", "{\"off\"}|\"on\""; |
|
36 "MaxFunEvals", "positive integer"; |
|
37 "MaxIter", "positive integer"; |
|
38 "OutputFun", "function|{[]}"; |
|
39 "TolFun", "positive scalar"; |
|
40 "TolX", "positive scalar" |
|
41 }; |
|
42 |
|
43 if (nargs == 0) |
|
44 if (nargout == 0) |
|
45 ## Display possibilities. |
|
46 tmp = opts'; |
|
47 disp (struct (tmp{:})); |
|
48 else |
|
49 ## Return structure with empty values. |
|
50 t1 = opts(:,1)'; |
|
51 t2 = cell (size (t1)); |
|
52 tmp = [t1; t2]; |
|
53 retval = struct (tmp{:}); |
|
54 endif |
|
55 elseif (nargs == 1 && ischar (varargin{1})) |
|
56 ## Return defaults for named function. |
|
57 fcn = varargin{1}; |
|
58 optfcn = sprintf ("__%s_defopts__", fcn); |
|
59 if (exist (optfcn)) |
|
60 retval = optimset (optimset (), feval (optfcn)); |
|
61 else |
|
62 error ("no defaults for function `%s'", fcn); |
|
63 endif |
|
64 elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2})) |
|
65 ## Set slots in old from nonempties in new. Should we be checking |
|
66 ## to ensure that the field names are expected? |
|
67 old = varargin{1}; |
|
68 new = varargin{2}; |
|
69 for [val, key] = new |
|
70 if (! isempty (val)) |
|
71 old.(key) = val; |
|
72 endif |
|
73 endfor |
|
74 retval = old; |
|
75 elseif (rem (nargs, 2) && isstruct (varargin{1})) |
|
76 ## Set values in old from name/value pairs. |
|
77 retval = optimset (varargin{1}, struct (varargin{2:end})); |
|
78 elseif (rem (nargs, 2) == 0) |
|
79 ## Create struct. Default values are replaced by those specified by |
|
80 ## name/value pairs. |
|
81 retval = optimset (optimset (), struct (varargin{:})); |
|
82 else |
|
83 print_usage (); |
|
84 endif |
|
85 |
|
86 endfunction |