52#ifndef MATH_VECTOR_HPP
53#define MATH_VECTOR_HPP
63 static constexpr RealType epsilon = 0.000001;
66 inline bool equal(T e1, T e2) {
67 if constexpr (std::is_same_v<T, RealType>)
68 return std::fabs(e1 - e2) < epsilon;
77 template<
typename Real,
unsigned int Dim>
80 using ElemType = Real;
81 using ElemPoinerType = Real*;
85 for (
unsigned int i = 0; i < Dim; i++)
94 if (
this == &v)
return *
this;
96 for (
unsigned int i = 0; i < Dim; i++)
97 this->data_[i] = v[i];
104 for (
unsigned int i = 0; i < Dim; i++)
105 this->data_[i] = v[i];
112 inline Vector(
const Real& s) {
113 for (
unsigned int i = 0; i < Dim; i++)
119 for (
unsigned int i = 0; i < Dim; i++)
120 this->data_[i] = v[i];
130 return this->data_[i];
140 return this->data_[i];
150 return this->data_[i];
160 return this->data_[i];
165 for (
unsigned int i = 0; i < Dim; i++) {
166 array[i] = this->data_[i];
179 for (
unsigned int i = 0; i < Dim; i++) {
180 if (!
equal(this->data_[i], v[i])) {
return false; }
195 for (
unsigned int i = 0; i < Dim; i++)
201 for (
unsigned int i = 0; i < Dim; i++)
202 this->data_[i] = -this->data_[i];
210 for (
unsigned int i = 0; i < Dim; i++)
211 this->data_[i] = -v1.data_[i];
219 for (
unsigned int i = 0; i < Dim; i++)
220 this->data_[i] += v1.data_[i];
229 for (
unsigned int i = 0; i < Dim; i++)
230 this->data_[i] = v1.data_[i] + v2.data_[i];
239 for (
unsigned int i = 0; i < Dim; i++)
240 this->data_[i] -= v1.data_[i];
250 for (
unsigned int i = 0; i < Dim; i++)
251 this->data_[i] = v1.data_[i] - v2.data_[i];
260 for (
unsigned int i = 0; i < Dim; i++)
271 for (
unsigned int i = 0; i < Dim; i++)
272 this->data_[i] = s * v1.data_[i];
285 for (
unsigned int i = 0; i < Dim; i++)
286 this->data_[i] = v1.data_[i] * v2.data_[i];
291 for (
unsigned int i = 0; i < Dim; i++) {
292 this->data_[i] = std::abs(this->data_[i]);
299 Real val = this->data_[0];
300 for (
unsigned int i = 0; i < Dim; i++) {
301 if (this->data_[i] > val) val = this->data_[i];
312 for (
unsigned int i = 0; i < Dim; i++)
323 for (
unsigned int i = 0; i < Dim; i++)
324 this->data_[i] = v1.data_[i] / s;
337 for (
unsigned int i = 0; i < Dim; i++)
338 this->data_[i] = v1.data_[i] / v2.data_[i];
372 for (
unsigned int i = 0; i < Dim; i++)
373 tmp += this->data_[i];
384 for (
unsigned int i = 0; i < Dim; i++)
385 tmp *= this->data_[i];
419 unsigned int size()
const {
return Dim; }
426 template<
typename Real,
unsigned int Dim>
439 template<
typename Real,
unsigned int Dim>
454 template<
typename Real,
unsigned int Dim>
468 template<
typename Real,
unsigned int Dim>
481 template<
typename Real,
unsigned int Dim>
494 template<
typename Real,
unsigned int Dim>
507 template<
typename Real,
unsigned int Dim>
512 for (
unsigned int i = 0; i < Dim; i++)
513 tmp += v1[i] * v2[i];
527 template<
typename Real,
unsigned int Dim>
533 for (
unsigned int i = 0; i < Dim; i++)
534 tmp += v1[i] * v2[i] * v3[i];
545 template<
typename Real,
unsigned int Dim>
549 return tempVector.
length();
558 template<
typename Real,
unsigned int Dim>
568 template<
typename Real,
unsigned int Dim>
572 for (
unsigned int i = 0; i < Dim; i++) {
575 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.
Real length()
Returns the length of this vector.
void getArray(Real *array)
Copy the internal data to an array.
void normalize()
Normalizes this vector in place.
Real & operator()(unsigned int i)
Returns reference of ith element.
Real componentProduct()
Returns the product of all elements of this vector.
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.
bool operator!=(const Vector< Real, Dim > &v)
Tests if this vetor is not equal to other vector.
Real sum()
Returns the sum of all elements of this 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)
Real * getArrayPointer()
Returns the pointer of internal array.
bool isNormalized()
Tests if this vector is normalized.
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 lengthSquare()
Returns the squared length of this vector.
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)
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)
Vector(Real *v)
Constructs and initializes a Vector from an array.
Vector()
default constructor
Vector< Real, Dim > & operator+=(const Vector< Real, Dim > &v1)
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).