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> 29 SparseBlockColumnMatrix::SparseBlockColumnMatrix() {
37 SparseBlockColumnMatrix::~SparseBlockColumnMatrix() {
46 void SparseBlockColumnMatrix::wipe() {
72 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
73 while ( it.hasNext() ) {
80 this->insert(it.key(),m);
115 bool SparseBlockColumnMatrix::insertMatrixBlock(
int nColumnBlock,
int nRows,
int nCols) {
117 if ( this->contains(nColumnBlock) )
130 this->insert(nColumnBlock,m);
141 void SparseBlockColumnMatrix::setStartColumn(
int nStartColumn) {
142 m_startColumn = nStartColumn;
151 int SparseBlockColumnMatrix::startColumn()
const {
152 return m_startColumn;
162 int SparseBlockColumnMatrix::numberOfElements() {
165 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
166 while ( it.hasNext() ) {
172 nElements += it.value()->size1()*it.value()->size2();
184 int SparseBlockColumnMatrix::numberOfColumns() {
188 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
189 while ( it.hasNext() ) {
195 nColumns = it.value()->size2();
209 int SparseBlockColumnMatrix::numberOfRows() {
212 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
213 while ( it.hasNext() ) {
220 int nRows = it.value()->size1();
233 void SparseBlockColumnMatrix::print(std::ostream& outstream) {
235 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
239 outstream <<
"Printing SparseBlockColumnMatrix..." << std::endl;
240 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
241 while ( it.hasNext() ) {
245 outstream << it.key() << std::endl << *(it.value()) << std::endl
248 outstream <<
"NULL block pointer at row[" <<
IString(it.key())
249 <<
"]!" << std::endl;
260 void SparseBlockColumnMatrix::printClean(std::ostream& outstream) {
262 outstream <<
"Empty SparseBlockColumnMatrix..." << std::endl;
266 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
267 while ( it.hasNext() ) {
272 int rows = m->size1();
273 int cols = m->size2();
275 for (
int i = 0; i < rows; i++ ) {
276 for (
int j = 0; j < cols; j++ ) {
277 double d = m->at_element(i,j);
279 outstream << std::setprecision(12) << d << std::endl;
281 outstream << std::setprecision(12) << d <<
",";
286 outstream << std::endl;
293 void SparseBlockColumnMatrix::zeroBlocks() {
294 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
295 while ( it.hasNext() ) {
310 int nBlocks = sbcm.size();
311 stream << (qint32)nBlocks;
313 QMapIterator<int, LinearAlgebra::Matrix *> it(sbcm);
314 while ( it.hasNext() ) {
320 int nRows = it.value()->size1();
321 int nCols = it.value()->size2();
324 stream << it.key() << (qint32)nRows << (qint32)nCols;
326 double* data = &it.value()->data()[0];
329 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(
double));
343 qint32 nBlocks, nBlockNumber, nRows, nCols;
348 for ( i = 0; i < nBlocks; i++ ) {
350 stream >> nBlockNumber >> nRows >> nCols;
352 double data[nRows*nCols];
355 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
364 for ( r = 0; r < nRows; r++ ) {
365 for ( c = 0; c < nCols; c++ ) {
366 int nLocation = r*nRows + c;
367 (*matrix)(r,c) = data[nLocation];
383 dbg.space() <<
"New Block" << endl;
385 QMapIterator<int, LinearAlgebra::Matrix *> it(sbcm);
386 while ( it.hasNext() ) {
396 int nRows = matrix->size1();
397 int nCols = matrix->size2();
399 dbg.nospace() << qSetFieldWidth(4);
400 dbg.nospace() << qSetRealNumberPrecision(8);
402 for (
int r = 0; r < nRows; r++ ) {
403 for (
int c = 0; c < nCols; c++ ) {
404 dbg.space() << (*matrix)(r,c);
422 SparseBlockRowMatrix::~SparseBlockRowMatrix() {
431 void SparseBlockRowMatrix::wipe() {
432 qDeleteAll(values());
457 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
458 while ( it.hasNext() ) {
465 this->insert(it.key(),m);
501 bool SparseBlockRowMatrix::insertMatrixBlock(
int nRowBlock,
int nRows,
int nCols) {
502 if ( this->contains(nRowBlock) )
512 this->insert(nRowBlock,m);
524 int SparseBlockRowMatrix::numberOfElements() {
527 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
528 while ( it.hasNext() ) {
534 nElements += it.value()->size1()*it.value()->size2();
546 void SparseBlockRowMatrix::print(std::ostream& outstream) {
548 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
552 outstream <<
"Printing SparseBlockRowMatrix..." << std::endl;
553 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
554 while ( it.hasNext() ) {
558 outstream << it.key() << std::endl << *(it.value()) << std::endl
561 outstream <<
"NULL block pointer at column[" <<
IString(it.key())
562 <<
"]!" << std::endl;
572 void SparseBlockRowMatrix::printClean(std::ostream& outstream) {
574 outstream <<
"Empty SparseBlockRowMatrix..." << std::endl;
578 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
579 while ( it.hasNext() ) {
584 int rows = m->size1();
585 int cols = m->size2();
587 for (
int i = 0; i < rows; i++ ) {
588 for (
int j = 0; j < cols; j++ ) {
589 double d = m->at_element(i,j);
591 outstream << std::setprecision(9) << d << std::endl;
593 outstream << std::setprecision(9) << d <<
",";
598 outstream << std::endl;
605 void SparseBlockRowMatrix::zeroBlocks() {
606 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
607 while ( it.hasNext() ) {
620 void SparseBlockRowMatrix::copyToBoost(compressed_matrix<double>& B) {
623 int ncols, nstart, nend, nrowBlock;
624 range rRow = range(0,3);
627 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
628 while ( it.hasNext() ) {
631 nrowBlock = it.key();
636 nstart = nrowBlock*ncols;
637 nend = nstart + ncols;
639 rCol = range(nstart,nend);
641 matrix_range<compressed_matrix<double> > m1 (B, rRow, rCol);
655 int SparseBlockRowMatrix::getLeadingColumnsForBlock(
int nblockColumn) {
657 if ( nblockColumn == 0 )
660 int nLeadingColumnsElements = 0;
664 while ( nCol < nblockColumn ) {
665 if ( !(*
this)[nCol] ) {
670 int ncolumns = (*this)[nCol]->size2();
672 if ( ncolumns == -1 )
675 nLeadingColumnsElements += ncolumns;
680 return nLeadingColumnsElements;
692 int nBlocks = sbrm.size();
693 stream << (qint32)nBlocks;
695 QMapIterator<int, LinearAlgebra::Matrix *> it(sbrm);
696 while ( it.hasNext() ) {
702 int nRows = it.value()->size1();
703 int nCols = it.value()->size2();
706 stream << it.key() << (qint32)nRows << (qint32)nCols;
708 double* data = &it.value()->data()[0];
711 stream.writeRawData((
const char*)data, nRows*nCols*
sizeof(
double));
725 qint32 nBlocks, nBlockNumber, nRows, nCols;
730 for ( i = 0; i < nBlocks; i++ ) {
732 stream >> nBlockNumber >> nRows >> nCols;
734 double data[nRows*nCols];
737 stream.readRawData((
char*)data, nRows*nCols*
sizeof(
double));
746 for ( r = 0; r < nRows; r++ ) {
747 for ( c = 0; c < nCols; c++ ) {
748 int nLocation = r*nRows + c;
749 (*matrix)(r,c) = data[nLocation];
765 dbg.space() <<
"New Block" << endl;
767 QMapIterator<int, LinearAlgebra::Matrix *> it(sbrm);
768 while ( it.hasNext() ) {
778 int nRows = matrix->size1();
779 int nCols = matrix->size2();
781 dbg.nospace() << qSetFieldWidth(4);
782 dbg.nospace() << qSetRealNumberPrecision(8);
784 for (
int r = 0; r < nRows; r++ ) {
785 for (
int c = 0; c < nCols; c++ ) {
786 dbg.space() << (*matrix)(r,c);
803 SparseBlockMatrix::~SparseBlockMatrix() {
812 void SparseBlockMatrix::wipe() {
838 for(
int i = 0; i < src.size(); i++ ) {
867 bool SparseBlockMatrix::setNumberOfColumns(
int n ) {
869 for(
int i = 0; i < n; i++ )
889 bool SparseBlockMatrix::insertMatrixBlock(
int nColumnBlock,
int nRowBlock,
int nRows,
int nCols) {
890 return (*
this)[nColumnBlock]->insertMatrixBlock(nRowBlock, nRows, nCols);
899 int SparseBlockMatrix::numberOfBlocks() {
902 for(
int i = 0; i < size(); i++ ) {
906 nBlocks += (*this)[i]->size();
918 int SparseBlockMatrix::numberOfDiagonalBlocks() {
921 for(
int i = 0; i < size(); i++ ) {
927 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
928 while ( it.hasNext() ) {
931 if( it.key() == i ) {
947 int SparseBlockMatrix::numberOfOffDiagonalBlocks() {
948 return (numberOfBlocks() - numberOfDiagonalBlocks());
957 int SparseBlockMatrix::numberOfElements() {
960 for(
int i = 0; i < size(); i++ ) {
964 nElements += (*this)[i]->numberOfElements();
981 return (*(*
this)[column])[row];
988 void SparseBlockMatrix::zeroBlocks() {
989 for (
int i = 0; i < size(); i++ )
990 (*
this)[i]->zeroBlocks();
999 void SparseBlockMatrix::print(std::ostream& outstream) {
1000 if ( size() == 0 ) {
1001 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
1005 outstream <<
"Printing SparseBlockMatrix..." << std::endl;
1006 for(
int i = 0; i < size(); i++ ) {
1010 column->
print(outstream);
1012 outstream <<
"NULL column pointer at column[" <<
IString(i)
1013 <<
"]!" << std::endl;
1023 void SparseBlockMatrix::printClean(std::ostream& outstream) {
1024 if ( size() == 0 ) {
1025 outstream <<
"Empty SparseBlockMatrix..." << std::endl;
1029 for(
int i = 0; i < size(); i++ ) {
1035 outstream <<
"NULL column pointer at column[" <<
IString(i)
1036 <<
"]!" << std::endl;
1048 int SparseBlockMatrix::getLeadingColumnsForBlock(
int nblockColumn) {
1050 if ( nblockColumn == 0 )
1053 int nLeadingColumnsElements = 0;
1057 while ( nCol < nblockColumn ) {
1058 if ( !(*
this)[nCol] )
1061 int ncolumns = (*this)[nCol]->numberOfColumns();
1063 if ( ncolumns == -1 )
1066 nLeadingColumnsElements += ncolumns;
1071 return nLeadingColumnsElements;
1082 int SparseBlockMatrix::getLeadingRowsForBlock(
int nblockRow) {
1084 if ( nblockRow == 0 )
1088 int nLeadingRows = 0;
1090 while ( i < nblockRow ) {
1096 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
1098 while ( it.hasNext() ) {
1102 nLeadingRows += it.value()->size1();
1107 return nLeadingRows;
1118 int nBlockColumns = sparseBlockMatrix.size();
1120 stream << (qint32)nBlockColumns;
1122 for (
int i =0; i < nBlockColumns; i++ )
1123 stream << *sparseBlockMatrix.at(i);
1136 qint32 nBlockColumns;
1139 stream >> nBlockColumns;
1142 for (
int i =0; i < nBlockColumns; i++ )
1143 stream >> *sparseBlockMatrix.at(i);
1155 int nBlockColumns = m.size();
1157 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.
Namespace for ISIS/Bullet specific routines.
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).
int startColumn() const
Sets starting column for block in full matrix.