annotate scripts/polynomial/polyval.m @ 29359:7854d5752dd2

maint: merge stable to default.
author John W. Eaton <jwe@octave.org>
date Wed, 10 Feb 2021 10:10:40 -0500
parents 90fea9cc9caa 0a5b15007766
children 796f54d4ddbf
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 ##
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27985
diff changeset
3 ## Copyright (C) 1994-2021 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27252
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/>.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
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
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
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.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
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.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
20 ## 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: 6046
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 ########################################################################
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 561
diff changeset
25
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3202
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
27 ## @deftypefn {} {@var{y} =} polyval (@var{p}, @var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
28 ## @deftypefnx {} {@var{y} =} polyval (@var{p}, @var{x}, [], @var{mu})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
29 ## @deftypefnx {} {[@var{y}, @var{dy}] =} polyval (@var{p}, @var{x}, @var{s})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
30 ## @deftypefnx {} {[@var{y}, @var{dy}] =} polyval (@var{p}, @var{x}, @var{s}, @var{mu})
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
31 ##
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
32 ## Evaluate the polynomial @var{p} at the specified values of @var{x}.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
33 ##
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
34 ## If @var{x} is a vector or matrix, the polynomial is evaluated for each of
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3202
diff changeset
35 ## the elements of @var{x}.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
36 ##
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
37 ## When @var{mu} is present, evaluate the polynomial for
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
38 ## @w{(@var{x} - @var{mu}(1)) / @var{mu}(2)}.
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
39 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
40 ## In addition to evaluating the polynomial, the second output represents the
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
41 ## prediction interval, @var{y} +/- @var{dy}, which contains at least 50% of
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
42 ## the future predictions. To calculate the prediction interval, the
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
43 ## structured variable @var{s}, originating from @code{polyfit}, must be
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
44 ## supplied.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
45 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
46 ## @seealso{polyvalm, polyaffine, polyfit, roots, poly}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3202
diff changeset
47 ## @end deftypefn
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 904
diff changeset
48
12982
5e37369ea13c polyval.m: Return zeros when polynomial is empty (ML Compatibility).
Ben Abbott <bpabbott@mac.com>
parents: 12482
diff changeset
49 function [y, dy] = polyval (p, x, s = [], mu)
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
50
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
51 if (nargin < 2 || (nargout == 2 && nargin < 3))
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
52 print_usage ();
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
53 endif
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
54
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
55 ## Algorithm requires floating point values
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
56 if (! isfloat (p) || (! isvector (p) && ! isempty (p)))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
57 error ("polyval: P must be a numeric floating point vector");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
58 endif
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
59 if (! isfloat (x))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
60 error ("polyval: X must be numeric floating point");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
61 endif
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
62
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
63 if (nargout > 1)
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
64 if (isempty (s))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
65 error ("polyval: S input is required for DY output argument");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
66 elseif (isstruct (s))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
67 if (! all (ismember ({"R", "normr", "df"}, fieldnames (s))))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
68 error ("polyval: S input is missing required fields");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
69 endif
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
70 else
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
71 error ("polyval: S input must be a structure");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
72 endif
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
73 endif
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
74
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
75 if (nargin == 4 && (! isfloat (mu) || numel (mu) < 2))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
76 error ("polyval: MU must be numeric floating point with 2 values");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
77 endif
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
78
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
79 if (isempty (p) || isempty (x))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
80 if (isa (p, "single") || isa (x, "single"))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
81 y = zeros (size (x), "single");
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
82 else
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
83 y = zeros (size (x));
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
84 endif
12982
5e37369ea13c polyval.m: Return zeros when polynomial is empty (ML Compatibility).
Ben Abbott <bpabbott@mac.com>
parents: 12482
diff changeset
85 return;
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
86 endif
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
87
21757
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
88 if (nargin == 4)
12482
3244c6e0af4a polynomial/polyval.m: Compute offset/normalization only when needed.
Ben Abbott <bpabbott@mac.com>
parents: 12481
diff changeset
89 x = (x - mu(1)) / mu(2);
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
90 endif
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
91
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
92 n = numel (p) - 1;
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
93 y = p(1) * ones (size (x), class (x));
11110
0be2d25700a7 polynomial/polyval.m: Use Horner's method.
Marco Caliari <marco.caliari@univr.it>
parents: 10687
diff changeset
94 for i = 2:n+1
12481
97d103fcb6dd polynomial/polyval.m: Minor simplification in polynomial evaluation
Ben Abbott <bpabbott@mac.com>
parents: 12475
diff changeset
95 y = y .* x + p(i);
11110
0be2d25700a7 polynomial/polyval.m: Use Horner's method.
Marco Caliari <marco.caliari@univr.it>
parents: 10687
diff changeset
96 endfor
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
97
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
98 if (nargout > 1)
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
99 ## Note: the F-Distribution is generally considered to be single-sided.
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
100 ## http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
101 ## t = finv (1-alpha, s.df, s.df);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
102 ## dy = t * sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df)
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
103 ## If my inference is correct, then t must equal 1 for polyval.
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
104 ## This is because finv (0.5, n, n) = 1.0 for any n.
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
105 k = numel (x);
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
106 A = (x(:) * ones (1, n+1)) .^ (ones (k, 1) * (n:-1:0));
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
107 dy = sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df);
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
108 dy = reshape (dy, size (x));
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
109 endif
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 904
diff changeset
110
561
e79ff1f4df3c [project @ 1994-07-25 22:32:08 by jwe]
jwe
parents:
diff changeset
111 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
112
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
113
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
114 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
115 %! r = 0:10:50;
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
116 %! p = poly (r);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
117 %! p = p / max (abs (p));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
118 %! x = linspace (0,50,11);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
119 %! y = polyval (p,x) + 0.25*sin (100*x);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
120 %! [pf, s] = polyfit (x, y, numel (r));
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
121 %! [y1, delta] = polyval (pf, x, s);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
122 %! expected = [0.37235, 0.35854, 0.32231, 0.32448, 0.31328, ...
21757
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
123 %! 0.32036, 0.31328, 0.32448, 0.32231, 0.35854, 0.37235];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
124 %! assert (delta, expected, 0.00001);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
125
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
126 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
127 %! x = 10 + (-2:2);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
128 %! y = [0, 0, 1, 0, 2];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
129 %! p = polyfit (x, y, numel (x) - 1);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
130 %! [pn, s, mu] = polyfit (x, y, numel (x) - 1);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
131 %! y1 = polyval (p, x);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
132 %! yn = polyval (pn, x, [], mu);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
133 %! assert (y1, y, sqrt (eps));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
134 %! assert (yn, y, sqrt (eps));
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
135
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
136 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
137 %! p = [0, 1, 0];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
138 %! x = 1:10;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
139 %! assert (x, polyval (p,x), eps);
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
140 %! x = x(:);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
141 %! assert (x, polyval (p,x), eps);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
142 %! x = reshape (x, [2, 5]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
143 %! assert (x, polyval (p,x), eps);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
144 %! x = reshape (x, [5, 2]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
145 %! assert (x, polyval (p,x), eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
146 %! x = reshape (x, [1, 1, 5, 2]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
147 %! assert (x, polyval (p,x), eps);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
148
12475
009068efc66d Properly evaluate zeroth order polynomials.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
149 %!test
009068efc66d Properly evaluate zeroth order polynomials.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
150 %! p = [1];
009068efc66d Properly evaluate zeroth order polynomials.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
151 %! x = 1:10;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
152 %! y = ones (size (x));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
153 %! assert (y, polyval (p,x), eps);
12475
009068efc66d Properly evaluate zeroth order polynomials.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
154 %! x = x(:);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
155 %! y = ones (size (x));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
156 %! assert (y, polyval (p,x), eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
157 %! x = reshape (x, [2, 5]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
158 %! y = ones (size (x));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
159 %! assert (y, polyval (p,x), eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
160 %! x = reshape (x, [5, 2]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
161 %! y = ones (size (x));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
162 %! assert (y, polyval (p,x), eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
163 %! x = reshape (x, [1, 1, 5, 2]);
12475
009068efc66d Properly evaluate zeroth order polynomials.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
164
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
165 ## Test empty combinations
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
166 %!assert (polyval ([], 1:10), zeros (1, 10))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
167 %!assert (class (polyval (single ([]), 1:10)), "single")
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
168 %!assert (class (polyval ([], single (1:10))), "single")
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
169 %!assert (polyval (1, []), [])
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
170 %!assert (polyval ([], []), [])
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
171 %!assert (polyval (1, zeros (0,3)), zeros (0, 3))
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
172 %!assert (class (polyval (single (1), [])), "single")
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
173 %!assert (class (polyval (1, single ([]))), "single")
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
174 %!assert (class (polyval (single ([]), [])), "single")
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
175 %!assert (class (polyval ([], single ([]))), "single")
21757
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
176
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
177 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
178 %!error <Invalid call> polyval ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
179 %!error <Invalid call> polyval (1)
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
180 %!error <Invalid call> [y, dy] = polyval (1, 2)
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
181 %!error <P must be a numeric floating point vector> polyval ({1, 0}, 0:10)
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
182 %!error <P must be a numeric floating point vector> polyval (int8 ([1]), 0:10)
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
183 %!error <P must be a numeric floating point vector> polyval ([1,0;0,1], 0:10)
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
184 %!error <X must be numeric floating point> polyval ([1,0], {0:10})
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
185 %!error <X must be numeric floating point> polyval ([1,0], int8 (0:10))
21757
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
186 %!error <S input is required> [y, dy] = polyval (1, 1, [])
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
187 %!error <S input is missing required fields>
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
188 %! [y, dy] = polyval (1, 1, struct ("T", 0, "normr", 1, "df", 2));
21757
c94fde6d7c27 polyval.m: Standardize error messages and add input validation BIST tests.
Rik <rik@octave.org>
parents: 21755
diff changeset
189 %!error <S input must be a structure> [y, dy] = polyval (1, 1, 2)
27252
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
190 %!error <MU must be numeric floating point with 2 values>
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
191 %! polyval (1, 1, [], {1, 2});
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
192 %!error <MU must be numeric floating point with 2 values>
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
193 %! polyval (1, 1, [], int8 ([1,2]));
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
194 %!error <MU must be numeric floating point with 2 values>
23523700b6b8 polyval.m: Overhaul input validation (bug #56632).
Rik <rik@octave.org>
parents: 26376
diff changeset
195 %! polyval (1, 1, [], [1]);