IMP logo
IMP Reference Guide  2.20.2
The Integrative Modeling Platform
Linear.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/Linear.h \brief A linear function.
3  *
4  * Copyright 2007-2022 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPCORE_LINEAR_H
8 #define IMPCORE_LINEAR_H
9 
10 #include <IMP/core/core_config.h>
11 #include <IMP/UnaryFunction.h>
12 #include <cereal/access.hpp>
13 #include <cereal/types/base_class.hpp>
14 #include <cereal/types/polymorphic.hpp>
15 
16 IMPCORE_BEGIN_NAMESPACE
17 
18 //! %Linear function
19 /** \note The offset is not meaningful for optimization, but does
20  make the displayed energies nicer.
21  */
22 class Linear : public UnaryFunction {
23  public:
24  //! Create with the given offset and slope.
25  Linear(double offset, double slope) : slope_(slope), offset_(offset) {}
26  Linear() {}
27 
28  void set_slope(double f) { slope_ = f; }
29 
30  void set_offset(double f) { offset_ = f; }
31 
32  virtual double evaluate(double feature) const override {
33  return (feature - offset_) * slope_;
34  }
35 
36  virtual DerivativePair evaluate_with_derivative(double feature) const
37  override {
38  return DerivativePair(evaluate(feature), slope_);
39  }
40 
42 
43  private:
44  double slope_, offset_;
45 
46  friend class cereal::access;
47 
48  template<class Archive> void serialize(Archive &ar) {
49  ar(cereal::base_class<UnaryFunction>(this),
50  slope_, offset_);
51  }
53 };
54 
55 IMPCORE_END_NAMESPACE
56 
57 #endif /* IMPCORE_LINEAR_H */
#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 override
Calculate score and derivative with respect to the given feature.
Definition: Linear.h:36
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Linear function
Definition: Linear.h:22
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
virtual double evaluate(double feature) const override
Calculate score with respect to the given feature.
Definition: Linear.h:32
Linear(double offset, double slope)
Create with the given offset and slope.
Definition: Linear.h:25
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:27