annotate scripts/optimization/optimget.m @ 33658:b1d6e40ac737 stable tip

NEWS.9.md: Fix typo and minor formatting changes. * etc/NEWS.9.md: Fix typo. Adjust whitespace. Use markdown syntax for code snippets.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 05 Jun 2024 11:27:35 +0200
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 32495
diff changeset
3 ## Copyright (C) 2008-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ##
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ##
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
32248
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
27 ## @deftypefn {} {@var{val} =} optimget (@var{options}, @var{par})
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
28 ## @deftypefnx {} {@var{val} =} optimget (@var{options}, @var{par}, @var{default})
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
29 ## Return the value of the specific parameter @var{par} from the optimization
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
30 ## options structure @var{options} created by @code{optimset}.
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
31 ##
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
32 ## If @var{par} is not defined then return the @var{default} value if
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
33 ## supplied, otherwise return an empty matrix.
18602
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
34 ##
32248
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
35 ## If @var{par} does not exactly match the name of a standard parameter,
32249
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
36 ## @code{optimget} will attempt to match @var{par} to a standard parameter
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
37 ## and will return that parameter's value if a match is found. Matching is
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
38 ## case insensitive and is based on character matching at the start of the
32487
998cba4600e9 doc: grammarcheck m-files before 9.1 release.
Rik <rik@octave.org>
parents: 32249
diff changeset
39 ## parameter name. @code{optimget} produces an error if it finds multiple
998cba4600e9 doc: grammarcheck m-files before 9.1 release.
Rik <rik@octave.org>
parents: 32249
diff changeset
40 ## ambiguous matches. If no standard parameter matches are found a warning is
32249
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
41 ## issued. See @code{optimset} for information about the standard options
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
42 ## list.
32248
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
43 ##
32487
998cba4600e9 doc: grammarcheck m-files before 9.1 release.
Rik <rik@octave.org>
parents: 32249
diff changeset
44 ## Note: Only parameter names from the standard list are considered when
32249
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
45 ## matching short parameter names, and @var{par} will always be expanded to
120134e43537 doc: Refine optimset, optimget docstring on paramter matching. (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32248
diff changeset
46 ## match a standard parameter even if an exact non-standard match exists. The
32495
386d05bbae5a doc: Spellcheck documentation ahead of 9.1 release.
Rik <rik@octave.org>
parents: 32487
diff changeset
47 ## value of a non-standard parameter that is ambiguous with one or more
32248
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
48 ## standard parameters cannot be returned by @code{optimget} and can only be
3df98fee838b doc: Expand optimset, optimget docstrings on parameter matching (bug #64525)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32240
diff changeset
49 ## accessed using @code{getfield} or dot notation for structs.
18602
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
50 ## @seealso{optimset}
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 ## @end deftypefn
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
53 function optval = optimget (options, optname, default)
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
55 if (nargin < 2 || ! isstruct (options) || ! ischar (optname))
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
56 print_usage ();
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
57 endif
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
58
18602
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
59 ## Expand partial-length names into full names
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
60 opts = __all_opts__ ();
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
61 idx = strncmpi (opts, optname, length (optname));
16674
bc79ac595a05 allow abbreviations for optimset and optimget (bug #38999)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
62 nmatch = sum (idx);
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
63
16674
bc79ac595a05 allow abbreviations for optimset and optimget (bug #38999)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
64 if (nmatch == 1)
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
65 optname = opts{idx};
16674
bc79ac595a05 allow abbreviations for optimset and optimget (bug #38999)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
66 elseif (nmatch == 0)
32240
d1bbd7a0982c optimget.m,optimset.m: Emit an error when short name matches multiple properties (bug #64525)
Rik <rik@octave.org>
parents: 31706
diff changeset
67 warning ("optimget: unrecognized option: '%s'", optname);
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
68 else
32240
d1bbd7a0982c optimget.m,optimset.m: Emit an error when short name matches multiple properties (bug #64525)
Rik <rik@octave.org>
parents: 31706
diff changeset
69 fmt = sprintf ("optimget: ambiguous option: '%%s' (%s%%s)",
16674
bc79ac595a05 allow abbreviations for optimset and optimget (bug #38999)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
70 repmat ("%s, ", 1, nmatch-1));
32240
d1bbd7a0982c optimget.m,optimset.m: Emit an error when short name matches multiple properties (bug #64525)
Rik <rik@octave.org>
parents: 31706
diff changeset
71 error (fmt, optname, opts{idx});
8647
06f5dd901f30 implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
72 endif
18602
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
73
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
74 if (isfield (options, optname) && ! isempty (options.(optname)))
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
75 optval = options.(optname);
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 elseif (nargin > 2)
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
77 optval = default;
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78 else
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
79 optval = [];
8304
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80 endif
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 endfunction
eeaee297c0da modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 16674
diff changeset
84
16674
bc79ac595a05 allow abbreviations for optimset and optimget (bug #38999)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
85 %!shared opts
bc79ac595a05 allow abbreviations for optimset and optimget (bug #38999)
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
86 %! opts = optimset ("tolx", 0.1, "maxit", 100);
18602
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
87 %!assert (optimget (opts, "TolX"), 0.1)
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
88 %!assert (optimget (opts, "maxit"), 100)
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
89 %!assert (optimget (opts, "MaxITer"), 100)
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
90 %!assert (optimget (opts, "TolFun"), [])
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
91 %!assert (optimget (opts, "TolFun", 1e-3), 1e-3)
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 16674
diff changeset
92
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
93 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
94 %!error <Invalid call> optimget ()
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
95 %!error <Invalid call> optimget (1)
18602
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
96 %!error optimget (1, "name")
8d4d78285c8e Overhaul optimset/optimget functions for Matlab compatibility (bug #41915).
Rik <rik@octave.org>
parents: 17744
diff changeset
97 %!error optimget (struct (), 2)
32240
d1bbd7a0982c optimget.m,optimset.m: Emit an error when short name matches multiple properties (bug #64525)
Rik <rik@octave.org>
parents: 31706
diff changeset
98 %!warning <unrecognized option: 'foobar'> (optimget (opts, "foobar"));
d1bbd7a0982c optimget.m,optimset.m: Emit an error when short name matches multiple properties (bug #64525)
Rik <rik@octave.org>
parents: 31706
diff changeset
99 %!error <ambiguous option: 'Max'> (optimget (opts, "Max"));