Mercurial > hg > octave-lyh
annotate scripts/polynomial/roots.m @ 14868:5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
* lin2mu.m, loadaudio.m, wavread.m, accumarray.m, bicubic.m, celldisp.m,
colon.m, cplxpair.m, dblquad.m, divergence.m, genvarname.m, gradient.m,
int2str.m, interp1.m, interp1q.m, interp2.m, interpn.m, loadobj.m, nthargout.m,
__isequal__.m, __splinen__.m, quadgk.m, quadl.m, quadv.m, rat.m, rot90.m,
rotdim.m, saveobj.m, subsindex.m, triplequad.m, delaunay3.m, griddata.m,
inpolygon.m, tsearchn.m, voronoi.m, get_first_help_sentence.m, which.m,
gray2ind.m, pink.m, dlmwrite.m, strread.m, textread.m, textscan.m, housh.m,
ishermitian.m, issymmetric.m, krylov.m, logm.m, null.m, rref.m,
compare_versions.m, copyfile.m, dump_prefs.m, edit.m, fileparts.m,
getappdata.m, isappdata.m, movefile.m, orderfields.m, parseparams.m,
__xzip__.m, rmappdata.m, setappdata.m, swapbytes.m, unpack.m, ver.m, fminbnd.m,
fminunc.m, fsolve.m, glpk.m, lsqnonneg.m, qp.m, sqp.m, configure_make.m,
copy_files.m, describe.m, get_description.m, get_forge_pkg.m, install.m,
installed_packages.m, is_architecture_dependent.m, load_package_dirs.m,
print_package_description.m, rebuild.m, repackage.m, save_order.m, shell.m,
allchild.m, ancestor.m, area.m, axes.m, axis.m, clabel.m, close.m, colorbar.m,
comet.m, comet3.m, contour.m, cylinder.m, ezmesh.m, ezsurf.m, findobj.m,
fplot.m, hist.m, isocolors.m, isonormals.m, isosurface.m, isprop.m, legend.m,
mesh.m, meshz.m, pareto.m, pcolor.m, peaks.m, plot3.m, plotmatrix.m, plotyy.m,
polar.m, print.m, __add_datasource__.m, __add_default_menu__.m,
__axes_limits__.m, __bar__.m, __clabel__.m, __contour__.m, __errcomm__.m,
__errplot__.m, __ezplot__.m, __file_filter__.m, __fltk_print__.m,
__ghostscript__.m, __gnuplot_print__.m, __go_draw_axes__.m,
__go_draw_figure__.m, __interp_cube__.m, __marching_cube__.m, __patch__.m,
__pie__.m, __plt__.m, __print_parse_opts__.m, __quiver__.m, __scatter__.m,
__stem__.m, __tight_eps_bbox__.m, __uigetdir_fltk__.m, __uigetfile_fltk__.m,
__uiputfile_fltk__.m, quiver.m, quiver3.m, rectangle.m, refreshdata.m,
ribbon.m, scatter.m, semilogy.m, shading.m, slice.m, subplot.m, surface.m,
surfl.m, surfnorm.m, text.m, uigetfile.m, uiputfile.m, whitebg.m, deconv.m,
mkpp.m, pchip.m, polyaffine.m, polyder.m, polygcd.m, polyout.m, polyval.m,
ppint.m, ppjumps.m, ppval.m, residue.m, roots.m, spline.m, splinefit.m,
addpref.m, getpref.m, setpref.m, ismember.m, setxor.m, arch_fit.m, arch_rnd.m,
arch_test.m, autoreg_matrix.m, diffpara.m, fftconv.m, filter2.m, hanning.m,
hurst.m, periodogram.m, triangle_sw.m, sinc.m, spectral_xdf.m, spencer.m,
stft.m, synthesis.m, unwrap.m, yulewalker.m, bicgstab.m, gmres.m, pcg.m, pcr.m,
__sprand_impl__.m, speye.m, spfun.m, sprandn.m, spstats.m, svds.m,
treelayout.m, treeplot.m, bessel.m, factor.m, legendre.m, perms.m, primes.m,
magic.m, toeplitz.m, corr.m, cov.m, mean.m, median.m, mode.m, qqplot.m,
quantile.m, ranks.m, zscore.m, logistic_regression_likelihood.m,
bartlett_test.m, chisquare_test_homogeneity.m, chisquare_test_independence.m,
kolmogorov_smirnov_test.m, run_test.m, u_test.m, wilcoxon_test.m, z_test.m,
z_test_2.m, bin2dec.m, dec2base.m, mat2str.m, strcat.m, strchr.m, strjust.m,
strtok.m, substr.m, untabify.m, assert.m, demo.m, example.m, fail.m, speed.m,
test.m, now.m: Use Octave coding conventions for cuddling parentheses in
scripts directory.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 17 Jul 2012 07:08:39 -0700 |
parents | f3d52523cde1 |
children | 80b9a9e1c965 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14104
diff
changeset
|
1 ## Copyright (C) 1994-2012 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 -*- |
20 ## @deftypefn {Function File} {} roots (@var{v}) | |
3426 | 21 ## |
3499 | 22 ## For a vector @var{v} with @math{N} components, return |
3368 | 23 ## the roots of the polynomial |
24 ## @tex | |
25 ## $$ | |
26 ## v_1 z^{N-1} + \cdots + v_{N-1} z + v_N. | |
27 ## $$ | |
28 ## @end tex | |
6850 | 29 ## @ifnottex |
3426 | 30 ## |
3368 | 31 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## v(1) * z^(N-1) + @dots{} + v(N-1) * z + v(N) |
3368 | 33 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
34 ## |
6850 | 35 ## @end ifnottex |
36 ## | |
37 ## As an example, the following code finds the roots of the quadratic | |
38 ## polynomial | |
39 ## @tex | |
40 ## $$ p(x) = x^2 - 5. $$ | |
41 ## @end tex | |
42 ## @ifnottex | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
43 ## |
6850 | 44 ## @example |
45 ## p(x) = x^2 - 5. | |
46 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
47 ## |
6850 | 48 ## @end ifnottex |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
49 ## |
6850 | 50 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
51 ## @group |
6850 | 52 ## c = [1, 0, -5]; |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 ## roots (c) |
6850 | 54 ## @result{} 2.2361 |
55 ## @result{} -2.2361 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
56 ## @end group |
6850 | 57 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
58 ## |
6850 | 59 ## Note that the true result is |
60 ## @tex | |
61 ## $\pm \sqrt{5}$ | |
62 ## @end tex | |
63 ## @ifnottex | |
64 ## @math{+/- sqrt(5)} | |
65 ## @end ifnottex | |
66 ## which is roughly | |
67 ## @tex | |
68 ## $\pm 2.2361$. | |
69 ## @end tex | |
70 ## @ifnottex | |
71 ## @math{+/- 2.2361}. | |
72 ## @end ifnottex | |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
73 ## @seealso{poly, compan, fzero} |
3368 | 74 ## @end deftypefn |
2311 | 75 |
5428 | 76 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 77 ## Created: 24 December 1993 |
78 ## Adapted-By: jwe | |
79 | |
787 | 80 function r = roots (v) |
904 | 81 |
6372 | 82 if (nargin != 1 || min (size (v)) > 1) |
6046 | 83 print_usage (); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
84 elseif (any (isnan (v) | isinf (v))) |
8664 | 85 error ("roots: inputs must not contain Inf or NaN"); |
1598 | 86 endif |
2325 | 87 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
88 n = numel (v); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
89 v = v(:); |
1025 | 90 |
2303 | 91 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
2325 | 92 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904 | 93 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
94 if (isempty (v)) |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
95 f = v; |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
96 else |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
97 f = find (v ./ max (abs (v))); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
98 endif |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
99 m = numel (f); |
2325 | 100 |
1598 | 101 if (m > 0 && n > 1) |
102 v = v(f(1):f(m)); | |
787 | 103 l = max (size (v)); |
104 if (l > 1) | |
105 A = diag (ones (1, l-2), -1); | |
1598 | 106 A(1,:) = -v(2:l) ./ v(1); |
2325 | 107 r = eig (A); |
1598 | 108 if (f(m) < n) |
109 tmp = zeros (n - f(m), 1); | |
3426 | 110 r = [r; tmp]; |
787 | 111 endif |
112 else | |
113 r = zeros (n - f(m), 1); | |
114 endif | |
115 else | |
1598 | 116 r = []; |
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 ([]))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
128 %!assert (isempty (roots (1))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
129 %!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
|
130 |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
131 %!assert(roots ([1e-200, -1e200, 1]), 1e-200) |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
132 %!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
|
133 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
134 %!error roots () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
135 %!error roots (1,2) |
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; 3, 4]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
137 %!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
|
138 %!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
|
139 |