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