Mercurial > hg > octave-nkf
annotate liboctave/boolSparse.cc @ 10421:99e9bae2d81e
improve sparse indexing interface
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 18 Mar 2010 14:55:52 +0100 |
parents | 07ebe522dac2 |
children | bdf5d85cfc5e |
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 |
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)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
55 return false; |
5164 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
111 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
112 if (jj < cidx(i+1) && ridx(jj) == j) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
113 jj++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
114 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
115 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
116 r.data(ii) = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
117 r.ridx(ii++) = j; |
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 } |
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 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
172 octave_quit (); |
5275 | 173 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
|
174 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
5164 | 175 } |
176 | |
177 return os; | |
178 } | |
179 | |
180 std::istream& | |
181 operator >> (std::istream& is, SparseBoolMatrix& a) | |
182 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
183 typedef SparseBoolMatrix::element_type elt_type; |
5164 | 184 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
185 return read_sparse_matrix<elt_type> (is, a, octave_read_value<bool>); |
5164 | 186 } |
187 | |
188 SparseBoolMatrix | |
189 SparseBoolMatrix::squeeze (void) const | |
190 { | |
191 return Sparse<bool>::squeeze (); | |
192 } | |
193 | |
194 SparseBoolMatrix | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
195 SparseBoolMatrix::index (const idx_vector& i, bool resize_ok) const |
5164 | 196 { |
197 return Sparse<bool>::index (i, resize_ok); | |
198 } | |
199 | |
200 SparseBoolMatrix | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
201 SparseBoolMatrix::index (const idx_vector& i, const idx_vector& j, bool resize_ok) const |
5164 | 202 { |
203 return Sparse<bool>::index (i, j, resize_ok); | |
204 } | |
205 | |
206 SparseBoolMatrix | |
207 SparseBoolMatrix::reshape (const dim_vector& new_dims) const | |
208 { | |
209 return Sparse<bool>::reshape (new_dims); | |
210 } | |
211 | |
212 SparseBoolMatrix | |
5275 | 213 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164 | 214 { |
215 return Sparse<bool>::permute (vec, inv); | |
216 } | |
217 | |
218 SparseBoolMatrix | |
5275 | 219 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164 | 220 { |
221 return Sparse<bool>::ipermute (vec); | |
222 } | |
223 | |
224 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) | |
225 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) | |
226 | |
227 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) | |
228 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) | |
229 | |
230 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) | |
231 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) |