annotate scripts/polynomial/roots.m @ 33617:ec2635a02328 bytecode-interpreter tip

maint: Merge default to bytecode-interpreter.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 May 2024 18:29:03 +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: 31706
diff changeset
3 ## Copyright (C) 1994-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/>.
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: 6850
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 ########################################################################
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 944
diff changeset
25
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
26 ## -*- texinfo -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{r} =} roots (@var{c})
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
28 ##
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
29 ## Compute the roots of the polynomial @var{c}.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
30 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
31 ## For a vector @var{c} with @math{N} components, return the roots of the
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
32 ## polynomial
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
33 ## @tex
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
34 ## $$
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
35 ## c_1 x^{N-1} + \cdots + c_{N-1} x + c_N.
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
36 ## $$
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
37 ## @end tex
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
38 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
39 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
40 ## @example
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
41 ## c(1) * x^(N-1) + @dots{} + c(N-1) * x + c(N)
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
42 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
43 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
44 ## @end ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
45 ##
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
46 ## As an example, the following code finds the roots of the quadratic
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
47 ## polynomial
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
48 ## @tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
49 ## $$ p(x) = x^2 - 5. $$
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
50 ## @end tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
51 ## @ifnottex
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
52 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
53 ## @example
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
54 ## p(x) = x^2 - 5.
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
55 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
56 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
57 ## @end ifnottex
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
58 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
59 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
60 ## @group
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
61 ## c = [1, 0, -5];
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
62 ## roots (c)
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
63 ## @result{} 2.2361
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
64 ## @result{} -2.2361
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
65 ## @end group
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
66 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
67 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
68 ## Note that the true result is
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
69 ## @tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
70 ## $\pm \sqrt{5}$
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
71 ## @end tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
72 ## @ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
73 ## @math{+/- sqrt(5)}
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
74 ## @end ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
75 ## which is roughly
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
76 ## @tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
77 ## $\pm 2.2361$.
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
78 ## @end tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
79 ## @ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
80 ## @math{+/- 2.2361}.
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
81 ## @end ifnottex
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
82 ## @seealso{poly, compan, fzero}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
83 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
84
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
85 function r = roots (c)
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 787
diff changeset
86
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
87 if (nargin < 1 || (! isvector (c) && ! isempty (c)))
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
88 print_usage ();
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
89 elseif (any (! isfinite (c)))
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7558
diff changeset
90 error ("roots: inputs must not contain Inf or NaN");
1598
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
91 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
92
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
93 c = c(:);
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
94 n = numel (c);
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 944
diff changeset
95
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
96 ## If c = [ 0 ... 0 c(k+1) ... c(k+l) 0 ... 0 ],
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
97 ## we can remove the leading k zeros,
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
98 ## and n - k - l roots of the polynomial are zero.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 787
diff changeset
99
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
100 c_max = max (abs (c));
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
101 if (isempty (c) || c_max == 0)
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
102 r = [];
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
103 return;
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
104 endif
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
105
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
106 f = find (c ./ c_max);
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
107 m = numel (f);
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
108
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
109 c = c(f(1):f(m));
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
110 l = numel (c);
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
111 if (l > 1)
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
112 A = diag (ones (1, l-2), -1);
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
113 A(1,:) = -c(2:l) ./ c(1);
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
114 r = eig (A);
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
115 if (f(m) < n)
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
116 r = [r; zeros(n - f(m), 1)];
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
117 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
118 else
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
119 r = zeros (n - f(m), 1);
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
120 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
121
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
122 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
123
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
124
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
125 %!test
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
126 %! p = [poly([3 3 3 3]), 0 0 0 0];
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
127 %! r = sort (roots (p));
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
128 %! assert (r, [0; 0; 0; 0; 3; 3; 3; 3], 0.001);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
129
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
130 %!assert (isempty (roots ([])))
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
131 %!assert (isempty (roots ([0 0])))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
132 %!assert (isempty (roots (1)))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
133 %!assert (roots ([1, -6, 11, -6]), [3; 2; 1], sqrt (eps))
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
134
16765
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
135 %!assert (roots ([1e-200, -1e200, 1]), 1e-200)
80b9a9e1c965 roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents: 14868
diff changeset
136 %!assert (roots ([1e-200, -1e200 * 1i, 1]), -1e-200 * 1i)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
137
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27985
diff changeset
138 %!error <Invalid call> roots ()
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
139 %!error roots ([1, 2; 3, 4])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
140 %!error <inputs must not contain Inf or NaN> roots ([1 Inf 1])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
141 %!error <inputs must not contain Inf or NaN> roots ([1 NaN 1])