Mercurial > hg > octave-nkf
annotate scripts/optimization/optimset.m @ 8596:8833c0b18eb2
enable default settings queries in optim funcs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 27 Jan 2009 08:15:08 +0100 |
parents | eeaee297c0da |
children | 11cf7bc4a871 |
rev | line source |
---|---|
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 | |
8304
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
48 ## Return empty structure. |
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
49 ## We're incompatible with Matlab at this point. |
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
50 retval = struct (); |
6226 | 51 endif |
52 elseif (nargs == 1 && ischar (varargin{1})) | |
53 ## Return defaults for named function. | |
54 fcn = varargin{1}; | |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
55 try |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
56 retval = feval (fcn, 'defaults'); |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
57 catch |
6226 | 58 error ("no defaults for function `%s'", fcn); |
59 endif | |
60 elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2})) | |
61 ## Set slots in old from nonempties in new. Should we be checking | |
62 ## to ensure that the field names are expected? | |
63 old = varargin{1}; | |
64 new = varargin{2}; | |
6227 | 65 fnames = fieldnames (old); |
6226 | 66 for [val, key] = new |
6227 | 67 mask = strcmpi (fnames, key); |
68 if (any (mask)) | |
69 key = fnames (mask); | |
6226 | 70 endif |
6227 | 71 old.(key) = val; |
6226 | 72 endfor |
73 retval = old; | |
74 elseif (rem (nargs, 2) && isstruct (varargin{1})) | |
75 ## Set values in old from name/value pairs. | |
76 retval = optimset (varargin{1}, struct (varargin{2:end})); | |
77 elseif (rem (nargs, 2) == 0) | |
78 ## Create struct. Default values are replaced by those specified by | |
79 ## name/value pairs. | |
8304
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
80 retval = optimset (struct (), struct (varargin{:})); |
6226 | 81 else |
82 print_usage (); | |
83 endif | |
84 | |
85 endfunction |