Mercurial > hg > octave-nkf
annotate src/ov-base-sparse.cc @ 7926:d74f996e005d
__magick_read__.cc: configuration and style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 14 Jul 2008 16:00:09 -0400 |
parents | 91d7440211e7 |
children | 283989f2da9b |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
7016 | 4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
5 | |
6 This file is part of Octave. | |
5164 | 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. | |
5164 | 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/>. | |
5164 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
28 #include <iomanip> |
5164 | 29 #include <iostream> |
30 | |
31 #include "oct-obj.h" | |
32 #include "ov-base.h" | |
33 #include "quit.h" | |
34 #include "pr-output.h" | |
35 | |
36 #include "byte-swap.h" | |
37 #include "ls-oct-ascii.h" | |
38 #include "ls-utils.h" | |
39 #if defined (HAVE_HDF5) | |
40 #include "ls-hdf5.h" | |
41 #endif | |
42 | |
43 #include "boolSparse.h" | |
44 #include "ov-base-sparse.h" | |
5731 | 45 #include "pager.h" |
5164 | 46 |
47 template <class T> | |
48 octave_value | |
49 octave_base_sparse<T>::do_index_op (const octave_value_list& idx, | |
5885 | 50 bool resize_ok) |
5164 | 51 { |
52 octave_value retval; | |
53 | |
5275 | 54 octave_idx_type n_idx = idx.length (); |
5164 | 55 |
56 int nd = matrix.ndims (); | |
57 | |
58 switch (n_idx) | |
59 { | |
60 case 0: | |
5539 | 61 retval = matrix; |
5164 | 62 break; |
63 | |
64 case 1: | |
65 { | |
66 idx_vector i = idx (0).index_vector (); | |
67 | |
68 if (! error_state) | |
69 retval = octave_value (matrix.index (i, resize_ok)); | |
70 } | |
71 break; | |
72 | |
73 default: | |
74 { | |
75 if (n_idx == 2 && nd == 2) | |
76 { | |
77 idx_vector i = idx (0).index_vector (); | |
78 | |
79 if (! error_state) | |
80 { | |
81 idx_vector j = idx (1).index_vector (); | |
82 | |
83 if (! error_state) | |
84 retval = octave_value (matrix.index (i, j, resize_ok)); | |
85 } | |
86 } | |
87 else | |
88 { | |
89 Array<idx_vector> idx_vec (n_idx); | |
90 | |
5275 | 91 for (octave_idx_type i = 0; i < n_idx; i++) |
5164 | 92 { |
93 idx_vec(i) = idx(i).index_vector (); | |
94 | |
95 if (error_state) | |
96 break; | |
97 } | |
98 | |
99 if (! error_state) | |
100 retval = octave_value (matrix.index (idx_vec, resize_ok)); | |
101 } | |
102 } | |
103 break; | |
104 } | |
105 | |
106 return retval; | |
107 } | |
108 | |
109 template <class T> | |
110 octave_value | |
111 octave_base_sparse<T>::subsref (const std::string& type, | |
112 const std::list<octave_value_list>& idx) | |
113 { | |
114 octave_value retval; | |
115 | |
116 switch (type[0]) | |
117 { | |
118 case '(': | |
119 retval = do_index_op (idx.front ()); | |
120 break; | |
121 | |
122 case '{': | |
123 case '.': | |
124 { | |
125 std::string nm = type_name (); | |
126 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
127 } | |
128 break; | |
129 | |
130 default: | |
131 panic_impossible (); | |
132 } | |
133 | |
134 return retval.next_subsref (type, idx); | |
135 } | |
136 | |
137 template <class T> | |
138 octave_value | |
139 octave_base_sparse<T>::subsasgn (const std::string& type, | |
140 const std::list<octave_value_list>& idx, | |
141 const octave_value& rhs) | |
142 { | |
143 octave_value retval; | |
144 | |
145 switch (type[0]) | |
146 { | |
147 case '(': | |
148 { | |
149 if (type.length () == 1) | |
150 retval = numeric_assign (type, idx, rhs); | |
151 else | |
152 { | |
153 std::string nm = type_name (); | |
154 error ("in indexed assignment of %s, last lhs index must be ()", | |
155 nm.c_str ()); | |
156 } | |
157 } | |
158 break; | |
159 | |
160 case '{': | |
161 case '.': | |
162 { | |
163 if (is_empty ()) | |
164 { | |
165 octave_value tmp = octave_value::empty_conv (type, rhs); | |
166 | |
167 retval = tmp.subsasgn (type, idx, rhs); | |
168 } | |
169 else | |
170 { | |
171 std::string nm = type_name (); | |
172 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
173 } | |
174 } | |
175 break; | |
176 | |
177 default: | |
178 panic_impossible (); | |
179 } | |
180 | |
181 return retval; | |
182 } | |
183 | |
184 template <class T> | |
185 void | |
186 octave_base_sparse<T>::assign (const octave_value_list& idx, const T& rhs) | |
187 { | |
5275 | 188 octave_idx_type len = idx.length (); |
5164 | 189 |
5275 | 190 for (octave_idx_type i = 0; i < len; i++) |
5164 | 191 matrix.set_index (idx(i).index_vector ()); |
192 | |
193 ::assign (matrix, rhs); | |
5322 | 194 |
195 // Invalidate matrix type. | |
196 typ.invalidate_type (); | |
5164 | 197 } |
198 | |
5731 | 199 template <class T> |
200 octave_value | |
201 octave_base_sparse<T>::resize (const dim_vector& dv, bool) const | |
202 { | |
203 T retval (matrix); | |
204 retval.resize (dv); | |
205 return retval; | |
206 } | |
5164 | 207 |
208 template <class T> | |
209 bool | |
210 octave_base_sparse<T>::is_true (void) const | |
211 { | |
212 bool retval = false; | |
213 dim_vector dv = matrix.dims (); | |
5275 | 214 octave_idx_type nel = dv.numel (); |
5604 | 215 octave_idx_type nz = nzmax (); |
5164 | 216 |
217 if (nz == nel && nel > 0) | |
218 { | |
219 T t1 (matrix.reshape (dim_vector (nel, 1))); | |
220 | |
221 SparseBoolMatrix t2 = t1.all (); | |
222 | |
223 retval = t2(0); | |
224 } | |
225 | |
226 return retval; | |
227 } | |
228 | |
229 template <class T> | |
230 bool | |
231 octave_base_sparse<T>::print_as_scalar (void) const | |
232 { | |
233 dim_vector dv = dims (); | |
234 | |
235 return (dv.all_ones () || dv.any_zero ()); | |
236 } | |
237 | |
238 template <class T> | |
239 void | |
240 octave_base_sparse<T>::print (std::ostream& os, bool pr_as_read_syntax) const | |
241 { | |
242 print_raw (os, pr_as_read_syntax); | |
243 newline (os); | |
244 } | |
245 | |
246 template <class T> | |
247 void | |
248 octave_base_sparse<T>::print_info (std::ostream& os, | |
249 const std::string& prefix) const | |
250 { | |
251 matrix.print_info (os, prefix); | |
252 } | |
253 | |
254 template <class T> | |
255 void | |
256 octave_base_sparse<T>::print_raw (std::ostream& os, | |
5355 | 257 bool pr_as_read_syntax) const |
5164 | 258 { |
5275 | 259 octave_idx_type nr = matrix.rows (); |
260 octave_idx_type nc = matrix.cols (); | |
5604 | 261 octave_idx_type nz = nnz (); |
5164 | 262 |
5775 | 263 // FIXME -- this should probably all be handled by a |
5355 | 264 // separate octave_print_internal function that can handle format |
265 // compact, loose, etc. | |
266 | |
267 os << "Compressed Column Sparse (rows = " << nr | |
268 << ", 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
|
269 << ", nnz = " << nz; |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
270 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
271 double dnel = matrix.numel (); |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
272 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
273 if (dnel > 0) |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
274 os << " [" << std::setprecision (2) << (nz / dnel * 100) << "%])\n"; |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
275 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
276 os << ")\n"; |
5164 | 277 |
278 // add one to the printed indices to go from | |
279 // zero-based to one-based arrays | |
280 | |
281 if (nz != 0) | |
282 { | |
5275 | 283 for (octave_idx_type j = 0; j < nc; j++) |
5164 | 284 { |
285 OCTAVE_QUIT; | |
5355 | 286 |
5775 | 287 // FIXME -- is there an easy way to get the max row |
5355 | 288 // and column indices so we can set the width appropriately |
289 // and line up the columns here? Similarly, we should look | |
290 // at all the nonzero values and display them with the same | |
291 // formatting rules that apply to columns of a matrix. | |
292 | |
5275 | 293 for (octave_idx_type i = matrix.cidx(j); i < matrix.cidx(j+1); i++) |
5164 | 294 { |
295 os << "\n"; | |
296 os << " (" << matrix.ridx(i)+1 << | |
5355 | 297 ", " << j+1 << ") -> "; |
298 | |
299 octave_print_internal (os, matrix.data(i), pr_as_read_syntax); | |
5164 | 300 } |
301 } | |
302 } | |
303 } | |
304 | |
305 template <class T> | |
306 bool | |
6974 | 307 octave_base_sparse<T>::save_ascii (std::ostream& os) |
5164 | 308 { |
309 dim_vector dv = this->dims (); | |
310 | |
311 // Ensure that additional memory is deallocated | |
312 matrix.maybe_compress (); | |
313 | |
5604 | 314 os << "# nnz: " << nzmax () << "\n"; |
5164 | 315 os << "# rows: " << dv (0) << "\n"; |
316 os << "# columns: " << dv (1) << "\n"; | |
317 | |
318 os << this->matrix; | |
319 | |
320 return true; | |
321 } | |
322 | |
323 template <class T> | |
324 bool | |
325 octave_base_sparse<T>::load_ascii (std::istream& is) | |
326 { | |
5275 | 327 octave_idx_type nz = 0; |
328 octave_idx_type nr = 0; | |
329 octave_idx_type nc = 0; | |
5164 | 330 bool success = true; |
331 | |
332 if (extract_keyword (is, "nnz", nz, true) && | |
333 extract_keyword (is, "rows", nr, true) && | |
334 extract_keyword (is, "columns", nc, true)) | |
335 { | |
336 T tmp (nr, nc, nz); | |
337 | |
338 is >> tmp; | |
339 | |
340 if (!is) | |
341 { | |
342 error ("load: failed to load matrix constant"); | |
343 success = false; | |
344 } | |
345 | |
346 matrix = tmp; | |
347 } | |
348 else | |
349 { | |
350 error ("load: failed to extract number of rows and columns"); | |
351 success = false; | |
352 } | |
353 | |
354 return success; | |
355 } | |
356 | |
357 /* | |
358 ;;; Local Variables: *** | |
359 ;;; mode: C++ *** | |
360 ;;; End: *** | |
361 */ |