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