Mercurial > hg > octave-nkf
annotate liboctave/array/boolSparse.cc @ 17769:49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
* liboctave/array/Array-C.cc, liboctave/array/Array-b.cc,
liboctave/array/Array-ch.cc, liboctave/array/Array-d.cc,
liboctave/array/Array-f.cc, liboctave/array/Array-fC.cc,
liboctave/array/Array-util.cc, liboctave/array/Array-util.h,
liboctave/array/Array.cc, liboctave/array/Array.h, liboctave/array/Array3.h,
liboctave/array/CColVector.cc, liboctave/array/CColVector.h,
liboctave/array/CDiagMatrix.cc, liboctave/array/CDiagMatrix.h,
liboctave/array/CMatrix.cc, liboctave/array/CMatrix.h,
liboctave/array/CNDArray.cc, liboctave/array/CNDArray.h,
liboctave/array/CRowVector.cc, liboctave/array/CRowVector.h,
liboctave/array/CSparse.cc, liboctave/array/CSparse.h,
liboctave/array/DiagArray2.h, liboctave/array/MArray.cc,
liboctave/array/MArray.h, liboctave/array/MDiagArray2.cc,
liboctave/array/MDiagArray2.h, liboctave/array/MSparse.cc,
liboctave/array/MSparse.h, liboctave/array/MatrixType.cc,
liboctave/array/MatrixType.h, liboctave/array/PermMatrix.h,
liboctave/array/Range.cc, liboctave/array/Range.h, liboctave/array/Sparse.cc,
liboctave/array/Sparse.h, liboctave/array/boolMatrix.cc,
liboctave/array/boolMatrix.h, liboctave/array/boolNDArray.cc,
liboctave/array/boolNDArray.h, liboctave/array/boolSparse.cc,
liboctave/array/boolSparse.h, liboctave/array/chMatrix.cc,
liboctave/array/chMatrix.h, liboctave/array/chNDArray.cc,
liboctave/array/chNDArray.h, liboctave/array/dColVector.h,
liboctave/array/dDiagMatrix.cc, liboctave/array/dDiagMatrix.h,
liboctave/array/dMatrix.cc, liboctave/array/dMatrix.h,
liboctave/array/dNDArray.cc, liboctave/array/dNDArray.h,
liboctave/array/dRowVector.h, liboctave/array/dSparse.cc,
liboctave/array/dSparse.h, liboctave/array/dim-vector.cc,
liboctave/array/dim-vector.h, liboctave/array/fCColVector.cc,
liboctave/array/fCColVector.h, liboctave/array/fCDiagMatrix.cc,
liboctave/array/fCDiagMatrix.h, liboctave/array/fCMatrix.cc,
liboctave/array/fCMatrix.h, liboctave/array/fCNDArray.cc,
liboctave/array/fCNDArray.h, liboctave/array/fCRowVector.cc,
liboctave/array/fCRowVector.h, liboctave/array/fColVector.h,
liboctave/array/fDiagMatrix.cc, liboctave/array/fDiagMatrix.h,
liboctave/array/fMatrix.cc, liboctave/array/fMatrix.h,
liboctave/array/fNDArray.cc, liboctave/array/fNDArray.h,
liboctave/array/fRowVector.h, liboctave/array/idx-vector.cc,
liboctave/array/idx-vector.h, liboctave/array/intNDArray.cc,
liboctave/array/intNDArray.h, liboctave/cruft/misc/blaswrap.c,
liboctave/cruft/misc/quit.cc, liboctave/numeric/CmplxCHOL.cc,
liboctave/numeric/CmplxCHOL.h, liboctave/numeric/CmplxGEPBAL.cc,
liboctave/numeric/CmplxGEPBAL.h, liboctave/numeric/CmplxHESS.h,
liboctave/numeric/CmplxLU.cc, liboctave/numeric/CmplxLU.h,
liboctave/numeric/CmplxQR.cc, liboctave/numeric/CmplxQRP.cc,
liboctave/numeric/CmplxQRP.h, liboctave/numeric/CmplxSCHUR.h,
liboctave/numeric/CmplxSVD.cc, liboctave/numeric/CmplxSVD.h,
liboctave/numeric/CollocWt.h, liboctave/numeric/DAE.h,
liboctave/numeric/DAEFunc.h, liboctave/numeric/DAERT.h,
liboctave/numeric/DAERTFunc.h, liboctave/numeric/DASPK.cc,
liboctave/numeric/DASRT.cc, liboctave/numeric/DASRT.h,
liboctave/numeric/DASSL.cc, liboctave/numeric/DET.h, liboctave/numeric/EIG.cc,
liboctave/numeric/EIG.h, liboctave/numeric/LSODE.cc, liboctave/numeric/ODE.h,
liboctave/numeric/ODEFunc.h, liboctave/numeric/ODES.h,
liboctave/numeric/ODESFunc.h, liboctave/numeric/Quad.cc,
liboctave/numeric/Quad.h, liboctave/numeric/SparseCmplxCHOL.h,
liboctave/numeric/SparseCmplxLU.cc, liboctave/numeric/SparseCmplxLU.h,
liboctave/numeric/SparseCmplxQR.cc, liboctave/numeric/SparseCmplxQR.h,
liboctave/numeric/SparseQR.cc, liboctave/numeric/SparseQR.h,
liboctave/numeric/SparsedbleCHOL.h, liboctave/numeric/SparsedbleLU.cc,
liboctave/numeric/SparsedbleLU.h, liboctave/numeric/base-aepbal.h,
liboctave/numeric/base-dae.h, liboctave/numeric/base-de.h,
liboctave/numeric/base-lu.cc, liboctave/numeric/base-lu.h,
liboctave/numeric/base-min.h, liboctave/numeric/base-qr.h,
liboctave/numeric/bsxfun.h, liboctave/numeric/dbleCHOL.cc,
liboctave/numeric/dbleCHOL.h, liboctave/numeric/dbleGEPBAL.h,
liboctave/numeric/dbleHESS.h, liboctave/numeric/dbleLU.cc,
liboctave/numeric/dbleLU.h, liboctave/numeric/dbleQR.cc,
liboctave/numeric/dbleQRP.cc, liboctave/numeric/dbleQRP.h,
liboctave/numeric/dbleSCHUR.cc, liboctave/numeric/dbleSCHUR.h,
liboctave/numeric/dbleSVD.cc, liboctave/numeric/dbleSVD.h,
liboctave/numeric/eigs-base.cc, liboctave/numeric/fCmplxAEPBAL.cc,
liboctave/numeric/fCmplxAEPBAL.h, liboctave/numeric/fCmplxCHOL.cc,
liboctave/numeric/fCmplxCHOL.h, liboctave/numeric/fCmplxGEPBAL.cc,
liboctave/numeric/fCmplxGEPBAL.h, liboctave/numeric/fCmplxHESS.h,
liboctave/numeric/fCmplxLU.cc, liboctave/numeric/fCmplxLU.h,
liboctave/numeric/fCmplxQR.cc, liboctave/numeric/fCmplxQR.h,
liboctave/numeric/fCmplxQRP.cc, liboctave/numeric/fCmplxQRP.h,
liboctave/numeric/fCmplxSCHUR.cc, liboctave/numeric/fCmplxSCHUR.h,
liboctave/numeric/fCmplxSVD.h, liboctave/numeric/fEIG.cc,
liboctave/numeric/fEIG.h, liboctave/numeric/floatCHOL.cc,
liboctave/numeric/floatCHOL.h, liboctave/numeric/floatGEPBAL.cc,
liboctave/numeric/floatGEPBAL.h, liboctave/numeric/floatHESS.h,
liboctave/numeric/floatLU.cc, liboctave/numeric/floatLU.h,
liboctave/numeric/floatQR.cc, liboctave/numeric/floatQRP.cc,
liboctave/numeric/floatQRP.h, liboctave/numeric/floatSCHUR.cc,
liboctave/numeric/floatSCHUR.h, liboctave/numeric/floatSVD.cc,
liboctave/numeric/floatSVD.h, liboctave/numeric/lo-mappers.cc,
liboctave/numeric/lo-mappers.h, liboctave/numeric/lo-specfun.cc,
liboctave/numeric/lo-specfun.h, liboctave/numeric/oct-convn.cc,
liboctave/numeric/oct-fftw.cc, liboctave/numeric/oct-fftw.h,
liboctave/numeric/oct-norm.cc, liboctave/numeric/oct-rand.cc,
liboctave/numeric/oct-rand.h, liboctave/numeric/randgamma.c,
liboctave/numeric/randgamma.h, liboctave/numeric/randmtzig.c,
liboctave/numeric/randpoisson.c, liboctave/numeric/randpoisson.h,
liboctave/numeric/sparse-base-chol.h, liboctave/numeric/sparse-base-lu.h,
liboctave/numeric/sparse-dmsolve.cc, liboctave/operators/Sparse-diag-op-defs.h,
liboctave/operators/Sparse-op-defs.h, liboctave/operators/mx-inlines.cc,
liboctave/system/dir-ops.h, liboctave/system/file-ops.cc,
liboctave/system/file-stat.cc, liboctave/system/file-stat.h,
liboctave/system/lo-sysdep.cc, liboctave/system/lo-sysdep.h,
liboctave/system/mach-info.cc, liboctave/system/mach-info.h,
liboctave/system/oct-env.cc, liboctave/system/oct-group.cc,
liboctave/system/oct-syscalls.cc, liboctave/system/oct-syscalls.h,
liboctave/system/oct-time.h, liboctave/system/tempname.c,
liboctave/util/action-container.h, liboctave/util/base-list.h,
liboctave/util/cmd-edit.cc, liboctave/util/cmd-edit.h,
liboctave/util/cmd-hist.cc, liboctave/util/cmd-hist.h,
liboctave/util/data-conv.cc, liboctave/util/data-conv.h,
liboctave/util/kpse.cc, liboctave/util/lo-array-gripes.cc,
liboctave/util/lo-cieee.c, liboctave/util/lo-regexp.cc,
liboctave/util/lo-utils.cc, liboctave/util/oct-alloc.cc,
liboctave/util/oct-base64.cc, liboctave/util/oct-binmap.h,
liboctave/util/oct-cmplx.h, liboctave/util/oct-glob.cc,
liboctave/util/oct-inttypes.cc, liboctave/util/oct-inttypes.h,
liboctave/util/oct-locbuf.cc, liboctave/util/oct-locbuf.h,
liboctave/util/oct-mem.h, liboctave/util/oct-mutex.cc,
liboctave/util/oct-refcount.h, liboctave/util/oct-shlib.cc,
liboctave/util/oct-shlib.h, liboctave/util/oct-sort.cc,
liboctave/util/oct-sort.h, liboctave/util/pathsearch.cc,
liboctave/util/pathsearch.h, liboctave/util/sparse-util.cc,
liboctave/util/str-vec.cc, liboctave/util/str-vec.h,
liboctave/util/unwind-prot.h, liboctave/util/url-transfer.cc,
liboctave/util/url-transfer.h: Use GNU style coding conventions.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 26 Oct 2013 18:57:05 -0700 |
parents | d63878346099 |
children | 8e056300994b |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
3 Copyright (C) 2004-2013 David Bateman |
11523 | 4 Copyright (C) 1998-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 "quit.h" | |
33 #include "lo-ieee.h" | |
34 #include "lo-mappers.h" | |
35 | |
36 #include "boolSparse.h" | |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
37 #include "dSparse.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++) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
57 if (cidx (i) != a.cidx (i)) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 return false; |
5164 | 59 |
5275 | 60 for (octave_idx_type i = 0; i < nz; i++) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
61 if (data (i) != a.data (i) || ridx (i) != a.ridx (i)) |
5164 | 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& | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
74 SparseBoolMatrix::insert (const SparseBoolMatrix& a, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
75 octave_idx_type r, octave_idx_type c) |
5164 | 76 { |
77 Sparse<bool>::insert (a, r, c); | |
78 return *this; | |
79 } | |
80 | |
6823 | 81 SparseBoolMatrix& |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 SparseBoolMatrix::insert (const SparseBoolMatrix& a, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
83 const Array<octave_idx_type>& indx) |
6823 | 84 { |
85 Sparse<bool>::insert (a, indx); | |
86 return *this; | |
87 } | |
88 | |
5164 | 89 SparseBoolMatrix |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 SparseBoolMatrix::concat (const SparseBoolMatrix& rb, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 const Array<octave_idx_type>& ra_idx) |
5164 | 92 { |
93 // Don't use numel to avoid all possiblity of an overflow | |
94 if (rb.rows () > 0 && rb.cols () > 0) | |
95 insert (rb, ra_idx(0), ra_idx(1)); | |
96 return *this; | |
97 } | |
98 | |
99 // unary operations | |
100 | |
101 SparseBoolMatrix | |
102 SparseBoolMatrix::operator ! (void) const | |
103 { | |
5275 | 104 octave_idx_type nr = rows (); |
105 octave_idx_type nc = cols (); | |
10506
bdf5d85cfc5e
replace nzmax by nnz where appropriate in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
106 octave_idx_type nz1 = nnz (); |
5275 | 107 octave_idx_type nz2 = nr*nc - nz1; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
108 |
5164 | 109 SparseBoolMatrix r (nr, nc, nz2); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
110 |
5275 | 111 octave_idx_type ii = 0; |
112 octave_idx_type jj = 0; | |
6217 | 113 r.cidx (0) = 0; |
5275 | 114 for (octave_idx_type i = 0; i < nc; i++) |
5164 | 115 { |
5275 | 116 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
|
117 { |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
118 if (jj < cidx (i+1) && ridx (jj) == j) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
119 jj++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
120 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
121 { |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
122 r.data (ii) = true; |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
123 r.ridx (ii++) = j; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
124 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
125 } |
6217 | 126 r.cidx (i+1) = ii; |
5164 | 127 } |
128 | |
129 return r; | |
130 } | |
131 | |
132 // other operations | |
133 | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 // FIXME: Do these really belong here? Maybe they should be in a base class? |
5164 | 135 |
136 SparseBoolMatrix | |
137 SparseBoolMatrix::all (int dim) const | |
138 { | |
139 SPARSE_ALL_OP (dim); | |
140 } | |
141 | |
142 SparseBoolMatrix | |
143 SparseBoolMatrix::any (int dim) const | |
144 { | |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
145 Sparse<bool> retval; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
146 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
|
147 if (dim == -1) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
148 dim = (nr == 1 && nc != 1) ? 1 : 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 if (dim == 0) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
151 { |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
152 // Result is a row vector. |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
153 retval = Sparse<bool> (1, nc); |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
154 retval.xcidx (0) = 0; |
11571
0e414f837c58
Fix indexing error in the construction of sparse matrices
David Bateman <dbateman@free.fr>
parents:
11570
diff
changeset
|
155 for (octave_idx_type i = 0; i < nc; i++) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
156 retval.xcidx (i+1) = retval.xcidx (i) + (cidx (i+1) > cidx (i)); |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
157 octave_idx_type new_nz = retval.xcidx (nc); |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
158 retval.change_capacity (new_nz); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
159 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
|
160 fill_or_memset (new_nz, true, retval.data ()); |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
161 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
162 else if (dim == 1) |
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 // Result is a column vector. |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
165 if (nz > nr/4) |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
166 { |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
167 // We can use O(nr) memory. |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
168 Array<bool> tmp (dim_vector (nr, 1), false); |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
169 for (octave_idx_type i = 0; i < nz; i++) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
170 tmp.xelem (ridx (i)) = true; |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
171 retval = tmp; |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
172 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
173 else |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
174 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
175 Array<octave_idx_type> tmp (dim_vector (nz, 1)); |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
176 copy_or_memcpy (nz, ridx (), tmp.fortran_vec ()); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
177 retval = Sparse<bool> (Array<bool> (dim_vector (1, 1), true), |
11287 | 178 idx_vector (tmp), |
179 idx_vector (static_cast<octave_idx_type> (0)), | |
180 nr, 1, false); | |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
181 } |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
182 } |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
183 |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
184 return retval; |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
185 } |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
186 |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
187 SparseMatrix |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
188 SparseBoolMatrix::sum (int dim) const |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
189 { |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
190 Sparse<double> retval; |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
191 octave_idx_type nr = rows (), nc = cols (), nz = nnz (); |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
192 if (dim == -1) |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
193 dim = (nr == 1 && nc != 1) ? 1 : 0; |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
194 |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
195 if (dim == 0) |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
196 { |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
197 // Result is a row vector. |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
198 retval = Sparse<double> (1, nc); |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
199 for (octave_idx_type i = 0; i < nc; i++) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
200 retval.xcidx (i+1) = retval.xcidx (i) + (cidx (i+1) > cidx (i)); |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
201 octave_idx_type new_nz = retval.xcidx (nc); |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
202 retval.change_capacity (new_nz); |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
203 fill_or_memset (new_nz, static_cast<octave_idx_type> (0), retval.ridx ()); |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
204 for (octave_idx_type i = 0, k = 0; i < nc; i++) |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
205 { |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
206 octave_idx_type c = cidx (i+1) - cidx (i); |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
207 if (c > 0) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
208 retval.xdata (k++) = c; |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
209 } |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
210 } |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
211 else if (dim == 1) |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
212 { |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
213 // Result is a column vector. |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
214 if (nz > nr) |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
215 { |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
216 // We can use O(nr) memory. |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
217 Array<double> tmp (dim_vector (nr, 1), 0); |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
218 for (octave_idx_type i = 0; i < nz; i++) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
219 tmp.xelem (ridx (i)) += 1.0; |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
220 retval = tmp; |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
221 } |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
222 else |
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
223 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
224 Array<octave_idx_type> tmp (dim_vector (nz, 1)); |
10983
4b51c0a20a98
optimize sum of sparse logical matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10976
diff
changeset
|
225 copy_or_memcpy (nz, ridx (), tmp.fortran_vec ()); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
226 retval = Sparse<double> (Array<double> (dim_vector (1, 1), 1.0), |
11287 | 227 idx_vector (tmp), |
228 idx_vector (static_cast<octave_idx_type> (0)), | |
229 nr, 1); | |
10976
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
230 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
231 } |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
232 |
80653e42a551
optimize any for sparse bool matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
233 return retval; |
5164 | 234 } |
235 | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
236 SparseBoolMatrix |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
237 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
|
238 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
239 return Sparse<bool>::diag (k); |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
240 } |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
241 |
5164 | 242 boolMatrix |
243 SparseBoolMatrix::matrix_value (void) const | |
244 { | |
5275 | 245 octave_idx_type nr = rows (); |
246 octave_idx_type nc = cols (); | |
5164 | 247 |
248 boolMatrix retval (nr, nc, false); | |
5275 | 249 for (octave_idx_type j = 0; j < nc; j++) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
250 for (octave_idx_type i = cidx (j); i < cidx (j+1); i++) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
251 retval.elem (ridx (i), j) = data (i); |
5164 | 252 |
253 return retval; | |
254 } | |
255 | |
256 std::ostream& | |
257 operator << (std::ostream& os, const SparseBoolMatrix& a) | |
258 { | |
5275 | 259 octave_idx_type nc = a.cols (); |
5164 | 260 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
261 // add one to the printed indices to go from |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
262 // zero-based to one-based arrays |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
263 for (octave_idx_type j = 0; j < nc; j++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
264 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
265 octave_quit (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
266 for (octave_idx_type i = a.cidx (j); i < a.cidx (j+1); i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
267 os << a.ridx (i) + 1 << " " << j + 1 << " " << a.data (i) << "\n"; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
268 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
269 |
5164 | 270 return os; |
271 } | |
272 | |
273 std::istream& | |
274 operator >> (std::istream& is, SparseBoolMatrix& a) | |
275 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
276 typedef SparseBoolMatrix::element_type elt_type; |
5164 | 277 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8999
diff
changeset
|
278 return read_sparse_matrix<elt_type> (is, a, octave_read_value<bool>); |
5164 | 279 } |
280 | |
281 SparseBoolMatrix | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
282 SparseBoolMatrix::squeeze (void) const |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
283 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
284 return Sparse<bool>::squeeze (); |
5164 | 285 } |
286 | |
287 SparseBoolMatrix | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
288 SparseBoolMatrix::index (const idx_vector& i, bool resize_ok) const |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
289 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
290 return Sparse<bool>::index (i, resize_ok); |
5164 | 291 } |
292 | |
293 SparseBoolMatrix | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
294 SparseBoolMatrix::index (const idx_vector& i, const idx_vector& j, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
295 bool resize_ok) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
296 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
297 return Sparse<bool>::index (i, j, resize_ok); |
5164 | 298 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11571
diff
changeset
|
299 |
5164 | 300 SparseBoolMatrix |
301 SparseBoolMatrix::reshape (const dim_vector& new_dims) const | |
302 { | |
303 return Sparse<bool>::reshape (new_dims); | |
304 } | |
305 | |
306 SparseBoolMatrix | |
5275 | 307 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164 | 308 { |
309 return Sparse<bool>::permute (vec, inv); | |
310 } | |
311 | |
312 SparseBoolMatrix | |
5275 | 313 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164 | 314 { |
315 return Sparse<bool>::ipermute (vec); | |
316 } | |
317 | |
318 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) | |
319 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) | |
320 | |
321 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) | |
322 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) | |
323 | |
324 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) | |
325 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) |