IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
ExampleUnaryFunction.h
Go to the documentation of this file.
1 /**
2  * \file IMP/example/ExampleUnaryFunction.h
3  * \brief A simple unary function.
4  *
5  * Copyright 2007-2023 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEXAMPLE_EXAMPLE_UNARY_FUNCTION_H
10 #define IMPEXAMPLE_EXAMPLE_UNARY_FUNCTION_H
11 
12 #include <IMP/example/example_config.h>
13 #include <IMP/UnaryFunction.h>
14 #include <IMP/utility.h>
15 #include <IMP/algebra/utility.h>
16 #include <cereal/access.hpp>
17 #include <cereal/types/base_class.hpp>
18 #include <cereal/types/polymorphic.hpp>
19 
20 IMPEXAMPLE_BEGIN_NAMESPACE
21 
22 //! A simple unary function
23 /** This one happens to be a harmonic.
24  The source code is as follows:
25  \include ExampleUnaryFunction.h
26  */
27 class IMPEXAMPLEEXPORT ExampleUnaryFunction : public UnaryFunction {
28  Float center_;
29  Float k_;
30 
31  public:
32  /** Create with the given center and spring constant. While it
33  is generally bad form to have two Float arguments, it is
34  hard to avoid here, and there is a bit of a sanity check.*/
35  ExampleUnaryFunction(Float center, Float k) : center_(center), k_(k) {
36  IMP_USAGE_CHECK(k > 0, "The spring constant must be positive.");
37  }
38 
40 
41  virtual DerivativePair evaluate_with_derivative(double feature) const
42  override {
43  return DerivativePair(evaluate(feature), k_ * (feature - center_));
44  }
45  virtual double evaluate(double feature) const override {
46  return .5 * k_ * algebra::get_squared(feature - center_);
47  }
49 
50  private:
51  friend class cereal::access;
52  template<class Archive> void serialize(Archive &ar) {
53  ar(cereal::base_class<UnaryFunction>(this), center_, k_);
54  }
56 };
57 
58 IMPEXAMPLE_END_NAMESPACE
59 
60 #endif /* IMPEXAMPLE_EXAMPLE_UNARY_FUNCTION_H */
ExampleUnaryFunction(Float center, Float k)
virtual DerivativePair evaluate_with_derivative(double feature) const override
Calculate score and derivative with respect to the given feature.
virtual double evaluate(double feature) const override
Calculate score with respect to the given feature.
virtual double evaluate(double feature) const
Calculate score with respect to the given feature.
Definition: UnaryFunction.h:35
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
Functions to deal with very common math operations.
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Various general useful functions for IMP.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
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
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:27