Mercurial > octave
annotate libinterp/corefcn/tril.cc @ 23803:90689bdbe048
Use C++11 raw string literals to avoid escaping double quotes.
* parser.cc, workspace-view.cc, __lin_interpn__.cc, cellfun.cc, colloc.cc,
data.cc, dirfns.cc, eig.cc, file-io.cc, find.cc, gammainc.cc, gl2ps-print.cc,
graphics.cc, graphics.in.h, help.cc, input.cc, load-path.cc, load-save.cc,
oct-hist.cc, oct-stream.cc, pager.cc, pr-output.cc, spparms.cc, symtab.cc,
toplev.cc, tril.cc, urlwrite.cc, utils.cc, variables.cc, chol.cc, qr.cc,
symbfact.cc, ov-struct.cc, octave.cc, pt-id.h, version.cc, mkoctfile.in.cc:
Replace regular strings containing "\"" patterns with R"(..."...)".
author | Rik <rik@octave.org> |
---|---|
date | Fri, 28 Jul 2017 10:34:33 -0700 |
parents | b7747a2c88b2 |
children | 336f89b6208b |
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 |
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
3 Copyright (C) 2004-2017 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 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
9 under the terms of the GNU General Public License as published by |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
10 the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
11 (at your option) any later version. |
11104 | 12 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
13 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
16 GNU General Public License 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 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21660
diff
changeset
|
24 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
25 # include "config.h" |
9756
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" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
38 #include "ovl.h" |
9756
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. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21041
diff
changeset
|
41 template <typename 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 { |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
45 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
|
46 octave_idx_type nc = a.columns (); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
47 const T *avec = a.fortran_vec (); |
10258 | 48 octave_idx_type zero = 0; |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
49 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
50 if (pack) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
51 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
52 octave_idx_type j1 = std::min (std::max (zero, k), nc); |
10258 | 53 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
|
54 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
|
55 Array<T> r (dim_vector (n, 1)); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
56 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
58 { |
10258 | 59 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
|
60 rvec = std::copy (avec + ii, avec + nr, rvec); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
61 avec += nr; |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
64 return r; |
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 else |
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 Array<T> r (a.dims ()); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
69 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
70 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
71 { |
10258 | 72 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
|
73 std::fill (rvec, rvec + ii, T ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
74 std::copy (avec + ii, avec + nr, rvec + ii); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
75 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
76 rvec += nr; |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
79 return r; |
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 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21041
diff
changeset
|
83 template <typename T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
84 static Array<T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
85 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
|
86 { |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
87 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
|
88 octave_idx_type nc = a.columns (); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
89 const T *avec = a.fortran_vec (); |
10258 | 90 octave_idx_type zero = 0; |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
91 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
92 if (pack) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
93 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
94 octave_idx_type j1 = std::min (std::max (zero, k), nc); |
10258 | 95 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
|
96 octave_idx_type n |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 = ((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
|
98 Array<T> r (dim_vector (n, 1)); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
100 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
101 { |
10258 | 102 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
|
103 rvec = std::copy (avec, avec + ii, rvec); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
104 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
105 } |
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 return r; |
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 else |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
110 { |
23534
b6498c088fca
maint: Don't write '> >' for declaration of templates that use templates.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
111 NoAlias<Array<T>> r (a.dims ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
112 T *rvec = r.fortran_vec (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 for (octave_idx_type j = 0; j < nc; j++) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
114 { |
10258 | 115 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
|
116 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
|
117 std::fill (rvec + ii, rvec + nr, T ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
118 avec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
119 rvec += nr; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 } |
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 return r; |
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 } |
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 // These two are by David Bateman. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
127 // FIXME: optimizations possible. "pack" support missing. |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
128 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21041
diff
changeset
|
129 template <typename T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
130 static Sparse<T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
131 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
|
132 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
133 if (pack) // FIXME |
23803
90689bdbe048
Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents:
23583
diff
changeset
|
134 error (R"(tril: "pack" not implemented for sparse matrices)"); |
9756
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 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
|
137 octave_idx_type nc = m.cols (); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
138 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
139 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
|
140 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
|
141 if (m.ridx (i) < j-k) |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
142 m.data(i) = 0.; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
143 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
144 m.maybe_compress (true); |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20812
diff
changeset
|
145 |
9756
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 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21041
diff
changeset
|
149 template <typename 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 |
23803
90689bdbe048
Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents:
23583
diff
changeset
|
154 error (R"(triu: "pack" not implemented for sparse matrices)"); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
155 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
156 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
|
157 octave_idx_type nc = m.cols (); |
9756
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 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
|
160 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
|
161 if (m.ridx (i) > j-k) |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
162 m.data(i) = 0.; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
163 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
164 m.maybe_compress (true); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
165 return m; |
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 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
168 // Convenience dispatchers. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21041
diff
changeset
|
169 template <typename T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
170 static Array<T> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
171 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
|
172 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
173 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
|
174 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
175 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21041
diff
changeset
|
176 template <typename T> |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
177 static Sparse<T> |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
178 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
|
179 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
180 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
|
181 } |
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 static octave_value |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
184 do_trilu (const std::string& name, |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
185 const octave_value_list& args) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
186 { |
20812
d9ca869ca124
maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents:
20800
diff
changeset
|
187 bool lower = (name == "tril"); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
188 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
189 int nargin = args.length (); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
190 bool pack = false; |
20812
d9ca869ca124
maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents:
20800
diff
changeset
|
191 |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
192 if (nargin >= 2 && args(nargin-1).is_string ()) |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
193 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
194 pack = (args(nargin-1).string_value () == "pack"); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
195 nargin--; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
196 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
197 |
20812
d9ca869ca124
maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents:
20800
diff
changeset
|
198 if (nargin < 1 || nargin > 2) |
d9ca869ca124
maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents:
20800
diff
changeset
|
199 print_usage (); |
d9ca869ca124
maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents:
20800
diff
changeset
|
200 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
201 octave_idx_type k = 0; |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
202 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
|
203 k = args(1).idx_type_value (true); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
204 |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
205 octave_value arg = args(0); |
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
206 |
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
207 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
|
208 if (dims.ndims () != 2) |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
209 error ("%s: need a 2-D matrix", name.c_str ()); |
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
210 else if (k < -dims(0) || k > dims(1)) |
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
211 error ("%s: requested diagonal out of range", name.c_str ()); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
212 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
213 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
214 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
215 switch (arg.builtin_type ()) |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
216 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
217 case btyp_double: |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
218 if (arg.issparse ()) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
219 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
|
220 else |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
221 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
|
222 break; |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
223 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
224 case btyp_complex: |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
225 if (arg.issparse ()) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
226 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
|
227 pack); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
228 else |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
229 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
|
230 break; |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
231 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
232 case btyp_bool: |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
233 if (arg.issparse ()) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
234 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
|
235 pack); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
236 else |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
237 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
|
238 break; |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
239 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
240 #define ARRAYCASE(TYP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
241 case btyp_ ## TYP: \ |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
242 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
|
243 break |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
244 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
245 ARRAYCASE (float); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
246 ARRAYCASE (float_complex); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
247 ARRAYCASE (int8); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
248 ARRAYCASE (int16); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
249 ARRAYCASE (int32); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
250 ARRAYCASE (int64); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
251 ARRAYCASE (uint8); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
252 ARRAYCASE (uint16); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
253 ARRAYCASE (uint32); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
254 ARRAYCASE (uint64); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
255 ARRAYCASE (char); |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
256 |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
257 #undef ARRAYCASE |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
258 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
259 default: |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
260 { |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
261 // 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
|
262 // 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
|
263 if (pack) // FIXME |
23803
90689bdbe048
Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents:
23583
diff
changeset
|
264 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
|
265 name.c_str (), arg.class_name ().c_str ()); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
266 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
267 octave_value tmp = arg; |
23577
80c42f4cca13
maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents:
23534
diff
changeset
|
268 if (arg.isempty ()) |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
269 return arg; |
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_idx_type nr = dims(0); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
272 octave_idx_type nc = dims(1); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
273 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
274 // 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
|
275 // 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
|
276 // 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
|
277 // 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
|
278 // |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
279 // octave_value_list ov_idx; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
280 // 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
|
281 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
282 octave_value_list ov_idx; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
283 std::list<octave_value_list> idx_tmp; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
284 ov_idx(1) = static_cast<double> (nc+1); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
285 ov_idx(0) = Range (1, nr); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
286 idx_tmp.push_back (ov_idx); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
287 ov_idx(1) = static_cast<double> (nc); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
288 tmp = tmp.resize (dim_vector (0,0)); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
289 tmp = tmp.subsasgn ("(",idx_tmp, arg.do_index_op (ov_idx)); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
290 tmp = tmp.resize (dims); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
291 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
292 if (lower) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
293 { |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
294 octave_idx_type st = (nc < nr + k ? nc : nr + k); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
295 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
296 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
|
297 { |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
298 octave_idx_type nr_limit = (1 > j - k ? 1 : j - k); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
299 ov_idx(1) = static_cast<double> (j); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
300 ov_idx(0) = Range (nr_limit, nr); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
301 std::list<octave_value_list> idx; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
302 idx.push_back (ov_idx); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
303 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
304 tmp = tmp.subsasgn ("(", idx, arg.do_index_op (ov_idx)); |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
305 } |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
306 } |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
307 else |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
308 { |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
309 octave_idx_type st = (k + 1 > 1 ? k + 1 : 1); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
310 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
311 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
|
312 { |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
313 octave_idx_type nr_limit = (nr < j - k ? nr : j - k); |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
314 ov_idx(1) = static_cast<double> (j); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
315 ov_idx(0) = Range (1, nr_limit); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
316 std::list<octave_value_list> idx; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
317 idx.push_back (ov_idx); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
318 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
319 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
|
320 } |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
321 } |
20800
fb128aafc322
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
322 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
323 retval = tmp; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
324 } |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
325 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
326 |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
327 return retval; |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
328 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
329 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
330 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
|
331 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
332 @deftypefn {} {} tril (@var{A}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
333 @deftypefnx {} {} tril (@var{A}, @var{k}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
334 @deftypefnx {} {} tril (@var{A}, @var{k}, @var{pack}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
335 @deftypefnx {} {} triu (@var{A}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
336 @deftypefnx {} {} triu (@var{A}, @var{k}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
337 @deftypefnx {} {} triu (@var{A}, @var{k}, @var{pack}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
338 Return a new matrix formed by extracting the lower (@code{tril}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
339 or upper (@code{triu}) triangular part of the matrix @var{A}, and |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
340 setting all other elements to zero. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
341 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
342 The second argument is optional, and specifies how many diagonals above or |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
343 below the main diagonal should also be set to zero. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
344 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
345 The default value of @var{k} is zero, so that @code{triu} and @code{tril} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
346 normally include the main diagonal as part of the result. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
347 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
348 If the value of @var{k} is nonzero integer, the selection of elements starts |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
349 at an offset of @var{k} diagonals above or below the main diagonal; above |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
350 for positive @var{k} and below for negative @var{k}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
351 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
352 The absolute value of @var{k} must not be greater than the number of |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
353 subdiagonals or superdiagonals. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
354 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
355 For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
356 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
357 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
358 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
359 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
|
360 @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
|
361 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
|
362 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
|
363 @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
|
364 @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
|
365 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
366 @noindent |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
367 and |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
368 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
369 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
370 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
371 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
|
372 @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
|
373 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
|
374 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
|
375 @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
|
376 @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
|
377 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
378 If the option @qcode{"pack"} is given as third argument, the extracted |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
379 elements are not inserted into a matrix, but rather stacked column-wise one |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
380 above other. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
381 @seealso{diag} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
382 @end deftypefn */) |
9756
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 return do_trilu ("tril", args); |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
385 } |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
386 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
387 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
|
388 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
389 @deftypefn {} {} triu (@var{A}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
390 @deftypefnx {} {} triu (@var{A}, @var{k}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
391 @deftypefnx {} {} triu (@var{A}, @var{k}, @var{pack}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
392 See the documentation for the @code{tril} function (@pxref{tril}). |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
393 @seealso{tril} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
394 @end deftypefn */) |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
395 { |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
396 return do_trilu ("triu", args); |
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 |
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 %!test |
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
401 %! 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
|
402 %! |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
403 %! 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
|
404 %! 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
|
405 %! 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
|
406 %! 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
|
407 %! 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
|
408 %! 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
|
409 %! 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
|
410 %! |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
411 %! 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
|
412 %! 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
|
413 %! 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
|
414 %! 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
|
415 %! assert (tril (a), l0); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
416 %! 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
|
417 %! assert (tril (a, 2), l2); |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
418 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
419 %!error tril () |
9756
b134960cea23
implement built-in tril/triu
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
420 */ |