Mercurial > hg > octave-nkf
annotate scripts/polynomial/roots.m @ 20818:9d2023d1a63c
binoinv.m: Implement binary search algorithm for 28X performance increase (bug #34363).
* binoinv.m: Call new functions scalar_binoinv or vector_binoinv to calculate
binoinv. If there are still uncalculated values then call bin_search_binoinv
to perform binary search for remaining values. Add more BIST tests.
* binoinv.m (scalar_binoinv): New subfunction to calculate binoinv for scalar x.
Stops when x > 1000.
* binoinv.m (vector_binoinv): New subfunction to calculate binoinv for scalar x.
Stops when x > 1000.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Sun, 11 Oct 2015 19:49:40 -0700 |
parents | f1d0f506ee78 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1994-2015 John W. Eaton |
2313 | 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. | |
2313 | 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/>. | |
1025 | 18 |
3368 | 19 ## -*- texinfo -*- |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
20 ## @deftypefn {Function File} {} roots (@var{c}) |
3426 | 21 ## |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
22 ## 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:
19898
diff
changeset
|
23 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
24 ## 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:
19898
diff
changeset
|
25 ## polynomial |
3368 | 26 ## @tex |
27 ## $$ | |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
28 ## c_1 x^{N-1} + \cdots + c_{N-1} x + c_N. |
3368 | 29 ## $$ |
30 ## @end tex | |
6850 | 31 ## @ifnottex |
3426 | 32 ## |
3368 | 33 ## @example |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
34 ## c(1) * x^(N-1) + @dots{} + c(N-1) * x + c(N) |
3368 | 35 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
36 ## |
6850 | 37 ## @end ifnottex |
38 ## | |
39 ## As an example, the following code finds the roots of the quadratic | |
40 ## polynomial | |
41 ## @tex | |
42 ## $$ p(x) = x^2 - 5. $$ | |
43 ## @end tex | |
44 ## @ifnottex | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
45 ## |
6850 | 46 ## @example |
47 ## p(x) = x^2 - 5. | |
48 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
49 ## |
6850 | 50 ## @end ifnottex |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
51 ## |
6850 | 52 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
53 ## @group |
6850 | 54 ## c = [1, 0, -5]; |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
55 ## roots (c) |
6850 | 56 ## @result{} 2.2361 |
57 ## @result{} -2.2361 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
58 ## @end group |
6850 | 59 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
60 ## |
6850 | 61 ## Note that the true result is |
62 ## @tex | |
63 ## $\pm \sqrt{5}$ | |
64 ## @end tex | |
65 ## @ifnottex | |
66 ## @math{+/- sqrt(5)} | |
67 ## @end ifnottex | |
68 ## which is roughly | |
69 ## @tex | |
70 ## $\pm 2.2361$. | |
71 ## @end tex | |
72 ## @ifnottex | |
73 ## @math{+/- 2.2361}. | |
74 ## @end ifnottex | |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 ## @seealso{poly, compan, fzero} |
3368 | 76 ## @end deftypefn |
2311 | 77 |
5428 | 78 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 79 ## Created: 24 December 1993 |
80 ## Adapted-By: jwe | |
81 | |
787 | 82 function r = roots (v) |
904 | 83 |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
84 if (nargin != 1 || (! isvector (v) && ! isempty (v))) |
6046 | 85 print_usage (); |
16969
58188d5a2587
Use isfinite() to replace isinf() | isnan() combination for 30% speed-up.
Rik <rik@octave.org>
parents:
16765
diff
changeset
|
86 elseif (any (! isfinite (v))) |
8664 | 87 error ("roots: inputs must not contain Inf or NaN"); |
1598 | 88 endif |
2325 | 89 |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
90 v = v(:); |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
91 n = numel (v); |
1025 | 92 |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
93 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
94 ## 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
|
95 ## and n - k - l roots of the polynomial are zero. |
904 | 96 |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
97 v_max = max (abs (v)); |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
98 if (isempty (v) || v_max == 0) |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
99 r = []; |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
100 return; |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
101 endif |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
102 |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
103 f = find (v ./ v_max); |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
104 m = numel (f); |
2325 | 105 |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
106 v = v(f(1):f(m)); |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
107 l = numel (v); |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
108 if (l > 1) |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
109 A = diag (ones (1, l-2), -1); |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
110 A(1,:) = -v(2:l) ./ v(1); |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
111 r = eig (A); |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
112 if (f(m) < n) |
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
113 r = [r; zeros(n - f(m), 1)]; |
787 | 114 endif |
115 else | |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
116 r = zeros (n - f(m), 1); |
787 | 117 endif |
2325 | 118 |
787 | 119 endfunction |
7411 | 120 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
121 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
122 %!test |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
123 %! 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
|
124 %! 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
|
125 %! 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
|
126 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
127 %!assert (isempty (roots ([]))) |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
128 %!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
|
129 %!assert (isempty (roots (1))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
130 %!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
|
131 |
16765
80b9a9e1c965
roots.m: Fix bug when input is all zeros (bug #38855)
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
132 %!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
|
133 %!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
|
134 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
135 %!error roots () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
136 %!error roots (1,2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
137 %!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
|
138 %!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
|
139 %!error <inputs must not contain Inf or NaN> roots ([1 NaN 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
140 |