IMP logo
IMP Reference Guide  develop.7400db2aee,2024/11/23
The Integrative Modeling Platform
score_functor/OpenCubicSpline.h
Go to the documentation of this file.
1 /**
2  * \file IMP/score_functor/OpenCubicSpline.h
3  * \brief A Score on the distance between a pair of particles.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPSCORE_FUNCTOR_OPEN_CUBIC_SPLINE_H
9 #define IMPSCORE_FUNCTOR_OPEN_CUBIC_SPLINE_H
10 
11 #include <IMP/score_functor/score_functor_config.h>
12 #include "Score.h"
13 #include "internal/RawOpenCubicSpline.h"
14 IMPSCOREFUNCTOR_BEGIN_NAMESPACE
15 
16 //! Open cubic spline function.
17 /** This function interpolates a set of evenly spaced values using a spline.
18  The second derivative at the termini is set to zero. See
19  core::ClosedCubicSpline for a periodic version.
20 
21  \see core::ClosedCubicSpline
22  */
23 class OpenCubicSpline : public Score {
24  double spacing_;
25  double inverse_spacing_;
26  internal::RawOpenCubicSpline spline_;
27  double minrange_;
28  double maxrange_;
29  bool extend_;
30 
31  public:
32  //! Constructor.
33  /** \param[in] values Score value at each spline point
34  \param[in] minrange Feature value at first spline point
35  \param[in] spacing Distance (in feature space) between points
36  \param[in] extend If extend is true, the nearest value is used
37  as the value for any feature outside of the range.
38  */
39  OpenCubicSpline(const Floats &values, Float minrange, Float spacing,
40  bool extend = false)
41  : spacing_(spacing),
42  inverse_spacing_(1.0 / spacing_),
43  spline_(values, spacing_, inverse_spacing_),
44  minrange_(minrange),
45  maxrange_(minrange_ + spacing_ * (values.size() - 1)),
46  extend_(extend) {
47  IMP_USAGE_CHECK(spacing > 0,
48  "The spacing between values must be positive.");
49  IMP_USAGE_CHECK(values.size() >= 1, "You must provide at least one value.");
50  }
51  // depend on get_is_trivially_zero
52  template <unsigned int D>
53  double get_score(Model *,
55  double distance) const {
56  // check for feature in range
57  if (distance < minrange_ || distance > maxrange_) {
58  if (extend_) {
59  if (distance < minrange_)
60  return spline_.get_first();
61  else
62  return spline_.get_last();
63  } else {
64  IMP_THROW("Value " << distance << " out of range [" << minrange_ << ", "
65  << maxrange_ << "] for open cubic spline", ValueException);
66  }
67  }
68  return spline_.evaluate(distance - minrange_, spacing_, inverse_spacing_);
69  }
70  template <unsigned int D>
71  DerivativePair get_score_and_derivative(
72  Model *, const Array<D, ParticleIndex> &,
73  double distance) const {
74  // check for distance in range
75  if (distance < minrange_ || distance > maxrange_) {
76  if (extend_) {
77  if (distance < minrange_)
78  return std::make_pair(spline_.get_first(), 0.0);
79  else
80  return std::make_pair(spline_.get_last(), 0.0);
81  } else {
82  IMP_THROW("Value " << distance << " out of range [" << minrange_ << ", "
83  << maxrange_ << "] for open cubic spline", ValueException);
84  }
85  }
86  return spline_.evaluate_with_derivative(distance - minrange_, spacing_,
87  inverse_spacing_);
88  }
89  template <unsigned int D>
90  double get_maximum_range(
91  Model *, const Array<D, ParticleIndex> &) const {
92  if (!extend_ || spline_.get_last() == 0)
93  return maxrange_;
94  else
95  return std::numeric_limits<double>::max();
96  }
97  template <unsigned int D>
98  bool get_is_trivially_zero(Model *,
99  const Array<D, ParticleIndex> &,
100  double squared_distance) const {
101  if (!extend_ || spline_.get_last() == 0) {
102  return squared_distance > algebra::get_squared(maxrange_);
103  } else
104  return false;
105  }
106 };
107 
108 IMPSCOREFUNCTOR_END_NAMESPACE
109 
110 #endif /* IMPSCORE_FUNCTOR_OPEN_CUBIC_SPLINE_H */
A class to store a fixed array of same-typed values.
Definition: Array.h:40
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
OpenCubicSpline(const Floats &values, Float minrange, Float spacing, bool extend=false)
Constructor.
A Score on the distance between a pair of particles.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
A functor for computing a distance based score for D particles.
Definition: Score.h:20
An exception for an invalid value being passed to IMP.
Definition: exception.h:136