1########################################################################
2##
3## Copyright (C) 2009-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn {} {} polyaffine (@var{f}, @var{mu})
28## Return the coefficients of the polynomial vector @var{f} after an affine
29## transformation.
30##
31## If @var{f} is the vector representing the polynomial f(x), then
32## @code{@var{g} = polyaffine (@var{f}, @var{mu})} is the vector representing:
33##
34## @example
35## g(x) = f( (x - @var{mu}(1)) / @var{mu}(2) )
36## @end example
37##
38## @seealso{polyval, polyfit}
39## @end deftypefn
40
41function g = polyaffine (f, mu)
42
43   if (nargin != 2)
44      print_usage ();
45   endif
46
47   if (! isvector (f))
48      error ("polyaffine: F must be a vector");
49   endif
50
51   if (! isvector (mu) || length (mu) != 2)
52      error ("polyaffine: MU must be a two-element vector");
53   endif
54
55   lf = length (f);
56
57   ## Ensure that f is a row vector
58   if (rows (f) > 1)
59      f = f.';
60   endif
61
62   g = f;
63
64   ## Scale.
65   if (mu(2) != 1)
66     g ./= mu(2) .^ (lf-1:-1:0);
67   endif
68
69   ## Translate.
70   if (mu(1) != 0)
71     w = (-mu(1)) .^ (0:lf-1);
72     ii = lf:-1:1;
73     g = g(ii) * (toeplitz (w) .* pascal (lf, -1));
74     g = g(ii);
75   endif
76
77endfunction
78
79
80%!demo
81%! f = [1/5 4/5 -7/5 -2];
82%! g = polyaffine (f, [1, 1.2]);
83%! x = linspace (-4,4,100);
84%! plot (x,polyval (f, x), x,polyval (g, x));
85%! legend ("original", "affine");
86%! axis ([-4 4 -3 5]);
87%! grid on;
88
89%!test
90%! f = [1/5 4/5 -7/5 -2];
91%! mu = [1, 1.2];
92%! g = polyaffine (f, mu);
93%! x = linspace (-4,4,100);
94%! assert (polyval (f, x, [], mu), polyval (g, x), 1e-10);
95