RDCode: BoundsMatrix.h Source File

# BoundsMatrix.h

Go to the documentation of this file.

    00001 //
    00002 //  Copyright (C) 2004-2006 Rational Discovery LLC
    00003 //   All Rights Reserved
    00004 //
    00005 //   @@ All Rights Reserved  @@
    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   //! Class to store the distance bound
    00018   /*!
    00019     Basically a N by N matrix
    00020     with lower distance bounds on the lower traingle and upper bounds in the upper
    00021     triangle
    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     //! Get the upper bound between points i and j
    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     //! Set the lower bound between points i and j
    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     //! Set the upper bound between points i and j only if it is better than
    00056     //! previously existing value (i.e. the new value is smaller)
    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     //! Set the lower bound between points i and j
    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     //! Set the lower bound between points i and j only if it is better than
    00076     //! previously existing value (i.e. the new value is larger)
    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     //! Get the lower bound between points i and j
    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     //! Do a simple check of the current bounds - i.e. all lower bounds are
    00096     //! smaller than the existing upper bounds
    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