8 #ifndef IMPALGEBRA_VECTOR_D_H
9 #define IMPALGEBRA_VECTOR_D_H
11 #include <IMP/algebra/algebra_config.h>
20 #include <boost/random/variate_generator.hpp>
21 #include <boost/random/normal_distribution.hpp>
22 #include <boost/range.hpp>
23 #include "internal/vector.h"
27 #include <boost/random/normal_distribution.hpp>
28 #include <boost/static_assert.hpp>
30 #if IMP_HAS_CHECKS >= IMP_INTERNAL
31 #define IMP_ALGEBRA_VECTOR_CHECK check_vector()
33 #define IMP_ALGEBRA_VECTOR_CHECK
36 #if IMP_HAS_CHECKS >= IMP_USAGE
37 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
38 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
39 check_compatible_vector(o); \
42 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
43 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
46 IMPALGEBRA_BEGIN_NAMESPACE
59 IMP_ALGEBRA_VECTOR_METHODS(D);
64 std::vector<double> get_range(
double x0,
double x1,
double x2,
double x3,
65 double x4,
double x5) {
68 std::vector<double> ret;
70 if (x1 != std::numeric_limits<double>::max()) ret.push_back(x1);
71 if (x2 != std::numeric_limits<double>::max()) ret.push_back(x2);
72 if (x3 != std::numeric_limits<double>::max()) ret.push_back(x3);
73 if (x4 != std::numeric_limits<double>::max()) ret.push_back(x4);
74 if (x5 != std::numeric_limits<double>::max()) ret.push_back(x5);
79 IMP_ALGEBRA_VECTOR_METHODS(-1);
80 explicit VectorD(
double x0,
double x1 = std::numeric_limits<double>::max(),
81 double x2 = std::numeric_limits<double>::max(),
82 double x3 = std::numeric_limits<double>::max(),
83 double x4 = std::numeric_limits<double>::max(),
84 double x5 = std::numeric_limits<double>::max())
85 : P(get_range(x0, x1, x2, x3, x4, x5)) {}
89 class VectorD<1> :
public VectorBaseD<1> {
91 IMP_ALGEBRA_VECTOR_METHODS(1);
94 VectorD(
double x) { P::operator[](0) = x; }
97 class VectorD<2> :
public VectorBaseD<2> {
99 IMP_ALGEBRA_VECTOR_METHODS(2);
101 VectorD(
double x,
double y) {
102 P::operator[](0) = x;
103 P::operator[](1) = y;
107 class VectorD<3> :
public VectorBaseD<3> {
109 IMP_ALGEBRA_VECTOR_METHODS(3);
111 VectorD(
double x,
double y,
double z) {
112 P::operator[](0) = x;
113 P::operator[](1) = y;
114 P::operator[](2) = z;
118 class VectorD<4> :
public VectorBaseD<4> {
120 IMP_ALGEBRA_VECTOR_METHODS(4);
122 VectorD(
double x0,
double x1,
double x2,
double x3) {
123 P::operator[](0) = x0;
124 P::operator[](1) = x1;
125 P::operator[](2) = x2;
126 P::operator[](3) = x3;
130 class VectorD<5> :
public VectorBaseD<5> {
132 IMP_ALGEBRA_VECTOR_METHODS(5);
134 VectorD(
double x0,
double x1,
double x2,
double x3,
double x4) {
135 P::operator[](0) = x0;
136 P::operator[](1) = x1;
137 P::operator[](2) = x2;
138 P::operator[](3) = x3;
139 P::operator[](4) = x4;
143 class VectorD<6> :
public VectorBaseD<6> {
145 IMP_ALGEBRA_VECTOR_METHODS(6);
147 VectorD(
double x0,
double x1,
double x2,
double x3,
double x4,
double x5) {
148 P::operator[](0) = x0;
149 P::operator[](1) = x1;
150 P::operator[](2) = x2;
151 P::operator[](3) = x3;
152 P::operator[](4) = x4;
153 P::operator[](5) = x5;
160 inline std::ostream &operator<<(std::ostream &out, const VectorD<D> &v) {
168 inline std::istream &operator>>(std::istream &in, VectorD<D> &v) {
169 for (
unsigned int i = 0; i < D; ++i) {
185 "Dimensions don't match.");
186 for (
unsigned int i = 0; i < a.get_dimension(); ++i) {
189 else if (a[i] > b[i])
206 return (v1 - v2).get_squared_magnitude();
227 IMP_USAGE_CHECK(coordinate < D,
"There are only " << D <<
" basis vectors");
229 for (
unsigned int i = 0; i < D; ++i) {
243 return get_basis_vector_d<D>(coordinate);
251 "There are only " << D <<
" basis vectors");
252 boost::scoped_array<double> vs(
new double[D]);
253 for (
int i = 0; i < D; ++i) {
254 if (i == static_cast<int>(coordinate))
259 return VectorD<-1>(vs.get(), vs.get() + D);
272 for (
int i = 0; i < D; ++i) {
283 return get_zero_vector_d<D>();
291 return VectorD<-1>(vs.begin(), vs.end());
304 for (
unsigned int i = 0; i < D; ++i) {
316 return get_ones_vector_d<D>(v);
323 boost::scoped_array<double> vv(
new double[D]);
324 for (
unsigned int i = 0; i < D; ++i) {
327 return VectorD<-1>(vv.get(), vv.get() + D);
346 inline double get_l2_norm(
const VectorD<D> &v) {
347 return v.get_magnitude();
351 inline double get_l1_norm(
const VectorD<D> &v) {
352 double n = std::abs(v[0]);
353 for (
unsigned int i = 1; i < v.get_dimension(); ++i) {
360 inline double get_linf_norm(
const VectorD<D> &v) {
361 double n = std::abs(v[0]);
362 for (
unsigned int i = 1; i < v.get_dimension(); ++i) {
363 n = std::max(n, std::abs(v[i]));
374 const VectorD<D> &v_;
375 SpacesIO(
const VectorD<D> &v) : v_(v) {}
380 const VectorD<D> &v_;
381 CommasIO(
const VectorD<D> &v) : v_(v) {}
384 inline std::ostream &operator<<(std::ostream &out, const SpacesIO<D> &s) {
385 s.v_.show(out,
" ",
false);
389 inline std::ostream &operator<<(std::ostream &out, const CommasIO<D> &s) {
390 s.v_.show(out,
", ",
false);
400 inline SpacesIO<D> spaces_io(
const VectorD<D> &v) {
401 return SpacesIO<D>(v);
410 inline CommasIO<D> commas_io(
const VectorD<D> &v) {
411 return CommasIO<D>(v);
452 template <
class C,
class E>
464 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
475 IMP_USAGE_CHECK(a.size() == b.get_dimension(),
"Dimensions don't match,");
477 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
483 IMPALGEBRA_END_NAMESPACE
Vector< VectorD< 1 > > Vector1Ds
Vector< VectorD< 3 > > Vector3Ds
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Vector< VectorD< 5 > > Vector5Ds
Exception definitions and assertions.
Vector< VectorD< 2 > > Vector2Ds
const VectorD< C::DIMENSION > & get_vector_geometry(const C &g)
A Cartesian vector in D-dimensions.
#define IMP_UNUSED(variable)
VectorD< D > get_elementwise_product(const Ints &a, const algebra::VectorD< D > &b)
Return the vector that is the elementwise product of the two.
int compare(const VectorD< D > &a, const VectorD< D > &b)
lexicographic comparison of two vectors
VectorD< D > get_zero_vector_d()
Return a vector of zeros.
Vector< VectorD<-1 > > VectorKDs
VectorD< D > get_basis_vector_d(unsigned int coordinate)
Return the basis vector for the given coordinate.
void set_vector_geometry(C &g, const E &v)
Vector< VectorD< 6 > > Vector6Ds
Various general useful functions for IMP.
Vector< VectorD< 4 > > Vector4Ds
VectorD<-1 > get_ones_vector_kd(unsigned int D, double v)
Return a vector of ones (or another constant)
VectorD< D > get_ones_vector_d(double v=1)
Return a vector of ones (or another constant)
Helper macros for throwing and handling exceptions.
VectorD<-1 > get_basis_vector_kd(int D, unsigned int coordinate)
Return a dynamically sized basis vector.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
VectorD< D > operator*(double s, VectorD< D > o)
double get_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the distance between two vectors.
Random number generators used by IMP.
A Cartesian vector in D-dimensions.
VectorD<-1 > get_zero_vector_kd(int D)
Return a dynamically sized vector of zeros.