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)
31  : RDNumeric::SquareMatrix<double>(N, 0.0){};
32  BoundsMatrix(unsigned int N, DATA_SPTR data)
33  : RDNumeric::SquareMatrix<double>(N, data){};
34 
35  //! Get the upper bound between points i and j
36  inline double getUpperBound(unsigned int i, unsigned int j) const {
37  URANGE_CHECK(i, d_nRows - 1);
38  URANGE_CHECK(j, d_nCols - 1);
39 
40  if (i < j) {
41  return getVal(i, j);
42  } else {
43  return getVal(j, i);
44  }
45  }
46 
47  //! Set the lower bound between points i and j
48  inline void setUpperBound(unsigned int i, unsigned int j, double val) {
49  URANGE_CHECK(i, d_nRows - 1);
50  URANGE_CHECK(j, d_nCols - 1);
51  CHECK_INVARIANT(val >= 0.0, "Negative upper bound");
52  if (i < j) {
53  setVal(i, j, val);
54  } else {
55  setVal(j, i, val);
56  }
57  }
58 
59  //! Set the upper bound between points i and j only if it is better than
60  //! previously existing value (i.e. the new value is smaller)
61  inline void setUpperBoundIfBetter(unsigned int i, unsigned int j,
62  double val) {
63  if ((val < getUpperBound(i, j)) && (val > getLowerBound(i, j))) {
64  setUpperBound(i, j, val);
65  }
66  }
67 
68  //! Set the lower bound between points i and j
69  inline void setLowerBound(unsigned int i, unsigned int j, double val) {
70  URANGE_CHECK(i, d_nRows - 1);
71  URANGE_CHECK(j, d_nCols - 1);
72  CHECK_INVARIANT(val >= 0.0, "Negative lower bound");
73  if (i < j) {
74  setVal(j, i, val);
75  } else {
76  setVal(i, j, val);
77  }
78  }
79 
80  //! Set the lower bound between points i and j only if it is better than
81  //! previously existing value (i.e. the new value is larger)
82  inline void setLowerBoundIfBetter(unsigned int i, unsigned int j,
83  double val) {
84  if ((val > getLowerBound(i, j)) && (val < getUpperBound(i, j))) {
85  setLowerBound(i, j, val);
86  }
87  }
88 
89  //! Get the lower bound between points i and j
90  inline double getLowerBound(unsigned int i, unsigned int j) const {
91  URANGE_CHECK(i, d_nRows - 1);
92  URANGE_CHECK(j, d_nCols - 1);
93 
94  if (i < j) {
95  return getVal(j, i);
96  } else {
97  return getVal(i, j);
98  }
99  }
100 
101  //! Do a simple check of the current bounds - i.e. all lower bounds are
102  //! smaller than the existing upper bounds
103  inline bool checkValid() const {
104  unsigned int i, j;
105  for (i = 1; i < d_nRows; i++) {
106  for (j = 0; j < i; j++) {
107  if (getUpperBound(i, j) < getLowerBound(i, j)) {
108  return false;
109  }
110  }
111  }
112  return true;
113  }
114 };
115 
116 typedef boost::shared_ptr<BoundsMatrix> BoundsMatPtr;
117 }
118 
119 #endif
void setLowerBoundIfBetter(unsigned int i, unsigned int j, double val)
Definition: BoundsMatrix.h:82
#define CHECK_INVARIANT(expr, mess)
Definition: Invariant.h:99
virtual void setVal(unsigned int i, unsigned int j, doubleval)
sets a particular element of the matrix
Definition: Matrix.h:94
SquareMatrix()
brief Square matrix of size N
Definition: SquareMatrix.h:20
bool checkValid() const
Definition: BoundsMatrix.h:103
double getUpperBound(unsigned int i, unsigned int j) const
Get the upper bound between points i and j.
Definition: BoundsMatrix.h:36
boost::shared_ptr< BoundsMatrix > BoundsMatPtr
Definition: BoundsMatrix.h:116
void setUpperBound(unsigned int i, unsigned int j, double val)
Set the lower bound between points i and j.
Definition: BoundsMatrix.h:48
void setUpperBoundIfBetter(unsigned int i, unsigned int j, double val)
Definition: BoundsMatrix.h:61
unsigned int d_nCols
Definition: Matrix.h:233
BoundsMatrix(unsigned int N)
Definition: BoundsMatrix.h:30
#define URANGE_CHECK(x, hi)
Definition: Invariant.h:140
unsigned int d_nRows
Definition: Matrix.h:231
void setLowerBound(unsigned int i, unsigned int j, double val)
Set the lower bound between points i and j.
Definition: BoundsMatrix.h:69
virtual double getVal(unsigned int i, unsigned int j) const
returns a particular element of the matrix
Definition: Matrix.h:86
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:90
BoundsMatrix(unsigned int N, DATA_SPTR data)
Definition: BoundsMatrix.h:32