BoundsMatrix.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2004-2006 Rational Discovery LLC
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
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   //! Class to store the distance bound
00021   /*! 
00022     Basically a N by N matrix 
00023     with lower distance bounds on the lower traingle and upper bounds in the upper 
00024     triangle
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     //! Get the upper bound between points i and j
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     //! Set the lower bound between points i and j
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     //! Set the upper bound between points i and j only if it is better than 
00059     //! previously existing value (i.e. the new value is smaller)
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     //! Set the lower bound between points i and j 
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     //! Set the lower bound between points i and j only if it is better than 
00079     //! previously existing value (i.e. the new value is larger)
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     //! Get the lower bound between points i and j
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     //! Do a simple check of the current bounds - i.e. all lower bounds are
00099     //! smaller than the existing upper bounds
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