IMP  2.0.0
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-2013 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/base/types.h>
14 #include <IMP/base/Object.h>
15 #include <iostream>
16 #include <IMP/base/Pointer.h>
17 #include <IMP/base/WeakPointer.h>
18 #include <algorithm>
19 #include <numeric>
20 
21 IMPALGEBRA_BEGIN_NAMESPACE
22 
23 /** The base class for a metric on VectorKDs.
24  */
25 class IMPALGEBRAEXPORT VectorKDMetric: public base::Object {
26 public:
27  VectorKDMetric(std::string name);
28  virtual double get_distance(const VectorKD &a,
29  const VectorKD &b) const=0;
30  virtual VectorKD get_centroid(const VectorKDs &vs) const=0;
31 };
32 
34 
35 /** The l2 norm on the distance vector.
36  */
37 class IMPALGEBRAEXPORT EuclideanVectorKDMetric: public VectorKDMetric {
38 public:
39  EuclideanVectorKDMetric(std::string name="EuclideanVectorKDMetric%1%");
40  double get_distance(const VectorKD &a,
41  const VectorKD &b) const {
42  return get_l2_norm(a-b);
43  }
44  VectorKD get_centroid(const VectorKDs &vs) const {
45  IMP_USAGE_CHECK(!vs.empty(), "Needs things to have a centroid");
46  VectorKD sum = std::accumulate(vs.begin(), vs.end(),
47  get_zero_vector_kd(vs[0].get_dimension()));
48  return sum / vs.size();
49  }
51 };
52 
53 
54 
55 /** The l-infinity norm on the difference between the two vectors. And the
56  centroid is the center of the bounding box of the vectors.
57  */
58 class IMPALGEBRAEXPORT MaxVectorKDMetric: public VectorKDMetric {
59 public:
60  MaxVectorKDMetric(std::string name="MaxVectorKDMetric%1%");
61  double get_distance(const VectorKD &a,
62  const VectorKD &b) const {
63  return get_linf_norm(a-b);
64  }
65  VectorKD get_centroid(const VectorKDs &vs) const {
66  IMP_USAGE_CHECK(!vs.empty(), "Needs things to have a centroid");
67  BoundingBoxKD bb= std::accumulate(vs.begin(), vs.end(),
68  BoundingBoxKD(vs[0].get_dimension()));
69  return .5*(bb.get_corner(0)+bb.get_corner(1));
70  }
72 };
73 
74 IMPALGEBRA_END_NAMESPACE
75 
76 #endif /* IMPALGEBRA_VECTOR_METRICS_H */