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