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 |
|
17 along with this program; see the file COPYING. If not, write to the Free |
|
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
19 |
|
20 */ |
|
21 |
|
22 #ifdef HAVE_CONFIG_H |
|
23 #include <config.h> |
|
24 #endif |
|
25 |
|
26 #include <iostream> |
|
27 |
|
28 #include "oct-obj.h" |
|
29 #include "ov-base.h" |
|
30 #include "quit.h" |
|
31 #include "pr-output.h" |
|
32 |
|
33 #include "byte-swap.h" |
|
34 #include "ls-oct-ascii.h" |
|
35 #include "ls-utils.h" |
|
36 #if defined (HAVE_HDF5) |
|
37 #include "ls-hdf5.h" |
|
38 #endif |
|
39 |
|
40 #include "boolSparse.h" |
|
41 #include "ov-base-sparse.h" |
|
42 |
|
43 template <class T> |
|
44 octave_value |
|
45 octave_base_sparse<T>::do_index_op (const octave_value_list& idx, |
|
46 int resize_ok) |
|
47 { |
|
48 octave_value retval; |
|
49 |
|
50 int n_idx = idx.length (); |
|
51 |
|
52 int nd = matrix.ndims (); |
|
53 |
|
54 switch (n_idx) |
|
55 { |
|
56 case 0: |
|
57 error ("invalid number of indices (= 0) for %d-dimensional array", nd); |
|
58 break; |
|
59 |
|
60 case 1: |
|
61 { |
|
62 idx_vector i = idx (0).index_vector (); |
|
63 |
|
64 if (! error_state) |
|
65 retval = octave_value (matrix.index (i, resize_ok)); |
|
66 } |
|
67 break; |
|
68 |
|
69 default: |
|
70 { |
|
71 if (n_idx == 2 && nd == 2) |
|
72 { |
|
73 idx_vector i = idx (0).index_vector (); |
|
74 |
|
75 if (! error_state) |
|
76 { |
|
77 idx_vector j = idx (1).index_vector (); |
|
78 |
|
79 if (! error_state) |
|
80 retval = octave_value (matrix.index (i, j, resize_ok)); |
|
81 } |
|
82 } |
|
83 else |
|
84 { |
|
85 Array<idx_vector> idx_vec (n_idx); |
|
86 |
|
87 for (int i = 0; i < n_idx; i++) |
|
88 { |
|
89 idx_vec(i) = idx(i).index_vector (); |
|
90 |
|
91 if (error_state) |
|
92 break; |
|
93 } |
|
94 |
|
95 if (! error_state) |
|
96 retval = octave_value (matrix.index (idx_vec, resize_ok)); |
|
97 } |
|
98 } |
|
99 break; |
|
100 } |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
|
105 template <class T> |
|
106 octave_value |
|
107 octave_base_sparse<T>::subsref (const std::string& type, |
|
108 const std::list<octave_value_list>& idx) |
|
109 { |
|
110 octave_value retval; |
|
111 |
|
112 switch (type[0]) |
|
113 { |
|
114 case '(': |
|
115 retval = do_index_op (idx.front ()); |
|
116 break; |
|
117 |
|
118 case '{': |
|
119 case '.': |
|
120 { |
|
121 std::string nm = type_name (); |
|
122 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
123 } |
|
124 break; |
|
125 |
|
126 default: |
|
127 panic_impossible (); |
|
128 } |
|
129 |
|
130 return retval.next_subsref (type, idx); |
|
131 } |
|
132 |
|
133 template <class T> |
|
134 octave_value |
|
135 octave_base_sparse<T>::subsasgn (const std::string& type, |
|
136 const std::list<octave_value_list>& idx, |
|
137 const octave_value& rhs) |
|
138 { |
|
139 octave_value retval; |
|
140 |
|
141 switch (type[0]) |
|
142 { |
|
143 case '(': |
|
144 { |
|
145 if (type.length () == 1) |
|
146 retval = numeric_assign (type, idx, rhs); |
|
147 else |
|
148 { |
|
149 std::string nm = type_name (); |
|
150 error ("in indexed assignment of %s, last lhs index must be ()", |
|
151 nm.c_str ()); |
|
152 } |
|
153 } |
|
154 break; |
|
155 |
|
156 case '{': |
|
157 case '.': |
|
158 { |
|
159 if (is_empty ()) |
|
160 { |
|
161 octave_value tmp = octave_value::empty_conv (type, rhs); |
|
162 |
|
163 retval = tmp.subsasgn (type, idx, rhs); |
|
164 } |
|
165 else |
|
166 { |
|
167 std::string nm = type_name (); |
|
168 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
169 } |
|
170 } |
|
171 break; |
|
172 |
|
173 default: |
|
174 panic_impossible (); |
|
175 } |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 template <class T> |
|
181 void |
|
182 octave_base_sparse<T>::assign (const octave_value_list& idx, const T& rhs) |
|
183 { |
|
184 int len = idx.length (); |
|
185 |
|
186 for (int i = 0; i < len; i++) |
|
187 matrix.set_index (idx(i).index_vector ()); |
|
188 |
|
189 ::assign (matrix, rhs); |
|
190 } |
|
191 |
|
192 |
|
193 template <class T> |
|
194 bool |
|
195 octave_base_sparse<T>::is_true (void) const |
|
196 { |
|
197 bool retval = false; |
|
198 dim_vector dv = matrix.dims (); |
|
199 int nel = dv.numel (); |
|
200 int nz = nnz (); |
|
201 |
|
202 if (nz == nel && nel > 0) |
|
203 { |
|
204 T t1 (matrix.reshape (dim_vector (nel, 1))); |
|
205 |
|
206 SparseBoolMatrix t2 = t1.all (); |
|
207 |
|
208 retval = t2(0); |
|
209 } |
|
210 |
|
211 return retval; |
|
212 } |
|
213 |
|
214 template <class T> |
|
215 bool |
|
216 octave_base_sparse<T>::print_as_scalar (void) const |
|
217 { |
|
218 dim_vector dv = dims (); |
|
219 |
|
220 return (dv.all_ones () || dv.any_zero ()); |
|
221 } |
|
222 |
|
223 template <class T> |
|
224 void |
|
225 octave_base_sparse<T>::print (std::ostream& os, bool pr_as_read_syntax) const |
|
226 { |
|
227 print_raw (os, pr_as_read_syntax); |
|
228 newline (os); |
|
229 } |
|
230 |
|
231 template <class T> |
|
232 void |
|
233 octave_base_sparse<T>::print_info (std::ostream& os, |
|
234 const std::string& prefix) const |
|
235 { |
|
236 matrix.print_info (os, prefix); |
|
237 } |
|
238 |
|
239 template <class T> |
|
240 void |
|
241 octave_base_sparse<T>::print_raw (std::ostream& os, |
|
242 bool pr_as_read_syntax) const |
|
243 { |
|
244 int nr = matrix.rows (); |
|
245 int nc = matrix.cols (); |
|
246 int nz = nonzero (); |
|
247 |
|
248 os << "Compressed Column Sparse (rows=" << nr << |
|
249 ", cols=" << nc << |
|
250 ", nnz=" << nz << ")"; |
|
251 |
|
252 // add one to the printed indices to go from |
|
253 // zero-based to one-based arrays |
|
254 |
|
255 if (nz != 0) |
|
256 { |
|
257 for (int j = 0; j < nc; j++) |
|
258 { |
|
259 OCTAVE_QUIT; |
|
260 for (int i = matrix.cidx(j); i < matrix.cidx(j+1); i++) |
|
261 { |
|
262 os << "\n"; |
|
263 os << " (" << matrix.ridx(i)+1 << |
|
264 " , " << j+1 << ") -> "; |
|
265 octave_print_internal( os, matrix.data(i), pr_as_read_syntax); |
|
266 } |
|
267 } |
|
268 } |
|
269 } |
|
270 |
|
271 template <class T> |
|
272 bool |
|
273 octave_base_sparse<T>::save_ascii (std::ostream& os, bool&, bool) |
|
274 { |
|
275 dim_vector dv = this->dims (); |
|
276 |
|
277 // Ensure that additional memory is deallocated |
|
278 matrix.maybe_compress (); |
|
279 |
|
280 os << "# nnz: " << nnz () << "\n"; |
|
281 os << "# rows: " << dv (0) << "\n"; |
|
282 os << "# columns: " << dv (1) << "\n"; |
|
283 |
|
284 os << this->matrix; |
|
285 |
|
286 return true; |
|
287 } |
|
288 |
|
289 template <class T> |
|
290 bool |
|
291 octave_base_sparse<T>::load_ascii (std::istream& is) |
|
292 { |
|
293 int nz = 0; |
|
294 int nr = 0; |
|
295 int nc = 0; |
|
296 bool success = true; |
|
297 |
|
298 if (extract_keyword (is, "nnz", nz, true) && |
|
299 extract_keyword (is, "rows", nr, true) && |
|
300 extract_keyword (is, "columns", nc, true)) |
|
301 { |
|
302 T tmp (nr, nc, nz); |
|
303 |
|
304 is >> tmp; |
|
305 |
|
306 if (!is) |
|
307 { |
|
308 error ("load: failed to load matrix constant"); |
|
309 success = false; |
|
310 } |
|
311 |
|
312 matrix = tmp; |
|
313 } |
|
314 else |
|
315 { |
|
316 error ("load: failed to extract number of rows and columns"); |
|
317 success = false; |
|
318 } |
|
319 |
|
320 return success; |
|
321 } |
|
322 |
|
323 /* |
|
324 ;;; Local Variables: *** |
|
325 ;;; mode: C++ *** |
|
326 ;;; End: *** |
|
327 */ |