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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
/*
* Copyright (c) 2009 Samit Basu
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __SparseMatrix_hpp__
#define __SparseMatrix_hpp__
#include <QMap>
#include <QtGlobal>
#include <iostream>
#include "Types.hpp"
#include "NTuple.hpp"
#include "BasicArray.hpp"
#include "Cast.hpp"
#include <cmath>
template <typename T>
class SparseSlice : public QMap<index_t,T> {
};
template <typename T>
class SparseData : public QMap<index_t,SparseSlice<T> > {
};
template <typename T>
class ConstSparseIterator;
template <typename T>
class SparseMatrix {
SparseData<T> m_data;
NTuple m_dims;
public:
SparseMatrix(const NTuple &dims) {
m_dims = dims;
}
const SparseData<T>& constData() const {return m_data;}
SparseData<T>& data() {return m_data;}
SparseMatrix(QVector<index_t> row, QVector<index_t> col, QVector<T> val) {
if (!((row.size() == col.size()) &&
(row.size() == val.size())))
throw Exception("Invalid sizes for IJV style constructor");
index_t maxcol = 0;
index_t maxrow = 0;
for (int i=0;i<row.size();i++) {
m_data[col[i]][row[i]] = val[i];
maxcol = qMax(maxcol,col[i]);
maxrow = qMax(maxrow,row[i]);
}
m_dims = NTuple(maxrow,maxcol);
}
SparseMatrix(const BasicArray<T> &A) {
ConstBasicIterator<T> source(&A,0);
index_t col = 1;
while (source.isValid()) {
SparseSlice<T> this_col;
for (index_t row=1;row<=source.size();row++) {
if (source.get() != T(0))
this_col[row] = source.get();
source.next();
}
if (this_col.count() > 0)
m_data[col] = this_col;
++col;
source.nextSlice();
}
m_dims = A.dimensions();
}
inline const NTuple dimensions() const {
return m_dims;
}
inline index_t length() const {return m_dims.count();}
inline index_t isScalar() const {return length() == 1;}
inline bool isEmpty() const {return (length() == 0);}
inline bool isColumnVector() const {return m_dims.isColumnVector();}
inline bool isRowVector() const {return m_dims.isRowVector();}
inline bool isVector() const {return isColumnVector() || isRowVector();}
inline index_t rows() const {return m_dims.rows();}
inline index_t cols() const {return m_dims.cols();}
inline index_t columns() const {return m_dims.cols();}
inline const T operator[](const NTuple& pos) const {
if (m_dims.validate(pos)) {
if (!m_data.contains(pos[1]))
return T(0);
const SparseSlice<T>& col = m_data.value(pos[1]);
if (!col.contains(pos[0]))
return T(0);
return col.value(pos[0]);
}
throw Exception("Out of range");
}
inline T& operator[](const NTuple& pos) {
if (m_dims.validate(pos)) {
return m_data[pos[1]][pos[0]];
}
throw Exception("Out of range");
}
inline const T operator[](index_t pos) const {
NTuple tpos;
m_dims.map(pos,tpos);
return (*this)[tpos];
}
inline T& operator[](index_t pos) {
NTuple tpos;
m_dims.map(pos,tpos);
return (*this)[tpos];
}
inline const T get(index_t pos) const {
return (*this)[pos];
}
inline const T get(const NTuple& pos) const {
return (*this)[pos];
}
void erase(const NTuple& pos) {
if (!m_data.contains(pos[1])) return;
SparseSlice<T> &col = m_data[pos[1]];
if (!col.contains(pos[0])) return;
col.remove(pos[0]);
}
void set(const NTuple& pos, const T& val) {
if (dimensions() <= pos) resize(pos);
if (val == T(0)) {
erase(pos);
return;
}
(*this)[pos] = val;
}
void set(index_t pos, const T& val) {
if (dimensions().count() <= pos) resize(pos);
if (val == T(0)) {
NTuple tpos;
m_dims.map(pos,tpos);
erase(tpos);
return;
}
(*this)[pos] = val;
}
SparseMatrix<T> slice(const IndexArrayVector& index) const {
SparseMatrix<T> ret(NTuple(m_dims[0],1));
ret.m_data[1] = m_data[index[1].get((index_t)1)];
return ret;
}
void deleteColumns(const IndexArray& index) {
QSet<uint64> delete_set;
for (index_t i=1;i<=index.length();i++)
delete_set.insert(uint64(index.get(i)));
SparseData<T> copy;
index_t deleted_count = 0;
for (index_t i=1;i<=cols();i++) {
if (delete_set.contains(uint64(i)))
deleted_count++;
else {
if (m_data.contains(i))
copy[i-deleted_count] = m_data[i];
}
}
m_dims = NTuple(m_dims[0],m_dims[1] - delete_set.count());
m_data = copy;
}
void deleteRows(const IndexArray& index) {
QSet<uint64> delete_set;
for (index_t i=1;i<=index.length();++i)
delete_set.insert(uint64(index.get(i)));
for (typename SparseData<T>::iterator i=m_data.begin();i!=m_data.end();++i) {
index_t deleted_count = 0;
SparseSlice<T> copy;
index_t dp = 1;
for (typename SparseSlice<T>::iterator j=i.value().begin();j!=i.value().end();++j) {
if (!delete_set.contains(uint64(j.key()))) {
while (dp < j.key()) {
if (delete_set.contains(uint64(dp))) ++deleted_count;
++dp;
}
copy[j.key()-deleted_count] = j.value();
}
}
i.value() = copy;
}
m_dims = NTuple(m_dims[0] - delete_set.count(),m_dims[1]);
}
void del(const IndexArray& index) {
if (IsColonOp(index)) {
*this = SparseMatrix(NTuple(0,0));
return;
}
QSet<uint64> delete_set;
for (index_t i=1;i<=index.length();i++)
delete_set.insert(uint64(index.get(i)));
index_t newSize = length() - delete_set.count();
if (newSize == length()) return;
NTuple newDim;
if (isRowVector())
newDim = NTuple(1,newSize);
else
newDim = NTuple(newSize,1);
SparseMatrix<T> ret(newDim);
ConstSparseIterator<T> source(this);
index_t deleted_count = 0;
index_t dp = 1;
while (source.isValid()) {
index_t source_pos = m_dims.map(source.pos());
if (!delete_set.contains(uint64(source_pos))) {
// This element was not deleted.
while (dp < source_pos) {
if (delete_set.contains(uint64(dp))) ++deleted_count;
++dp;
}
ret.set(source_pos-deleted_count,source.value());
}
source.next();
}
*this = ret;
}
void del(const IndexArrayVector& index) {
if (IsColonOp(index[0]) && IsColonOp(index[1])) {
*this = SparseMatrix(NTuple(0,0));
return;
}
if (IsColonOp(index[0]))
deleteColumns(index[1]);
else if (IsColonOp(index[1]))
deleteRows(index[0]);
else
throw Exception("Unsupported deletion for sparse matrices.");
}
void resize(const NTuple& pos) {
m_dims = pos;
}
void resize(index_t len) {
if (len > length()) {
NTuple newDim;
if (isEmpty() || isScalar()) {
newDim = NTuple(1,len);
} else if (isVector()) {
if (rows() != 1)
newDim = NTuple(len,1);
else
newDim = NTuple(1,len);
} else {
newDim = NTuple(1,len);
}
SparseMatrix<T> ret(newDim);
ConstSparseIterator<T> source(this);
while (source.isValid()) {
index_t ndx = source.row() + (source.col()-1)*rows();
if (newDim[0] == 1)
ret.set(NTuple(1,ndx),source.value());
else
ret.set(NTuple(ndx,1),source.value());
source.next();
}
*this = ret;
}
}
void reshape(const NTuple& sze) {
if (length() != sze.count())
throw Exception("Illegal reshape");
SparseMatrix<T> ret(sze);
ConstSparseIterator<T> source(this);
while (source.isValid()) {
index_t ndx = source.row() + (source.col()-1)*rows();
NTuple pos;
sze.map(ndx,pos);
ret.set(pos,source.value());
source.next();
}
*this = ret;
}
const BasicArray<T> asDense() const {
ConstSparseIterator<T> source(this);
BasicArray<T> retvec(dimensions());
while (source.isValid()) {
retvec.set(source.pos(),source.value());
source.next();
}
return retvec;
}
bool operator==(const SparseMatrix<T> &data) const {
ConstSparseIterator<T> source(this);
ConstSparseIterator<T> dest(&data);
while (source.isValid() && dest.isValid()) {
if (!(source.pos() == dest.pos())) return false;
if (source.value() != dest.value()) return false;
source.next();
dest.next();
}
return true;
}
index_t bytes() const {
index_t total = 0;
ConstSparseIterator<T> source(this);
while (source.isValid()) {
total += TSizeOf<T>(source.value())+sizeof(index_t)*2;
source.next();
}
return total;
}
};
template <typename T>
void PrintMe(const SparseMatrix<T> &a, Interpreter* io);
template <typename T>
void PrintMe(const SparseMatrix<T> &ar, const SparseMatrix<T> &ai, Interpreter* io);
template <typename T>
inline std::ostream& operator<<(std::ostream& o, const SparseMatrix<T>& arg) {
arg.printMe(o);
return o;
}
template <typename T>
class ConstSparseIterator {
const SparseMatrix<T> *m_ptr;
typename SparseData<T>::const_iterator m_col;
typename SparseSlice<T>::const_iterator m_row;
public:
ConstSparseIterator(const SparseMatrix<T> *ptr) {
m_ptr = ptr;
m_col = m_ptr->constData().constBegin();
if (m_col != m_ptr->constData().constEnd())
m_row = m_col.value().constBegin();
}
inline index_t rows() const {
return m_ptr->rows();
}
inline void next() {
if (!isValid()) return;
++m_row;
if (m_row == m_col.value().constEnd()) {
++m_col;
if (m_col != m_ptr->constData().constEnd())
m_row = m_col.value().constBegin();
}
}
bool isValid() const {
return (m_col != m_ptr->constData().constEnd());
}
T value() const {
if (!isValid()) throw Exception("Error -- attempt to retrieve value of invalid iterator");
return m_row.value();
}
const NTuple pos() const {
if (isValid())
return NTuple(m_row.key(),m_col.key());
else
return NTuple(1,m_ptr->cols()+1);
}
index_t row() const {
if (isValid())
return m_row.key();
else
return 1;
}
index_t col() const {
if (isValid())
return m_col.key();
else
return m_ptr->cols()+1;
}
};
template <typename T>
class ConstComplexSparseIterator {
ConstSparseIterator<T> m_real;
ConstSparseIterator<T> m_imag;
NTuple m_dims;
public:
ConstComplexSparseIterator(const SparseMatrix<T> *real,
const SparseMatrix<T> *imag) :
m_real(real), m_imag(imag), m_dims(real->dimensions()) {}
inline index_t rows() const {
return m_real.rows();
}
void next() {
if (m_dims.map(m_real.pos()) < m_dims.map(m_imag.pos()))
m_real.next();
else if (m_dims.map(m_imag.pos()) < m_dims.map(m_real.pos()))
m_imag.next();
else {
m_real.next();
m_imag.next();
}
}
const NTuple pos() const {
if (m_dims.map(m_real.pos()) <= m_dims.map(m_imag.pos()))
return m_real.pos();
else
return m_imag.pos();
}
index_t row() const {
return pos()[0];
}
index_t col() const {
return pos()[1];
}
bool isValid() const {
return (m_real.isValid() || m_imag.isValid());
}
T realValue() const {
if (m_dims.map(m_real.pos()) <= m_dims.map(m_imag.pos()))
return m_real.value();
else
return 0;
}
T imagValue() const {
if (m_dims.map(m_imag.pos()) <= m_dims.map(m_real.pos()))
return m_imag.value();
else
return 0;
}
};
template <typename T>
bool IsNonNegative(const SparseMatrix<T> &x) {
ConstSparseIterator<T> i(&x);
while (i.isValid()) {
if (i.value() < 0) return false;
i.next();
}
return true;
}
template <typename T>
bool IsInteger(const SparseMatrix<T> &x) {
ConstSparseIterator<T> i(&x);
while (i.isValid()) {
if (IsInteger(i.value())) return false;
i.next();
}
return true;
}
template <typename T, typename S>
SparseMatrix<T> ConvertSparseArray(const SparseMatrix<S> &x) {
SparseMatrix<T> retvec(x.dimensions());
ConstSparseIterator<S> i(&x);
while (i.isValid()) {
retvec.set(i.pos(),CastConvert<T,S>(i.value()));
i.next();
}
return retvec;
}
// c_ij = sum_k a_ik * b_kj
// For j fixed
// For all i
// c_i = sum_k a_ik * b_k
template <typename T>
SparseMatrix<T> MatrixMultiply(const SparseMatrix<T> &A, const SparseMatrix<T> &B) {
if (A.columns() != B.rows())
throw Exception("Non conforming arrays for matrix multiply");
typename SparseData<T>::const_iterator B_iter(B.constData().constBegin());
SparseMatrix<T> C(NTuple(A.rows(),B.cols()));
while (B_iter != B.constData().constEnd()) {
SparseSlice<T> c_slice;
SparseSlice<T> b_slice(B_iter.value());
ConstSparseIterator<T> A_iter(&A);
while (A_iter.isValid()) {
c_slice[A_iter.row()] += A_iter.value() * b_slice[A_iter.col()];
A_iter.next();
}
C.data()[B_iter.key()] = c_slice;
B_iter++;
}
return C;
}
template <typename T>
SparseMatrix<T> MatrixMultiply(const SparseMatrix<T> &A, const BasicArray<T> &B) {
if (A.columns() != B.rows())
throw Exception("Non conforming arrays for matrix multiply");
SparseMatrix<T> C(NTuple(A.rows(),B.cols()));
for (index_t col = 1;col <= B.cols();col++) {
SparseSlice<T> c_slice;
ConstSparseIterator<T> A_iter(&A);
while (A_iter.isValid()) {
c_slice[A_iter.row()] += A_iter.value() * B.get(NTuple(A_iter.col(),col));
A_iter.next();
}
C.data()[col] = c_slice;
}
return C;
}
template <typename T>
SparseMatrix<T> MatrixMultiply(const BasicArray<T> &A, const SparseMatrix<T> &B) {
if (A.columns() != B.rows())
throw Exception("Non conforming arrays for matrix multiply");
SparseMatrix<T> C(NTuple(A.rows(),B.cols()));
ConstSparseIterator<T> Biter(&B);
while (Biter.isValid()) {
SparseSlice<T> c_slice;
index_t col_number = Biter.col();
while (Biter.col() == col_number) {
for (index_t i=1;i <= A.rows(); i++)
c_slice[i] += A[NTuple(i,Biter.row())] * Biter.value();
Biter.next();
}
C.data()[col_number] = c_slice;
}
return C;
}
template <typename T>
SparseMatrix<T> Apply(const SparseMatrix<T>& arg, T (*func)(T)) {
SparseMatrix<T> retval(arg.dimensions());
ConstSparseIterator<T> Aiter(&arg);
while (Aiter.isValid()) {
retval.set(Aiter.pos(),func(Aiter.value()));
Aiter.next();
}
return retval;
}
template <typename T>
SparseMatrix<T> Negate(const SparseMatrix<T>& arg) {
return Apply<T>(arg,neg);
}
template <typename T>
SparseMatrix<T> Transpose(const SparseMatrix<T> &arg) {
SparseMatrix<T> retval(NTuple(arg.cols(),arg.rows()));
ConstSparseIterator<T> Aiter(&arg);
while (Aiter.isValid()) {
retval.set(NTuple(Aiter.col(),Aiter.row()),Aiter.value());
Aiter.next();
}
return retval;
}
template <typename T>
bool IsSymmetric(const SparseMatrix<T> &arg) {
ConstSparseIterator<T> Aiter(&arg);
while (Aiter.isValid()) {
if (arg.get(NTuple(Aiter.col(),Aiter.row())) != Aiter.value())
return false;
Aiter.next();
}
return true;
}
template <typename T>
bool IsSymmetric(const SparseMatrix<T> &arg, const SparseMatrix<T> &img) {
ConstComplexSparseIterator<T> Aiter(&arg,&img);
while (Aiter.isValid()) {
if (arg.get(NTuple(Aiter.col(),Aiter.row())) != Aiter.realValue()) return false;
if (img.get(NTuple(Aiter.col(),Aiter.row())) != -Aiter.imagValue()) return false;
Aiter.next();
}
return true;
}
template <typename T>
bool AnyNotFinite(const SparseMatrix<T> &arg) {
ConstSparseIterator<T> Aiter(&arg);
while (Aiter.isValid()) {
if (!IsFinite(Aiter.value()))
return true;
Aiter.next();
}
return false;
}
template <typename T>
bool AllZeros(const SparseMatrix<T> &arg) {
ConstSparseIterator<T> Aiter(&arg);
while (Aiter.isValid()) {
if (Aiter.value() != 0) return false;
Aiter.next();
}
return true;
}
template <typename T>
SparseMatrix<T> GetDiagonal(const SparseMatrix<T>& arg, int diagonal) {
index_t outLen;
if (diagonal < 0) {
outLen = qMax(index_t(0),qMin(arg.rows()+diagonal,arg.cols()));
if (outLen == 0) return SparseMatrix<T>(NTuple(0,0));
SparseMatrix<T> retvec(NTuple(outLen,1));
for (index_t i=1;i<=outLen;i++)
retvec[i] = arg[NTuple(i-diagonal,i)];
return retvec;
} else {
outLen = qMax(index_t(0),qMin(arg.rows(),arg.cols()-diagonal));
if (outLen == 0) return SparseMatrix<T>(NTuple(0,0));
SparseMatrix<T> retvec(NTuple(outLen,1));
for (index_t i=1;i<=outLen;i++)
retvec[i] = arg[NTuple(i,i+diagonal)];
return retvec;
}
}
template <typename T>
SparseMatrix<T> DiagonalArray(const SparseMatrix<T> &arg, int diagonal) {
index_t outLen = arg.length();
index_t M = outLen + abs(diagonal);
SparseMatrix<T> retval(NTuple(M,M));
if (diagonal < 0) {
for (index_t i=1;i<=outLen;i++)
retval[NTuple(i-diagonal,i)] = arg[i];
} else {
for (index_t i=1;i<=outLen;i++)
retval[NTuple(i,i+diagonal)] = arg[i];
}
return retval;
}
#endif
|