VectorN.hh
#ifndef RIVET_MATH_VECTORN
#define RIVET_MATH_VECTORN
#include "Rivet/Math/MathHeader.hh"
#include "Rivet/Math/MathUtils.hh"
#include "Rivet/Math/eigen/vector.h"
namespace Rivet {
template <size_t N>
class Vector;
template <size_t N>
class Matrix;
template <size_t N>
Vector<N> multiply(const Matrix<N>& a, const Vector<N>& b);
template <size_t N>
class Vector {
template <size_t M>
friend Vector<M> multiply(const Matrix<M>& a, const Vector<M>& b);
public:
Vector() { _vec.loadZero(); }
Vector(const Vector<N>& other)
: _vec(other._vec) { }
const double& get(const size_t index) const {
if (index >= N) {
throw std::runtime_error("Tried to access an invalid vector index.");
} else {
return _vec(index);
}
}
const double& operator[](const size_t index) const {
return get(index);
}
double& operator[](const size_t index) {
return get(index);
}
Vector<N>& set(const size_t index, const double value) {
if (index >= N) {
throw std::runtime_error("Tried to access an invalid vector index.");
} else {
_vec[index] = value;
}
return *this;
}
size_t size() const {
return N;
}
bool isZero(double tolerance=1E-5) const {
for (size_t i=0; i < N; ++i) {
if (! Rivet::isZero(_vec[i], tolerance) ) return false;
}
return true;
}
double mod2() const {
double mod2 = 0.0;
for (size_t i = 0; i < size(); ++i) {
const double element = get(i);
mod2 += element*element;
}
return mod2;
}
double mod() const {
const double norm = mod2();
assert(norm >= 0);
return sqrt(norm);
}
Vector<N> operator-() const {
Vector<N> rtn;
rtn._vec = -_vec;
return rtn;
}
bool operator==(const Vector<N>& a) const {
return _vec == a._vec;
}
bool operator!=(const Vector<N>& a) const {
return _vec != a._vec;
}
bool operator<(const Vector<N>& a) const {
return _vec < a._vec;
}
bool operator<=(const Vector<N>& a) const {
return _vec <= a._vec;
}
bool operator>(const Vector<N>& a) const {
return _vec > a._vec;
}
bool operator>=(const Vector<N>& a) const {
return _vec >= a._vec;
}
protected:
double& get(const size_t index) {
if (index >= N) {
throw std::runtime_error("Tried to access an invalid vector index.");
} else {
return _vec(index);
}
}
Eigen::Vector<double,N> _vec;
};
template <size_t N>
inline double mod2(const Vector<N>& v) {
return v.mod2();
}
template <size_t N>
inline double mod(const Vector<N>& v) {
return v.mod();
}
template <size_t N>
inline const string toString(const Vector<N>& v) {
ostringstream out;
out << "(";
for (size_t i = 0; i < v.size(); ++i) {
out << (fabs(v[i]) < 1E-30 ? 0.0 : v[i]);
if (i < v.size()-1) out << ", ";
}
out << ")";
return out.str();
}
template <size_t N>
inline std::ostream& operator<<(std::ostream& out, const Vector<N>& v) {
out << toString(v);
return out;
}
template <size_t N>
inline bool fuzzyEquals(const Vector<N>& va, const Vector<N>& vb, double tolerance=1E-5) {
for (size_t i = 0; i < N; ++i) {
const double a = va.get(i);
const double b = vb.get(i);
if (!Rivet::fuzzyEquals(a, b, tolerance)) return false;
}
return true;
}
template <size_t N>
inline bool isZero(const Vector<N>& v, double tolerance=1E-5) {
return v.isZero(tolerance);
}
}
#endif
Rivet
Definition: MC_JetAnalysis.hh:9
Rivet::Vector::operator-
Vector< N > operator-() const
Invert the vector.
Definition: VectorN.hh:94
Rivet::Vector::isZero
bool isZero(double tolerance=1E-5) const
Check for nullness, allowing for numerical precision.
Definition: VectorN.hh:67
Rivet::Vector::operator[]
double & operator[](const size_t index)
Direct access to vector elements by index.
Definition: VectorN.hh:47
Rivet::Vector::mod
double mod() const
Calculate the modulus of a vector. .
Definition: VectorN.hh:87
Rivet::Vector::set
Vector< N > & set(const size_t index, const double value)
Set indexed value.
Definition: VectorN.hh:52
Rivet::Vector::size
size_t size() const
Vector dimensionality.
Definition: VectorN.hh:62
Rivet::Matrix
General -dimensional mathematical matrix object.
Definition: MatrixN.hh:14
Rivet::isZero
bool isZero(double val, double tolerance=1E-8)
Definition: MathUtils.hh:17
Rivet::Vector
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
Rivet::mod2
double mod2(const Vector< N > &v)
Definition: VectorN.hh:143
Rivet::Vector::operator[]
const double & operator[](const size_t index) const
Direct access to vector elements by index.
Definition: VectorN.hh:42
Rivet::mod
double mod(const Vector< N > &v)
Definition: VectorN.hh:150
Rivet::Vector::mod2
double mod2() const
Calculate the modulus-squared of a vector. .
Definition: VectorN.hh:76
Rivet::toString
std::string toString(const AnalysisInfo &ai)
String representation.
Definition: AnalysisInfo.cc:234
Rivet::fuzzyEquals
bool fuzzyEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for equality with a degree of fuzziness.
Definition: MathUtils.hh:34