Mercurial > octave-dspies
annotate libinterp/octave-value/ov-base-mat.cc @ 19010:3fb030666878 draft default tip dspies
Added special-case logical-indexing function
* logical-index.h (New file) : Logical-indexing function. May be called on
octave_value types via call_bool_index
* nz-iterators.h : Add base-class nz_iterator for iterator types. Array has
template bool for whether to internally store row-col or compute on the fly
Add skip_ahead method which skips forward to the next nonzero after its
argument
Add flat_index for computing octave_idx_type index of current position (with
assertion failure in the case of overflow)
Move is_zero to separate file
* ov-base-diag.cc, ov-base-mat.cc, ov-base-sparse.cc, ov-perm.cc
(do_index_op): Add call to call_bool_index in logical-index.h
* Array.h : Move forward-declaration for array_iterator to separate header file
* dim-vector.cc (dim_max): Refers to idx-bounds.h (max_idx)
* array-iter-decl.h (New file): Header file for forward declaration of
array-iterator
* direction.h : Add constants fdirc and bdirc to avoid having to reconstruct
them
* dv-utils.h, dv-utils.cc (New files) :
Utility functions for querying and constructing dim-vectors
* idx-bounds.h (New file) :
Utility constants and functions for determining whether things will overflow
the maximum allowed bounds
* interp-idx.h (New function : to_flat_idx) : Converts row-col pair to linear
index of octave_idx_type
* is-zero.h (New file) : Function for determining whether an element is zero
* logical-index.tst : Add tests for correct return-value dimensions and large
sparse matrix behavior
author | David Spies <dnspies@gmail.com> |
---|---|
date | Fri, 25 Jul 2014 13:39:31 -0600 |
parents | bcd71a2531d3 |
children |
rev | line source |
---|---|
3219 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
3 Copyright (C) 1996-2013 John W. Eaton |
11523 | 4 Copyright (C) 2009-2010 VZLU Prague |
3219 | 5 |
6 This file is part of Octave. | |
7 | |
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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3219 | 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. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3219 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
3219 | 29 |
3933 | 30 #include "Cell.h" |
3220 | 31 #include "oct-obj.h" |
3933 | 32 #include "oct-map.h" |
3219 | 33 #include "ov-base.h" |
34 #include "ov-base-mat.h" | |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
35 #include "ov-base-scalar.h" |
3572 | 36 #include "pr-output.h" |
19010
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
37 #include "logical-index.h" |
3219 | 38 |
39 template <class MT> | |
3220 | 40 octave_value |
4247 | 41 octave_base_matrix<MT>::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
42 const std::list<octave_value_list>& idx) |
3933 | 43 { |
44 octave_value retval; | |
45 | |
46 switch (type[0]) | |
47 { | |
48 case '(': | |
49 retval = do_index_op (idx.front ()); | |
50 break; | |
51 | |
52 case '{': | |
53 case '.': | |
54 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
55 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
56 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
3933 | 57 } |
58 break; | |
59 | |
60 default: | |
61 panic_impossible (); | |
62 } | |
63 | |
64 return retval.next_subsref (type, idx); | |
65 } | |
66 | |
67 template <class MT> | |
68 octave_value | |
4247 | 69 octave_base_matrix<MT>::subsasgn (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
70 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
71 const octave_value& rhs) |
3933 | 72 { |
73 octave_value retval; | |
74 | |
75 switch (type[0]) | |
76 { | |
77 case '(': | |
78 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
79 if (type.length () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
80 retval = numeric_assign (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
81 else if (is_empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
82 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
83 // Allow conversion of empty matrix to some other type in |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
84 // cases like |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
85 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
86 // x = []; x(i).f = rhs |
4436 | 87 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
88 if (type[1] == '.') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
89 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
90 octave_value tmp = octave_value::empty_conv (type, rhs); |
4436 | 91 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 retval = tmp.subsasgn (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
94 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
95 error ("invalid assignment expression"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
97 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
99 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
100 error ("in indexed assignment of %s, last lhs index must be ()", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
101 nm.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 } |
3933 | 103 } |
104 break; | |
105 | |
106 case '{': | |
107 case '.': | |
108 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
109 if (is_empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
110 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
111 octave_value tmp = octave_value::empty_conv (type, rhs); |
4834 | 112 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
113 retval = tmp.subsasgn (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
114 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
115 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
116 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
117 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
118 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
119 } |
3933 | 120 } |
121 break; | |
122 | |
123 default: | |
124 panic_impossible (); | |
125 } | |
126 | |
127 return retval; | |
128 } | |
129 | |
130 template <class MT> | |
131 octave_value | |
132 octave_base_matrix<MT>::do_index_op (const octave_value_list& idx, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
133 bool resize_ok) |
3220 | 134 { |
135 octave_value retval; | |
136 | |
5275 | 137 octave_idx_type n_idx = idx.length (); |
3220 | 138 |
4742 | 139 int nd = matrix.ndims (); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
140 const MT& cmatrix = matrix; |
3220 | 141 |
4742 | 142 switch (n_idx) |
143 { | |
144 case 0: | |
5539 | 145 retval = matrix; |
3220 | 146 break; |
147 | |
148 case 1: | |
149 { | |
19010
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
150 if (idx(0).is_bool_type ()) |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
151 retval = call_bool_index (matrix, idx(0)); |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
152 else |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
153 { |
19010
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
154 idx_vector i = idx (0).index_vector (); |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
155 |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
156 if (! error_state) |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
157 { |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
158 // optimize single scalar index. |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
159 if (! resize_ok && i.is_scalar ()) |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
160 retval = cmatrix.checkelem (i(0)); |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
161 else |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
162 retval = MT (matrix.index (i, resize_ok)); |
3fb030666878
Added special-case logical-indexing function
David Spies <dnspies@gmail.com>
parents:
18416
diff
changeset
|
163 } |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
164 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
165 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
166 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
167 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
168 case 2: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
169 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
170 idx_vector i = idx (0).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
171 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
172 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
173 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
174 idx_vector j = idx (1).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
175 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
176 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
177 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
178 // optimize two scalar indices. |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
179 if (! resize_ok && i.is_scalar () && j.is_scalar ()) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
180 retval = cmatrix.checkelem (i(0), j(0)); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
181 else |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
182 retval = MT (matrix.index (i, j, resize_ok)); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
183 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
184 } |
3220 | 185 } |
186 break; | |
187 | |
188 default: | |
189 { | |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
190 Array<idx_vector> idx_vec (dim_vector (n_idx, 1)); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
191 bool scalar_opt = n_idx == nd && ! resize_ok; |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
192 const dim_vector dv = matrix.dims (); |
4858 | 193 |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
194 for (octave_idx_type i = 0; i < n_idx; i++) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
195 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
196 idx_vec(i) = idx(i).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
197 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
198 if (error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
199 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
200 |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
201 scalar_opt = (scalar_opt && idx_vec(i).is_scalar ()); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
202 } |
3220 | 203 |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
204 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
205 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
206 if (scalar_opt) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
207 retval = cmatrix.checkelem (conv_to_int_array (idx_vec)); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
208 else |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
209 retval = MT (matrix.index (idx_vec, resize_ok)); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
210 } |
3220 | 211 } |
212 break; | |
213 } | |
214 | |
215 return retval; | |
216 } | |
217 | |
3928 | 218 template <class MT> |
219 void | |
220 octave_base_matrix<MT>::assign (const octave_value_list& idx, const MT& rhs) | |
221 { | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
222 octave_idx_type n_idx = idx.length (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
223 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
224 switch (n_idx) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
225 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
226 case 0: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
227 panic_impossible (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
228 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
229 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
230 case 1: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
231 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
232 idx_vector i = idx (0).index_vector (); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
233 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
234 if (! error_state) |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
235 matrix.assign (i, rhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
236 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
237 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
238 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
239 case 2: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
240 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
241 idx_vector i = idx (0).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
242 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
243 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
244 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
245 idx_vector j = idx (1).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
246 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
247 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
248 matrix.assign (i, j, rhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
249 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
250 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
251 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
252 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
253 default: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
254 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
255 Array<idx_vector> idx_vec (dim_vector (n_idx, 1)); |
3928 | 256 |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
257 for (octave_idx_type i = 0; i < n_idx; i++) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
258 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
259 idx_vec(i) = idx(i).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
260 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
261 if (error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
262 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
263 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
264 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
265 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
266 matrix.assign (idx_vec, rhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
267 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
268 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
269 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
270 |
9350
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
271 // Clear cache. |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
272 clear_cached_info (); |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
273 } |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
274 |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
275 template <class MT> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
276 MatrixType |
9350
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
277 octave_base_matrix<MT>::matrix_type (const MatrixType& _typ) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
278 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
279 delete typ; |
9350
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
280 typ = new MatrixType (_typ); |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
281 return *typ; |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
282 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
283 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
284 template <class MT> |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
285 void |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
286 octave_base_matrix<MT>::assign (const octave_value_list& idx, |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
287 typename MT::element_type rhs) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
288 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
289 octave_idx_type n_idx = idx.length (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
290 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
291 int nd = matrix.ndims (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
292 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
293 MT mrhs (dim_vector (1, 1), rhs); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
294 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
295 switch (n_idx) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
296 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
297 case 0: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
298 panic_impossible (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
299 break; |
3928 | 300 |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
301 case 1: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
302 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
303 idx_vector i = idx (0).index_vector (); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
304 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
305 if (! error_state) |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
306 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
307 // optimize single scalar index. |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
308 if (i.is_scalar () && i(0) < matrix.numel ()) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
309 matrix(i(0)) = rhs; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
310 else |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
311 matrix.assign (i, mrhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
312 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
313 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
314 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
315 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
316 case 2: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
317 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
318 idx_vector i = idx (0).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
319 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
320 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
321 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
322 idx_vector j = idx (1).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
323 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
324 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
325 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
326 // optimize two scalar indices. |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
327 if (i.is_scalar () && j.is_scalar () && nd == 2 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
328 && i(0) < matrix.rows () && j(0) < matrix.columns ()) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
329 matrix(i(0), j(0)) = rhs; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
330 else |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
331 matrix.assign (i, j, mrhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
332 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
333 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
334 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
335 break; |
6195 | 336 |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
337 default: |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
338 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
339 Array<idx_vector> idx_vec (dim_vector (n_idx, 1)); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
340 bool scalar_opt = n_idx == nd; |
9239
f29db0a0aa85
fix another indexing bug in liboctinterp
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
341 const dim_vector dv = matrix.dims ().redim (n_idx); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
342 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
343 for (octave_idx_type i = 0; i < n_idx; i++) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
344 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
345 idx_vec(i) = idx(i).index_vector (); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
346 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
347 if (error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
348 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
349 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
350 scalar_opt = (scalar_opt && idx_vec(i).is_scalar () |
9239
f29db0a0aa85
fix another indexing bug in liboctinterp
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
351 && idx_vec(i)(0) < dv(i)); |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
352 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
353 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
354 if (! error_state) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
355 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
356 if (scalar_opt) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
357 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
358 // optimize all scalar indices. Don't construct an index array, |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
359 // but rather calc a scalar index directly. |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17875
diff
changeset
|
360 octave_idx_type k = 1; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17875
diff
changeset
|
361 octave_idx_type j = 0; |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
362 for (octave_idx_type i = 0; i < n_idx; i++) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
363 { |
8821
352f111b04ce
ov-base-mat.cc: correctly compute all-scalar index for assignment
John W. Eaton <jwe@octave.org>
parents:
8753
diff
changeset
|
364 j += idx_vec(i)(0) * k; |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
365 k *= dv (i); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
366 } |
8753
c141078e083a
fix small bug in scalar-to-array assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8679
diff
changeset
|
367 matrix(j) = rhs; |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
368 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
369 else |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
370 matrix.assign (idx_vec, mrhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
371 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
372 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
373 break; |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
374 } |
6195 | 375 |
9350
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
376 // Clear cache. |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
377 clear_cached_info (); |
3928 | 378 } |
379 | |
3220 | 380 template <class MT> |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
381 void |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
382 octave_base_matrix<MT>::delete_elements (const octave_value_list& idx) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
383 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
384 octave_idx_type len = idx.length (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
385 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
386 Array<idx_vector> ra_idx (dim_vector (len, 1)); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
387 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
388 for (octave_idx_type i = 0; i < len; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
389 ra_idx(i) = idx(i).index_vector (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
390 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
391 matrix.delete_elements (ra_idx); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
392 |
9350
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
393 // Clear cache. |
16a5f9e1fdb3
cache idx_vector result in matrices once used for indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9239
diff
changeset
|
394 clear_cached_info (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
395 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
396 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
397 template <class MT> |
5731 | 398 octave_value |
399 octave_base_matrix<MT>::resize (const dim_vector& dv, bool fill) const | |
400 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
401 MT retval (matrix); |
5731 | 402 if (fill) |
403 retval.resize (dv, 0); | |
404 else | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
405 retval.resize (dv); |
5731 | 406 return retval; |
407 } | |
408 | |
409 template <class MT> | |
3220 | 410 bool |
411 octave_base_matrix<MT>::is_true (void) const | |
412 { | |
413 bool retval = false; | |
4875 | 414 dim_vector dv = matrix.dims (); |
415 int nel = dv.numel (); | |
4915 | 416 |
4875 | 417 if (nel > 0) |
3220 | 418 { |
4875 | 419 MT t1 (matrix.reshape (dim_vector (nel, 1))); |
420 | |
8626
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8290
diff
changeset
|
421 if (t1.any_element_is_nan ()) |
11129
0de5cc44e690
use gripe functions for NaN to logical and NaN to character conversions more consistently
John W. Eaton <jwe@octave.org>
parents:
10670
diff
changeset
|
422 gripe_nan_to_logical_conversion (); |
8626
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8290
diff
changeset
|
423 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
424 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
425 boolNDArray t2 = t1.all (); |
4875 | 426 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
427 retval = t2(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
428 } |
3220 | 429 } |
4875 | 430 |
3220 | 431 return retval; |
432 } | |
433 | |
434 template <class MT> | |
3219 | 435 bool |
436 octave_base_matrix<MT>::print_as_scalar (void) const | |
437 { | |
4654 | 438 dim_vector dv = dims (); |
3219 | 439 |
4654 | 440 return (dv.all_ones () || dv.any_zero ()); |
3219 | 441 } |
442 | |
443 template <class MT> | |
444 void | |
18416
bcd71a2531d3
Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents:
18100
diff
changeset
|
445 octave_base_matrix<MT>::print (std::ostream& os, bool pr_as_read_syntax) |
3219 | 446 { |
447 print_raw (os, pr_as_read_syntax); | |
448 newline (os); | |
449 } | |
450 | |
451 template <class MT> | |
452 void | |
3933 | 453 octave_base_matrix<MT>::print_info (std::ostream& os, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
454 const std::string& prefix) const |
3933 | 455 { |
456 matrix.print_info (os, prefix); | |
457 } | |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
458 |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
459 template <class MT> |
17870 | 460 void |
461 octave_base_matrix<MT>::short_disp (std::ostream& os) const | |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
462 { |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
463 if (matrix.is_empty ()) |
17870 | 464 os << "[]"; |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
465 else if (matrix.ndims () == 2) |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
466 { |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
467 // FIXME: should this be configurable? |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
468 octave_idx_type max_elts = 10; |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
469 octave_idx_type elts = 0; |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
470 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
471 octave_idx_type nel = matrix.numel (); |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
472 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
473 octave_idx_type nr = matrix.rows (); |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
474 octave_idx_type nc = matrix.columns (); |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
475 |
17870 | 476 os << "["; |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
477 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
478 for (octave_idx_type i = 0; i < nr; i++) |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
479 { |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
480 for (octave_idx_type j = 0; j < nc; j++) |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
481 { |
17870 | 482 std::ostringstream buf; |
483 octave_print_internal (buf, matrix(j*nr+i)); | |
484 std::string tmp = buf.str (); | |
485 size_t pos = tmp.find_first_not_of (" "); | |
486 os << tmp.substr (pos); | |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
487 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
488 if (++elts >= max_elts) |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
489 goto done; |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
490 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
491 if (j < nc - 1) |
17870 | 492 os << ", "; |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
493 } |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
494 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
495 if (i < nr - 1 && elts < max_elts) |
17870 | 496 os << "; "; |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
497 } |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
498 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
499 done: |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
500 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
501 if (nel <= max_elts) |
17870 | 502 os << "]"; |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
503 } |
17875
8fde5edabedd
Display '...' for ND-arrays in GUI Variable window.
Rik <rik@octave.org>
parents:
17870
diff
changeset
|
504 else |
8fde5edabedd
Display '...' for ND-arrays in GUI Variable window.
Rik <rik@octave.org>
parents:
17870
diff
changeset
|
505 os << "..."; |
17866
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
506 } |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
507 |
ea0ecbe2eaf5
display matrix values in GUI workspace viewer (bug #40499)
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
508 template <class MT> |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
509 octave_value |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
510 octave_base_matrix<MT>::fast_elem_extract (octave_idx_type n) const |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
511 { |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
512 if (n < matrix.numel ()) |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
513 return matrix(n); |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
514 else |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
515 return octave_value (); |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
516 } |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
517 |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
518 template <class MT> |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
519 bool |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
520 octave_base_matrix<MT>::fast_elem_insert (octave_idx_type n, |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
521 const octave_value& x) |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
522 { |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
523 if (n < matrix.numel ()) |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
524 { |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
525 // Don't use builtin_type () here to avoid an extra VM call. |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
526 typedef typename MT::element_type ET; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
527 const builtin_type_t btyp = class_to_btyp<ET>::btyp; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
528 if (btyp == btyp_unknown) // Dead branch? |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
529 return false; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
530 |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
531 // Set up the pointer to the proper place. |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
532 void *here = reinterpret_cast<void *> (&matrix(n)); |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
533 // Ask x to store there if it can. |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
534 return x.get_rep ().fast_elem_insert_self (here, btyp); |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
535 } |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
536 else |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
537 return false; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
538 } |