RDKit
Open-source cheminformatics and machine learning.
BoundsMatrix.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef __RD_BOUNDS_MATRIX_H__
11 #define __RD_BOUNDS_MATRIX_H__
12 
13 #include <RDGeneral/Invariant.h>
14 #include <boost/smart_ptr.hpp>
15 #include <iostream>
16 #include <iomanip>
17 #include <Numerics/SquareMatrix.h>
18 
19 namespace DistGeom {
20  //! Class to store the distance bound
21  /*!
22  Basically a N by N matrix
23  with lower distance bounds on the lower traingle and upper bounds in the upper
24  triangle
25  */
26  class BoundsMatrix : public RDNumeric::SquareMatrix<double> {
27  public:
28  typedef boost::shared_array<double> DATA_SPTR;
29 
30  explicit BoundsMatrix(unsigned int N) : RDNumeric::SquareMatrix<double>(N,0.0) {};
31  BoundsMatrix(unsigned int N, DATA_SPTR data) :
32  RDNumeric::SquareMatrix<double>(N,data) {};
33 
34  //! Get the upper bound between points i and j
35  inline double getUpperBound(unsigned int i, unsigned int j) const {
36  RANGE_CHECK(0, i, d_nRows-1);
37  RANGE_CHECK(0, j, d_nCols-1);
38 
39  if (i < j) {
40  return getVal(i,j);
41  } else {
42  return getVal(j,i);
43  }
44  }
45 
46  //! Set the lower bound between points i and j
47  inline void setUpperBound(unsigned int i, unsigned int j, double val) {
48  RANGE_CHECK(0, i, d_nRows-1);
49  RANGE_CHECK(0, j, d_nCols-1);
50  CHECK_INVARIANT(val >= 0.0, "Negative upper bound");
51  if (i < j) {
52  setVal(i,j,val);
53  } else {
54  setVal(j,i,val);
55  }
56  }
57 
58  //! Set the upper bound between points i and j only if it is better than
59  //! previously existing value (i.e. the new value is smaller)
60  inline void setUpperBoundIfBetter(unsigned int i, unsigned int j, double val) {
61  if ((val < getUpperBound(i, j)) && (val > getLowerBound(i, j)) ) {
62  setUpperBound(i, j, val);
63  }
64  }
65 
66  //! Set the lower bound between points i and j
67  inline void setLowerBound(unsigned int i, unsigned int j, double val) {
68  RANGE_CHECK(0, i, d_nRows-1);
69  RANGE_CHECK(0, j, d_nCols-1);
70  CHECK_INVARIANT(val >= 0.0, "Negative lower bound");
71  if (i < j) {
72  setVal(j,i,val);
73  } else {
74  setVal(i,j,val);
75  }
76  }
77 
78  //! Set the lower bound between points i and j only if it is better than
79  //! previously existing value (i.e. the new value is larger)
80  inline void setLowerBoundIfBetter(unsigned int i, unsigned int j, double val) {
81  if ((val > getLowerBound(i,j)) && (val < getUpperBound(i,j))) {
82  setLowerBound(i,j, val);
83  }
84  }
85 
86  //! Get the lower bound between points i and j
87  inline double getLowerBound(unsigned int i, unsigned int j) const {
88  RANGE_CHECK(0, i, d_nRows-1);
89  RANGE_CHECK(0, j, d_nCols-1);
90 
91  if (i < j) {
92  return getVal(j,i);
93  } else {
94  return getVal(i,j);
95  }
96  }
97 
98  //! Do a simple check of the current bounds - i.e. all lower bounds are
99  //! smaller than the existing upper bounds
100  inline bool checkValid() const {
101  unsigned int i, j;
102  for (i = 1; i < d_nRows; i++) {
103  for (j = 0; j < i; j++) {
104  if (getUpperBound(i,j) < getLowerBound(i,j)) {
105  return false;
106  }
107  }
108  }
109  return true;
110  }
111  };
112 
113  typedef boost::shared_ptr<BoundsMatrix> BoundsMatPtr;
114 }
115 
116 #endif
117 
#define RANGE_CHECK(lo, x, hi)
Definition: Invariant.h:133
void setLowerBoundIfBetter(unsigned int i, unsigned int j, double val)
Definition: BoundsMatrix.h:80
#define CHECK_INVARIANT(expr, mess)
Definition: Invariant.h:114
virtual void setVal(unsigned int i, unsigned int j, doubleval)
sets a particular element of the matrix
Definition: Matrix.h:99
SquareMatrix()
brief Square matrix of size N
Definition: SquareMatrix.h:19
bool checkValid() const
Definition: BoundsMatrix.h:100
double getUpperBound(unsigned int i, unsigned int j) const
Get the upper bound between points i and j.
Definition: BoundsMatrix.h:35
boost::shared_ptr< BoundsMatrix > BoundsMatPtr
Definition: BoundsMatrix.h:113
void setUpperBound(unsigned int i, unsigned int j, double val)
Set the lower bound between points i and j.
Definition: BoundsMatrix.h:47
void setUpperBoundIfBetter(unsigned int i, unsigned int j, double val)
Definition: BoundsMatrix.h:60
unsigned int d_nCols
Definition: Matrix.h:236
BoundsMatrix(unsigned int N)
Definition: BoundsMatrix.h:30
unsigned int d_nRows
Definition: Matrix.h:234
void setLowerBound(unsigned int i, unsigned int j, double val)
Set the lower bound between points i and j.
Definition: BoundsMatrix.h:67
virtual double getVal(unsigned int i, unsigned int j) const
returns a particular element of the matrix
Definition: Matrix.h:91
Class to store the distance bound.
Definition: BoundsMatrix.h:26
boost::shared_array< double > DATA_SPTR
Definition: BoundsMatrix.h:28
double getLowerBound(unsigned int i, unsigned int j) const
Get the lower bound between points i and j.
Definition: BoundsMatrix.h:87
BoundsMatrix(unsigned int N, DATA_SPTR data)
Definition: BoundsMatrix.h:31