Mercurial > hg > octave-nkf
annotate src/ov-base-sparse.cc @ 10513:c5005bc2b7a9
implement working spalloc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 13 Apr 2010 12:36:24 +0200 |
parents | aac9f4265048 |
children | 4d1fc073fbb7 |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 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 #include "ls-hdf5.h" | |
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, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
48 bool resize_ok) |
5164 | 49 { |
50 octave_value retval; | |
51 | |
5275 | 52 octave_idx_type n_idx = idx.length (); |
5164 | 53 |
54 switch (n_idx) | |
55 { | |
56 case 0: | |
5539 | 57 retval = matrix; |
5164 | 58 break; |
59 | |
60 case 1: | |
61 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
62 idx_vector i = idx (0).index_vector (); |
5164 | 63 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
64 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
65 retval = octave_value (matrix.index (i, resize_ok)); |
5164 | 66 } |
67 break; | |
68 | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
69 case 2: |
5164 | 70 { |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
71 idx_vector i = idx (0).index_vector (); |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
72 |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
73 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
74 { |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
75 idx_vector j = idx (1).index_vector (); |
5164 | 76 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
77 if (! error_state) |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
78 retval = octave_value (matrix.index (i, j, resize_ok)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
79 } |
5164 | 80 } |
81 break; | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
82 default: |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
83 error ("sparse indexing needs 1 or 2 indices"); |
5164 | 84 } |
85 | |
86 return retval; | |
87 } | |
88 | |
89 template <class T> | |
90 octave_value | |
91 octave_base_sparse<T>::subsref (const std::string& type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 const std::list<octave_value_list>& idx) |
5164 | 93 { |
94 octave_value retval; | |
95 | |
96 switch (type[0]) | |
97 { | |
98 case '(': | |
99 retval = do_index_op (idx.front ()); | |
100 break; | |
101 | |
102 case '{': | |
103 case '.': | |
104 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
105 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
106 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
5164 | 107 } |
108 break; | |
109 | |
110 default: | |
111 panic_impossible (); | |
112 } | |
113 | |
114 return retval.next_subsref (type, idx); | |
115 } | |
116 | |
117 template <class T> | |
118 octave_value | |
119 octave_base_sparse<T>::subsasgn (const std::string& type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
120 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
121 const octave_value& rhs) |
5164 | 122 { |
123 octave_value retval; | |
124 | |
125 switch (type[0]) | |
126 { | |
127 case '(': | |
128 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
129 if (type.length () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
130 retval = numeric_assign (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
131 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
132 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
133 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
134 error ("in indexed assignment of %s, last lhs index must be ()", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
135 nm.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
136 } |
5164 | 137 } |
138 break; | |
139 | |
140 case '{': | |
141 case '.': | |
142 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
143 if (is_empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
144 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
145 octave_value tmp = octave_value::empty_conv (type, rhs); |
5164 | 146 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
147 retval = tmp.subsasgn (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
148 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
149 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
150 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
152 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
153 } |
5164 | 154 } |
155 break; | |
156 | |
157 default: | |
158 panic_impossible (); | |
159 } | |
160 | |
161 return retval; | |
162 } | |
163 | |
164 template <class T> | |
165 void | |
166 octave_base_sparse<T>::assign (const octave_value_list& idx, const T& rhs) | |
167 { | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
168 |
5275 | 169 octave_idx_type len = idx.length (); |
5164 | 170 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
171 switch (len) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
172 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
173 case 1: |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
174 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
175 idx_vector i = idx (0).index_vector (); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
176 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
177 if (! error_state) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
178 matrix.assign (i, rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
179 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
180 break; |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
181 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
182 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
183 case 2: |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
184 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
185 idx_vector i = idx (0).index_vector (); |
5164 | 186 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
187 if (! error_state) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
188 { |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
189 idx_vector j = idx (1).index_vector (); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
190 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
191 if (! error_state) |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
192 matrix.assign (i, j, rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
193 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
194 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
195 break; |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
196 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
197 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
198 default: |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
199 error ("sparse indexing needs 1 or 2 indices"); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
200 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
201 |
5322 | 202 |
203 // Invalidate matrix type. | |
204 typ.invalidate_type (); | |
5164 | 205 } |
206 | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
207 template <class MT> |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
208 void |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
209 octave_base_sparse<MT>::delete_elements (const octave_value_list& idx) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
210 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
211 octave_idx_type len = idx.length (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
212 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
213 switch (len) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
214 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
215 case 1: |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
216 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
217 idx_vector i = idx (0).index_vector (); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
218 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
219 if (! error_state) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
220 matrix.delete_elements (i); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
221 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
222 break; |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
223 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
224 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
225 case 2: |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
226 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
227 idx_vector i = idx (0).index_vector (); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
228 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
229 if (! error_state) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
230 { |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
231 idx_vector j = idx (1).index_vector (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
232 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
233 if (! error_state) |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
234 matrix.delete_elements (i, j); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
235 } |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
236 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
237 break; |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
238 } |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
239 |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
240 default: |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
241 error ("sparse indexing needs 1 or 2 indices"); |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
242 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
243 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
244 // Invalidate the matrix type |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
245 typ.invalidate_type (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
246 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
247 |
5731 | 248 template <class T> |
249 octave_value | |
250 octave_base_sparse<T>::resize (const dim_vector& dv, bool) const | |
251 { | |
252 T retval (matrix); | |
253 retval.resize (dv); | |
254 return retval; | |
255 } | |
5164 | 256 |
257 template <class T> | |
258 bool | |
259 octave_base_sparse<T>::is_true (void) const | |
260 { | |
261 bool retval = false; | |
262 dim_vector dv = matrix.dims (); | |
5275 | 263 octave_idx_type nel = dv.numel (); |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
264 octave_idx_type nz = nnz (); |
5164 | 265 |
266 if (nz == nel && nel > 0) | |
267 { | |
268 T t1 (matrix.reshape (dim_vector (nel, 1))); | |
269 | |
270 SparseBoolMatrix t2 = t1.all (); | |
271 | |
272 retval = t2(0); | |
273 } | |
274 | |
275 return retval; | |
276 } | |
277 | |
278 template <class T> | |
279 bool | |
280 octave_base_sparse<T>::print_as_scalar (void) const | |
281 { | |
282 dim_vector dv = dims (); | |
283 | |
284 return (dv.all_ones () || dv.any_zero ()); | |
285 } | |
286 | |
287 template <class T> | |
288 void | |
289 octave_base_sparse<T>::print (std::ostream& os, bool pr_as_read_syntax) const | |
290 { | |
291 print_raw (os, pr_as_read_syntax); | |
292 newline (os); | |
293 } | |
294 | |
295 template <class T> | |
296 void | |
297 octave_base_sparse<T>::print_info (std::ostream& os, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
298 const std::string& prefix) const |
5164 | 299 { |
300 matrix.print_info (os, prefix); | |
301 } | |
302 | |
303 template <class T> | |
304 void | |
305 octave_base_sparse<T>::print_raw (std::ostream& os, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
306 bool pr_as_read_syntax) const |
5164 | 307 { |
5275 | 308 octave_idx_type nr = matrix.rows (); |
309 octave_idx_type nc = matrix.cols (); | |
5604 | 310 octave_idx_type nz = nnz (); |
5164 | 311 |
5775 | 312 // FIXME -- this should probably all be handled by a |
5355 | 313 // separate octave_print_internal function that can handle format |
314 // compact, loose, etc. | |
315 | |
316 os << "Compressed Column Sparse (rows = " << nr | |
317 << ", 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
|
318 << ", nnz = " << nz; |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
319 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
320 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
|
321 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
322 if (dnel > 0) |
8848
7557cf34ffcd
ov-base-sparse.cc (octave_base_sparse<T>::print_raw): remove extra ")\n" from output
John W. Eaton <jwe@octave.org>
parents:
8676
diff
changeset
|
323 os << " [" << std::setprecision (2) << (nz / dnel * 100) << "%]"; |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
324 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
325 os << ")\n"; |
5164 | 326 |
327 // add one to the printed indices to go from | |
328 // zero-based to one-based arrays | |
329 | |
330 if (nz != 0) | |
331 { | |
5275 | 332 for (octave_idx_type j = 0; j < nc; j++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
333 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
334 octave_quit (); |
5355 | 335 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
336 // FIXME -- is there an easy way to get the max row |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
337 // and column indices so we can set the width appropriately |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
338 // and line up the columns here? Similarly, we should look |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
339 // at all the nonzero values and display them with the same |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
340 // formatting rules that apply to columns of a matrix. |
5355 | 341 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
342 for (octave_idx_type i = matrix.cidx(j); i < matrix.cidx(j+1); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
343 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
344 os << "\n"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
345 os << " (" << matrix.ridx(i)+1 << |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
346 ", " << j+1 << ") -> "; |
5355 | 347 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
348 octave_print_internal (os, matrix.data(i), pr_as_read_syntax); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
349 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
350 } |
5164 | 351 } |
352 } | |
353 | |
354 template <class T> | |
355 bool | |
6974 | 356 octave_base_sparse<T>::save_ascii (std::ostream& os) |
5164 | 357 { |
358 dim_vector dv = this->dims (); | |
359 | |
360 // Ensure that additional memory is deallocated | |
361 matrix.maybe_compress (); | |
362 | |
5604 | 363 os << "# nnz: " << nzmax () << "\n"; |
5164 | 364 os << "# rows: " << dv (0) << "\n"; |
365 os << "# columns: " << dv (1) << "\n"; | |
366 | |
367 os << this->matrix; | |
368 | |
369 return true; | |
370 } | |
371 | |
372 template <class T> | |
373 bool | |
374 octave_base_sparse<T>::load_ascii (std::istream& is) | |
375 { | |
5275 | 376 octave_idx_type nz = 0; |
377 octave_idx_type nr = 0; | |
378 octave_idx_type nc = 0; | |
5164 | 379 bool success = true; |
380 | |
381 if (extract_keyword (is, "nnz", nz, true) && | |
382 extract_keyword (is, "rows", nr, true) && | |
383 extract_keyword (is, "columns", nc, true)) | |
384 { | |
385 T tmp (nr, nc, nz); | |
386 | |
387 is >> tmp; | |
388 | |
389 if (!is) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
390 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
391 error ("load: failed to load matrix constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
392 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
393 } |
5164 | 394 |
395 matrix = tmp; | |
396 } | |
397 else | |
398 { | |
399 error ("load: failed to extract number of rows and columns"); | |
400 success = false; | |
401 } | |
402 | |
403 return success; | |
404 } | |
405 | |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
406 template <class T> |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
407 octave_value |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
408 octave_base_sparse<T>::map (octave_base_value::unary_mapper_t umap) const |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
409 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
410 // Try the map on the dense value. |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
411 octave_value retval = this->full_value ().map (umap); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
412 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
413 // Sparsify the result if possible. |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
414 // FIXME: intentionally skip this step for string mappers. Is this wanted? |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
415 if (umap >= umap_xisalnum && umap <= umap_xtoupper) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
416 return retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
417 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
418 switch (retval.builtin_type ()) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
419 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
420 case btyp_double: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
421 retval = retval.sparse_matrix_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
422 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
423 case btyp_complex: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
424 retval = retval.sparse_complex_matrix_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
425 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
426 case btyp_bool: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
427 retval = retval.sparse_bool_matrix_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
428 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
429 default: |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
430 break; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
431 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
432 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
433 return retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
434 } |