IMP logo
IMP Reference Guide  2.12.0
The Integrative Modeling Platform
WeightedSum.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/WeightedSum.h \brief Weighted sum of unary functions.
3  *
4  * Copyright 2007-2019 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPCORE_WEIGHTED_SUM_H
8 #define IMPCORE_WEIGHTED_SUM_H
9 
10 #include <IMP/core/core_config.h>
11 #include <IMP/UnaryFunction.h>
12 
13 IMPCORE_BEGIN_NAMESPACE
14 
15 //! Weighted sum of unary functions.
16 /** A unary function that computes the weighted sum of multiple functions.
17  */
18 class WeightedSum : public UnaryFunction {
19  public:
20  /** Create with the functions and their respective weights */
21  WeightedSum(UnaryFunctions funcs, Floats weights) : funcs_(funcs), weights_(weights) {
22  IMP_USAGE_CHECK(weights.size() == funcs.size(),
23  "Number of functions and weights must match.");
24  IMP_USAGE_CHECK(funcs.size() > 1,
25  "More than one function and weight must be provided.");
26  }
27 
28  virtual DerivativePair evaluate_with_derivative(double feature) const {
29  double eval = 0;
30  double derv = 0;
31  DerivativePair fout;
32  for (unsigned int i = 0; i < funcs_.size(); ++i) {
33  fout = funcs_[i]->evaluate_with_derivative(feature);
34  eval += weights_[i] * fout.first;
35  derv += weights_[i] * fout.second;
36  }
37  return DerivativePair(eval, derv);
38  }
39 
40  virtual double evaluate(double feature) const {
41  double ret = 0;
42  for (unsigned int i = 0; i < funcs_.size(); ++i) {
43  ret += weights_[i] * funcs_[i]->evaluate(feature);
44  }
45  return ret;
46  }
47 
48  //! Get the number of functions
49  unsigned int get_function_number() {
50  return funcs_.size();
51  }
52 
53  //! Set the function weights
54  void set_weights(Floats weights) {
55  IMP_USAGE_CHECK(weights.size() == get_function_number(),
56  "Number of weights and functions must match.");
57  weights_ = weights;
58  }
59 
60  //! Get the function weights
61  Floats get_weights() { return weights_; }
62 
63  //! Get function weight at index
64  double get_weight(unsigned int i) const {
65  IMP_USAGE_CHECK(i < weights_.size(), "Invalid weight index");
66  return weights_[i];
67  }
68 
69  //! Get function at index
70  UnaryFunction* get_function(unsigned int i) {
71  IMP_USAGE_CHECK(i < get_function_number(), "Invalid function index");
72  return funcs_[i];
73  }
74 
76 
77  private:
78  UnaryFunctions funcs_;
79  Floats weights_;
80 };
81 
82 IMPCORE_END_NAMESPACE
83 
84 #endif /* IMPCORE_WEIGHTED_SUM_H */
unsigned int get_function_number()
Get the number of functions.
Definition: WeightedSum.h:49
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
Weighted sum of unary functions.
Definition: WeightedSum.h:18
double get_weight(unsigned int i) const
Get function weight at index.
Definition: WeightedSum.h:64
virtual double evaluate(double feature) const
Calculate score with respect to the given feature.
Definition: WeightedSum.h:40
UnaryFunction * get_function(unsigned int i)
Get function at index.
Definition: WeightedSum.h:70
Floats get_weights()
Get the function weights.
Definition: WeightedSum.h:61
virtual DerivativePair evaluate_with_derivative(double feature) const
Calculate score and derivative with respect to the given feature.
Definition: WeightedSum.h:28
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:23
void set_weights(Floats weights)
Set the function weights.
Definition: WeightedSum.h:54
#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
WeightedSum(UnaryFunctions funcs, Floats weights)
Definition: WeightedSum.h:21