Skip to content

Commit

Permalink
Only check VectorKD for same dimension at runtime
Browse files Browse the repository at this point in the history
When combining two VectorD, we only need to check
that the dimensions are the same at runtime if D==-1
(i.e. a variable-sized vector, VectorKD) since
otherwise this is enforced at compile time.
  • Loading branch information
benmwebb committed Feb 1, 2024
1 parent 8e8ca3f commit 5972a94
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
19 changes: 13 additions & 6 deletions modules/algebra/include/VectorBaseD.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@

#if IMP_HAS_CHECKS >= IMP_INTERNAL
#define IMP_ALGEBRA_VECTOR_CHECK check_vector()
#define IMP_ALGEBRA_VECTOR_CHECK_OTHER(o) o.check_vector()
#else
#define IMP_ALGEBRA_VECTOR_CHECK
#define IMP_ALGEBRA_VECTOR_CHECK_OTHER(o)
#endif

/* Should only need to check for "compatible" (same dimension) vectors
when using variable-dimension vectors; otherwise, it is checked at
compile time */
#if IMP_HAS_CHECKS >= IMP_USAGE
#define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
#define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
check_compatible_vector(o); \
o.check_vector()
if (D == -1) { check_compatible_vector(o); }
#else
#define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
#define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
Expand Down Expand Up @@ -128,9 +132,10 @@ class VectorBaseD : public GeometricPrimitiveD<D> {
//! Default constructor
VectorBaseD() {}

double get_scalar_product(const VectorBaseD<D> &o) const {
IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
double get_scalar_product(const VectorBaseD &o) const {
IMP_ALGEBRA_VECTOR_CHECK;
IMP_ALGEBRA_VECTOR_CHECK_OTHER(o);
IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
double ret = 0;
for (unsigned int i = 0; i < get_dimension(); ++i) {
ret += operator[](i) * o.operator[](i);
Expand Down Expand Up @@ -158,17 +163,19 @@ class VectorBaseD : public GeometricPrimitiveD<D> {
}

VectorBaseD &operator+=(const VectorBaseD &o) {
IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
IMP_ALGEBRA_VECTOR_CHECK;
IMP_ALGEBRA_VECTOR_CHECK_OTHER(o);
IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
for (unsigned int i = 0; i < get_dimension(); ++i) {
operator[](i) += o[i];
}
return *this;
}

VectorBaseD &operator-=(const VectorBaseD &o) {
IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
IMP_ALGEBRA_VECTOR_CHECK;
IMP_ALGEBRA_VECTOR_CHECK_OTHER(o);
IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o);
for (unsigned int i = 0; i < get_dimension(); ++i) {
operator[](i) -= o[i];
}
Expand Down
16 changes: 0 additions & 16 deletions modules/algebra/include/VectorD.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@
#include <cmath>
#include <boost/random/normal_distribution.hpp>

#if IMP_HAS_CHECKS >= IMP_INTERNAL
#define IMP_ALGEBRA_VECTOR_CHECK check_vector()
#else
#define IMP_ALGEBRA_VECTOR_CHECK
#endif

#if IMP_HAS_CHECKS >= IMP_USAGE
#define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i) check_index(i)
#define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o) \
check_compatible_vector(o); \
o.check_vector()
#else
#define IMP_ALGEBRA_VECTOR_CHECK_INDEX(i)
#define IMP_ALGEBRA_VECTOR_CHECK_COMPATIBLE(o)
#endif

IMPALGEBRA_BEGIN_NAMESPACE
//! A Cartesian vector in D-dimensions.
/** Store a vector of Cartesian coordinates. It supports all expected
Expand Down

0 comments on commit 5972a94

Please sign in to comment.