IMP logo
IMP Reference Guide  2.13.0
The Integrative Modeling Platform
TruncatedHarmonic.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/TruncatedHarmonic.h \brief Truncated harmonic.
3  *
4  * Copyright 2007-2020 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPCORE_TRUNCATED_HARMONIC_H
8 #define IMPCORE_TRUNCATED_HARMONIC_H
9 
10 #include <IMP/core/core_config.h>
11 #include "internal/truncated_harmonic.h"
12 #include <IMP/UnaryFunction.h>
13 #include <IMP/object_macros.h>
14 #include <IMP/utility.h>
15 
16 IMPCORE_BEGIN_NAMESPACE
17 
18 enum BoundDirection {
19  LOWER,
20  BOTH,
21  UPPER
22 };
23 
24 //! A function that is harmonic over an interval.
25 /** This function is harmonic between center and threshold and then
26  asymptotically converges to the limit value.
27 
28  The function form above the threshold is currently limit-b/(x-o)
29  where x is the offset from the center and b,o are constants chosen to
30  make the function smooth and continuous. This form may change
31  without notice unless someone tells us it is important that it does
32  not.
33 
34  \param[in] DIRECTION Whether the harmonic is of an upper bound, lower bound, or
35  both directions type. It should be one of the BoundDirection enum
36  values. If it is LOWER, than the function is 0 for all values above
37  the passed center.
38  \see Harmonic
39  \see HarmonicLowerBound
40  \see HarmonicUpperBound
41  */
42 template <int DIRECTION>
44  public:
45  /** \param[in] center The center point for the truncated harmonic.
46  \param[in] k The spring constant for the truncated harmonic.
47  \param[in] threshold How far the harmonic term extends from the center.
48  \param[in] limit The value to which the function converges above the
49  threshold.
50 
51  \note I don't like having 4 floats on the initializer list, but
52  don't really see an alternative. There are a few sanity checks, so
53  the order is a bit hard to get wrong.
54  */
55  TruncatedHarmonic(Float center, Float k, Float threshold, Float limit)
56  : d_(center, k, threshold, limit) {}
57  /** Same as other constructor, but automatically set limit to a reasonable default value. */
58  TruncatedHarmonic(Float center, Float k, Float threshold)
59  : d_(center, k, threshold, k * square(threshold)) {}
60  virtual DerivativePair evaluate_with_derivative(double feature) const
61  IMP_OVERRIDE {
62  return DerivativePair(evaluate(feature),
63  ((DIRECTION == LOWER && (feature > d_.c_)) ||
64  (DIRECTION == UPPER && (feature < d_.c_)))
65  ? 0
66  : d_.evaluate_with_derivative(feature).second);
67  }
68  virtual double evaluate(double feature) const IMP_OVERRIDE {
69  return ((DIRECTION == LOWER && (feature > d_.c_)) ||
70  (DIRECTION == UPPER && (feature < d_.c_)))
71  ? 0
72  : d_.evaluate(feature);
73  }
75 
76  private:
77  internal::TruncatedHarmonicData d_;
78 };
79 
80 //! A specialization of TruncatedHarmonic that may be non-zero only
81 //! above the center value (always zero below it)
83 
84 //! A specialization of TruncatedHarmonic that may be non-zero only
85 //! below the center value (always zero above it)
87 
88 //! A specialization of TruncatedHarmonic that may be non-zero in both
89 //! directions, below or above the center value
91 
92 IMPCORE_END_NAMESPACE
93 
94 #endif /* IMPCORE_TRUNCATED_HARMONIC_H */
Various general useful macros for IMP.
A function that is harmonic over an interval.
TruncatedHarmonic(Float center, Float k, Float threshold, Float limit)
TruncatedHarmonic(Float center, Float k, Float threshold)
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
virtual DerivativePair evaluate_with_derivative(double feature) const
Calculate score and derivative with respect to the given feature.
virtual double evaluate(double feature) const
Calculate score with respect to the given feature.
For backwards compatibility.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:23
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
TruncatedHarmonic< BOTH > TruncatedHarmonicBound
TruncatedHarmonic< UPPER > TruncatedHarmonicUpperBound
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:25
TruncatedHarmonic< LOWER > TruncatedHarmonicLowerBound
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.