7#include "SparseBlockMatrix.h"
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"
28using namespace boost::numeric::ublas;
78 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
79 while ( it.hasNext() ) {
86 this->insert(it.key(),m);
123 if ( this->contains(nColumnBlock) )
136 this->insert(nColumnBlock,m);
171 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
172 while ( it.hasNext() ) {
178 nElements += it.value()->size1()*it.value()->size2();
194 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
195 while ( it.hasNext() ) {
201 nColumns = it.value()->size2();
218 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
219 while ( it.hasNext() ) {
226 int nRows = it.value()->size1();
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;
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;
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));
364 sbcm.insertMatrixBlock(nBlockNumber, nRows, nCols);
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" << Qt::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);
412 dbg.space() << Qt::endl;
415 dbg.space() << Qt::endl;
438 qDeleteAll(values());
463 QMapIterator<int, LinearAlgebra::Matrix *> it(src);
464 while ( it.hasNext() ) {
471 this->insert(it.key(),m);
508 if ( this->contains(nRowBlock) )
518 this->insert(nRowBlock,m);
533 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
534 while ( it.hasNext() ) {
540 nElements += it.value()->size1()*it.value()->size2();
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;
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;
612 QMapIterator<int, LinearAlgebra::Matrix *> it(*
this);
613 while ( it.hasNext() ) {
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);
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));
746 sbrm.insertMatrixBlock(nBlockNumber, nRows, nCols);
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" << Qt::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);
794 dbg.space() << Qt::endl;
796 dbg.space() << Qt::endl;
844 for(
int i = 0; i < src.size(); i++ ) {
875 for(
int i = 0; i < n; i++ )
896 return (*
this)[nColumnBlock]->insertMatrixBlock(nRowBlock, nRows, nCols);
908 for(
int i = 0; i < size(); i++ ) {
912 nBlocks += (*this)[i]->size();
927 for(
int i = 0; i < size(); i++ ) {
933 QMapIterator<int, LinearAlgebra::Matrix *> it(*column);
934 while ( it.hasNext() ) {
937 if( it.key() == i ) {
966 for(
int i = 0; i < size(); i++ ) {
970 nElements += (*this)[i]->numberOfElements();
987 return (*(*
this)[column])[row];
995 for (
int i = 0; i < size(); i++ )
996 (*
this)[i]->zeroBlocks();
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;
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;
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;
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;
1146 sparseBlockMatrix.setNumberOfColumns(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++ )
Adds specific functionality to C++ strings.
boost::numeric::ublas::matrix< double > Matrix
Definition for an Isis::LinearAlgebra::Matrix of doubles.
int numberOfElements()
Returns total number of matrix elements in map (NOTE: NOT the number of matrix blocks).
void wipe()
Deletes all pointer elements and removes them from the map.
void printClean(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
int startColumn() const
Sets starting column for block in full matrix.
~SparseBlockColumnMatrix()
Destructor.
void print(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
SparseBlockColumnMatrix & operator=(const SparseBlockColumnMatrix &src)
"Equals" operator.
void zeroBlocks()
Sets all elements of all matrix blocks to zero.
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...
int numberOfColumns()
Returns total number of columns in map (NOTE: NOT the number of matrix blocks).
int numberOfRows()
Returns total number of rows in map (this needs to be clarified and maybe rewritten).
int m_startColumn
starting column for this Block Column in full matrix e.g.
SparseBlockColumnMatrix()
Default constructor.
void copy(const SparseBlockColumnMatrix &src)
Copy method.
void setStartColumn(int nStartColumn)
Sets starting column for block in full matrix.
void copy(const SparseBlockMatrix &src)
Copy method.
void zeroBlocks()
Sets all elements of all matrix blocks to zero.
int numberOfBlocks()
Returns total number of blocks in matrix.
int numberOfDiagonalBlocks()
Returns number of diagonal matrix blocks (equivalent to size - there is one per column).
int getLeadingRowsForBlock(int nblockRow)
Sums and returns the number of rows in each matrix block prior to nblockRow.
void wipe()
Deletes all pointer elements and removes them from the list.
bool insertMatrixBlock(int nColumnBlock, int nRowBlock, int nRows, int nCols)
Inserts a "newed" boost LinearAlgebra::Matrix pointer of size (nRows, nCols) into the matrix at nColu...
int numberOfOffDiagonalBlocks()
Returns number of off-diagonal matrix blocks.
void print(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
SparseBlockMatrix & operator=(const SparseBlockMatrix &src)
"Equals" operator.
~SparseBlockMatrix()
Destructor.
void printClean(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
LinearAlgebra::Matrix * getBlock(int column, int row)
Returns pointer to boost matrix at position (column, row).
bool setNumberOfColumns(int n)
Initializes number of columns (SparseBlockColumnMatrix).
int getLeadingColumnsForBlock(int nblockColumn)
Sums and returns the number of columns in each matrix block prior to nblockColumn.
int numberOfElements()
Returns number of matrix elements in matrix.
int numberOfElements()
Returns total number of matrix elements in map (NOTE: NOT the number of matrix blocks).
void print(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
void copy(const SparseBlockRowMatrix &src)
Copy method.
void printClean(std::ostream &outstream)
Prints matrix blocks to std output stream out for debugging.
SparseBlockRowMatrix & operator=(const SparseBlockRowMatrix &src)
"Equals" operator.
~SparseBlockRowMatrix()
Destructor.
void copyToBoost(boost::numeric::ublas::compressed_matrix< double > &B)
Copies a SparseBlockRowMatrix to a Boost compressed_matrix.
void zeroBlocks()
Sets all elements of all matrix blocks to zero.
void wipe()
Deletes all pointer elements and removes them from the map.
int getLeadingColumnsForBlock(int nblockColumn)
Sums and returns the number of columns in each matrix block prior to nblockColumn.
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...
This is free and unencumbered software released into the public domain.
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.