00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __RD_BOUNDS_MATRIX_H__
00011 #define __RD_BOUNDS_MATRIX_H__
00012
00013 #include <RDGeneral/Invariant.h>
00014 #include <boost/smart_ptr.hpp>
00015 #include <iostream>
00016 #include <iomanip>
00017 #include <Numerics/SquareMatrix.h>
00018
00019 namespace DistGeom {
00020
00021
00022
00023
00024
00025
00026 class BoundsMatrix : public RDNumeric::SquareMatrix<double> {
00027 public:
00028 typedef boost::shared_array<double> DATA_SPTR;
00029
00030 explicit BoundsMatrix(unsigned int N) : RDNumeric::SquareMatrix<double>(N,0.0) {};
00031 BoundsMatrix(unsigned int N, DATA_SPTR data) :
00032 RDNumeric::SquareMatrix<double>(N,data) {};
00033
00034
00035 inline double getUpperBound(unsigned int i, unsigned int j) const {
00036 RANGE_CHECK(0, i, d_nRows-1);
00037 RANGE_CHECK(0, j, d_nCols-1);
00038
00039 if (i < j) {
00040 return getVal(i,j);
00041 } else {
00042 return getVal(j,i);
00043 }
00044 }
00045
00046
00047 inline void setUpperBound(unsigned int i, unsigned int j, double val) {
00048 RANGE_CHECK(0, i, d_nRows-1);
00049 RANGE_CHECK(0, j, d_nCols-1);
00050 CHECK_INVARIANT(val >= 0.0, "Negative upper bound");
00051 if (i < j) {
00052 setVal(i,j,val);
00053 } else {
00054 setVal(j,i,val);
00055 }
00056 }
00057
00058
00059
00060 inline void setUpperBoundIfBetter(unsigned int i, unsigned int j, double val) {
00061 if ((val < getUpperBound(i, j)) && (val > getLowerBound(i, j)) ) {
00062 setUpperBound(i, j, val);
00063 }
00064 }
00065
00066
00067 inline void setLowerBound(unsigned int i, unsigned int j, double val) {
00068 RANGE_CHECK(0, i, d_nRows-1);
00069 RANGE_CHECK(0, j, d_nCols-1);
00070 CHECK_INVARIANT(val >= 0.0, "Negative lower bound");
00071 if (i < j) {
00072 setVal(j,i,val);
00073 } else {
00074 setVal(i,j,val);
00075 }
00076 }
00077
00078
00079
00080 inline void setLowerBoundIfBetter(unsigned int i, unsigned int j, double val) {
00081 if ((val > getLowerBound(i,j)) && (val < getUpperBound(i,j))) {
00082 setLowerBound(i,j, val);
00083 }
00084 }
00085
00086
00087 inline double getLowerBound(unsigned int i, unsigned int j) const {
00088 RANGE_CHECK(0, i, d_nRows-1);
00089 RANGE_CHECK(0, j, d_nCols-1);
00090
00091 if (i < j) {
00092 return getVal(j,i);
00093 } else {
00094 return getVal(i,j);
00095 }
00096 }
00097
00098
00099
00100 inline bool checkValid() const {
00101 unsigned int i, j;
00102 for (i = 1; i < d_nRows; i++) {
00103 for (j = 0; j < i; j++) {
00104 if (getUpperBound(i,j) < getLowerBound(i,j)) {
00105 return false;
00106 }
00107 }
00108 }
00109 return true;
00110 }
00111 };
00112
00113 typedef boost::shared_ptr<BoundsMatrix> BoundsMatPtr;
00114 }
00115
00116 #endif
00117