IMP  2.2.1
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-2014 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)
Import IMP/kernel/UnaryFunction.h in the namespace.
virtual DerivativePair evaluate_with_derivative(double feature) const
Calculate score and derivative with respect to the given feature.
Abstract single variable functor class for score functions.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
virtual double evaluate(double feature) const
Calculate score with respect to the given feature.
Import IMP/kernel/utility.h in the namespace.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: base/types.h:23
double Float
Basic floating-point value (could be float, double...)
Definition: base/types.h:20