Mercurial > hg > octave-nkf
annotate scripts/polynomial/polyaffine.m @ 12118:973f585cfdf2 release-3-2-x
include PTHREAD_CFLAGS in LINK_DEPS for liboctave
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 22 Jan 2010 10:21:33 +0100 |
parents | a8be2f7c81ee |
children | 09da0bd91412 |
rev | line source |
---|---|
9114
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
1 ## Copyright (C) 2009 Tony Richardson, Jaroslav Hajek |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
2 ## |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
3 ## This program is free software; you can redistribute it and/or modify |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
4 ## it under the terms of the GNU General Public License as published by |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
5 ## the Free Software Foundation; either version 3 of the License, or |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
6 ## (at your option) any later version. |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
7 ## |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
8 ## This program is distributed in the hope that it will be useful, |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
11 ## GNU General Public License for more details. |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
12 ## |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
13 ## You should have received a copy of the GNU General Public License |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>. |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
15 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
16 ## -*- texinfo -*- |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
17 ## @deftypefn {Function File} {} polyaffine (@var{f}, @var{mu}) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
18 ## Return the coefficients of the polynomial whose coefficients are given by |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
19 ## vector @var{f} after an affine tranformation. If @var{f} is the vector |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 ## representing the polynomial f(x), then @var{g} = polytrans (@var{f}, |
9116
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
21 ## @var{mu}) is the vector representing |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
22 ## @example |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
23 ## g(x) = f((x-@var{mu}(1))/@var{mu}(2)). |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
24 ## @end example |
9114
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
25 ## |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 ## @seealso{polyval} |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
27 ## @end deftypefn |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
28 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
29 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
30 function g = polyaffine (f, mu) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
31 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 if (nargin != 2) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 print_usage (); |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
34 endif |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
35 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
36 if (! isvector (f)) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
37 error ("polyaffine: first argument must be a vector."); |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
38 endif |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
39 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
40 if (! isvector (mu) || length (mu) != 2) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
41 error ("polyaffine: second argument must be a two-element vector."); |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
42 endif |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
43 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
44 lf = length (f); |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
45 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
46 ## Ensure that f is a row vector |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
47 if (rows (f) > 1) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
48 f = f.'; |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
49 endif |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
50 |
9116
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
51 g = f; |
9114
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
52 |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
53 ## Scale. |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
54 if (mu(2) != 1) |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
55 g = g ./ (mu(2) .^ (lf-1:-1:0)); |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
56 endif |
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 |
9116
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
58 ## Translate. |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
59 if (mu(1) != 0) |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
60 w = (-mu(1)) .^ (0:lf-1); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
61 ii = lf:-1:1; |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
62 g = g(ii) * (toeplitz (w) .* pascal (lf, -1)); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
63 g = g(ii); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
64 endif |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
65 |
9114
79e90381b402
merge polytrans & polyscale -> polyaffine, make compatible with polyval
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
66 endfunction |
9116
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
67 |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
68 %!test |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
69 %! f = [1/5 4/5 -7/5 -2]; |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
70 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
71 %! mu = [1, 1.2]; |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
72 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
73 %! g = polyaffine (f, mu); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
74 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
75 %! x = linspace (-4, 4, 100); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
76 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
77 %! assert (polyval(f, x, [], mu), polyval (g, x), 1e-10); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
78 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
79 %!demo |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
80 %! f = [1/5 4/5 -7/5 -2]; |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
81 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
82 %! g = polyaffine (f, [1, 1.2]); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
83 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
84 %! x = linspace (-4, 4, 100); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
85 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
86 %! plot(x, polyval (f, x), x, polyval (g, x)); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
87 %! |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
88 %! axis ([-4 4 -3 5]); |
a8be2f7c81ee
improve docs for polyaffine, fix bugs
Jaroslav Hajek <highegg@gmail.com>
parents:
9114
diff
changeset
|
89 %! grid ("on"); |