55#ifndef MATH_VECTOR_HPP
56#define MATH_VECTOR_HPP
66 static constexpr RealType epsilon = 0.000001;
69 inline bool equal(T e1, T e2) {
70 if constexpr (std::is_same_v<T, RealType>)
71 return std::fabs(e1 - e2) < epsilon;
80 template<
typename Real,
unsigned int Dim>
83 using ElemType = Real;
84 using ElemPoinerType = Real*;
88 for (
unsigned int i = 0; i < Dim; i++)
97 if (
this == &v)
return *
this;
99 for (
unsigned int i = 0; i < Dim; i++)
100 this->data_[i] = v[i];
107 for (
unsigned int i = 0; i < Dim; i++)
108 this->data_[i] = v[i];
115 inline Vector(
const Real& s) {
116 for (
unsigned int i = 0; i < Dim; i++)
122 for (
unsigned int i = 0; i < Dim; i++)
123 this->data_[i] = v[i];
133 return this->data_[i];
143 return this->data_[i];
153 return this->data_[i];
163 return this->data_[i];
168 for (
unsigned int i = 0; i < Dim; i++) {
169 array[i] = this->data_[i];
183 for (
unsigned int i = 0; i < Dim; i++) {
184 if (!
equal(this->data_[i], v[i])) {
return false; }
199 for (
unsigned int i = 0; i < Dim; i++)
205 for (
unsigned int i = 0; i < Dim; i++)
206 this->data_[i] = -this->data_[i];
214 for (
unsigned int i = 0; i < Dim; i++)
215 this->data_[i] = -v1.data_[i];
223 for (
unsigned int i = 0; i < Dim; i++)
224 this->data_[i] += v1.data_[i];
233 for (
unsigned int i = 0; i < Dim; i++)
234 this->data_[i] = v1.data_[i] + v2.data_[i];
243 for (
unsigned int i = 0; i < Dim; i++)
244 this->data_[i] -= v1.data_[i];
254 for (
unsigned int i = 0; i < Dim; i++)
255 this->data_[i] = v1.data_[i] - v2.data_[i];
264 for (
unsigned int i = 0; i < Dim; i++)
275 for (
unsigned int i = 0; i < Dim; i++)
276 this->data_[i] = s * v1.data_[i];
289 for (
unsigned int i = 0; i < Dim; i++)
290 this->data_[i] = v1.data_[i] * v2.data_[i];
295 for (
unsigned int i = 0; i < Dim; i++) {
296 this->data_[i] = std::abs(this->data_[i]);
302 inline Real max()
const {
303 Real val = this->data_[0];
304 for (
unsigned int i = 0; i < Dim; i++) {
305 if (this->data_[i] > val) val = this->data_[i];
316 for (
unsigned int i = 0; i < Dim; i++)
327 for (
unsigned int i = 0; i < Dim; i++)
328 this->data_[i] = v1.data_[i] / s;
341 for (
unsigned int i = 0; i < Dim; i++)
342 this->data_[i] = v1.data_[i] / v2.data_[i];
376 for (
unsigned int i = 0; i < Dim; i++)
377 tmp += this->data_[i];
388 for (
unsigned int i = 0; i < Dim; i++)
389 tmp *= this->data_[i];
420 unsigned int size()
const {
return Dim; }
427 template<
typename Real,
unsigned int Dim>
440 template<
typename Real,
unsigned int Dim>
455 template<
typename Real,
unsigned int Dim>
469 template<
typename Real,
unsigned int Dim>
482 template<
typename Real,
unsigned int Dim>
495 template<
typename Real,
unsigned int Dim>
508 template<
typename Real,
unsigned int Dim>
513 for (
unsigned int i = 0; i < Dim; i++)
514 tmp += v1[i] * v2[i];
528 template<
typename Real,
unsigned int Dim>
534 for (
unsigned int i = 0; i < Dim; i++)
535 tmp += v1[i] * v2[i] * v3[i];
546 template<
typename Real,
unsigned int Dim>
550 return tempVector.
length();
559 template<
typename Real,
unsigned int Dim>
569 template<
typename Real,
unsigned int Dim>
573 for (
unsigned int i = 0; i < Dim; i++) {
576 if (i != Dim - 1) { o <<
", "; }
Vector< Real, Dim > & operator=(const Vector< Real, Dim > &v)
copy assignment operator
Vector< Real, Dim > & operator=(const Real *v)
array assignment operator
void div(const Vector< Real, Dim > &v1, Real s)
Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ).
const Real & operator()(unsigned int i) const
Returns constant reference of ith element.
void div(Real s)
Sets the value of this vector to the scalar division of itself (*this /= s ).
void zero()
Zeros out the values in this vector in place.
const Real * getArrayPointer() const
Returns the pointer of internal array.
void normalize()
Normalizes this vector in place.
Real & operator()(unsigned int i)
Returns reference of ith element.
void mul(Real s)
Sets the value of this vector to the scalar multiplication of itself (*this *= s).
Vector(const Vector< Real, Dim > &v)
Constructs and initializes a Vector from a vector.
Real sum() const
Returns the sum of all elements of this vector.
bool operator!=(const Vector< Real, Dim > &v)
Tests if this vetor is not equal to other vector.
void sub(const Vector< Real, Dim > &v1)
Sets the value of this vector to the difference of itself and v1 (*this -= v1).
void Vmul(const Vector< Real, Dim > &v1, const Vector< Real, Dim > &v2)
Sets the elements of this vector to the multiplication of elements of two other vectors.
Vector< Real, Dim > & operator*=(Real s)
void sub(const Vector< Real, Dim > &v1, const Vector &v2)
Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2).
Real length() const
Returns the length of this vector.
void getArray(Real *array) const
Copy the internal data to an array.
void negate(const Vector< Real, Dim > &v1)
Sets the value of this vector to the negation of vector v1.
Real & operator[](unsigned int i)
Returns reference of ith element.
const Real & operator[](unsigned int i) const
Returns constant reference of ith element.
bool operator==(const Vector< Real, Dim > &v)
Tests if this vetor is equal to other vector.
Vector< Real, Dim > & operator-=(const Vector< Real, Dim > &v1)
Real lengthSquare() const
Returns the squared length of this vector.
void add(const Vector< Real, Dim > &v1, const Vector< Real, Dim > &v2)
Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
void mul(const Vector< Real, Dim > &v1, Real s)
Sets the value of this vector to the scalar multiplication of vector v1 (*this = s * v1).
void negate()
Negates the value of this vector in place.
void Vdiv(const Vector< Real, Dim > &v1, const Vector< Real, Dim > &v2)
Sets the elements of this vector to the division of elements of two other vectors.
void add(const Vector< Real, Dim > &v1)
Sets the value of this vector to the sum of itself and v1 (*this += v1).
Vector< Real, Dim > & operator/=(Real s)
Real componentProduct() const
Returns the product of all elements of this vector.
Vector(Real *v)
Constructs and initializes a Vector from an array.
Vector()
default constructor
Vector< Real, Dim > & operator+=(const Vector< Real, Dim > &v1)
bool isNormalized() const
Tests if this vector is normalized.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
DynamicRectMatrix< Real > operator-(const DynamicRectMatrix< Real > &m)
Negate the value of every element of this matrix.
bool equal(const Polynomial< Real > &p1, const Polynomial< Real > &p2)
Tests if two polynomial have the same exponents.
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.
DynamicRectMatrix< Real > operator*(const DynamicRectMatrix< Real > &m, Real s)
Return the multiplication of scalar and matrix (m * s).
Real distanceSquare(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the squared distance between two DynamicVectors.
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.
DynamicRectMatrix< Real > operator/(const DynamicRectMatrix< Real > &m, Real s)
Return the scalar division of matrix (m / s).