| 1 |
// Author: David Alexander |
| 2 |
|
| 3 |
#include <ConsensusCore/Matrix/DenseMatrix.hpp> |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
#include <boost/tuple/tuple.hpp> |
| 7 |
#include <cassert> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
#include <ConsensusCore/LFloat.hpp> |
| 11 |
|
| 12 |
using boost::numeric::ublas::matrix; |
| 13 |
using boost::numeric::ublas::row_major; |
| 14 |
|
| 15 |
namespace ConsensusCore { |
| 16 |
|
| 17 |
// Performance insensitive routines are not inlined |
| 18 |
|
| 19 |
DenseMatrix::DenseMatrix(int rows, int cols) |
| 20 |
: boost_dense_matrix(rows, cols), usedRanges_(cols, Interval(0, 0)), columnBeingEdited_(-1) |
| 21 |
{ |
| 22 |
for (int j = 0; j < cols; j++) { |
| 23 |
CheckInvariants(j); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
DenseMatrix::~DenseMatrix() {} |
| 28 |
|
| 29 |
int DenseMatrix::UsedEntries() const |
| 30 |
{ |
| 31 |
// use column ranges |
| 32 |
int filledEntries = 0; |
| 33 |
for (int col = 0; col < Columns(); ++col) { |
| 34 |
int start, end; |
| 35 |
boost::tie(start, end) = UsedRowRange(col); |
| 36 |
filledEntries += (end - start); |
| 37 |
} |
| 38 |
return filledEntries; |
| 39 |
} |
| 40 |
|
| 41 |
int DenseMatrix::AllocatedEntries() const { return Rows() * Columns(); } |
| 42 |
|
| 43 |
void DenseMatrix::ToHostMatrix(float** mat, int* rows, int* cols) const |
| 44 |
{ |
| 45 |
// TODO(dalexander): make sure SWIG client deallocates this memory -- use |
| 46 |
// %newobject flag |
| 47 |
matrix<lfloat, row_major> rowMajorPeer(*this); |
| 48 |
*mat = new float[Rows() * Columns()]; |
| 49 |
std::copy(rowMajorPeer.data().begin(), rowMajorPeer.data().end(), *mat); |
| 50 |
*rows = Rows(); |
| 51 |
*cols = Columns(); |
| 52 |
} |
| 53 |
|
| 54 |
void DenseMatrix::CheckInvariants(int column) const |
| 55 |
{ |
| 56 |
// make sure no used entries are outside of the bands |
| 57 |
int start, end; |
| 58 |
boost::tie(start, end) = UsedRowRange(column); |
| 59 |
assert(0 <= start && start <= end && end <= Rows()); |
| 60 |
for (int i = 0; i < Rows(); i++) { |
| 61 |
if (!(start <= i && i < end)) { |
| 62 |
assert((*this)(i, column) == value_type()); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |