IMP logo
IMP Reference Guide  2.19.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-2022 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 
29  double feature) const override {
30  double eval = 0;
31  double derv = 0;
32  DerivativePair fout;
33  for (unsigned int i = 0; i < funcs_.size(); ++i) {
34  fout = funcs_[i]->evaluate_with_derivative(feature);
35  eval += weights_[i] * fout.first;
36  derv += weights_[i] * fout.second;
37  }
38  return DerivativePair(eval, derv);
39  }
40 
41  virtual double evaluate(double feature) const override {
42  double ret = 0;
43  for (unsigned int i = 0; i < funcs_.size(); ++i) {
44  ret += weights_[i] * funcs_[i]->evaluate(feature);
45  }
46  return ret;
47  }
48 
49  //! Get the number of functions
50  unsigned int get_function_number() {
51  return funcs_.size();
52  }
53 
54  //! Set the function weights
55  void set_weights(Floats weights) {
56  IMP_USAGE_CHECK(weights.size() == get_function_number(),
57  "Number of weights and functions must match.");
58  weights_ = weights;
59  }
60 
61  //! Get the function weights
62  Floats get_weights() { return weights_; }
63 
64  //! Get function weight at index
65  double get_weight(unsigned int i) const {
66  IMP_USAGE_CHECK(i < weights_.size(), "Invalid weight index");
67  return weights_[i];
68  }
69 
70  //! Get function at index
71  UnaryFunction* get_function(unsigned int i) {
72  IMP_USAGE_CHECK(i < get_function_number(), "Invalid function index");
73  return funcs_[i];
74  }
75 
77 
78  private:
79  UnaryFunctions funcs_;
80  Floats weights_;
81 };
82 
83 IMPCORE_END_NAMESPACE
84 
85 #endif /* IMPCORE_WEIGHTED_SUM_H */
unsigned int get_function_number()
Get the number of functions.
Definition: WeightedSum.h:50
virtual DerivativePair evaluate_with_derivative(double feature) const override
Calculate score and derivative with respect to the given feature.
Definition: WeightedSum.h:28
#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:65
UnaryFunction * get_function(unsigned int i)
Get function at index.
Definition: WeightedSum.h:71
virtual double evaluate(double feature) const override
Calculate score with respect to the given feature.
Definition: WeightedSum.h:41
Floats get_weights()
Get the function weights.
Definition: WeightedSum.h:62
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
void set_weights(Floats weights)
Set the function weights.
Definition: WeightedSum.h:55
#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:27
WeightedSum(UnaryFunctions funcs, Floats weights)
Definition: WeightedSum.h:21