IMP  2.1.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-2013 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>
15 #include <IMP/utility.h>
16 
17 IMPEXAMPLE_BEGIN_NAMESPACE
18 
19 //! A simple unary function
20 /** This one happens to be a harmonic.
21  The source code is as follows:
22  \include ExampleUnaryFunction.h
23 
24  \note The class does not have an IMPEXAMPLEEXPORT
25  since it is all defined in a header.
26  */
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 
39  virtual DerivativePair evaluate_with_derivative(double feature) const
40  IMP_OVERRIDE {
41  return DerivativePair(evaluate(feature), k_ * (feature - center_));
42  }
43  virtual double evaluate(double feature) const IMP_OVERRIDE {
44  return .5 * k_ * algebra::get_squared(feature - center_);
45  }
47 };
48 
49 IMPEXAMPLE_END_NAMESPACE
50 
51 #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
Import IMP/kernel/unary_function_macros.h in the namespace.
double Float
Basic floating-point value (could be float, double...)
Definition: base/types.h:20