3 #include <boost/numeric/ublas/matrix_sparse.hpp>
4 #include <boost/numeric/ublas/matrix_proxy.hpp>
5 #include <boost/numeric/ublas/io.hpp>
10 #include <QMapIterator>
11 #include <QListIterator>
15 using namespace boost::numeric::ublas;
22 SparseBlockColumnMatrix::~SparseBlockColumnMatrix() {
32 void SparseBlockColumnMatrix::wipe() {
54 QMapIterator<int, matrix<double>*> it(src);
55 while (it.hasNext()) {
59 matrix<double>* m =
new matrix<double>(*(it.value()));
62 this->insert(it.key(),m);
91 bool SparseBlockColumnMatrix::InsertMatrixBlock(
int nColumnBlock,
int nRows,
95 if ( this->contains(nColumnBlock) )
99 matrix<double>* m =
new matrix<double>(nRows,nCols);
108 this->insert(nColumnBlock,m);
118 int SparseBlockColumnMatrix::numberOfElements() {
121 QMapIterator<int, matrix<double>*> it(*
this);
122 while (it.hasNext()) {
128 nElements += it.value()->size1()*it.value()->size2();
139 int SparseBlockColumnMatrix::numberOfColumns() {
143 QMapIterator<int, matrix<double>*> it(*
this);
144 while (it.hasNext()) {
150 nColumns = it.value()->size2();
162 int SparseBlockColumnMatrix::numberOfRows() {
165 QMapIterator<int, matrix<double>*> it(*
this);
166 while (it.hasNext()) {
173 int nRows = it.value()->size1();
182 void SparseBlockColumnMatrix::print(std::ostream& outstream) {
184 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
188 outstream <<
"Printing SparseBlockColumnMatrix..." << std::endl;
189 QMapIterator<int, matrix<double>*> it(*
this);
190 while (it.hasNext()) {
194 outstream << it.key() << std::endl << *(it.value()) << std::endl
197 outstream <<
"NULL block pointer at row[" <<
IString(it.key())
198 <<
"]!" << std::endl;
206 void SparseBlockColumnMatrix::zeroBlocks() {
207 QMapIterator<int, matrix<double>*> it(*
this);
208 while ( it.hasNext() ) {
220 int nBlocks = sbcm.size();
223 QMapIterator<int, matrix<double>*> it(sbcm);
224 while ( it.hasNext() ) {
230 int nRows = it.value()->size1();
231 int nCols = it.value()->size2();
234 stream << it.key() << nRows << nCols;
236 double* data = &it.value()->data()[0];
239 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(
double));
250 int nBlocks, nBlockNumber, nRows, nCols;
255 for ( i = 0; i < nBlocks; i++) {
257 stream >> nBlockNumber >> nRows >> nCols;
259 double data[nRows*nCols];
262 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
268 matrix<double>* matrix = sbcm[nBlockNumber];
271 for (r = 0; r < nRows; r++ ) {
272 for (c = 0; c < nCols; c++ ) {
273 int nLocation = r*nRows + c;
274 (*matrix)(r,c) = data[nLocation];
286 dbg.space() <<
"New Block" << endl;
288 QMapIterator<int, matrix<double>*> it(sbcm);
289 while ( it.hasNext() ) {
296 matrix<double>* matrix = it.value();
299 int nRows = matrix->size1();
300 int nCols = matrix->size2();
302 dbg.nospace() << qSetFieldWidth(4);
303 dbg.nospace() << qSetRealNumberPrecision(8);
305 for (
int r = 0; r < nRows; r++ ) {
306 for (
int c = 0; c < nCols; c++ ) {
307 dbg.space() << (*matrix)(r,c);
324 SparseBlockRowMatrix::~SparseBlockRowMatrix() {
334 void SparseBlockRowMatrix::wipe() {
335 qDeleteAll(values());
356 QMapIterator<int, matrix<double>*> it(src);
357 while (it.hasNext()) {
361 matrix<double>* m =
new matrix<double>(*(it.value()));
364 this->insert(it.key(),m);
393 bool SparseBlockRowMatrix::InsertMatrixBlock(
int nRowBlock,
int nRows,
395 if ( this->contains(nRowBlock) )
398 matrix<double>* m =
new matrix<double>(nRows,nCols);
405 this->insert(nRowBlock,m);
415 int SparseBlockRowMatrix::numberOfElements() {
418 QMapIterator<int, matrix<double>*> it(*
this);
419 while ( it.hasNext() ) {
425 nElements += it.value()->size1()*it.value()->size2();
435 void SparseBlockRowMatrix::print(std::ostream& outstream) {
437 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
441 outstream <<
"Printing SparseBlockRowMatrix..." << std::endl;
442 QMapIterator<int, matrix<double>*> it(*
this);
443 while ( it.hasNext() ) {
447 outstream << it.key() << std::endl << *(it.value()) << std::endl
450 outstream <<
"NULL block pointer at column[" <<
IString(it.key())
451 <<
"]!" << std::endl;
459 void SparseBlockRowMatrix::zeroBlocks() {
460 QMapIterator<int, matrix<double>*> it(*
this);
461 while ( it.hasNext() ) {
472 void SparseBlockRowMatrix::copyToBoost(compressed_matrix<double>& B) {
475 int ncols, nstart, nend, nrowBlock;
476 range rRow = range(0,3);
479 QMapIterator<int, matrix<double>*> it(*
this);
480 while ( it.hasNext() ) {
483 nrowBlock = it.key();
484 matrix<double>* m = it.value();
488 nstart = nrowBlock*ncols;
489 nend = nstart + ncols;
491 rCol = range(nstart,nend);
493 matrix_range<compressed_matrix<double> > m1 (B, rRow, rCol);
504 int nBlocks = sbrm.size();
507 QMapIterator<int, matrix<double>*> it(sbrm);
508 while ( it.hasNext() ) {
514 int nRows = it.value()->size1();
515 int nCols = it.value()->size2();
518 stream << it.key() << nRows << nCols;
520 double* data = &it.value()->data()[0];
523 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(
double));
534 int nBlocks, nBlockNumber, nRows, nCols;
539 for ( i = 0; i < nBlocks; i++) {
541 stream >> nBlockNumber >> nRows >> nCols;
543 double data[nRows*nCols];
546 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
552 matrix<double>* matrix = sbrm[nBlockNumber];
555 for (r = 0; r < nRows; r++ ) {
556 for (c = 0; c < nCols; c++ ) {
557 int nLocation = r*nRows + c;
558 (*matrix)(r,c) = data[nLocation];
570 dbg.space() <<
"New Block" << endl;
572 QMapIterator<int, matrix<double>*> it(sbrm);
573 while ( it.hasNext() ) {
580 matrix<double>* matrix = it.value();
583 int nRows = matrix->size1();
584 int nCols = matrix->size2();
586 dbg.nospace() << qSetFieldWidth(4);
587 dbg.nospace() << qSetRealNumberPrecision(8);
589 for (
int r = 0; r < nRows; r++ ) {
590 for (
int c = 0; c < nCols; c++ ) {
591 dbg.space() << (*matrix)(r,c);
608 SparseBlockMatrix::~SparseBlockMatrix() {
618 void SparseBlockMatrix::wipe() {
639 for(
int i = 0; i < src.size(); i++ ) {
663 bool SparseBlockMatrix::setNumberOfColumns(
int n ) {
665 for(
int i = 0; i < n; i++ )
683 bool SparseBlockMatrix::InsertMatrixBlock(
int nColumnBlock,
int nRowBlock,
684 int nRows,
int nCols) {
685 return (*
this)[nColumnBlock]->InsertMatrixBlock(nRowBlock, nRows, nCols);
692 int SparseBlockMatrix::numberOfBlocks() {
695 for(
int i = 0; i < size(); i++ ) {
699 nBlocks += (*this)[i]->size();
710 int SparseBlockMatrix::numberOfDiagonalBlocks() {
713 for(
int i = 0; i < size(); i++ ) {
719 QMapIterator<int, matrix<double>*> it(*column);
720 while (it.hasNext()) {
723 if( it.key() == i ) {
737 int SparseBlockMatrix::numberOfOffDiagonalBlocks() {
738 return (numberOfBlocks() - numberOfDiagonalBlocks());
745 int SparseBlockMatrix::numberOfElements() {
748 for(
int i = 0; i < size(); i++ ) {
752 nElements += (*this)[i]->numberOfElements();
765 matrix<double>* SparseBlockMatrix::getBlock(
int column,
int row) {
766 return (*(*
this)[column])[row];
773 void SparseBlockMatrix::zeroBlocks() {
774 for (
int i = 0; i < size(); i++ )
775 (*
this)[i]->zeroBlocks();
782 void SparseBlockMatrix::print(std::ostream& outstream) {
784 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
788 outstream <<
"Printing SparseBlockMatrix..." << std::endl;
789 for(
int i = 0; i < size(); i++ ) {
793 column->
print(outstream);
795 outstream <<
"NULL column pointer at column[" <<
IString(i)
796 <<
"]!" << std::endl;
805 int nBlockColumns = sparseBlockMatrix.size();
807 stream << nBlockColumns;
809 for (
int i =0; i < nBlockColumns; i++)
810 stream << *sparseBlockMatrix.at(i);
823 stream >> nBlockColumns;
826 for (
int i =0; i < nBlockColumns; i++)
827 stream >> *sparseBlockMatrix.at(i);
836 int nBlockColumns = m.size();
838 for (
int i =0; i < nBlockColumns; i++)