annotate scripts/optimization/optimset.m @ 13183:cc1fd6a58151

optimset.m: When called with 1 output argument, return a struct with all possible options initialized to []. * optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
author Rik <octave@nomad.inbox5.com>
date Tue, 20 Sep 2011 21:19:56 -0700
parents fd0a3ac60b0e
children 519390f1b67f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11451
diff changeset
1 ## Copyright (C) 2007-2011 John W. Eaton
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
3 ##
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
5 ##
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6227
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6227
diff changeset
9 ## your option) any later version.
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
10 ##
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
15 ##
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6227
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6227
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
19
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
21 ## @deftypefn {Function File} {} optimset ()
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
22 ## @deftypefnx {Function File} {} optimset (@var{par}, @var{val}, @dots{})
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
23 ## @deftypefnx {Function File} {} optimset (@var{old}, @var{par}, @var{val}, @dots{})
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
24 ## @deftypefnx {Function File} {} optimset (@var{old}, @var{new})
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
25 ## Create options struct for optimization functions.
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
27
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
28 function retval = optimset (varargin)
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
29
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
30 nargs = nargin ();
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
31
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
32 ## Add more as needed.
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
33 opts = __all_opts__ ();
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
34
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
35 if (nargs == 0)
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
36 if (nargout == 0)
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
37 ## Display possibilities.
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
38 puts ("\nAll possible optimization options:\n\n");
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
39 printf (" %s\n", opts{:});
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
40 puts ("\n");
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
41 else
13183
cc1fd6a58151 optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
42 ## Return struct with all options initialized to []
cc1fd6a58151 optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
43 retval = cell2struct (repmat ({[]}, size (opts)), opts, 2);
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
44 endif
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
45 elseif (nargs == 1 && ischar (varargin{1}))
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
46 ## Return defaults for named function.
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
47 fcn = varargin{1};
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
48 try
11407
ed827ffa5a43 optimset, optimget, __all_opts__: consistently use lower case for storing and searching options
John W. Eaton <jwe@octave.org>
parents: 11346
diff changeset
49 retval = feval (fcn, "defaults");
8596
8833c0b18eb2 enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
50 catch
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
51 error ("optimset: no defaults for function `%s'", fcn);
8598
11cf7bc4a871 fix invalid block end
Jaroslav Hajek <highegg@gmail.com>
parents: 8596
diff changeset
52 end_try_catch
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
53 elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2}))
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
54 ## Set slots in old from nonempties in new. Should we be checking
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
55 ## to ensure that the field names are expected?
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
56 old = varargin{1};
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
57 new = varargin{2};
6227
4c252a611d47 [project @ 2007-01-05 20:23:59 by jwe]
jwe
parents: 6226
diff changeset
58 fnames = fieldnames (old);
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
59 ## skip validation if we're in the internal query
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
60 validation = ! isempty (opts);
11407
ed827ffa5a43 optimset, optimget, __all_opts__: consistently use lower case for storing and searching options
John W. Eaton <jwe@octave.org>
parents: 11346
diff changeset
61 lopts = tolower (opts);
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
62 for [val, key] = new
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
63 if (validation)
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
64 ## Case insensitive lookup in all options.
11451
48cb431db87b optimset: remove "i" option from call to lookup
John W. Eaton <jwe@octave.org>
parents: 11407
diff changeset
65 i = lookup (lopts, tolower (key));
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
66 ## Validate option.
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
67 if (i > 0 && strcmpi (opts{i}, key))
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
68 ## Use correct case.
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
69 key = opts{i};
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
70 else
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
71 warning ("unrecognized option: %s", key);
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
72 endif
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
73 endif
6227
4c252a611d47 [project @ 2007-01-05 20:23:59 by jwe]
jwe
parents: 6226
diff changeset
74 old.(key) = val;
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
75 endfor
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
76 retval = old;
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
77 elseif (rem (nargs, 2) && isstruct (varargin{1}))
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
78 ## Set values in old from name/value pairs.
11121
134d357e1c03 optimization/optimset.m: Use cell2struct instead of struct to handle cell arrays as options
Olaf Till <olaf.till@uni-jena.de>
parents: 10793
diff changeset
79 pairs = reshape (varargin(2:end), 2, []);
134d357e1c03 optimization/optimset.m: Use cell2struct instead of struct to handle cell arrays as options
Olaf Till <olaf.till@uni-jena.de>
parents: 10793
diff changeset
80 retval = optimset (varargin{1}, cell2struct (pairs(2, :), pairs(1, :), 2));
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
81 elseif (rem (nargs, 2) == 0)
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
82 ## Create struct. Default values are replaced by those specified by
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
83 ## name/value pairs.
11121
134d357e1c03 optimization/optimset.m: Use cell2struct instead of struct to handle cell arrays as options
Olaf Till <olaf.till@uni-jena.de>
parents: 10793
diff changeset
84 pairs = reshape (varargin, 2, []);
134d357e1c03 optimization/optimset.m: Use cell2struct instead of struct to handle cell arrays as options
Olaf Till <olaf.till@uni-jena.de>
parents: 10793
diff changeset
85 retval = optimset (struct (), cell2struct (pairs(2, :), pairs(1, :), 2));
6226
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
86 else
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
87 print_usage ();
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
88 endif
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
89
984d85191d0d [project @ 2007-01-05 18:41:24 by jwe]
jwe
parents:
diff changeset
90 endfunction
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
91
13183
cc1fd6a58151 optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
92
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
93 %!assert (optimget (optimset ('tolx', 1e-2), 'tOLx'), 1e-2)
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8598
diff changeset
94 %!assert (isfield (optimset ('tolFun', 1e-3), 'TolFun'))
13183
cc1fd6a58151 optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
95
cc1fd6a58151 optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
96 %!error (optimset ("%NOT_A_REAL_FUNCTION_NAME%"))
cc1fd6a58151 optimset.m: When called with 1 output argument, return a struct with all possible options initialized to [].
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
97