annotate libinterp/corefcn/tril.cc @ 29931:7faff48840eb

prefer data over fortran_vec for read-only access to data See also the discussion here: https://octave.discourse.group/t/rename-uses-of-fortran-vec-to-data-for-clarity/1439 * Array.h (const T * Array<T>::fortran_vec (void) const): Deprecate. Change all uses to call data instead.
author John W. Eaton <jwe@octave.org>
date Fri, 30 Jul 2021 11:46:05 -0400
parents 270ffe1e7647
children 32c3a5805893
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
3 // Copyright (C) 2004-2021 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #endif
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include <algorithm>
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
31 #include "Array.h"
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32 #include "Sparse.h"
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 #include "mx-base.h"
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 #include "ov.h"
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 #include "Cell.h"
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
38 #include "defun.h"
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39 #include "error.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
40 #include "ovl.h"
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
41
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 // The bulk of the work.
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21041
diff changeset
43 template <typename T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
44 static Array<T>
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 do_tril (const Array<T>& a, octave_idx_type k, bool pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 {
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
47 octave_idx_type nr = a.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
48 octave_idx_type nc = a.columns ();
29931
7faff48840eb prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents: 29606
diff changeset
49 const T *avec = a.data ();
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
50 octave_idx_type zero = 0;
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52 if (pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
54 octave_idx_type j1 = std::min (std::max (zero, k), nc);
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
55 octave_idx_type j2 = std::min (std::max (zero, nr + k), nc);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
56 octave_idx_type n = j1 * nr + ((j2 - j1) * (nr-(j1-k) + nr-(j2-1-k))) / 2;
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
57 Array<T> r (dim_vector (n, 1));
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58 T *rvec = r.fortran_vec ();
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 for (octave_idx_type j = 0; j < nc; j++)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60 {
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
61 octave_idx_type ii = std::min (std::max (zero, j - k), nr);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 rvec = std::copy (avec + ii, avec + nr, rvec);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
63 avec += nr;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 return r;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 else
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70 Array<T> r (a.dims ());
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 T *rvec = r.fortran_vec ();
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
72 for (octave_idx_type j = 0; j < nc; j++)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 {
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
74 octave_idx_type ii = std::min (std::max (zero, j - k), nr);
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
75 std::fill (rvec, rvec + ii, T ());
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 std::copy (avec + ii, avec + nr, rvec + ii);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
77 avec += nr;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78 rvec += nr;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
79 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81 return r;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21041
diff changeset
85 template <typename T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
86 static Array<T>
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
87 do_triu (const Array<T>& a, octave_idx_type k, bool pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
88 {
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
89 octave_idx_type nr = a.rows ();
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
90 octave_idx_type nc = a.columns ();
29931
7faff48840eb prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents: 29606
diff changeset
91 const T *avec = a.data ();
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
92 octave_idx_type zero = 0;
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
94 if (pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
95 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
96 octave_idx_type j1 = std::min (std::max (zero, k), nc);
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
97 octave_idx_type j2 = std::min (std::max (zero, nr + k), nc);
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
98 octave_idx_type n
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
99 = ((j2 - j1) * ((j1+1-k) + (j2-k))) / 2 + (nc - j2) * nr;
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
100 Array<T> r (dim_vector (n, 1));
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101 T *rvec = r.fortran_vec ();
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 for (octave_idx_type j = 0; j < nc; j++)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
103 {
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
104 octave_idx_type ii = std::min (std::max (zero, j + 1 - k), nr);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
105 rvec = std::copy (avec, avec + ii, rvec);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
106 avec += nr;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
107 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
108
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
109 return r;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
110 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
111 else
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
112 {
27388
90dfdf0d09ad Avoid unnecessary copy on return in triu (bug #56752).
Carlo de Falco <carlo.defalco@polimi.it>
parents: 26376
diff changeset
113 Array<T> r (a.dims ());
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 T *rvec = r.fortran_vec ();
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 for (octave_idx_type j = 0; j < nc; j++)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
116 {
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10155
diff changeset
117 octave_idx_type ii = std::min (std::max (zero, j + 1 - k), nr);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118 std::copy (avec, avec + ii, rvec);
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
119 std::fill (rvec + ii, rvec + nr, T ());
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
120 avec += nr;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 rvec += nr;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 return r;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 // These two are by David Bateman.
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 // FIXME: optimizations possible. "pack" support missing.
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21041
diff changeset
131 template <typename T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
132 static Sparse<T>
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 do_tril (const Sparse<T>& a, octave_idx_type k, bool pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 if (pack) // FIXME
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23583
diff changeset
136 error (R"(tril: "pack" not implemented for sparse matrices)");
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
137
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138 Sparse<T> m = a;
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
139 octave_idx_type nc = m.cols ();
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
140
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
141 for (octave_idx_type j = 0; j < nc; j++)
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
142 for (octave_idx_type i = m.cidx (j); i < m.cidx (j+1); i++)
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
143 if (m.ridx (i) < j-k)
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
144 m.data(i) = 0.;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146 m.maybe_compress (true);
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20812
diff changeset
147
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
148 return m;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21041
diff changeset
151 template <typename T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
152 static Sparse<T>
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
153 do_triu (const Sparse<T>& a, octave_idx_type k, bool pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 if (pack) // FIXME
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23583
diff changeset
156 error (R"(triu: "pack" not implemented for sparse matrices)");
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158 Sparse<T> m = a;
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
159 octave_idx_type nc = m.cols ();
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161 for (octave_idx_type j = 0; j < nc; j++)
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
162 for (octave_idx_type i = m.cidx (j); i < m.cidx (j+1); i++)
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
163 if (m.ridx (i) > j-k)
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
164 m.data(i) = 0.;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
165
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 m.maybe_compress (true);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
167 return m;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 // Convenience dispatchers.
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21041
diff changeset
171 template <typename T>
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172 static Array<T>
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
173 do_trilu (const Array<T>& a, octave_idx_type k, bool lower, bool pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
174 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175 return lower ? do_tril (a, k, pack) : do_triu (a, k, pack);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21041
diff changeset
178 template <typename T>
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179 static Sparse<T>
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 do_trilu (const Sparse<T>& a, octave_idx_type k, bool lower, bool pack)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 return lower ? do_tril (a, k, pack) : do_triu (a, k, pack);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
184
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 static octave_value
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 do_trilu (const std::string& name,
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
187 const octave_value_list& args)
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
188 {
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20800
diff changeset
189 bool lower = (name == "tril");
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
190
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
191 int nargin = args.length ();
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
192 bool pack = false;
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20800
diff changeset
193
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
194 if (nargin >= 2 && args(nargin-1).is_string ())
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
195 {
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
196 pack = (args(nargin-1).string_value () == "pack");
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
197 nargin--;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
198 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
199
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20800
diff changeset
200 if (nargin < 1 || nargin > 2)
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20800
diff changeset
201 print_usage ();
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20800
diff changeset
202
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
203 octave_idx_type k = 0;
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
204 if (nargin == 2)
21041
63374982750b Usage of corresponding value extractor for octave_idx_type. Improves support
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20974
diff changeset
205 k = args(1).idx_type_value (true);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
206
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
207 octave_value arg = args(0);
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
208
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
209 dim_vector dims = arg.dims ();
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21301
diff changeset
210 if (dims.ndims () != 2)
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
211 error ("%s: need a 2-D matrix", name.c_str ());
28110
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
212 else if (k < -dims(0))
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
213 k = -dims(0);
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
214 else if (k > dims(1))
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
215 k = dims(1);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
216
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
217 octave_value retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
218
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
219 switch (arg.builtin_type ())
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
220 {
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
221 case btyp_double:
23583
b7747a2c88b2 maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents: 23577
diff changeset
222 if (arg.issparse ())
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
223 retval = do_trilu (arg.sparse_matrix_value (), k, lower, pack);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
224 else
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
225 retval = do_trilu (arg.array_value (), k, lower, pack);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
226 break;
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
227
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
228 case btyp_complex:
23583
b7747a2c88b2 maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents: 23577
diff changeset
229 if (arg.issparse ())
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
230 retval = do_trilu (arg.sparse_complex_matrix_value (), k, lower,
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
231 pack);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
232 else
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
233 retval = do_trilu (arg.complex_array_value (), k, lower, pack);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
234 break;
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
235
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
236 case btyp_bool:
23583
b7747a2c88b2 maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents: 23577
diff changeset
237 if (arg.issparse ())
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
238 retval = do_trilu (arg.sparse_bool_matrix_value (), k, lower,
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
239 pack);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
240 else
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
241 retval = do_trilu (arg.bool_array_value (), k, lower, pack);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
242 break;
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
243
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 21966
diff changeset
244 #define ARRAYCASE(TYP) \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 21966
diff changeset
245 case btyp_ ## TYP: \
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
246 retval = do_trilu (arg.TYP ## _array_value (), k, lower, pack); \
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
247 break
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
248
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
249 ARRAYCASE (float);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
250 ARRAYCASE (float_complex);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
251 ARRAYCASE (int8);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
252 ARRAYCASE (int16);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
253 ARRAYCASE (int32);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
254 ARRAYCASE (int64);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
255 ARRAYCASE (uint8);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
256 ARRAYCASE (uint16);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
257 ARRAYCASE (uint32);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
258 ARRAYCASE (uint64);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
259 ARRAYCASE (char);
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
260
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
261 #undef ARRAYCASE
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
262
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
263 default:
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
264 {
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
265 // Generic code that works on octave-values, that is slow
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
266 // but will also work on arbitrary user types
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
267 if (pack) // FIXME
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23583
diff changeset
268 error (R"(%s: "pack" not implemented for class %s)",
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
269 name.c_str (), arg.class_name ().c_str ());
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
270
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
271 octave_value tmp = arg;
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23534
diff changeset
272 if (arg.isempty ())
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
273 return arg;
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
274
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
275 octave_idx_type nr = dims(0);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
276 octave_idx_type nc = dims(1);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
277
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
278 // The sole purpose of this code is to force the correct matrix size.
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
279 // This would not be necessary if the octave_value resize function
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
280 // allowed a fill_value. It also allows odd attributes in some user
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
281 // types to be handled. With a fill_value, it should be replaced with
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
282 //
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
283 // octave_value_list ov_idx;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
284 // tmp = tmp.resize(dim_vector (0,0)).resize (dims, fill_value);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
285
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
286 octave_value_list ov_idx;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
287 std::list<octave_value_list> idx_tmp;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
288 ov_idx(1) = static_cast<double> (nc+1);
28638
98192ec1621f replace Range with range<double>
John W. Eaton <jwe@octave.org>
parents: 28631
diff changeset
289 ov_idx(0) = octave::range<double> (1, nr);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
290 idx_tmp.push_back (ov_idx);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
291 ov_idx(1) = static_cast<double> (nc);
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
292 tmp = tmp.resize (dim_vector (0,0));
28631
70cdf8de553d move non-member octave_value operator functions to octave namespace
John W. Eaton <jwe@octave.org>
parents: 28110
diff changeset
293 tmp = tmp.subsasgn ("(", idx_tmp, arg.index_op (ov_idx));
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
294 tmp = tmp.resize (dims);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
295
29606
270ffe1e7647 tril.cc: Use standard functions for min and max operations.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
296 octave_idx_type one = 1;
270ffe1e7647 tril.cc: Use standard functions for min and max operations.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
297
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
298 if (lower)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
299 {
29606
270ffe1e7647 tril.cc: Use standard functions for min and max operations.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
300 octave_idx_type st = std::min (nc, nr + k);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
301
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
302 for (octave_idx_type j = 1; j <= st; j++)
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
303 {
29606
270ffe1e7647 tril.cc: Use standard functions for min and max operations.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
304 octave_idx_type nr_limit = std::max (one, j - k);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
305 ov_idx(1) = static_cast<double> (j);
28638
98192ec1621f replace Range with range<double>
John W. Eaton <jwe@octave.org>
parents: 28631
diff changeset
306 ov_idx(0) = octave::range<double> (nr_limit, nr);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
307 std::list<octave_value_list> idx;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
308 idx.push_back (ov_idx);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
309
28631
70cdf8de553d move non-member octave_value operator functions to octave namespace
John W. Eaton <jwe@octave.org>
parents: 28110
diff changeset
310 tmp = tmp.subsasgn ("(", idx, arg.index_op (ov_idx));
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
311 }
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
312 }
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
313 else
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
314 {
29606
270ffe1e7647 tril.cc: Use standard functions for min and max operations.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
315 octave_idx_type st = std::max (k + 1, one);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
316
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
317 for (octave_idx_type j = st; j <= nc; j++)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
318 {
29606
270ffe1e7647 tril.cc: Use standard functions for min and max operations.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
319 octave_idx_type nr_limit = std::min (nr, j - k);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
320 ov_idx(1) = static_cast<double> (j);
28638
98192ec1621f replace Range with range<double>
John W. Eaton <jwe@octave.org>
parents: 28631
diff changeset
321 ov_idx(0) = octave::range<double> (1, nr_limit);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
322 std::list<octave_value_list> idx;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
323 idx.push_back (ov_idx);
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
324
28631
70cdf8de553d move non-member octave_value operator functions to octave namespace
John W. Eaton <jwe@octave.org>
parents: 28110
diff changeset
325 tmp = tmp.subsasgn ("(", idx, arg.index_op (ov_idx));
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
326 }
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
327 }
20800
fb128aafc322 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
328
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
329 retval = tmp;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
330 }
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
331 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
332
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
333 return retval;
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
334 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
335
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
336 DEFUN (tril, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
337 doc: /* -*- texinfo -*-
25018
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
338 @deftypefn {} {@var{A_LO} =} tril (@var{A})
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
339 @deftypefnx {} {@var{A_LO} =} tril (@var{A}, @var{k})
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
340 @deftypefnx {} {@var{A_LO} =} tril (@var{A}, @var{k}, @var{pack})
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
341 Return a new matrix formed by extracting the lower triangular part of the
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
342 matrix @var{A}, and setting all other elements to zero.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
343
25018
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
344 The optional second argument specifies how many diagonals above or below the
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
345 main diagonal should also be set to zero. The default value of @var{k} is
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
346 zero which includes the main diagonal as part of the result. If the value of
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
347 @var{k} is a nonzero integer then the selection of elements starts at an offset
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
348 of @var{k} diagonals above the main diagonal for positive @var{k} or below the
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
349 main diagonal for negative @var{k}. The absolute value of @var{k} may not be
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
350 greater than the number of subdiagonals or superdiagonals.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
351
25018
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
352 Example 1 : exclude main diagonal
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
353
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
354 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
355 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
356 tril (ones (3), -1)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
357 @result{} 0 0 0
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
358 1 0 0
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
359 1 1 0
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
360 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
361 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
362
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
363 @noindent
25018
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
364
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
365 Example 2 : include first superdiagonal
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
366
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
367 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
368 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
369 tril (ones (3), 1)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
370 @result{} 1 1 0
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
371 1 1 1
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
372 1 1 1
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
373 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
374 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
375
25018
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
376 If the optional third argument @qcode{"pack"} is given then the extracted
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
377 elements are not inserted into a matrix, but instead stacked column-wise one
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
378 above another, and returned as a column vector.
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
379 @seealso{triu, istril, diag}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
380 @end deftypefn */)
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
381 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
382 return do_trilu ("tril", args);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
383 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
384
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
385 DEFUN (triu, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
386 doc: /* -*- texinfo -*-
25018
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
387 @deftypefn {} {@var{A_UP} =} triu (@var{A})
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
388 @deftypefnx {} {@var{A_UP} =} triu (@var{A}, @var{k})
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
389 @deftypefnx {} {@var{A_UP} =} triu (@var{A}, @var{k}, @var{pack})
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
390 Return a new matrix formed by extracting the upper triangular part of the
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
391 matrix @var{A}, and setting all other elements to zero.
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
392
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
393 The optional second argument specifies how many diagonals above or below the
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
394 main diagonal should also be set to zero. The default value of @var{k} is
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
395 zero which includes the main diagonal as part of the result. If the value of
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
396 @var{k} is a nonzero integer then the selection of elements starts at an offset
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
397 of @var{k} diagonals above the main diagonal for positive @var{k} or below the
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
398 main diagonal for negative @var{k}. The absolute value of @var{k} may not be
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
399 greater than the number of subdiagonals or superdiagonals.
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
400
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
401 Example 1 : exclude main diagonal
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
402
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
403 @example
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
404 @group
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
405 triu (ones (3), 1)
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
406 @result{} 0 1 1
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
407 0 0 1
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
408 0 0 0
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
409 @end group
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
410 @end example
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
411
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
412 @noindent
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
413
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
414 Example 2 : include first subdiagonal
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
415
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
416 @example
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
417 @group
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
418 triu (ones (3), -1)
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
419 @result{} 1 1 1
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
420 1 1 1
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
421 0 1 1
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
422 @end group
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
423 @end example
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
424
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
425 If the optional third argument @qcode{"pack"} is given then the extracted
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
426 elements are not inserted into a matrix, but instead stacked column-wise one
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
427 above another, and returned as a column vector.
ed3d7a33551b doc: Redo documentation for tril and triu.
Rik <rik@octave.org>
parents: 24534
diff changeset
428 @seealso{tril, istriu, diag}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
429 @end deftypefn */)
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
430 {
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
431 return do_trilu ("triu", args);
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
432 }
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
433
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
434 /*
28110
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
435 %!shared a, l2, l1, l0, lm1, lm2, lm3, lm4
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
436 %! a = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12];
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
437 %!
28110
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
438 %! l2 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12];
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
439 %! l1 = [1, 2, 0; 4, 5, 6; 7, 8, 9; 10, 11, 12];
28110
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
440 %! l0 = [1, 0, 0; 4, 5, 0; 7, 8, 9; 10, 11, 12];
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
441 %! lm1 = [0, 0, 0; 4, 0, 0; 7, 8, 0; 10, 11, 12];
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
442 %! lm2 = [0, 0, 0; 0, 0, 0; 7, 0, 0; 10, 11, 0];
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
443 %! lm3 = [0, 0, 0; 0, 0, 0; 0, 0, 0; 10, 0, 0];
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
444 %! lm4 = [0, 0, 0; 0, 0, 0; 0, 0, 0; 0, 0, 0];
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
445 %!
28907
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
446 %!assert (tril (a, 3), l2)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
447 %!assert (tril (a, 2), l2)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
448 %!assert (tril (a, 1), l1)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
449 %!assert (tril (a, 0), l0)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
450 %!assert (tril (a), l0)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
451 %!assert (tril (a, -1), lm1)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
452 %!assert (tril (a, -2), lm2)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
453 %!assert (tril (a, -3), lm3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
454 %!assert (tril (a, -4), lm4)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
455 %!assert (tril (a, -5), lm4)
28110
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
456
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
457 %!shared a, u3, u2, u1, u0, um1, um2, um3
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
458 %!
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
459 %! a = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
460 %!
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
461 %! u3 = [0, 0, 0; 0, 0, 0; 0, 0, 0; 0, 0, 0];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
462 %! u2 = [0, 0, 3; 0, 0, 0; 0, 0, 0; 0, 0, 0];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
463 %! u1 = [0, 2, 3; 0, 0, 6; 0, 0, 0; 0, 0, 0];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
464 %! u0 = [1, 2, 3; 0, 5, 6; 0, 0, 9; 0, 0, 0];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
465 %! um1 = [1, 2, 3; 4, 5, 6; 0, 8, 9; 0, 0, 12];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
466 %! um2 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 0, 11, 12];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
467 %! um3 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12];
91f245908132 allow K to be out of range in tril and triu functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
468 %!
28907
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
469 %!assert (triu (a, 4), u3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
470 %!assert (triu (a, 3), u3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
471 %!assert (triu (a, 2), u2)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
472 %!assert (triu (a, 1), u1)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
473 %!assert (triu (a, 0), u0)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
474 %!assert (triu (a), u0)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
475 %!assert (triu (a, -1), um1)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
476 %!assert (triu (a, -2), um2)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
477 %!assert (triu (a, -3), um3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
478 %!assert (triu (a, -4), um3)
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
479
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
480 %!error tril ()
28888
06c8e0877864 maint: Backed out changeset 09c071328135.
Rik <rik@octave.org>
parents: 28887
diff changeset
481 %!error triu ()
9756
b134960cea23 implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
482 */