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