Mercurial > hg > octave-nkf
annotate liboctave/boolSparse.cc @ 10976:80653e42a551
optimize any for sparse bool matrices
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 15 Sep 2010 14:37:30 +0200 |
parents | 4d1fc073fbb7 |
children | 4b51c0a20a98 |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008 David Bateman |
7016 | 4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10506
diff
changeset
|
5 Copyright (C) 2010 VZLU Prague |
7016 | 6 |
7 This file is part of Octave. | |
5164 | 8 |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
5164 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
5164 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
29 #include <iostream> | |
30 #include <vector> | |
31 | |
32 #include "config.h" | |
33 #include "quit.h" | |
34 #include "lo-ieee.h" | |
35 #include "lo-mappers.h" | |
36 | |
37 #include "boolSparse.h" | |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
38 #include "oct-mem.h" |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
39 #include "oct-locbuf.h" |
5164 | 40 |
41 // SparseBoolMatrix class. | |
42 | |
43 bool | |
44 SparseBoolMatrix::operator == (const SparseBoolMatrix& a) const | |
45 { | |
5275 | 46 octave_idx_type nr = rows (); |
47 octave_idx_type nc = cols (); | |
10506
bdf5d85cfc5e
replace nzmax by nnz where appropriate in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
48 octave_idx_type nz = nnz (); |
5275 | 49 octave_idx_type nr_a = a.rows (); |
50 octave_idx_type nc_a = a.cols (); | |
10506
bdf5d85cfc5e
replace nzmax by nnz where appropriate in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
51 octave_idx_type nz_a = a.nnz (); |
5164 | 52 |
53 if (nr != nr_a || nc != nc_a || nz != nz_a) | |
54 return false; | |
55 | |
5275 | 56 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164 | 57 if (cidx(i) != a.cidx(i)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
58 return false; |
5164 | 59 |
5275 | 60 for (octave_idx_type i = 0; i < nz; i++) |
5164 | 61 if (data(i) != a.data(i) || ridx(i) != a.ridx(i)) |
62 return false; | |
63 | |
64 return true; | |
65 } | |
66 | |
67 bool | |
68 SparseBoolMatrix::operator != (const SparseBoolMatrix& a) const | |
69 { | |
70 return !(*this == a); | |
71 } | |
72 | |
73 SparseBoolMatrix& | |
5275 | 74 SparseBoolMatrix::insert (const SparseBoolMatrix& a, octave_idx_type r, octave_idx_type c) |
5164 | 75 { |
76 Sparse<bool>::insert (a, r, c); | |
77 return *this; | |
78 } | |
79 | |
6823 | 80 SparseBoolMatrix& |
81 SparseBoolMatrix::insert (const SparseBoolMatrix& a, const Array<octave_idx_type>& indx) | |
82 { | |
83 Sparse<bool>::insert (a, indx); | |
84 return *this; | |
85 } | |
86 | |
5164 | 87 SparseBoolMatrix |
5275 | 88 SparseBoolMatrix::concat (const SparseBoolMatrix& rb, const Array<octave_idx_type>& ra_idx) |
5164 | 89 { |
90 // Don't use numel to avoid all possiblity of an overflow | |
91 if (rb.rows () > 0 && rb.cols () > 0) | |
92 insert (rb, ra_idx(0), ra_idx(1)); | |
93 return *this; | |
94 } | |
95 | |
96 // unary operations | |
97 | |
98 SparseBoolMatrix | |
99 SparseBoolMatrix::operator ! (void) const | |
100 { | |
5275 | 101 octave_idx_type nr = rows (); |
102 octave_idx_type nc = cols (); | |
10506
bdf5d85cfc5e
replace nzmax by nnz where appropriate in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
103 octave_idx_type nz1 = nnz (); |
5275 | 104 octave_idx_type nz2 = nr*nc - nz1; |
5164 | 105 |
106 SparseBoolMatrix r (nr, nc, nz2); | |
107 | |
5275 | 108 octave_idx_type ii = 0; |
109 octave_idx_type jj = 0; | |
6217 | 110 r.cidx (0) = 0; |
5275 | 111 for (octave_idx_type i = 0; i < nc; i++) |
5164 | 112 { |
5275 | 113 for (octave_idx_type j = 0; j < nr; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
114 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
115 if (jj < cidx(i+1) && ridx(jj) == j) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
116 jj++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
117 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
118 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
119 r.data(ii) = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
120 r.ridx(ii++) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
121 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
122 } |
6217 | 123 r.cidx (i+1) = ii; |
5164 | 124 } |
125 | |
126 return r; | |
127 } | |
128 | |
129 // other operations | |
130 | |
5775 | 131 // FIXME Do these really belong here? Maybe they should be |
5164 | 132 // in a base class? |
133 | |
134 SparseBoolMatrix | |
135 SparseBoolMatrix::all (int dim) const | |
136 { | |
137 SPARSE_ALL_OP (dim); | |
138 } | |
139 | |
140 SparseBoolMatrix | |
141 SparseBoolMatrix::any (int dim) const | |
142 { | |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
143 Sparse<bool> retval; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
144 octave_idx_type nr = rows (), nc = cols (), nz = nnz (); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
145 if (dim == -1) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
146 dim = (nr == 1 && nc != 1) ? 1 : 0; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
147 |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
148 if (dim == 0) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
149 { |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
150 // Result is a row vector. |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
151 retval = Sparse<bool> (1, nc); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
152 for(octave_idx_type i = 0; i < nc; i++) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
153 retval.xcidx(i+1) = retval.xcidx(i) + (cidx(i+1) > cidx(i)); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
154 octave_idx_type new_nz = retval.xcidx(nc); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
155 retval.change_capacity (new_nz); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
156 fill_or_memset (new_nz, static_cast<octave_idx_type> (0), retval.ridx ()); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
157 fill_or_memset (new_nz, true, retval.data ()); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
158 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
159 else if (dim == 1) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
160 { |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
161 // Result is a column vector. |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
162 if (nz > nr/4) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
163 { |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
164 // We can use O(nr) memory. |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
165 Array<bool> tmp (nr, 1, false); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
166 for (octave_idx_type i = 0; i < nz; i++) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
167 tmp.xelem(ridx(i)) = true; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
168 retval = tmp; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
169 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
170 else |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
171 { |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
172 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmpridx, nz); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
173 copy_or_memcpy (nz, ridx (), tmpridx); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
174 std::sort (tmpridx, tmpridx+nz); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
175 octave_idx_type new_nz = std::unique (tmpridx, tmpridx + nz) - tmpridx; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
176 retval = Sparse<bool> (nr, 1, new_nz); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
177 copy_or_memcpy (new_nz, tmpridx, retval.ridx ()); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
178 fill_or_memset (new_nz, true, retval.data ()); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
179 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
180 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
181 |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
182 return retval; |
5164 | 183 } |
184 | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
185 SparseBoolMatrix |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
186 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
|
187 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
188 return Sparse<bool>::diag (k); |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
189 } |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
190 |
5164 | 191 boolMatrix |
192 SparseBoolMatrix::matrix_value (void) const | |
193 { | |
5275 | 194 octave_idx_type nr = rows (); |
195 octave_idx_type nc = cols (); | |
5164 | 196 |
197 boolMatrix retval (nr, nc, false); | |
5275 | 198 for (octave_idx_type j = 0; j < nc; j++) |
199 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) | |
5164 | 200 retval.elem (ridx(i), j) = data (i); |
201 | |
202 return retval; | |
203 } | |
204 | |
205 std::ostream& | |
206 operator << (std::ostream& os, const SparseBoolMatrix& a) | |
207 { | |
5275 | 208 octave_idx_type nc = a.cols (); |
5164 | 209 |
210 // add one to the printed indices to go from | |
211 // zero-based to one-based arrays | |
5275 | 212 for (octave_idx_type j = 0; j < nc; j++) |
5164 | 213 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
214 octave_quit (); |
5275 | 215 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
216 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
5164 | 217 } |
218 | |
219 return os; | |
220 } | |
221 | |
222 std::istream& | |
223 operator >> (std::istream& is, SparseBoolMatrix& a) | |
224 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
225 typedef SparseBoolMatrix::element_type elt_type; |
5164 | 226 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
227 return read_sparse_matrix<elt_type> (is, a, octave_read_value<bool>); |
5164 | 228 } |
229 | |
230 SparseBoolMatrix | |
231 SparseBoolMatrix::squeeze (void) const | |
232 { | |
233 return Sparse<bool>::squeeze (); | |
234 } | |
235 | |
236 SparseBoolMatrix | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
237 SparseBoolMatrix::index (const idx_vector& i, bool resize_ok) const |
5164 | 238 { |
239 return Sparse<bool>::index (i, resize_ok); | |
240 } | |
241 | |
242 SparseBoolMatrix | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
243 SparseBoolMatrix::index (const idx_vector& i, const idx_vector& j, bool resize_ok) const |
5164 | 244 { |
245 return Sparse<bool>::index (i, j, resize_ok); | |
246 } | |
247 | |
248 SparseBoolMatrix | |
249 SparseBoolMatrix::reshape (const dim_vector& new_dims) const | |
250 { | |
251 return Sparse<bool>::reshape (new_dims); | |
252 } | |
253 | |
254 SparseBoolMatrix | |
5275 | 255 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164 | 256 { |
257 return Sparse<bool>::permute (vec, inv); | |
258 } | |
259 | |
260 SparseBoolMatrix | |
5275 | 261 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164 | 262 { |
263 return Sparse<bool>::ipermute (vec); | |
264 } | |
265 | |
266 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) | |
267 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) | |
268 | |
269 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) | |
270 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) | |
271 | |
272 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) | |
273 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) |