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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6226
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6226
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} optimset () |
|
21 ## @deftypefnx {Function File} {} optimset (@var{par}, @var{val}, @dots{}) |
|
22 ## @deftypefnx {Function File} {} optimset (@var{old}, @var{par}, @var{val}, @dots{}) |
|
23 ## @deftypefnx {Function File} {} optimset (@var{old}, @var{new}) |
|
24 ## Create options struct for optimization functions. |
|
25 ## @end deftypefn |
|
26 |
|
27 function retval = optimset (varargin) |
|
28 |
|
29 nargs = nargin (); |
|
30 |
|
31 ## Add more as needed. |
|
32 persistent opts = { |
|
33 "Display", "\"off\"|\"iter\"|{\"final\"}|\"notify\""; |
|
34 "FunValCheck", "{\"off\"}|\"on\""; |
|
35 "MaxFunEvals", "positive integer"; |
|
36 "MaxIter", "positive integer"; |
|
37 "OutputFun", "function|{[]}"; |
|
38 "TolFun", "positive scalar"; |
|
39 "TolX", "positive scalar" |
|
40 }; |
|
41 |
|
42 if (nargs == 0) |
|
43 if (nargout == 0) |
|
44 ## Display possibilities. |
|
45 tmp = opts'; |
|
46 disp (struct (tmp{:})); |
|
47 else |
|
48 ## Return structure with empty values. |
|
49 t1 = opts(:,1)'; |
|
50 t2 = cell (size (t1)); |
|
51 tmp = [t1; t2]; |
|
52 retval = struct (tmp{:}); |
|
53 endif |
|
54 elseif (nargs == 1 && ischar (varargin{1})) |
|
55 ## Return defaults for named function. |
|
56 fcn = varargin{1}; |
|
57 optfcn = sprintf ("__%s_defopts__", fcn); |
|
58 if (exist (optfcn)) |
|
59 retval = optimset (optimset (), feval (optfcn)); |
|
60 else |
|
61 error ("no defaults for function `%s'", fcn); |
|
62 endif |
|
63 elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2})) |
|
64 ## Set slots in old from nonempties in new. Should we be checking |
|
65 ## to ensure that the field names are expected? |
|
66 old = varargin{1}; |
|
67 new = varargin{2}; |
6227
|
68 fnames = fieldnames (old); |
6226
|
69 for [val, key] = new |
6227
|
70 mask = strcmpi (fnames, key); |
|
71 if (any (mask)) |
|
72 key = fnames (mask); |
6226
|
73 endif |
6227
|
74 old.(key) = val; |
6226
|
75 endfor |
|
76 retval = old; |
|
77 elseif (rem (nargs, 2) && isstruct (varargin{1})) |
|
78 ## Set values in old from name/value pairs. |
|
79 retval = optimset (varargin{1}, struct (varargin{2:end})); |
|
80 elseif (rem (nargs, 2) == 0) |
|
81 ## Create struct. Default values are replaced by those specified by |
|
82 ## name/value pairs. |
|
83 retval = optimset (optimset (), struct (varargin{:})); |
|
84 else |
|
85 print_usage (); |
|
86 endif |
|
87 |
|
88 endfunction |