Mercurial > octave
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 |
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 | 7 ## |
8 ## This file is part of Octave. | |
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 | 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 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
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 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 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 | 25 |
3368 | 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 | 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 | 33 ## @tex |
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 | 36 ## $$ |
37 ## @end tex | |
6850 | 38 ## @ifnottex |
3426 | 39 ## |
3368 | 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 | 42 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
43 ## |
6850 | 44 ## @end ifnottex |
45 ## | |
46 ## As an example, the following code finds the roots of the quadratic | |
47 ## polynomial | |
48 ## @tex | |
49 ## $$ p(x) = x^2 - 5. $$ | |
50 ## @end tex | |
51 ## @ifnottex | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
52 ## |
6850 | 53 ## @example |
54 ## p(x) = x^2 - 5. | |
55 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
56 ## |
6850 | 57 ## @end ifnottex |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
58 ## |
6850 | 59 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
60 ## @group |
6850 | 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 | 63 ## @result{} 2.2361 |
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 | 66 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
67 ## |
6850 | 68 ## Note that the true result is |
69 ## @tex | |
70 ## $\pm \sqrt{5}$ | |
71 ## @end tex | |
72 ## @ifnottex | |
73 ## @math{+/- sqrt(5)} | |
74 ## @end ifnottex | |
75 ## which is roughly | |
76 ## @tex | |
77 ## $\pm 2.2361$. | |
78 ## @end tex | |
79 ## @ifnottex | |
80 ## @math{+/- 2.2361}. | |
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 | 83 ## @end deftypefn |
2311 | 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 | 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 | 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 | 90 error ("roots: inputs must not contain Inf or NaN"); |
1598 | 91 endif |
2325 | 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 | 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 | 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 | 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 | 117 endif |
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 | 120 endif |
2325 | 121 |
787 | 122 endfunction |
7411 | 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]) |