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_USAGE
31 #define IMP_ALGEBRA_VECTOR_CHECK check_vector()
32 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
33 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
34 check_compatible_vector(o); \
37 #define IMP_ALGEBRA_VECTOR_CHECK
38 #define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
39 #define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
42 IMPALGEBRA_BEGIN_NAMESPACE
55 IMP_ALGEBRA_VECTOR_METHODS(D);
60 std::vector<double> get_range(
double x0,
double x1,
double x2,
double x3,
61 double x4,
double x5) {
64 std::vector<double> ret;
66 if (x1 != std::numeric_limits<double>::max()) ret.push_back(x1);
67 if (x2 != std::numeric_limits<double>::max()) ret.push_back(x2);
68 if (x3 != std::numeric_limits<double>::max()) ret.push_back(x3);
69 if (x4 != std::numeric_limits<double>::max()) ret.push_back(x4);
70 if (x5 != std::numeric_limits<double>::max()) ret.push_back(x5);
75 IMP_ALGEBRA_VECTOR_METHODS(-1);
76 explicit VectorD(
double x0,
double x1 = std::numeric_limits<double>::max(),
77 double x2 = std::numeric_limits<double>::max(),
78 double x3 = std::numeric_limits<double>::max(),
79 double x4 = std::numeric_limits<double>::max(),
80 double x5 = std::numeric_limits<double>::max())
81 : P(get_range(x0, x1, x2, x3, x4, x5)) {}
85 class VectorD<1> :
public VectorBaseD<1> {
87 IMP_ALGEBRA_VECTOR_METHODS(1);
90 VectorD(
double x) { P::operator[](0) = x; }
93 class VectorD<2> :
public VectorBaseD<2> {
95 IMP_ALGEBRA_VECTOR_METHODS(2);
97 VectorD(
double x,
double y) {
103 class VectorD<3> :
public VectorBaseD<3> {
105 IMP_ALGEBRA_VECTOR_METHODS(3);
107 VectorD(
double x,
double y,
double z) {
108 P::operator[](0) = x;
109 P::operator[](1) = y;
110 P::operator[](2) = z;
114 class VectorD<4> :
public VectorBaseD<4> {
116 IMP_ALGEBRA_VECTOR_METHODS(4);
118 VectorD(
double x0,
double x1,
double x2,
double x3) {
119 P::operator[](0) = x0;
120 P::operator[](1) = x1;
121 P::operator[](2) = x2;
122 P::operator[](3) = x3;
126 class VectorD<5> :
public VectorBaseD<5> {
128 IMP_ALGEBRA_VECTOR_METHODS(5);
130 VectorD(
double x0,
double x1,
double x2,
double x3,
double x4) {
131 P::operator[](0) = x0;
132 P::operator[](1) = x1;
133 P::operator[](2) = x2;
134 P::operator[](3) = x3;
135 P::operator[](4) = x4;
139 class VectorD<6> :
public VectorBaseD<6> {
141 IMP_ALGEBRA_VECTOR_METHODS(6);
143 VectorD(
double x0,
double x1,
double x2,
double x3,
double x4,
double x5) {
144 P::operator[](0) = x0;
145 P::operator[](1) = x1;
146 P::operator[](2) = x2;
147 P::operator[](3) = x3;
148 P::operator[](4) = x4;
149 P::operator[](5) = x5;
156 inline std::ostream &operator<<(std::ostream &out, const VectorD<D> &v) {
164 inline std::istream &operator>>(std::istream &in, VectorD<D> &v) {
165 for (
unsigned int i = 0; i < D; ++i) {
181 "Dimensions don't match.");
182 for (
unsigned int i = 0; i < a.get_dimension(); ++i) {
185 else if (a[i] > b[i])
202 return (v1 - v2).get_squared_magnitude();
223 IMP_USAGE_CHECK(coordinate < D,
"There are only " << D <<
" basis vectors");
225 for (
unsigned int i = 0; i < D; ++i) {
238 "There are only " << D <<
" basis vectors");
239 boost::scoped_array<double> vs(
new double[D]);
240 for (
int i = 0; i < D; ++i) {
241 if (i == static_cast<int>(coordinate))
246 return VectorD<-1>(vs.get(), vs.get() + D);
254 for (
int i = 0; i < D; ++i) {
265 return get_zero_vector_d<D>();
272 return VectorD<-1>(vs.begin(), vs.end());
280 for (
unsigned int i = 0; i < D; ++i) {
292 return get_ones_vector_d<D>(v);
298 boost::scoped_array<double> vv(
new double[D]);
299 for (
unsigned int i = 0; i < D; ++i) {
302 return VectorD<-1>(vv.get(), vv.get() + D);
316 inline double get_l2_norm(
const VectorD<D> &v) {
317 return v.get_magnitude();
321 inline double get_l1_norm(
const VectorD<D> &v) {
322 double n = std::abs(v[0]);
323 for (
unsigned int i = 1; i < v.get_dimension(); ++i) {
330 inline double get_linf_norm(
const VectorD<D> &v) {
331 double n = std::abs(v[0]);
332 for (
unsigned int i = 1; i < v.get_dimension(); ++i) {
333 n = std::max(n, std::abs(v[i]));
344 const VectorD<D> &v_;
345 SpacesIO(
const VectorD<D> &v) : v_(v) {}
350 const VectorD<D> &v_;
351 CommasIO(
const VectorD<D> &v) : v_(v) {}
354 inline std::ostream &operator<<(std::ostream &out, const SpacesIO<D> &s) {
355 s.v_.show(out,
" ",
false);
359 inline std::ostream &operator<<(std::ostream &out, const CommasIO<D> &s) {
360 s.v_.show(out,
", ",
false);
370 inline SpacesIO<D> spaces_io(
const VectorD<D> &v) {
371 return SpacesIO<D>(v);
380 inline CommasIO<D> commas_io(
const VectorD<D> &v) {
381 return CommasIO<D>(v);
422 template <
class C,
class E>
434 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
445 IMP_USAGE_CHECK(a.size() == b.get_dimension(),
"Dimensions don't match,");
447 for (
unsigned int i = 0; i < ret.get_dimension(); ++i) {
453 IMPALGEBRA_END_NAMESPACE
VectorD< D > get_zero_vector_kd(int Di)
Return a dynamically sized vector of zeros.
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.
VectorD< D > get_ones_vector_kd(unsigned int Di, double v=1)
Return a vector of ones (or another constant)
VectorD<-1 > get_basis_vector_kd(int D, unsigned int coordinate)
Return a dynamically sized basis vector.
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
For backwards compatibility.
Vector< VectorD< 4 > > Vector4Ds
VectorD< D > get_ones_vector_d(double v=1)
Return a vector of ones (or another constant)
Exception definitions and assertions.
#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.