Mercurial > hg > octave-nkf
annotate liboctave/boolSparse.cc @ 10804:3d5c6b84ddaf
speed-up fixes to dlmread
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 20 Jul 2010 12:50:54 +0200 |
parents | 4d1fc073fbb7 |
children | 80653e42a551 |
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" | |
38 | |
39 // SparseBoolMatrix class. | |
40 | |
41 bool | |
42 SparseBoolMatrix::operator == (const SparseBoolMatrix& a) const | |
43 { | |
5275 | 44 octave_idx_type nr = rows (); |
45 octave_idx_type nc = cols (); | |
10506
bdf5d85cfc5e
replace nzmax by nnz where appropriate in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
46 octave_idx_type nz = nnz (); |
5275 | 47 octave_idx_type nr_a = a.rows (); |
48 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
|
49 octave_idx_type nz_a = a.nnz (); |
5164 | 50 |
51 if (nr != nr_a || nc != nc_a || nz != nz_a) | |
52 return false; | |
53 | |
5275 | 54 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164 | 55 if (cidx(i) != a.cidx(i)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
56 return false; |
5164 | 57 |
5275 | 58 for (octave_idx_type i = 0; i < nz; i++) |
5164 | 59 if (data(i) != a.data(i) || ridx(i) != a.ridx(i)) |
60 return false; | |
61 | |
62 return true; | |
63 } | |
64 | |
65 bool | |
66 SparseBoolMatrix::operator != (const SparseBoolMatrix& a) const | |
67 { | |
68 return !(*this == a); | |
69 } | |
70 | |
71 SparseBoolMatrix& | |
5275 | 72 SparseBoolMatrix::insert (const SparseBoolMatrix& a, octave_idx_type r, octave_idx_type c) |
5164 | 73 { |
74 Sparse<bool>::insert (a, r, c); | |
75 return *this; | |
76 } | |
77 | |
6823 | 78 SparseBoolMatrix& |
79 SparseBoolMatrix::insert (const SparseBoolMatrix& a, const Array<octave_idx_type>& indx) | |
80 { | |
81 Sparse<bool>::insert (a, indx); | |
82 return *this; | |
83 } | |
84 | |
5164 | 85 SparseBoolMatrix |
5275 | 86 SparseBoolMatrix::concat (const SparseBoolMatrix& rb, const Array<octave_idx_type>& ra_idx) |
5164 | 87 { |
88 // Don't use numel to avoid all possiblity of an overflow | |
89 if (rb.rows () > 0 && rb.cols () > 0) | |
90 insert (rb, ra_idx(0), ra_idx(1)); | |
91 return *this; | |
92 } | |
93 | |
94 // unary operations | |
95 | |
96 SparseBoolMatrix | |
97 SparseBoolMatrix::operator ! (void) const | |
98 { | |
5275 | 99 octave_idx_type nr = rows (); |
100 octave_idx_type nc = cols (); | |
10506
bdf5d85cfc5e
replace nzmax by nnz where appropriate in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
101 octave_idx_type nz1 = nnz (); |
5275 | 102 octave_idx_type nz2 = nr*nc - nz1; |
5164 | 103 |
104 SparseBoolMatrix r (nr, nc, nz2); | |
105 | |
5275 | 106 octave_idx_type ii = 0; |
107 octave_idx_type jj = 0; | |
6217 | 108 r.cidx (0) = 0; |
5275 | 109 for (octave_idx_type i = 0; i < nc; i++) |
5164 | 110 { |
5275 | 111 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
|
112 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
113 if (jj < cidx(i+1) && ridx(jj) == j) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
114 jj++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
115 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
116 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
117 r.data(ii) = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
118 r.ridx(ii++) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
119 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
120 } |
6217 | 121 r.cidx (i+1) = ii; |
5164 | 122 } |
123 | |
124 return r; | |
125 } | |
126 | |
127 // other operations | |
128 | |
5775 | 129 // FIXME Do these really belong here? Maybe they should be |
5164 | 130 // in a base class? |
131 | |
132 SparseBoolMatrix | |
133 SparseBoolMatrix::all (int dim) const | |
134 { | |
135 SPARSE_ALL_OP (dim); | |
136 } | |
137 | |
138 SparseBoolMatrix | |
139 SparseBoolMatrix::any (int dim) const | |
140 { | |
141 SPARSE_ANY_OP (dim); | |
142 } | |
143 | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
144 SparseBoolMatrix |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
145 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
|
146 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
147 return Sparse<bool>::diag (k); |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
148 } |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
149 |
5164 | 150 boolMatrix |
151 SparseBoolMatrix::matrix_value (void) const | |
152 { | |
5275 | 153 octave_idx_type nr = rows (); |
154 octave_idx_type nc = cols (); | |
5164 | 155 |
156 boolMatrix retval (nr, nc, false); | |
5275 | 157 for (octave_idx_type j = 0; j < nc; j++) |
158 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) | |
5164 | 159 retval.elem (ridx(i), j) = data (i); |
160 | |
161 return retval; | |
162 } | |
163 | |
164 std::ostream& | |
165 operator << (std::ostream& os, const SparseBoolMatrix& a) | |
166 { | |
5275 | 167 octave_idx_type nc = a.cols (); |
5164 | 168 |
169 // add one to the printed indices to go from | |
170 // zero-based to one-based arrays | |
5275 | 171 for (octave_idx_type j = 0; j < nc; j++) |
5164 | 172 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
173 octave_quit (); |
5275 | 174 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
|
175 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
5164 | 176 } |
177 | |
178 return os; | |
179 } | |
180 | |
181 std::istream& | |
182 operator >> (std::istream& is, SparseBoolMatrix& a) | |
183 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
184 typedef SparseBoolMatrix::element_type elt_type; |
5164 | 185 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
186 return read_sparse_matrix<elt_type> (is, a, octave_read_value<bool>); |
5164 | 187 } |
188 | |
189 SparseBoolMatrix | |
190 SparseBoolMatrix::squeeze (void) const | |
191 { | |
192 return Sparse<bool>::squeeze (); | |
193 } | |
194 | |
195 SparseBoolMatrix | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
196 SparseBoolMatrix::index (const idx_vector& i, bool resize_ok) const |
5164 | 197 { |
198 return Sparse<bool>::index (i, resize_ok); | |
199 } | |
200 | |
201 SparseBoolMatrix | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
202 SparseBoolMatrix::index (const idx_vector& i, const idx_vector& j, bool resize_ok) const |
5164 | 203 { |
204 return Sparse<bool>::index (i, j, resize_ok); | |
205 } | |
206 | |
207 SparseBoolMatrix | |
208 SparseBoolMatrix::reshape (const dim_vector& new_dims) const | |
209 { | |
210 return Sparse<bool>::reshape (new_dims); | |
211 } | |
212 | |
213 SparseBoolMatrix | |
5275 | 214 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164 | 215 { |
216 return Sparse<bool>::permute (vec, inv); | |
217 } | |
218 | |
219 SparseBoolMatrix | |
5275 | 220 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164 | 221 { |
222 return Sparse<bool>::ipermute (vec); | |
223 } | |
224 | |
225 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) | |
226 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) | |
227 | |
228 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) | |
229 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) | |
230 | |
231 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) | |
232 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) |