IMP  2.0.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-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 {
29  Float center_;
30  Float k_;
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)
40  const 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 */