7 #include "SparseBlockMatrix.h"
14 #include <QDataStream>
16 #include <QMapIterator>
17 #include <QListIterator>
20 #include <boost/numeric/ublas/matrix_sparse.hpp>
21 #include <boost/numeric/ublas/matrix_proxy.hpp>
22 #include <boost/numeric/ublas/io.hpp>
26 #include "LinearAlgebra.h"
28 using namespace boost::numeric::ublas;
35 SparseBlockColumnMatrix::SparseBlockColumnMatrix() {
43 SparseBlockColumnMatrix::~SparseBlockColumnMatrix() {
52 void SparseBlockColumnMatrix::wipe() {
78 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
79 while ( it.hasNext() ) {
86 this->insert(it.key(),m);
121 bool SparseBlockColumnMatrix::insertMatrixBlock(
int nColumnBlock,
int nRows,
int nCols) {
123 if ( this->contains(nColumnBlock) )
136 this->insert(nColumnBlock,m);
147 void SparseBlockColumnMatrix::setStartColumn(
int nStartColumn) {
148 m_startColumn = nStartColumn;
157 int SparseBlockColumnMatrix::startColumn()
const {
158 return m_startColumn;
168 int SparseBlockColumnMatrix::numberOfElements() {
171 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
172 while ( it.hasNext() ) {
178 nElements += it.value()->size1()*it.value()->size2();
190 int SparseBlockColumnMatrix::numberOfColumns() {
194 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
195 while ( it.hasNext() ) {
201 nColumns = it.value()->size2();
215 int SparseBlockColumnMatrix::numberOfRows() {
218 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
219 while ( it.hasNext() ) {
226 int nRows = it.value()->size1();
239 void SparseBlockColumnMatrix::print(std::ostream& outstream) {
241 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
245 outstream <<
"Printing SparseBlockColumnMatrix..." << std::endl;
246 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
247 while ( it.hasNext() ) {
251 outstream << it.key() << std::endl << *(it.value()) << std::endl
254 outstream <<
"NULL block pointer at row[" <<
IString(it.key())
255 <<
"]!" << std::endl;
266 void SparseBlockColumnMatrix::printClean(std::ostream& outstream) {
268 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
272 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
273 while ( it.hasNext() ) {
278 int rows = m->size1();
279 int cols = m->size2();
281 for (
int i = 0; i < rows; i++ ) {
282 for (
int j = 0; j < cols; j++ ) {
283 double d = m->at_element(i,j);
285 outstream << std::setprecision(12) << d << std::endl;
287 outstream << std::setprecision(12) << d <<
",";
292 outstream << std::endl;
299 void SparseBlockColumnMatrix::zeroBlocks() {
300 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
301 while ( it.hasNext() ) {
316 int nBlocks = sbcm.size();
317 stream << (qint32)nBlocks;
319 QMapIterator<int, LinearAlgebra::Matrix *> it(sbcm);
320 while ( it.hasNext() ) {
326 int nRows = it.value()->size1();
327 int nCols = it.value()->size2();
330 stream << it.key() << (qint32)nRows << (qint32)nCols;
332 double* data = &it.value()->data()[0];
335 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(double));
349 qint32 nBlocks, nBlockNumber, nRows, nCols;
354 for ( i = 0; i < nBlocks; i++ ) {
356 stream >> nBlockNumber >> nRows >> nCols;
358 double data[nRows*nCols];
361 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
370 for ( r = 0; r < nRows; r++ ) {
371 for ( c = 0; c < nCols; c++ ) {
372 int nLocation = r*nRows + c;
373 (*matrix)(r,c) = data[nLocation];
389 dbg.space() <<
"New Block" << endl;
391 QMapIterator<int, LinearAlgebra::Matrix *> it(sbcm);
392 while ( it.hasNext() ) {
402 int nRows = matrix->size1();
403 int nCols = matrix->size2();
405 dbg.nospace() << qSetFieldWidth(4);
406 dbg.nospace() << qSetRealNumberPrecision(8);
408 for (
int r = 0; r < nRows; r++ ) {
409 for (
int c = 0; c < nCols; c++ ) {
410 dbg.space() << (*matrix)(r,c);
428 SparseBlockRowMatrix::~SparseBlockRowMatrix() {
437 void SparseBlockRowMatrix::wipe() {
438 qDeleteAll(values());
463 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
464 while ( it.hasNext() ) {
471 this->insert(it.key(),m);
507 bool SparseBlockRowMatrix::insertMatrixBlock(
int nRowBlock,
int nRows,
int nCols) {
508 if ( this->contains(nRowBlock) )
518 this->insert(nRowBlock,m);
530 int SparseBlockRowMatrix::numberOfElements() {
533 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
534 while ( it.hasNext() ) {
540 nElements += it.value()->size1()*it.value()->size2();
552 void SparseBlockRowMatrix::print(std::ostream& outstream) {
554 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
558 outstream <<
"Printing SparseBlockRowMatrix..." << std::endl;
559 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
560 while ( it.hasNext() ) {
564 outstream << it.key() << std::endl << *(it.value()) << std::endl
567 outstream <<
"NULL block pointer at column[" <<
IString(it.key())
568 <<
"]!" << std::endl;
578 void SparseBlockRowMatrix::printClean(std::ostream& outstream) {
580 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
584 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
585 while ( it.hasNext() ) {
590 int rows = m->size1();
591 int cols = m->size2();
593 for (
int i = 0; i < rows; i++ ) {
594 for (
int j = 0; j < cols; j++ ) {
595 double d = m->at_element(i,j);
597 outstream << std::setprecision(9) << d << std::endl;
599 outstream << std::setprecision(9) << d <<
",";
604 outstream << std::endl;
611 void SparseBlockRowMatrix::zeroBlocks() {
612 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
613 while ( it.hasNext() ) {
626 void SparseBlockRowMatrix::copyToBoost(compressed_matrix<double>& B) {
629 int ncols, nstart, nend, nrowBlock;
630 range rRow = range(0,3);
633 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
634 while ( it.hasNext() ) {
637 nrowBlock = it.key();
642 nstart = nrowBlock*ncols;
643 nend = nstart + ncols;
645 rCol = range(nstart,nend);
647 matrix_range<compressed_matrix<double> > m1 (B, rRow, rCol);
661 int SparseBlockRowMatrix::getLeadingColumnsForBlock(
int nblockColumn) {
663 if ( nblockColumn == 0 )
666 int nLeadingColumnsElements = 0;
670 while ( nCol < nblockColumn ) {
671 if ( !(*
this)[nCol] ) {
676 int ncolumns = (*this)[nCol]->size2();
678 if ( ncolumns == -1 )
681 nLeadingColumnsElements += ncolumns;
686 return nLeadingColumnsElements;
698 int nBlocks = sbrm.size();
699 stream << (qint32)nBlocks;
701 QMapIterator<int, LinearAlgebra::Matrix *> it(sbrm);
702 while ( it.hasNext() ) {
708 int nRows = it.value()->size1();
709 int nCols = it.value()->size2();
712 stream << it.key() << (qint32)nRows << (qint32)nCols;
714 double* data = &it.value()->data()[0];
717 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(double));
731 qint32 nBlocks, nBlockNumber, nRows, nCols;
736 for ( i = 0; i < nBlocks; i++ ) {
738 stream >> nBlockNumber >> nRows >> nCols;
740 double data[nRows*nCols];
743 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
752 for ( r = 0; r < nRows; r++ ) {
753 for ( c = 0; c < nCols; c++ ) {
754 int nLocation = r*nRows + c;
755 (*matrix)(r,c) = data[nLocation];
771 dbg.space() <<
"New Block" << endl;
773 QMapIterator<int, LinearAlgebra::Matrix *> it(sbrm);
774 while ( it.hasNext() ) {
784 int nRows = matrix->size1();
785 int nCols = matrix->size2();
787 dbg.nospace() << qSetFieldWidth(4);
788 dbg.nospace() << qSetRealNumberPrecision(8);
790 for (
int r = 0; r < nRows; r++ ) {
791 for (
int c = 0; c < nCols; c++ ) {
792 dbg.space() << (*matrix)(r,c);
809 SparseBlockMatrix::~SparseBlockMatrix() {
818 void SparseBlockMatrix::wipe() {
844 for(
int i = 0; i < src.size(); i++ ) {
873 bool SparseBlockMatrix::setNumberOfColumns(
int n ) {
875 for(
int i = 0; i < n; i++ )
895 bool SparseBlockMatrix::insertMatrixBlock(
int nColumnBlock,
int nRowBlock,
int nRows,
int nCols) {
896 return (*
this)[nColumnBlock]->insertMatrixBlock(nRowBlock, nRows, nCols);
905 int SparseBlockMatrix::numberOfBlocks() {
908 for(
int i = 0; i < size(); i++ ) {
912 nBlocks += (*this)[i]->size();
924 int SparseBlockMatrix::numberOfDiagonalBlocks() {
927 for(
int i = 0; i < size(); i++ ) {
933 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
934 while ( it.hasNext() ) {
937 if( it.key() == i ) {
953 int SparseBlockMatrix::numberOfOffDiagonalBlocks() {
954 return (numberOfBlocks() - numberOfDiagonalBlocks());
963 int SparseBlockMatrix::numberOfElements() {
966 for(
int i = 0; i < size(); i++ ) {
970 nElements += (*this)[i]->numberOfElements();
987 return (*(*
this)[column])[row];
994 void SparseBlockMatrix::zeroBlocks() {
995 for (
int i = 0; i < size(); i++ )
996 (*
this)[i]->zeroBlocks();
1005 void SparseBlockMatrix::print(std::ostream& outstream) {
1006 if ( size() == 0 ) {
1007 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
1011 outstream <<
"Printing SparseBlockMatrix..." << std::endl;
1012 for(
int i = 0; i < size(); i++ ) {
1016 column->
print(outstream);
1018 outstream <<
"NULL column pointer at column[" <<
IString(i)
1019 <<
"]!" << std::endl;
1029 void SparseBlockMatrix::printClean(std::ostream& outstream) {
1030 if ( size() == 0 ) {
1031 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
1035 for(
int i = 0; i < size(); i++ ) {
1041 outstream <<
"NULL column pointer at column[" <<
IString(i)
1042 <<
"]!" << std::endl;
1054 int SparseBlockMatrix::getLeadingColumnsForBlock(
int nblockColumn) {
1056 if ( nblockColumn == 0 )
1059 int nLeadingColumnsElements = 0;
1063 while ( nCol < nblockColumn ) {
1064 if ( !(*
this)[nCol] )
1067 int ncolumns = (*this)[nCol]->numberOfColumns();
1069 if ( ncolumns == -1 )
1072 nLeadingColumnsElements += ncolumns;
1077 return nLeadingColumnsElements;
1088 int SparseBlockMatrix::getLeadingRowsForBlock(
int nblockRow) {
1090 if ( nblockRow == 0 )
1094 int nLeadingRows = 0;
1096 while ( i < nblockRow ) {
1102 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
1104 while ( it.hasNext() ) {
1108 nLeadingRows += it.value()->size1();
1113 return nLeadingRows;
1124 int nBlockColumns = sparseBlockMatrix.size();
1126 stream << (qint32)nBlockColumns;
1128 for (
int i =0; i < nBlockColumns; i++ )
1129 stream << *sparseBlockMatrix.at(i);
1142 qint32 nBlockColumns;
1145 stream >> nBlockColumns;
1148 for (
int i =0; i < nBlockColumns; i++ )
1149 stream >> *sparseBlockMatrix.at(i);
1161 int nBlockColumns = m.size();
1163 for (
int i =0; i < nBlockColumns; i++ )