IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
DerivativeAccumulator.h
Go to the documentation of this file.
1/**
2 * \file IMP/DerivativeAccumulator.h
3 * \brief Class for adding derivatives from restraints to the model.
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef IMPKERNEL_DERIVATIVE_ACCUMULATOR_H
10#define IMPKERNEL_DERIVATIVE_ACCUMULATOR_H
11
12#include <IMP/kernel_config.h>
13#include <IMP/showable_macros.h>
14#include <IMP/check_macros.h>
15#include <IMP/value_macros.h>
16#include <IMP/math.h>
17#include <IMP/exception.h>
18#include <IMP/VectorD.h>
19
20IMPKERNEL_BEGIN_NAMESPACE
21
22//! Class for adding derivatives from restraints to the model.
23/** This class was created so that restraints can be weighted using
24 a RestraintSet and that the derivatives would be scaled appropriately */
25class IMPKERNELEXPORT DerivativeAccumulator {
26 public:
27 IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(DerivativeAccumulator);
28 //! the weight is one by default
29 DerivativeAccumulator(double weight = 1.0) : weight_(weight) {}
30
31 //! The weight is multiplied by the new weight
32 DerivativeAccumulator(const DerivativeAccumulator &copy, double weight)
33 : weight_(copy.weight_ * weight) {}
34
35 //! Scale a float value appropriately.
36 /** \param[in] value Value to add to the float attribute derivative.
37 */
38 double operator()(const double value) const {
39 IMP_INTERNAL_CHECK(!isnan(value), "Can't set derivative to NaN.");
40 return value * weight_;
41 }
42
43 //! Scale a Vector3D value appropriately.
44 /** \param[in] value Value to add to the Vector3D attribute derivative.
45 */
46 Vector3D operator()(const Vector3D &value) const {
47 return Vector3D(operator()(std::get<0>(value)),
48 operator()(std::get<1>(value)),
49 operator()(std::get<2>(value)));
50 }
51
52 //! Scale a Vector4D value appropriately.
53 /** \param[in] value Value to add to the Vector4D attribute derivative.
54 */
55 Vector4D operator()(const Vector4D &value) const {
56 return Vector4D(operator()(std::get<0>(value)),
57 operator()(std::get<1>(value)),
58 operator()(std::get<2>(value)),
59 operator()(std::get<3>(value)));
60 }
61
62 double get_weight() const { return weight_; }
63 IMP_SHOWABLE_INLINE(DerivativeAccumulator, out << weight_);
64
65 private:
66 double weight_;
67};
68
70
71IMPKERNEL_END_NAMESPACE
72
73#endif /* IMPKERNEL_DERIVATIVE_ACCUMULATOR_H */
Simple D vector class.
Helper macros for throwing and handling exceptions.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Class for adding derivatives from restraints to the model.
DerivativeAccumulator(double weight=1.0)
the weight is one by default
DerivativeAccumulator(const DerivativeAccumulator &copy, double weight)
The weight is multiplied by the new weight.
double operator()(const double value) const
Scale a float value appropriately.
Vector3D operator()(const Vector3D &value) const
Scale a Vector3D value appropriately.
Vector4D operator()(const Vector4D &value) const
Scale a Vector4D value appropriately.
Exception definitions and assertions.
Helper functions to check for NaN or infinity.
VectorD< 3 > Vector3D
Definition VectorD.h:407
VectorD< 4 > Vector4D
Definition VectorD.h:411
bool isnan(const T &a)
Return true if a number is NaN.
Definition math.h:24
IMP::Vector< DerivativeAccumulator > DerivativeAccumulators
Macros to help with objects that can be printed to a stream.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Macros to help in implementing Value objects.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.