1########################################################################
2##
3## Copyright (C) 1996-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  {} {} polyint (@var{p})
28## @deftypefnx {} {} polyint (@var{p}, @var{k})
29## Return the coefficients of the integral of the polynomial whose
30## coefficients are represented by the vector @var{p}.
31##
32## The variable @var{k} is the constant of integration, which by default is
33## set to zero.
34## @seealso{polyder, polyval}
35## @end deftypefn
36
37function retval = polyint (p, k)
38
39  if (nargin < 1 || nargin > 2)
40    print_usage ();
41  endif
42
43  if (nargin == 1)
44    k = 0;
45  elseif (! isscalar (k))
46    error ("polyint: the constant of integration must be a scalar");
47  endif
48
49  if (! (isvector (p) || isempty (p)))
50    error ("polyint: argument must be a vector");
51  endif
52
53  lp = length (p);
54
55  if (lp == 0)
56    retval = [];
57    return;
58  endif
59
60  if (rows (p) > 1)
61    ## Convert to column vector
62    p = p.';
63  endif
64
65  retval = [(p ./ [lp:-1:1]), k];
66
67endfunction
68
69
70%!test
71%! A = [3, 2, 1];
72%! assert (polyint (A), polyint (A,0));
73%! assert (polyint (A), polyint (A'));
74%! assert (polyint (A), [1, 1, 1, 0]);
75%! assert (polyint (A,1), ones (1,4));
76
77%!test
78%! A = ones (1,8);
79%! B = [length(A):-1:1];
80%! assert (polyint (A), [1./B, 0]);
81
82%!error polyint ()
83%!error polyint (ones (2,2))
84