1## Copyright 2015-2017 Oliver Heimlich
2## Copyright 2017 Joel Dahne
3##
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 3 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, see <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @documentencoding UTF-8
19## @defmethod {@@infsupdec} prepad (@var{X}, @var{L})
20## @defmethodx {@@infsupdec} prepad (@var{X}, @var{L}, @var{C})
21## @defmethodx {@@infsupdec} prepad (@var{X}, @var{L}, @var{C}, @var{DIM})
22##
23## Prepend the scalar interval value @var{C} to the interval vector @var{X}
24## until it is of length @var{L}.  If @var{C} is not given, a value of 0 is
25## used.
26##
27## If @code{length (@var{X}) > L}, elements from the beginning of @var{X} are
28## removed until an interval vector of length @var{L} is obtained.
29##
30## If @var{X} is an interval matrix, elements are prepended or removed from
31## each row or column.
32##
33## If the optional argument DIM is given, operate along this dimension.
34##
35## If DIM is larger than the dimension of X, the result will have DIM
36## dimensions.
37##
38## @example
39## @group
40## prepad (infsupdec (1 : 3), 5, 42)
41##   @result{} ans = 1×5 interval vector
42##      [42]_com   [42]_com   [1]_com   [2]_com   [3]_com
43## @end group
44## @end example
45## @seealso{@@infsupdec/reshape, @@infsup/cat, @@infsupdec/postpad, @@infsupdec/resize}
46## @end defmethod
47
48## Author: Oliver Heimlich
49## Keywords: interval
50## Created: 2015-04-19
51
52function x = prepad (x, len, c, dim)
53
54  if (nargin < 2 || nargin > 4)
55    print_usage ();
56    return
57  endif
58
59  if (not (isa (x, "infsupdec")))
60    x = infsupdec (x);
61  endif
62
63  if (nargin < 3)
64    c = infsupdec (0);
65  elseif (not (isa (c, "infsupdec")))
66    c = infsupdec (c);
67  endif
68
69  if (nargin < 4)
70    dim = find (size (x.dec) ~= 1, 1);
71    if (isempty (dim))
72      dim = 1;
73    endif
74  endif
75
76  x.infsup = prepad (x.infsup, len, c.infsup, dim);
77  x.dec = prepad (x.dec, len, c.dec, dim);
78
79endfunction
80
81%!assert (isequal (prepad (infsupdec (2:4), 4, 1), infsupdec (1:4)));
82%!assert (isequal (prepad (infsupdec (0:2), 2, 1), infsupdec (1:2)));
83%!test
84%! if (compare_versions (OCTAVE_VERSION (), "4.0.0", ">="))
85%!   assert (isequal (prepad (infsupdec (0), 10, 0, 3), infsupdec (zeros (1, 1, 10))));
86%! else
87%!   # In Octave 3.8.x it is not possible to increase the number of dimensions.
88%! endif
89%!assert (isequal (prepad (infsupdec (zeros (1, 2, 2)), 3), infsupdec (zeros (1, 3, 2))));
90