Mercurial > hg > octave-nkf
annotate src/ov-base-sparse.cc @ 15283:a95432e7309c stable release-3-6-3
Version 3.6.3 released.
* configure.ac (AC_INIT): Version is now 3.6.3.
(OCTAVE_RELEASE_DATE): Now 2012-09-04.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 04 Sep 2012 13:17:13 -0400 |
parents | aa491bd9e19b |
children | f7afecdd87ef |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
3 Copyright (C) 2004-2012 David Bateman |
11523 | 4 Copyright (C) 1998-2004 Andy Adler |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10513
diff
changeset
|
5 Copyright (C) 2010 VZLU Prague |
7016 | 6 |
7 This file is part of Octave. | |
5164 | 8 |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
5164 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
5164 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
29 #include <iomanip> |
5164 | 30 #include <iostream> |
31 | |
32 #include "oct-obj.h" | |
33 #include "ov-base.h" | |
34 #include "quit.h" | |
35 #include "pr-output.h" | |
36 | |
37 #include "byte-swap.h" | |
38 #include "ls-oct-ascii.h" | |
39 #include "ls-utils.h" | |
40 #include "ls-hdf5.h" | |
41 | |
42 #include "boolSparse.h" | |
43 #include "ov-base-sparse.h" | |
5731 | 44 #include "pager.h" |
5164 | 45 |
46 template <class T> | |
47 octave_value | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
48 octave_base_sparse<T>::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
|
49 bool resize_ok) |
5164 | 50 { |
51 octave_value retval; | |
52 | |
5275 | 53 octave_idx_type n_idx = idx.length (); |
5164 | 54 |
55 switch (n_idx) | |
56 { | |
57 case 0: | |
5539 | 58 retval = matrix; |
5164 | 59 break; |
60 | |
61 case 1: | |
62 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
63 idx_vector i = idx (0).index_vector (); |
5164 | 64 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
65 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
66 retval = octave_value (matrix.index (i, resize_ok)); |
5164 | 67 } |
68 break; | |
69 | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
70 case 2: |
5164 | 71 { |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
72 idx_vector i = idx (0).index_vector (); |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
73 |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
74 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
75 { |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
76 idx_vector j = idx (1).index_vector (); |
5164 | 77 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
78 if (! error_state) |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
79 retval = octave_value (matrix.index (i, j, resize_ok)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
80 } |
5164 | 81 } |
82 break; | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
83 default: |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
84 error ("sparse indexing needs 1 or 2 indices"); |
5164 | 85 } |
86 | |
87 return retval; | |
88 } | |
89 | |
90 template <class T> | |
91 octave_value | |
92 octave_base_sparse<T>::subsref (const std::string& type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 const std::list<octave_value_list>& idx) |
5164 | 94 { |
95 octave_value retval; | |
96 | |
97 switch (type[0]) | |
98 { | |
99 case '(': | |
100 retval = do_index_op (idx.front ()); | |
101 break; | |
102 | |
103 case '{': | |
104 case '.': | |
105 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
106 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
107 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
5164 | 108 } |
109 break; | |
110 | |
111 default: | |
112 panic_impossible (); | |
113 } | |
114 | |
115 return retval.next_subsref (type, idx); | |
116 } | |
117 | |
118 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
119 octave_value |
5164 | 120 octave_base_sparse<T>::subsasgn (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
121 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
122 const octave_value& rhs) |
5164 | 123 { |
124 octave_value retval; | |
125 | |
126 switch (type[0]) | |
127 { | |
128 case '(': | |
129 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
130 if (type.length () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
131 retval = numeric_assign (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
132 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
133 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
134 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
135 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
|
136 nm.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
137 } |
5164 | 138 } |
139 break; | |
140 | |
141 case '{': | |
142 case '.': | |
143 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
144 if (is_empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
145 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
146 octave_value tmp = octave_value::empty_conv (type, rhs); |
5164 | 147 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
148 retval = tmp.subsasgn (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
149 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
150 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
152 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
153 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
|
154 } |
5164 | 155 } |
156 break; | |
157 | |
158 default: | |
159 panic_impossible (); | |
160 } | |
161 | |
162 return retval; | |
163 } | |
164 | |
165 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
166 void |
5164 | 167 octave_base_sparse<T>::assign (const octave_value_list& idx, const T& rhs) |
168 { | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
169 |
5275 | 170 octave_idx_type len = idx.length (); |
5164 | 171 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
172 switch (len) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
173 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
174 case 1: |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
175 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
176 idx_vector i = idx (0).index_vector (); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
177 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
178 if (! error_state) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
179 matrix.assign (i, rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
180 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
181 break; |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
182 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
183 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
184 case 2: |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
185 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
186 idx_vector i = idx (0).index_vector (); |
5164 | 187 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
188 if (! error_state) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
189 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
190 idx_vector j = idx (1).index_vector (); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
191 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
192 if (! error_state) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
193 matrix.assign (i, j, rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
194 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
195 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
196 break; |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
197 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
198 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
199 default: |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
200 error ("sparse indexing needs 1 or 2 indices"); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
201 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
202 |
5322 | 203 |
204 // Invalidate matrix type. | |
205 typ.invalidate_type (); | |
5164 | 206 } |
207 | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
208 template <class MT> |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
209 void |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
210 octave_base_sparse<MT>::delete_elements (const octave_value_list& idx) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
211 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
212 octave_idx_type len = idx.length (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
213 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
214 switch (len) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
215 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
216 case 1: |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
217 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
218 idx_vector i = idx (0).index_vector (); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
219 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
220 if (! error_state) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
221 matrix.delete_elements (i); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
222 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
223 break; |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
224 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
225 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
226 case 2: |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
227 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
228 idx_vector i = idx (0).index_vector (); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
229 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
230 if (! error_state) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
231 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
232 idx_vector j = idx (1).index_vector (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
233 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
234 if (! error_state) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
235 matrix.delete_elements (i, j); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
236 } |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
237 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
238 break; |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
239 } |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
240 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
241 default: |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
242 error ("sparse indexing needs 1 or 2 indices"); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
243 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
244 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
245 // Invalidate the matrix type |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
246 typ.invalidate_type (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
247 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
248 |
5731 | 249 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
250 octave_value |
5731 | 251 octave_base_sparse<T>::resize (const dim_vector& dv, bool) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
252 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
253 T retval (matrix); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
254 retval.resize (dv); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
255 return retval; |
5731 | 256 } |
5164 | 257 |
258 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
259 bool |
5164 | 260 octave_base_sparse<T>::is_true (void) const |
261 { | |
262 bool retval = false; | |
263 dim_vector dv = matrix.dims (); | |
5275 | 264 octave_idx_type nel = dv.numel (); |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
265 octave_idx_type nz = nnz (); |
5164 | 266 |
267 if (nz == nel && nel > 0) | |
268 { | |
269 T t1 (matrix.reshape (dim_vector (nel, 1))); | |
270 | |
271 SparseBoolMatrix t2 = t1.all (); | |
272 | |
273 retval = t2(0); | |
274 } | |
275 | |
276 return retval; | |
277 } | |
278 | |
279 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
280 bool |
5164 | 281 octave_base_sparse<T>::print_as_scalar (void) const |
282 { | |
283 dim_vector dv = dims (); | |
284 | |
285 return (dv.all_ones () || dv.any_zero ()); | |
286 } | |
287 | |
288 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
289 void |
5164 | 290 octave_base_sparse<T>::print (std::ostream& os, bool pr_as_read_syntax) const |
291 { | |
292 print_raw (os, pr_as_read_syntax); | |
293 newline (os); | |
294 } | |
295 | |
296 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
297 void |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
298 octave_base_sparse<T>::print_info (std::ostream& os, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
299 const std::string& prefix) const |
5164 | 300 { |
301 matrix.print_info (os, prefix); | |
302 } | |
303 | |
304 template <class T> | |
305 void | |
306 octave_base_sparse<T>::print_raw (std::ostream& os, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
307 bool pr_as_read_syntax) const |
5164 | 308 { |
5275 | 309 octave_idx_type nr = matrix.rows (); |
310 octave_idx_type nc = matrix.cols (); | |
5604 | 311 octave_idx_type nz = nnz (); |
5164 | 312 |
5775 | 313 // FIXME -- this should probably all be handled by a |
5355 | 314 // separate octave_print_internal function that can handle format |
315 // compact, loose, etc. | |
316 | |
317 os << "Compressed Column Sparse (rows = " << nr | |
318 << ", cols = " << nc | |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
319 << ", nnz = " << nz; |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
320 |
14566
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
321 // Avoid calling numel here since it can easily overflow |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
322 // octave_idx_type even when there is no real problem storing the |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
323 // sparse array. |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
324 |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
325 double dnr = nr; |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
326 double dnc = nc; |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
327 double dnel = dnr * dnc; |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
328 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
329 if (dnel > 0) |
11439
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
330 { |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
331 double pct = (nz / dnel * 100); |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
332 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
333 int prec = 2; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
334 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
335 // Display at least 2 significant figures and up to 4 as we |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
336 // approach 100%. Avoid having limited precision of the display |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
337 // result in reporting 100% for matrices that are not actually |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
338 // 100% full. |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
339 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
340 if (pct == 100) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
341 prec = 3; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
342 else |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
343 { |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
344 if (pct > 99.9) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
345 prec = 4; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
346 else if (pct > 99) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
347 prec = 3; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
348 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
349 if (pct > 99.99) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
350 pct = 99.99; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
351 } |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
352 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
353 os << " [" << std::setprecision (prec) << pct << "%]"; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
354 } |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
355 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
356 os << ")\n"; |
5164 | 357 |
358 // add one to the printed indices to go from | |
359 // zero-based to one-based arrays | |
360 | |
361 if (nz != 0) | |
362 { | |
5275 | 363 for (octave_idx_type j = 0; j < nc; j++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
364 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
365 octave_quit (); |
5355 | 366 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
367 // FIXME -- is there an easy way to get the max row |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
368 // and column indices so we can set the width appropriately |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
369 // and line up the columns here? Similarly, we should look |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
370 // at all the nonzero values and display them with the same |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
371 // formatting rules that apply to columns of a matrix. |
5355 | 372 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
373 for (octave_idx_type i = matrix.cidx(j); i < matrix.cidx(j+1); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
374 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
375 os << "\n"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
376 os << " (" << matrix.ridx(i)+1 << |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
377 ", " << j+1 << ") -> "; |
5355 | 378 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
379 octave_print_internal (os, matrix.data(i), pr_as_read_syntax); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
380 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
381 } |
5164 | 382 } |
383 } | |
384 | |
385 template <class T> | |
386 bool | |
6974 | 387 octave_base_sparse<T>::save_ascii (std::ostream& os) |
5164 | 388 { |
389 dim_vector dv = this->dims (); | |
390 | |
391 // Ensure that additional memory is deallocated | |
392 matrix.maybe_compress (); | |
393 | |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
394 os << "# nnz: " << nnz () << "\n"; |
5164 | 395 os << "# rows: " << dv (0) << "\n"; |
396 os << "# columns: " << dv (1) << "\n"; | |
397 | |
398 os << this->matrix; | |
399 | |
400 return true; | |
401 } | |
402 | |
403 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
404 bool |
5164 | 405 octave_base_sparse<T>::load_ascii (std::istream& is) |
406 { | |
5275 | 407 octave_idx_type nz = 0; |
408 octave_idx_type nr = 0; | |
409 octave_idx_type nc = 0; | |
5164 | 410 bool success = true; |
411 | |
412 if (extract_keyword (is, "nnz", nz, true) && | |
413 extract_keyword (is, "rows", nr, true) && | |
414 extract_keyword (is, "columns", nc, true)) | |
415 { | |
416 T tmp (nr, nc, nz); | |
417 | |
418 is >> tmp; | |
419 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
420 if (!is) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
421 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
422 error ("load: failed to load matrix constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
423 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
424 } |
5164 | 425 |
426 matrix = tmp; | |
427 } | |
428 else | |
429 { | |
430 error ("load: failed to extract number of rows and columns"); | |
431 success = false; | |
432 } | |
433 | |
434 return success; | |
435 } | |
436 | |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
437 template <class T> |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
438 octave_value |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
439 octave_base_sparse<T>::map (octave_base_value::unary_mapper_t umap) const |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
440 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
441 // Try the map on the dense value. |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
442 octave_value retval = this->full_value ().map (umap); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
443 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
444 // Sparsify the result if possible. |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
445 // FIXME: intentionally skip this step for string mappers. Is this wanted? |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
446 if (umap >= umap_xisalnum && umap <= umap_xtoupper) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
447 return retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
448 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
449 switch (retval.builtin_type ()) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
450 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
451 case btyp_double: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
452 retval = retval.sparse_matrix_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
453 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
454 case btyp_complex: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
455 retval = retval.sparse_complex_matrix_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
456 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
457 case btyp_bool: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
458 retval = retval.sparse_bool_matrix_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
459 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
460 default: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
461 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
462 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
463 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
464 return retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
465 } |