1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
Copyright (C) 2004-2013 David Bateman
Copyright (C) 1998-2004 Andy Adler
Copyright (C) 2010 VZLU Prague
This file is part of Octave.
Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <vector>
#include "quit.h"
#include "lo-ieee.h"
#include "lo-mappers.h"
#include "boolSparse.h"
#include "dSparse.h"
#include "oct-mem.h"
#include "oct-locbuf.h"
// SparseBoolMatrix class.
bool
SparseBoolMatrix::operator == (const SparseBoolMatrix& a) const
{
octave_idx_type nr = rows ();
octave_idx_type nc = cols ();
octave_idx_type nz = nnz ();
octave_idx_type nr_a = a.rows ();
octave_idx_type nc_a = a.cols ();
octave_idx_type nz_a = a.nnz ();
if (nr != nr_a || nc != nc_a || nz != nz_a)
return false;
for (octave_idx_type i = 0; i < nc + 1; i++)
if (cidx (i) != a.cidx (i))
return false;
for (octave_idx_type i = 0; i < nz; i++)
if (data (i) != a.data (i) || ridx (i) != a.ridx (i))
return false;
return true;
}
bool
SparseBoolMatrix::operator != (const SparseBoolMatrix& a) const
{
return !(*this == a);
}
SparseBoolMatrix&
SparseBoolMatrix::insert (const SparseBoolMatrix& a,
octave_idx_type r, octave_idx_type c)
{
Sparse<bool>::insert (a, r, c);
return *this;
}
SparseBoolMatrix&
SparseBoolMatrix::insert (const SparseBoolMatrix& a,
const Array<octave_idx_type>& indx)
{
Sparse<bool>::insert (a, indx);
return *this;
}
SparseBoolMatrix
SparseBoolMatrix::concat (const SparseBoolMatrix& rb,
const Array<octave_idx_type>& ra_idx)
{
// Don't use numel to avoid all possiblity of an overflow
if (rb.rows () > 0 && rb.cols () > 0)
insert (rb, ra_idx(0), ra_idx(1));
return *this;
}
// unary operations
SparseBoolMatrix
SparseBoolMatrix::operator ! (void) const
{
octave_idx_type nr = rows ();
octave_idx_type nc = cols ();
octave_idx_type nz1 = nnz ();
octave_idx_type nz2 = nr*nc - nz1;
SparseBoolMatrix r (nr, nc, nz2);
octave_idx_type ii = 0;
octave_idx_type jj = 0;
r.cidx (0) = 0;
for (octave_idx_type i = 0; i < nc; i++)
{
for (octave_idx_type j = 0; j < nr; j++)
{
if (jj < cidx (i+1) && ridx (jj) == j)
jj++;
else
{
r.data (ii) = true;
r.ridx (ii++) = j;
}
}
r.cidx (i+1) = ii;
}
return r;
}
// other operations
// FIXME: Do these really belong here? Maybe they should be in a base class?
SparseBoolMatrix
SparseBoolMatrix::all (int dim) const
{
SPARSE_ALL_OP (dim);
}
SparseBoolMatrix
SparseBoolMatrix::any (int dim) const
{
Sparse<bool> retval;
octave_idx_type nr = rows (), nc = cols (), nz = nnz ();
if (dim == -1)
dim = (nr == 1 && nc != 1) ? 1 : 0;
if (dim == 0)
{
// Result is a row vector.
retval = Sparse<bool> (1, nc);
retval.xcidx (0) = 0;
for (octave_idx_type i = 0; i < nc; i++)
retval.xcidx (i+1) = retval.xcidx (i) + (cidx (i+1) > cidx (i));
octave_idx_type new_nz = retval.xcidx (nc);
retval.change_capacity (new_nz);
fill_or_memset (new_nz, static_cast<octave_idx_type> (0), retval.ridx ());
fill_or_memset (new_nz, true, retval.data ());
}
else if (dim == 1)
{
// Result is a column vector.
if (nz > nr/4)
{
// We can use O(nr) memory.
Array<bool> tmp (dim_vector (nr, 1), false);
for (octave_idx_type i = 0; i < nz; i++)
tmp.xelem (ridx (i)) = true;
retval = tmp;
}
else
{
Array<octave_idx_type> tmp (dim_vector (nz, 1));
copy_or_memcpy (nz, ridx (), tmp.fortran_vec ());
retval = Sparse<bool> (Array<bool> (dim_vector (1, 1), true),
idx_vector (tmp),
idx_vector (static_cast<octave_idx_type> (0)),
nr, 1, false);
}
}
return retval;
}
SparseMatrix
SparseBoolMatrix::sum (int dim) const
{
Sparse<double> retval;
octave_idx_type nr = rows (), nc = cols (), nz = nnz ();
if (dim == -1)
dim = (nr == 1 && nc != 1) ? 1 : 0;
if (dim == 0)
{
// Result is a row vector.
retval = Sparse<double> (1, nc);
for (octave_idx_type i = 0; i < nc; i++)
retval.xcidx (i+1) = retval.xcidx (i) + (cidx (i+1) > cidx (i));
octave_idx_type new_nz = retval.xcidx (nc);
retval.change_capacity (new_nz);
fill_or_memset (new_nz, static_cast<octave_idx_type> (0), retval.ridx ());
for (octave_idx_type i = 0, k = 0; i < nc; i++)
{
octave_idx_type c = cidx (i+1) - cidx (i);
if (c > 0)
retval.xdata (k++) = c;
}
}
else if (dim == 1)
{
// Result is a column vector.
if (nz > nr)
{
// We can use O(nr) memory.
Array<double> tmp (dim_vector (nr, 1), 0);
for (octave_idx_type i = 0; i < nz; i++)
tmp.xelem (ridx (i)) += 1.0;
retval = tmp;
}
else
{
Array<octave_idx_type> tmp (dim_vector (nz, 1));
copy_or_memcpy (nz, ridx (), tmp.fortran_vec ());
retval = Sparse<double> (Array<double> (dim_vector (1, 1), 1.0),
idx_vector (tmp),
idx_vector (static_cast<octave_idx_type> (0)),
nr, 1);
}
}
return retval;
}
SparseBoolMatrix
SparseBoolMatrix::diag (octave_idx_type k) const
{
return Sparse<bool>::diag (k);
}
boolMatrix
SparseBoolMatrix::matrix_value (void) const
{
octave_idx_type nr = rows ();
octave_idx_type nc = cols ();
boolMatrix retval (nr, nc, false);
for (octave_idx_type j = 0; j < nc; j++)
for (octave_idx_type i = cidx (j); i < cidx (j+1); i++)
retval.elem (ridx (i), j) = data (i);
return retval;
}
std::ostream&
operator << (std::ostream& os, const SparseBoolMatrix& a)
{
octave_idx_type nc = a.cols ();
// add one to the printed indices to go from
// zero-based to one-based arrays
for (octave_idx_type j = 0; j < nc; j++)
{
octave_quit ();
for (octave_idx_type i = a.cidx (j); i < a.cidx (j+1); i++)
os << a.ridx (i) + 1 << " " << j + 1 << " " << a.data (i) << "\n";
}
return os;
}
std::istream&
operator >> (std::istream& is, SparseBoolMatrix& a)
{
typedef SparseBoolMatrix::element_type elt_type;
return read_sparse_matrix<elt_type> (is, a, octave_read_value<bool>);
}
SparseBoolMatrix
SparseBoolMatrix::squeeze (void) const
{
return Sparse<bool>::squeeze ();
}
SparseBoolMatrix
SparseBoolMatrix::index (const idx_vector& i, bool resize_ok) const
{
return Sparse<bool>::index (i, resize_ok);
}
SparseBoolMatrix
SparseBoolMatrix::index (const idx_vector& i, const idx_vector& j,
bool resize_ok) const
{
return Sparse<bool>::index (i, j, resize_ok);
}
SparseBoolMatrix
SparseBoolMatrix::reshape (const dim_vector& new_dims) const
{
return Sparse<bool>::reshape (new_dims);
}
SparseBoolMatrix
SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const
{
return Sparse<bool>::permute (vec, inv);
}
SparseBoolMatrix
SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const
{
return Sparse<bool>::ipermute (vec);
}
SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, )
SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false)
SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, )
SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false)
SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, )
SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false)
|