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