Mercurial > octave-nkf
annotate liboctave/boolSparse.cc @ 8710:739141cde75a ss-3-1-52
fix typo in Array-f.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 09 Feb 2009 21:51:31 +0100 |
parents | 36594d5bbe13 |
children | eb63fbe60fab |
rev | line source |
---|---|
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 #include <vector> | |
30 | |
31 #include "config.h" | |
32 #include "quit.h" | |
33 #include "lo-ieee.h" | |
34 #include "lo-mappers.h" | |
35 | |
36 #include "boolSparse.h" | |
37 | |
38 // SparseBoolMatrix class. | |
39 | |
40 bool | |
41 SparseBoolMatrix::operator == (const SparseBoolMatrix& a) const | |
42 { | |
5275 | 43 octave_idx_type nr = rows (); |
44 octave_idx_type nc = cols (); | |
5604 | 45 octave_idx_type nz = nzmax (); |
5275 | 46 octave_idx_type nr_a = a.rows (); |
47 octave_idx_type nc_a = a.cols (); | |
5604 | 48 octave_idx_type nz_a = a.nzmax (); |
5164 | 49 |
50 if (nr != nr_a || nc != nc_a || nz != nz_a) | |
51 return false; | |
52 | |
5275 | 53 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164 | 54 if (cidx(i) != a.cidx(i)) |
55 return false; | |
56 | |
5275 | 57 for (octave_idx_type i = 0; i < nz; i++) |
5164 | 58 if (data(i) != a.data(i) || ridx(i) != a.ridx(i)) |
59 return false; | |
60 | |
61 return true; | |
62 } | |
63 | |
64 bool | |
65 SparseBoolMatrix::operator != (const SparseBoolMatrix& a) const | |
66 { | |
67 return !(*this == a); | |
68 } | |
69 | |
70 SparseBoolMatrix& | |
5275 | 71 SparseBoolMatrix::insert (const SparseBoolMatrix& a, octave_idx_type r, octave_idx_type c) |
5164 | 72 { |
73 Sparse<bool>::insert (a, r, c); | |
74 return *this; | |
75 } | |
76 | |
6823 | 77 SparseBoolMatrix& |
78 SparseBoolMatrix::insert (const SparseBoolMatrix& a, const Array<octave_idx_type>& indx) | |
79 { | |
80 Sparse<bool>::insert (a, indx); | |
81 return *this; | |
82 } | |
83 | |
5164 | 84 SparseBoolMatrix |
5275 | 85 SparseBoolMatrix::concat (const SparseBoolMatrix& rb, const Array<octave_idx_type>& ra_idx) |
5164 | 86 { |
87 // Don't use numel to avoid all possiblity of an overflow | |
88 if (rb.rows () > 0 && rb.cols () > 0) | |
89 insert (rb, ra_idx(0), ra_idx(1)); | |
90 return *this; | |
91 } | |
92 | |
93 // unary operations | |
94 | |
95 SparseBoolMatrix | |
96 SparseBoolMatrix::operator ! (void) const | |
97 { | |
5275 | 98 octave_idx_type nr = rows (); |
99 octave_idx_type nc = cols (); | |
5604 | 100 octave_idx_type nz1 = nzmax (); |
5275 | 101 octave_idx_type nz2 = nr*nc - nz1; |
5164 | 102 |
103 SparseBoolMatrix r (nr, nc, nz2); | |
104 | |
5275 | 105 octave_idx_type ii = 0; |
106 octave_idx_type jj = 0; | |
6217 | 107 r.cidx (0) = 0; |
5275 | 108 for (octave_idx_type i = 0; i < nc; i++) |
5164 | 109 { |
5275 | 110 for (octave_idx_type j = 0; j < nr; j++) |
5164 | 111 { |
112 if (jj < cidx(i+1) && ridx(jj) == j) | |
113 jj++; | |
114 else | |
115 { | |
116 r.data(ii) = true; | |
117 r.ridx(ii++) = j; | |
118 } | |
119 } | |
6217 | 120 r.cidx (i+1) = ii; |
5164 | 121 } |
122 | |
123 return r; | |
124 } | |
125 | |
126 // other operations | |
127 | |
5775 | 128 // FIXME Do these really belong here? Maybe they should be |
5164 | 129 // in a base class? |
130 | |
131 SparseBoolMatrix | |
132 SparseBoolMatrix::all (int dim) const | |
133 { | |
134 SPARSE_ALL_OP (dim); | |
135 } | |
136 | |
137 SparseBoolMatrix | |
138 SparseBoolMatrix::any (int dim) const | |
139 { | |
140 SPARSE_ANY_OP (dim); | |
141 } | |
142 | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
143 SparseBoolMatrix |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
144 SparseBoolMatrix::diag (octave_idx_type k) const |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
145 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
146 return Sparse<bool>::diag (k); |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
147 } |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
148 |
5164 | 149 boolMatrix |
150 SparseBoolMatrix::matrix_value (void) const | |
151 { | |
5275 | 152 octave_idx_type nr = rows (); |
153 octave_idx_type nc = cols (); | |
5164 | 154 |
155 boolMatrix retval (nr, nc, false); | |
5275 | 156 for (octave_idx_type j = 0; j < nc; j++) |
157 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) | |
5164 | 158 retval.elem (ridx(i), j) = data (i); |
159 | |
160 return retval; | |
161 } | |
162 | |
163 std::ostream& | |
164 operator << (std::ostream& os, const SparseBoolMatrix& a) | |
165 { | |
5275 | 166 octave_idx_type nc = a.cols (); |
5164 | 167 |
168 // add one to the printed indices to go from | |
169 // zero-based to one-based arrays | |
5275 | 170 for (octave_idx_type j = 0; j < nc; j++) |
5164 | 171 { |
172 OCTAVE_QUIT; | |
5275 | 173 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164 | 174 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
175 } | |
176 | |
177 return os; | |
178 } | |
179 | |
180 std::istream& | |
181 operator >> (std::istream& is, SparseBoolMatrix& a) | |
182 { | |
5275 | 183 octave_idx_type nr = a.rows (); |
184 octave_idx_type nc = a.cols (); | |
5604 | 185 octave_idx_type nz = a.nzmax (); |
5164 | 186 |
187 if (nr < 1 || nc < 1) | |
188 is.clear (std::ios::badbit); | |
189 else | |
190 { | |
5275 | 191 octave_idx_type itmp, jtmp, jold = 0; |
5164 | 192 bool tmp; |
5275 | 193 octave_idx_type ii = 0; |
5164 | 194 |
195 a.cidx (0) = 0; | |
5275 | 196 for (octave_idx_type i = 0; i < nz; i++) |
5164 | 197 { |
198 is >> itmp; | |
199 itmp--; | |
200 is >> jtmp; | |
201 jtmp--; | |
202 is >> tmp; | |
203 if (is) | |
204 { | |
205 if (jold != jtmp) | |
206 { | |
5275 | 207 for (octave_idx_type j = jold; j < jtmp; j++) |
5164 | 208 a.cidx(j+1) = ii; |
209 | |
210 jold = jtmp; | |
211 } | |
212 a.data (ii) = tmp; | |
213 a.ridx (ii++) = itmp; | |
214 } | |
215 else | |
216 goto done; | |
217 } | |
218 | |
5275 | 219 for (octave_idx_type j = jold; j < nc; j++) |
5164 | 220 a.cidx(j+1) = ii; |
221 } | |
222 | |
223 done: | |
224 | |
225 return is; | |
226 } | |
227 | |
228 SparseBoolMatrix | |
229 SparseBoolMatrix::squeeze (void) const | |
230 { | |
231 return Sparse<bool>::squeeze (); | |
232 } | |
233 | |
234 SparseBoolMatrix | |
235 SparseBoolMatrix::index (idx_vector& i, int resize_ok) const | |
236 { | |
237 return Sparse<bool>::index (i, resize_ok); | |
238 } | |
239 | |
240 SparseBoolMatrix | |
241 SparseBoolMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const | |
242 { | |
243 return Sparse<bool>::index (i, j, resize_ok); | |
244 } | |
245 | |
246 SparseBoolMatrix | |
247 SparseBoolMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const | |
248 { | |
249 return Sparse<bool>::index (ra_idx, resize_ok); | |
250 } | |
251 | |
252 SparseBoolMatrix | |
253 SparseBoolMatrix::reshape (const dim_vector& new_dims) const | |
254 { | |
255 return Sparse<bool>::reshape (new_dims); | |
256 } | |
257 | |
258 SparseBoolMatrix | |
5275 | 259 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164 | 260 { |
261 return Sparse<bool>::permute (vec, inv); | |
262 } | |
263 | |
264 SparseBoolMatrix | |
5275 | 265 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164 | 266 { |
267 return Sparse<bool>::ipermute (vec); | |
268 } | |
269 | |
270 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) | |
271 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) | |
272 | |
273 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) | |
274 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) | |
275 | |
276 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) | |
277 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) | |
278 | |
279 | |
280 /* | |
281 ;;; Local Variables: *** | |
282 ;;; mode: C++ *** | |
283 ;;; End: *** | |
284 */ |