IMP logo
IMP Reference Guide  2.8.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-2017 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 
16 IMPEXAMPLE_BEGIN_NAMESPACE
17 
18 //! A simple unary function
19 /** This one happens to be a harmonic.
20  The source code is as follows:
21  \include ExampleUnaryFunction.h
22 
23  \note The class does not have an IMPEXAMPLEEXPORT
24  since it is all defined in a header.
25  */
27  Float center_;
28  Float k_;
29 
30  public:
31  /** Create with the given center and spring constant. While it
32  is generally bad form to have two Float arguments, it is
33  hard to avoid here, and there is a bit of a sanity check.*/
34  ExampleUnaryFunction(Float center, Float k) : center_(center), k_(k) {
35  IMP_USAGE_CHECK(k > 0, "The spring constant must be positive.");
36  }
37 
38  virtual DerivativePair evaluate_with_derivative(double feature) const
39  IMP_OVERRIDE {
40  return DerivativePair(evaluate(feature), k_ * (feature - center_));
41  }
42  virtual double evaluate(double feature) const IMP_OVERRIDE {
43  return .5 * k_ * algebra::get_squared(feature - center_);
44  }
46 };
47 
48 IMPEXAMPLE_END_NAMESPACE
49 
50 #endif /* IMPEXAMPLE_EXAMPLE_UNARY_FUNCTION_H */
ExampleUnaryFunction(Float center, Float k)
#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
#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:25
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.