10 #include <QMapIterator>
11 #include <QListIterator>
14 #include <boost/numeric/ublas/matrix_sparse.hpp>
15 #include <boost/numeric/ublas/matrix_proxy.hpp>
16 #include <boost/numeric/ublas/io.hpp>
22 using namespace boost::numeric::ublas;
29 SparseBlockColumnMatrix::~SparseBlockColumnMatrix() {
38 void SparseBlockColumnMatrix::wipe() {
64 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
65 while ( it.hasNext() ) {
72 this->insert(it.key(),m);
105 bool SparseBlockColumnMatrix::insertMatrixBlock(
int nColumnBlock,
int nRows,
int nCols) {
107 if ( this->contains(nColumnBlock) )
120 this->insert(nColumnBlock,m);
132 int SparseBlockColumnMatrix::numberOfElements() {
135 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
136 while ( it.hasNext() ) {
142 nElements += it.value()->size1()*it.value()->size2();
154 int SparseBlockColumnMatrix::numberOfColumns() {
158 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
159 while ( it.hasNext() ) {
165 nColumns = it.value()->size2();
179 int SparseBlockColumnMatrix::numberOfRows() {
182 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
183 while ( it.hasNext() ) {
190 int nRows = it.value()->size1();
203 void SparseBlockColumnMatrix::print(std::ostream& outstream) {
205 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
209 outstream <<
"Printing SparseBlockColumnMatrix..." << std::endl;
210 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
211 while ( it.hasNext() ) {
215 outstream << it.key() << std::endl << *(it.value()) << std::endl
218 outstream <<
"NULL block pointer at row[" <<
IString(it.key())
219 <<
"]!" << std::endl;
230 void SparseBlockColumnMatrix::printClean(std::ostream& outstream) {
232 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
236 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
237 while ( it.hasNext() ) {
242 int rows = m->size1();
243 int cols = m->size2();
245 for (
int i = 0; i < rows; i++ ) {
246 for (
int j = 0; j < cols; j++ ) {
247 double d = m->at_element(i,j);
249 outstream << std::setprecision(12) << d << std::endl;
251 outstream << std::setprecision(12) << d <<
",";
256 outstream << std::endl;
263 void SparseBlockColumnMatrix::zeroBlocks() {
264 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
265 while ( it.hasNext() ) {
280 int nBlocks = sbcm.size();
281 stream << (qint32)nBlocks;
283 QMapIterator<int, LinearAlgebra::Matrix *> it(sbcm);
284 while ( it.hasNext() ) {
290 int nRows = it.value()->size1();
291 int nCols = it.value()->size2();
294 stream << it.key() << (qint32)nRows << (qint32)nCols;
296 double* data = &it.value()->data()[0];
299 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(
double));
313 qint32 nBlocks, nBlockNumber, nRows, nCols;
318 for ( i = 0; i < nBlocks; i++ ) {
320 stream >> nBlockNumber >> nRows >> nCols;
322 double data[nRows*nCols];
325 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
334 for ( r = 0; r < nRows; r++ ) {
335 for ( c = 0; c < nCols; c++ ) {
336 int nLocation = r*nRows + c;
337 (*matrix)(r,c) = data[nLocation];
353 dbg.space() <<
"New Block" << endl;
355 QMapIterator<int, LinearAlgebra::Matrix *> it(sbcm);
356 while ( it.hasNext() ) {
366 int nRows = matrix->size1();
367 int nCols = matrix->size2();
369 dbg.nospace() << qSetFieldWidth(4);
370 dbg.nospace() << qSetRealNumberPrecision(8);
372 for (
int r = 0; r < nRows; r++ ) {
373 for (
int c = 0; c < nCols; c++ ) {
374 dbg.space() << (*matrix)(r,c);
392 SparseBlockRowMatrix::~SparseBlockRowMatrix() {
401 void SparseBlockRowMatrix::wipe() {
402 qDeleteAll(values());
427 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
428 while ( it.hasNext() ) {
435 this->insert(it.key(),m);
471 bool SparseBlockRowMatrix::insertMatrixBlock(
int nRowBlock,
int nRows,
int nCols) {
472 if ( this->contains(nRowBlock) )
482 this->insert(nRowBlock,m);
494 int SparseBlockRowMatrix::numberOfElements() {
497 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
498 while ( it.hasNext() ) {
504 nElements += it.value()->size1()*it.value()->size2();
516 void SparseBlockRowMatrix::print(std::ostream& outstream) {
518 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
522 outstream <<
"Printing SparseBlockRowMatrix..." << std::endl;
523 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
524 while ( it.hasNext() ) {
528 outstream << it.key() << std::endl << *(it.value()) << std::endl
531 outstream <<
"NULL block pointer at column[" <<
IString(it.key())
532 <<
"]!" << std::endl;
542 void SparseBlockRowMatrix::printClean(std::ostream& outstream) {
544 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
548 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
549 while ( it.hasNext() ) {
554 int rows = m->size1();
555 int cols = m->size2();
557 for (
int i = 0; i < rows; i++ ) {
558 for (
int j = 0; j < cols; j++ ) {
559 double d = m->at_element(i,j);
561 outstream << std::setprecision(9) << d << std::endl;
563 outstream << std::setprecision(9) << d <<
",";
568 outstream << std::endl;
575 void SparseBlockRowMatrix::zeroBlocks() {
576 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
577 while ( it.hasNext() ) {
590 void SparseBlockRowMatrix::copyToBoost(compressed_matrix<double>& B) {
593 int ncols, nstart, nend, nrowBlock;
594 range rRow = range(0,3);
597 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
598 while ( it.hasNext() ) {
601 nrowBlock = it.key();
606 nstart = nrowBlock*ncols;
607 nend = nstart + ncols;
609 rCol = range(nstart,nend);
611 matrix_range<compressed_matrix<double> > m1 (B, rRow, rCol);
625 int SparseBlockRowMatrix::getLeadingColumnsForBlock(
int nblockColumn) {
627 if ( nblockColumn == 0 )
630 int nLeadingColumnsElements = 0;
634 while ( nCol < nblockColumn ) {
635 if ( !(*
this)[nCol] ) {
640 int ncolumns = (*this)[nCol]->size2();
642 if ( ncolumns == -1 )
645 nLeadingColumnsElements += ncolumns;
650 return nLeadingColumnsElements;
662 int nBlocks = sbrm.size();
663 stream << (qint32)nBlocks;
665 QMapIterator<int, LinearAlgebra::Matrix *> it(sbrm);
666 while ( it.hasNext() ) {
672 int nRows = it.value()->size1();
673 int nCols = it.value()->size2();
676 stream << it.key() << (qint32)nRows << (qint32)nCols;
678 double* data = &it.value()->data()[0];
681 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(
double));
695 qint32 nBlocks, nBlockNumber, nRows, nCols;
700 for ( i = 0; i < nBlocks; i++ ) {
702 stream >> nBlockNumber >> nRows >> nCols;
704 double data[nRows*nCols];
707 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
716 for ( r = 0; r < nRows; r++ ) {
717 for ( c = 0; c < nCols; c++ ) {
718 int nLocation = r*nRows + c;
719 (*matrix)(r,c) = data[nLocation];
735 dbg.space() <<
"New Block" << endl;
737 QMapIterator<int, LinearAlgebra::Matrix *> it(sbrm);
738 while ( it.hasNext() ) {
748 int nRows = matrix->size1();
749 int nCols = matrix->size2();
751 dbg.nospace() << qSetFieldWidth(4);
752 dbg.nospace() << qSetRealNumberPrecision(8);
754 for (
int r = 0; r < nRows; r++ ) {
755 for (
int c = 0; c < nCols; c++ ) {
756 dbg.space() << (*matrix)(r,c);
773 SparseBlockMatrix::~SparseBlockMatrix() {
782 void SparseBlockMatrix::wipe() {
808 for(
int i = 0; i < src.size(); i++ ) {
837 bool SparseBlockMatrix::setNumberOfColumns(
int n ) {
839 for(
int i = 0; i < n; i++ )
859 bool SparseBlockMatrix::insertMatrixBlock(
int nColumnBlock,
int nRowBlock,
int nRows,
int nCols) {
860 return (*
this)[nColumnBlock]->insertMatrixBlock(nRowBlock, nRows, nCols);
869 int SparseBlockMatrix::numberOfBlocks() {
872 for(
int i = 0; i < size(); i++ ) {
876 nBlocks += (*this)[i]->size();
888 int SparseBlockMatrix::numberOfDiagonalBlocks() {
891 for(
int i = 0; i < size(); i++ ) {
897 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
898 while ( it.hasNext() ) {
901 if( it.key() == i ) {
917 int SparseBlockMatrix::numberOfOffDiagonalBlocks() {
918 return (numberOfBlocks() - numberOfDiagonalBlocks());
927 int SparseBlockMatrix::numberOfElements() {
930 for(
int i = 0; i < size(); i++ ) {
934 nElements += (*this)[i]->numberOfElements();
951 return (*(*
this)[column])[row];
958 void SparseBlockMatrix::zeroBlocks() {
959 for (
int i = 0; i < size(); i++ )
960 (*
this)[i]->zeroBlocks();
969 void SparseBlockMatrix::print(std::ostream& outstream) {
971 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
975 outstream <<
"Printing SparseBlockMatrix..." << std::endl;
976 for(
int i = 0; i < size(); i++ ) {
980 column->
print(outstream);
982 outstream <<
"NULL column pointer at column[" <<
IString(i)
983 <<
"]!" << std::endl;
993 void SparseBlockMatrix::printClean(std::ostream& outstream) {
995 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
999 for(
int i = 0; i < size(); i++ ) {
1005 outstream <<
"NULL column pointer at column[" <<
IString(i)
1006 <<
"]!" << std::endl;
1018 int SparseBlockMatrix::getLeadingColumnsForBlock(
int nblockColumn) {
1020 if ( nblockColumn == 0 )
1023 int nLeadingColumnsElements = 0;
1027 while ( nCol < nblockColumn ) {
1028 if ( !(*
this)[nCol] )
1031 int ncolumns = (*this)[nCol]->numberOfColumns();
1033 if ( ncolumns == -1 )
1036 nLeadingColumnsElements += ncolumns;
1041 return nLeadingColumnsElements;
1052 int SparseBlockMatrix::getLeadingRowsForBlock(
int nblockRow) {
1054 if ( nblockRow == 0 )
1058 int nLeadingRows = 0;
1060 while ( i < nblockRow ) {
1066 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
1068 while ( it.hasNext() ) {
1072 nLeadingRows += it.value()->size1();
1077 return nLeadingRows;
1088 int nBlockColumns = sparseBlockMatrix.size();
1090 stream << (qint32)nBlockColumns;
1092 for (
int i =0; i < nBlockColumns; i++ )
1093 stream << *sparseBlockMatrix.at(i);
1106 qint32 nBlockColumns;
1109 stream >> nBlockColumns;
1112 for (
int i =0; i < nBlockColumns; i++ )
1113 stream >> *sparseBlockMatrix.at(i);
1125 int nBlockColumns = m.size();
1127 for (
int i =0; i < nBlockColumns; i++ )
boost::numeric::ublas::matrix< double > Matrix
Definition for an Isis::LinearAlgebra::Matrix of doubles.
void printClean(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint)
Display an Angle for a debugging statement.
bool insertMatrixBlock(int nRowBlock, int nRows, int nCols)
Inserts a "newed" LinearAlgebra::Matrix pointer of size (nRows, nCols) into the map with the block ro...
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
void print(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
Adds specific functionality to C++ strings.
bool insertMatrixBlock(int nColumnBlock, int nRows, int nCols)
Inserts a "newed" LinearAlgebra::Matrix pointer of size (nRows, nCols) into the map with the block co...
bool setNumberOfColumns(int n)
Initializes number of columns (SparseBlockColumnMatrix).