53 octave_idx_type n = j1 * nr + ((j2 - j1) * (nr-(j1-k) + nr-(j2-1-k))) / 2;
55 T *rvec = r.fortran_vec ();
59 rvec = std::copy (avec + ii, avec + nr, rvec);
72 std::fill (rvec, rvec + ii, T ());
73 std::copy (avec + ii, avec + nr, rvec + ii);
95 = ((j2 - j1) * ((j1+1-k) + (j2-k))) / 2 + (nc - j2) * nr;
101 rvec = std::copy (avec, avec + ii, rvec);
110 T *rvec = r.fortran_vec ();
114 std::copy (avec, avec + ii, rvec);
115 std::fill (rvec + ii, rvec + nr, T ());
133 error (
"tril: \"pack\" not implemented for sparse matrices");
142 if (m.
ridx (i) < j-k)
155 error (
"triu: \"pack\" not implemented for sparse matrices");
164 if (m.
ridx (i) > j-k)
190 bool lower = name ==
"tril";
193 int nargin = args.
length ();
196 if (nargin >= 2 && args(nargin-1).is_string ())
198 pack = args(nargin-1).string_value () ==
"pack";
204 k = args(1).int_value (
true);
210 if (nargin < 1 || nargin > 2)
218 error (
"%s: need a 2-D matrix", name.c_str ());
219 else if (k < -dims (0) || k > dims(1))
220 error (
"%s: requested diagonal out of range", name.c_str ());
245 #define ARRAYCASE(TYP) \
247 retval = do_trilu (arg.TYP ## _array_value (), k, lower, pack); \
268 error (
"%s: \"pack\" not implemented for class %s",
274 if (arg.
numel () == 0)
290 std::list<octave_value_list> idx_tmp;
291 ov_idx(1) =
static_cast<double> (nc+1);
292 ov_idx(0) =
Range (1, nr);
293 idx_tmp.push_back (ov_idx);
294 ov_idx(1) =
static_cast<double> (nc);
306 ov_idx(1) =
static_cast<double> (j);
307 ov_idx(0) =
Range (nr_limit, nr);
308 std::list<octave_value_list> idx;
309 idx.push_back (ov_idx);
324 ov_idx(1) =
static_cast<double> (j);
325 ov_idx(0) =
Range (1, nr_limit);
326 std::list<octave_value_list> idx;
327 idx.push_back (ov_idx);
347 @deftypefn {Function File} {} tril (@var{A})\n\
348 @deftypefnx {Function File} {} tril (@var{A}, @var{k})\n\
349 @deftypefnx {Function File} {} tril (@var{A}, @var{k}, @var{pack})\n\
350 @deftypefnx {Function File} {} triu (@var{A})\n\
351 @deftypefnx {Function File} {} triu (@var{A}, @var{k})\n\
352 @deftypefnx {Function File} {} triu (@var{A}, @var{k}, @var{pack})\n\
353 Return a new matrix formed by extracting the lower (@code{tril})\n\
354 or upper (@code{triu}) triangular part of the matrix @var{A}, and\n\
355 setting all other elements to zero. The second argument is optional,\n\
356 and specifies how many diagonals above or below the main diagonal should\n\
357 also be set to zero.\n\
359 The default value of @var{k} is zero, so that @code{triu} and\n\
360 @code{tril} normally include the main diagonal as part of the result.\n\
362 If the value of @var{k} is nonzero integer, the selection of elements\n\
363 starts at an offset of @var{k} diagonals above or below the main\n\
364 diagonal; above for positive @var{k} and below for negative @var{k}.\n\
366 The absolute value of @var{k} must not be greater than the number of\n\
367 sub-diagonals or super-diagonals.\n\
373 tril (ones (3), -1)\n\
385 tril (ones (3), 1)\n\
392 If the option @qcode{\"pack\"} is given as third argument, the extracted\n\
393 elements are not inserted into a matrix, but rather stacked column-wise one\n\
403 @deftypefn {Function File} {} triu (@var{A})\n\
404 @deftypefnx {Function File} {} triu (@var{A}, @var{k})\n\
405 @deftypefnx {Function File} {} triu (@var{A}, @var{k}, @var{pack})\n\
406 See the documentation for the @code{tril} function (@pxref{tril}).\n\