IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
vector_metrics.h
Go to the documentation of this file.
1/**
2 * \file IMP/algebra/vector_metrics.h \brief Functions to generate vectors.
3 *
4 * Copyright 2007-2026 IMP Inventors. All rights reserved.
5 *
6 */
7
8#ifndef IMPALGEBRA_VECTOR_METRICS_H
9#define IMPALGEBRA_VECTOR_METRICS_H
10
11#include "VectorD.h"
12#include "BoundingBoxD.h"
13#include <IMP/types.h>
14#include <IMP/Object.h>
15#include <iostream>
16#include <IMP/Pointer.h>
17#include <IMP/WeakPointer.h>
18#include <algorithm>
19#include <numeric>
20#include <cereal/access.hpp>
21#include <cereal/types/polymorphic.hpp>
22#include <cereal/types/base_class.hpp>
23
24IMPALGEBRA_BEGIN_NAMESPACE
25
26//! The base class for a metric on VectorKDs.
27class IMPALGEBRAEXPORT VectorKDMetric : public Object {
28 public:
29 VectorKDMetric(std::string name);
30 virtual double get_distance(const VectorKD &a, const VectorKD &b) const = 0;
31 virtual VectorKD get_centroid(const VectorKDs &vs) const = 0;
32};
33
35
36//! The l2 norm on the distance vector.
37class IMPALGEBRAEXPORT EuclideanVectorKDMetric : public VectorKDMetric {
38 friend class cereal::access;
39 template<class Archive> void serialize(Archive &ar) {
40 ar(cereal::base_class<Object>(this));
41 }
42 IMP_OBJECT_SERIALIZE_DECL(EuclideanVectorKDMetric);
43 public:
44 EuclideanVectorKDMetric(std::string name = "EuclideanVectorKDMetric%1%");
45 double get_distance(const VectorKD &a, const VectorKD &b) const override {
46 return get_l2_norm(a - b);
47 }
48 VectorKD get_centroid(const VectorKDs &vs) const override {
49 IMP_USAGE_CHECK(!vs.empty(), "Needs things to have a centroid");
50 VectorKD sum = std::accumulate(vs.begin(), vs.end(),
51 get_zero_vector_kd(vs[0].get_dimension()));
52 return sum / vs.size();
53 }
54 IMP_OBJECT_METHODS(EuclideanVectorKDMetric);
55};
56
57/** The l-infinity norm on the difference between the two vectors. And the
58 centroid is the center of the bounding box of the vectors.
59 */
60class IMPALGEBRAEXPORT MaxVectorKDMetric : public VectorKDMetric {
61 friend class cereal::access;
62 template<class Archive> void serialize(Archive &ar) {
63 ar(cereal::base_class<Object>(this));
64 }
65 IMP_OBJECT_SERIALIZE_DECL(MaxVectorKDMetric);
66 public:
67 MaxVectorKDMetric(std::string name = "MaxVectorKDMetric%1%");
68 double get_distance(const VectorKD &a, const VectorKD &b) const override {
69 return get_linf_norm(a - b);
70 }
71 VectorKD get_centroid(const VectorKDs &vs) const override {
72 IMP_USAGE_CHECK(!vs.empty(), "Needs things to have a centroid");
73 BoundingBoxKD bb = std::accumulate(vs.begin(), vs.end(),
74 BoundingBoxKD(vs[0].get_dimension()));
75 return .5 * (bb.get_corner_0() + bb.get_corner_1());
76 }
77 IMP_OBJECT_METHODS(MaxVectorKDMetric);
78};
79
80IMPALGEBRA_END_NAMESPACE
81
82#endif /* IMPALGEBRA_VECTOR_METRICS_H */
A bounding box in D dimensions.
A shared base class to help in debugging and things.
A nullptr-initialized pointer to an IMP Object.
A weak pointer to an Object or RefCountedObject.
Simple D vector class.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Object(std::string name)
Construct an object with the given name.
The base class for a metric on VectorKDs.
VectorD< D > get_zero_vector_kd(int Di)
Return a dynamically sized vector of zeros.
Definition VectorD.h:262
BoundingBoxD<-1 > BoundingBoxKD
Typedef for Python.
IMP::Vector< IMP::Pointer< VectorKDMetric > > VectorKDMetrics
Vector< VectorD<-1 > > VectorKDs
Definition VectorD.h:425
VectorD<-1 > VectorKD
Definition VectorD.h:423
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Basic types used by IMP.