Mercurial > hg > octave-nkf
annotate scripts/polynomial/polyout.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 614505385171 |
children | 4d917a6a858b |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14104
diff
changeset
|
1 ## Copyright (C) 1995-2012 Auburn University. All rights reserved. |
3424 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
7016 | 6 ## under the terms of the GNU General Public License as published by |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
3424 | 9 ## |
7016 | 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. | |
3424 | 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/>. | |
3424 | 18 |
19 ## -*- texinfo -*- | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} polyout (@var{c}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {} polyout (@var{c}, @var{x}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{str} =} polyout (@dots{}) |
3457 | 23 ## Write formatted polynomial |
5016 | 24 ## @tex |
25 ## $$ c(x) = c_1 x^n + \ldots + c_n x + c_{n+1} $$ | |
26 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8286
diff
changeset
|
27 ## @ifnottex |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
28 ## |
3424 | 29 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## c(x) = c(1) * x^n + @dots{} + c(n) x + c(n+1) |
3424 | 31 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
32 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8286
diff
changeset
|
33 ## @end ifnottex |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
13963
diff
changeset
|
34 ## and return it as a string or write it to the screen (if @var{nargout} is |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
13963
diff
changeset
|
35 ## zero). @var{x} defaults to the string @code{"s"}. |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
13963
diff
changeset
|
36 ## @seealso{polyreduce} |
5642 | 37 ## @end deftypefn |
3424 | 38 |
39 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> | |
40 ## Created: May 1995 | |
41 ## Nov 1998: Correctly handles complex coefficients | |
3426 | 42 |
3424 | 43 function y = polyout (c, x) |
44 | |
3457 | 45 if (nargin < 1) || (nargin > 2) || (nargout < 0) || (nargout > 1) |
6046 | 46 print_usage (); |
3424 | 47 endif |
48 | |
4030 | 49 if (! isvector (c)) |
8685
983ac67dc3d4
polyout.m: additional style fixes
John W. Eaton <jwe@octave.org>
parents:
8684
diff
changeset
|
50 error ("polyout: first argument must be a vector"); |
3424 | 51 endif |
3426 | 52 |
3424 | 53 if (nargin == 1) |
54 x = "s"; | |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
55 elseif (! ischar (x)) |
8685
983ac67dc3d4
polyout.m: additional style fixes
John W. Eaton <jwe@octave.org>
parents:
8684
diff
changeset
|
56 error ("polyout: second argument must be a string"); |
3424 | 57 endif |
58 | |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
59 n = length (c); |
3424 | 60 if(n > 0) |
61 n1 = n+1; | |
62 | |
4898 | 63 tmp = coeff (c(1)); |
3457 | 64 for ii = 2:n |
65 if (real (c(ii)) < 0) | |
10549 | 66 ns = " - "; |
67 c(ii) = -c(ii); | |
3457 | 68 else |
69 ns = " + "; | |
70 endif | |
3424 | 71 |
4898 | 72 tmp = sprintf ("%s*%s^%d%s%s", tmp, x, n1-ii, ns, coeff (c(ii))); |
3426 | 73 |
3424 | 74 endfor |
75 else | |
76 tmp = " "; | |
77 endif | |
78 | |
79 if(nargout == 0) | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
80 disp (tmp); |
3424 | 81 else |
82 y = tmp; | |
83 endif | |
84 | |
85 endfunction | |
4898 | 86 |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
87 function str = coeff (c) |
4898 | 88 if (imag (c)) |
89 if (real (c)) | |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
90 str = sprintf ("(%s)", num2str (c, 5)); |
4898 | 91 else |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
92 str = num2str (c, 5); |
4898 | 93 endif |
94 else | |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
95 str = num2str (c, 5); |
4898 | 96 endif |
8684
6d15bc6c4c15
polyout.m: Replace com2str with num2str.
Thomas D. Dean <tomdean@speakeasy.org>
parents:
8517
diff
changeset
|
97 endfunction |
12804
a44ec6d33cc9
codesprint: Wrote 5 tests for polyout.m
David Wells <drwells@vt.edu>
parents:
11589
diff
changeset
|
98 |
a44ec6d33cc9
codesprint: Wrote 5 tests for polyout.m
David Wells <drwells@vt.edu>
parents:
11589
diff
changeset
|
99 %!assert (polyout ([3 2 1]), '3*s^2 + 2*s^1 + 1') |
a44ec6d33cc9
codesprint: Wrote 5 tests for polyout.m
David Wells <drwells@vt.edu>
parents:
11589
diff
changeset
|
100 %!assert (polyout ([3 2 1], 'x'), '3*x^2 + 2*x^1 + 1') |
a44ec6d33cc9
codesprint: Wrote 5 tests for polyout.m
David Wells <drwells@vt.edu>
parents:
11589
diff
changeset
|
101 %!assert (polyout ([3 2 1], 'wxyz'), '3*wxyz^2 + 2*wxyz^1 + 1') |
a44ec6d33cc9
codesprint: Wrote 5 tests for polyout.m
David Wells <drwells@vt.edu>
parents:
11589
diff
changeset
|
102 %!assert (polyout ([5 4 3 2 1], '1'),'5*1^4 + 4*1^3 + 3*1^2 + 2*1^1 + 1') |
a44ec6d33cc9
codesprint: Wrote 5 tests for polyout.m
David Wells <drwells@vt.edu>
parents:
11589
diff
changeset
|
103 %!error polyout ([]) |