Mercurial > hg > octave-nkf
annotate libinterp/corefcn/tril.cc @ 19840:c5270263d466 gui-release
close gui-release branch
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 30 Jan 2015 17:41:50 -0500 |
parents | 175b392e91fe |
children | 6a71e5030df5 |
rev | line source |
---|---|
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
1 /* |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
2 |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
3 Copyright (C) 2004-2013 David Bateman |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
4 Copyright (C) 2009 VZLU Prague |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
5 |
11104 | 6 This file is part of Octave. |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
7 |
11104 | 8 Octave is free software; you can redistribute it and/or modify it |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 3 of the License, or (at your | |
11 option) any later version. | |
12 | |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
17 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
19 along with Octave; see the file COPYING. If not, see |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 <http://www.gnu.org/licenses/>. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
21 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
22 */ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
23 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
24 #ifdef HAVE_CONFIG_H |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
25 #include <config.h> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 #endif |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
27 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
28 #include <algorithm> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
29 #include "Array.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
30 #include "Sparse.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
31 #include "mx-base.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 #include "ov.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
34 #include "Cell.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
35 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
36 #include "defun.h" |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
37 #include "error.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
38 #include "oct-obj.h" |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
39 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
40 // The bulk of the work. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
41 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
42 static Array<T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
43 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
|
44 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
45 octave_idx_type nr = a.rows (), nc = a.columns (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
46 const T *avec = a.fortran_vec (); |
10258 | 47 octave_idx_type zero = 0; |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
48 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
49 if (pack) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
50 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
51 octave_idx_type j1 = std::min (std::max (zero, k), nc); |
10258 | 52 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
|
53 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
|
54 Array<T> r (dim_vector (n, 1)); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
55 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
56 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 { |
10258 | 58 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
|
59 rvec = std::copy (avec + ii, avec + nr, rvec); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
60 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
61 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
62 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
63 return r; |
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 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
66 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
67 Array<T> r (a.dims ()); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
68 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
69 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
70 { |
10258 | 71 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
|
72 std::fill (rvec, rvec + ii, T ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
73 std::copy (avec + ii, avec + nr, rvec + ii); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
74 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
75 rvec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
76 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
77 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
78 return r; |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
82 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
83 static Array<T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
84 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
|
85 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
86 octave_idx_type nr = a.rows (), nc = a.columns (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
87 const T *avec = a.fortran_vec (); |
10258 | 88 octave_idx_type zero = 0; |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
89 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
90 if (pack) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
91 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
92 octave_idx_type j1 = std::min (std::max (zero, k), nc); |
10258 | 93 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
|
94 octave_idx_type n |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
95 = ((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
|
96 Array<T> r (dim_vector (n, 1)); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
97 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
98 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 { |
10258 | 100 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
|
101 rvec = std::copy (avec, avec + ii, rvec); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
102 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
103 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
104 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
105 return r; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
106 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
107 else |
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 NoAlias<Array<T> > r (a.dims ()); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
110 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
111 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
112 { |
10258 | 113 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
|
114 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
|
115 std::fill (rvec + ii, rvec + nr, T ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
116 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
117 rvec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
118 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
119 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 return r; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
121 } |
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 // These two are by David Bateman. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
125 // FIXME: optimizations possible. "pack" support missing. |
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 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
128 static Sparse<T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
129 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
|
130 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
131 if (pack) // FIXME |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
132 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
133 error ("tril: \"pack\" not implemented for sparse matrices"); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
134 return Sparse<T> (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
135 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
136 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
137 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
|
138 octave_idx_type nc = m.cols (); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
139 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
140 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
|
141 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
|
142 if (m.ridx (i) < j-k) |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
143 m.data(i) = 0.; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
144 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
145 m.maybe_compress (true); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
146 return m; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
147 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
148 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
149 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
150 static Sparse<T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
151 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
|
152 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
153 if (pack) // FIXME |
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 error ("triu: \"pack\" not implemented for sparse matrices"); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
156 return Sparse<T> (); |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
159 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
|
160 octave_idx_type nc = m.cols (); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
161 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
162 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
|
163 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
|
164 if (m.ridx (i) > j-k) |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
165 m.data(i) = 0.; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
166 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
167 m.maybe_compress (true); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
168 return m; |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
171 // Convenience dispatchers. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
172 template <class T> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
173 static Array<T> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
174 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
|
175 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
176 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
|
177 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
178 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
179 template <class T> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
180 static Sparse<T> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
181 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
|
182 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
183 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
|
184 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
185 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
186 static octave_value |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
187 do_trilu (const std::string& name, |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
188 const octave_value_list& args) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
189 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
190 bool lower = name == "tril"; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
191 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
192 octave_value retval; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
193 int nargin = args.length (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
194 octave_idx_type k = 0; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
195 bool pack = false; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
196 if (nargin >= 2 && args(nargin-1).is_string ()) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
197 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
198 pack = args(nargin-1).string_value () == "pack"; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
199 nargin--; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
200 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
201 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
202 if (nargin == 2) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
203 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
204 k = args(1).int_value (true); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
205 |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
206 if (error_state) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9756
diff
changeset
|
207 return retval; |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
208 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
209 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
210 if (nargin < 1 || nargin > 2) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
211 print_usage (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
212 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
213 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
214 octave_value arg = args (0); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
215 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
216 dim_vector dims = arg.dims (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
217 if (dims.length () != 2) |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
218 error ("%s: need a 2-D matrix", name.c_str ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
219 else if (k < -dims (0) || k > dims(1)) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
220 error ("%s: requested diagonal out of range", name.c_str ()); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
221 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
222 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
223 switch (arg.builtin_type ()) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
224 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
225 case btyp_double: |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
226 if (arg.is_sparse_type ()) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
227 retval = do_trilu (arg.sparse_matrix_value (), k, lower, pack); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
228 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
229 retval = do_trilu (arg.array_value (), k, lower, pack); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
230 break; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
231 case btyp_complex: |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
232 if (arg.is_sparse_type ()) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
233 retval = do_trilu (arg.sparse_complex_matrix_value (), k, lower, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
234 pack); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
235 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
236 retval = do_trilu (arg.complex_array_value (), k, lower, pack); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
237 break; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
238 case btyp_bool: |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
239 if (arg.is_sparse_type ()) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
240 retval = do_trilu (arg.sparse_bool_matrix_value (), k, lower, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
241 pack); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
242 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
243 retval = do_trilu (arg.bool_array_value (), k, lower, pack); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
244 break; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
245 #define ARRAYCASE(TYP) \ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
246 case btyp_ ## TYP: \ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
247 retval = do_trilu (arg.TYP ## _array_value (), k, lower, pack); \ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
248 break |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
249 ARRAYCASE (float); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
250 ARRAYCASE (float_complex); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
251 ARRAYCASE (int8); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
252 ARRAYCASE (int16); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
253 ARRAYCASE (int32); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
254 ARRAYCASE (int64); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
255 ARRAYCASE (uint8); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
256 ARRAYCASE (uint16); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
257 ARRAYCASE (uint32); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
258 ARRAYCASE (uint64); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
259 ARRAYCASE (char); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
260 #undef ARRAYCASE |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
261 default: |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
262 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
263 // Generic code that works on octave-values, that is slow |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
264 // but will also work on arbitrary user types |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
265 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
266 if (pack) // FIXME |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
267 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
268 error ("%s: \"pack\" not implemented for class %s", |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
269 name.c_str (), arg.class_name ().c_str ()); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
270 return octave_value (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
271 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
272 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
273 octave_value tmp = arg; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
274 if (arg.numel () == 0) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
275 return arg; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
276 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
277 octave_idx_type nr = dims(0), nc = dims (1); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
278 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
279 // The sole purpose of the below is to force the correct |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
280 // matrix size. This would not be necessary if the |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
281 // octave_value resize function allowed a fill_value. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
282 // It also allows odd attributes in some user types |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
283 // to be handled. With a fill_value ot should be replaced |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
284 // with |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
285 // |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
286 // octave_value_list ov_idx; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
287 // tmp = tmp.resize(dim_vector (0,0)).resize (dims, fill_value); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
288 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
289 octave_value_list ov_idx; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
290 std::list<octave_value_list> idx_tmp; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
291 ov_idx(1) = static_cast<double> (nc+1); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
292 ov_idx(0) = Range (1, nr); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
293 idx_tmp.push_back (ov_idx); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
294 ov_idx(1) = static_cast<double> (nc); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
295 tmp = tmp.resize (dim_vector (0,0)); |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
296 tmp = tmp.subsasgn ("(",idx_tmp, arg.do_index_op (ov_idx)); |
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
297 tmp = tmp.resize (dims); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
298 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
299 if (lower) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
300 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
301 octave_idx_type st = nc < nr + k ? nc : nr + k; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
302 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
303 for (octave_idx_type j = 1; j <= st; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
304 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
305 octave_idx_type nr_limit = 1 > j - k ? 1 : j - k; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
306 ov_idx(1) = static_cast<double> (j); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
307 ov_idx(0) = Range (nr_limit, nr); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
308 std::list<octave_value_list> idx; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
309 idx.push_back (ov_idx); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
310 |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
311 tmp = tmp.subsasgn ("(", idx, arg.do_index_op (ov_idx)); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
312 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
313 if (error_state) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
314 return retval; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
315 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
316 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
317 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
318 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
319 octave_idx_type st = k + 1 > 1 ? k + 1 : 1; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
320 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
321 for (octave_idx_type j = st; j <= nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
322 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
323 octave_idx_type nr_limit = nr < j - k ? nr : j - k; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
324 ov_idx(1) = static_cast<double> (j); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
325 ov_idx(0) = Range (1, nr_limit); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
326 std::list<octave_value_list> idx; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
327 idx.push_back (ov_idx); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
328 |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
329 tmp = tmp.subsasgn ("(", idx, arg.do_index_op (ov_idx)); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
330 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
331 if (error_state) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
332 return retval; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
333 } |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
336 retval = tmp; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
337 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
338 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
339 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
340 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
341 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
342 return retval; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
343 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
344 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
345 DEFUN (tril, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
346 "-*- texinfo -*-\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
347 @deftypefn {Function File} {} tril (@var{A})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
348 @deftypefnx {Function File} {} tril (@var{A}, @var{k})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
349 @deftypefnx {Function File} {} tril (@var{A}, @var{k}, @var{pack})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
350 @deftypefnx {Function File} {} triu (@var{A})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
351 @deftypefnx {Function File} {} triu (@var{A}, @var{k})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
352 @deftypefnx {Function File} {} triu (@var{A}, @var{k}, @var{pack})\n\ |
11152
39ae406df598
Improve docstrings for functions found in undocumented list.
Rik <octave@nomad.inbox5.com>
parents:
11104
diff
changeset
|
353 Return a new matrix formed by extracting the lower (@code{tril})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
354 or upper (@code{triu}) triangular part of the matrix @var{A}, and\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
355 setting all other elements to zero. The second argument is optional,\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
356 and specifies how many diagonals above or below the main diagonal should\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
357 also be set to zero.\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
358 \n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
359 The default value of @var{k} is zero, so that @code{triu} and\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
360 @code{tril} normally include the main diagonal as part of the result.\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
361 \n\ |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
15624
diff
changeset
|
362 If the value of @var{k} is nonzero integer, the selection of elements\n\ |
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
15624
diff
changeset
|
363 starts at an offset of @var{k} diagonals above or below the main\n\ |
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
15624
diff
changeset
|
364 diagonal; above for positive @var{k} and below for negative @var{k}.\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
365 \n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
366 The absolute value of @var{k} must not be greater than the number of\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
367 sub-diagonals or super-diagonals.\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
368 \n\ |
10840 | 369 For example:\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
370 \n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
371 @example\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
372 @group\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
373 tril (ones (3), -1)\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
374 @result{} 0 0 0\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
375 1 0 0\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
376 1 1 0\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
377 @end group\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
378 @end example\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
379 \n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
380 @noindent\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
381 and\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
382 \n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
383 @example\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
384 @group\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
385 tril (ones (3), 1)\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
386 @result{} 1 1 0\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
387 1 1 1\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
388 1 1 1\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
389 @end group\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
390 @end example\n\ |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
391 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
392 If the option @qcode{\"pack\"} is given as third argument, the extracted\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
393 elements are not inserted into a matrix, but rather stacked column-wise one\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
394 above other.\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
12546
diff
changeset
|
395 @seealso{diag}\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
396 @end deftypefn") |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
397 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
398 return do_trilu ("tril", args); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
399 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
400 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
401 DEFUN (triu, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
402 "-*- texinfo -*-\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
403 @deftypefn {Function File} {} triu (@var{A})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
404 @deftypefnx {Function File} {} triu (@var{A}, @var{k})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
405 @deftypefnx {Function File} {} triu (@var{A}, @var{k}, @var{pack})\n\ |
12546
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
406 See the documentation for the @code{tril} function (@pxref{tril}).\n\ |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
407 @end deftypefn") |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
408 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
409 return do_trilu ("triu", args); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
410 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
411 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
412 /* |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
413 %!test |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
414 %! 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
|
415 %! |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
416 %! l0 = [1, 0, 0; 4, 5, 0; 7, 8, 9; 10, 11, 12]; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
417 %! l1 = [1, 2, 0; 4, 5, 6; 7, 8, 9; 10, 11, 12]; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
418 %! l2 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
419 %! 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
|
420 %! 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
|
421 %! 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
|
422 %! 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
|
423 %! |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
424 %! assert (tril (a, -4), lm4); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
425 %! assert (tril (a, -3), lm3); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
426 %! assert (tril (a, -2), lm2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
427 %! assert (tril (a, -1), lm1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
428 %! assert (tril (a), l0); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
429 %! assert (tril (a, 1), l1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
430 %! assert (tril (a, 2), l2); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
431 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
432 %!error tril () |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
433 */ |