55#ifndef MATH_DYNAMICVECTOR_HPP
56#define MATH_DYNAMICVECTOR_HPP
61#include <initializer_list>
73 template<
typename Real,
typename Alloc = std::allocator<Real>>
76 using value_type = Real;
77 using allocator_type = Alloc;
78 using VectorType = std::vector<Real, Alloc>;
79 using size_type =
typename VectorType::size_type;
80 using difference_type =
typename VectorType::difference_type;
81 using reference =
typename VectorType::reference;
82 using const_reference =
typename VectorType::const_reference;
83 using pointer =
typename VectorType::pointer;
84 using const_pointer =
typename VectorType::const_pointer;
85 using iterator =
typename VectorType::iterator;
86 using const_iterator =
typename VectorType::const_iterator;
87 using reverse_iterator =
typename VectorType::reverse_iterator;
88 using const_reverse_iterator =
typename VectorType::const_reverse_iterator;
94 explicit DynamicVector(
const allocator_type& alloc = allocator_type()) :
106 const allocator_type& alloc = allocator_type()) :
107 data_(n, value, alloc) {}
118 const allocator_type& alloc = allocator_type()) :
127 template<
typename InputIterator>
129 const allocator_type& alloc = allocator_type()) :
130 data_(first, last, alloc) {}
138 const allocator_type& alloc = allocator_type()) :
139 data_(init, alloc) {}
142 reference operator[](size_type i) {
return data_[i]; }
143 const_reference operator[](size_type i)
const {
return data_[i]; }
145 reference operator()(size_type i) {
return data_[i]; }
146 const_reference operator()(size_type i)
const {
return data_[i]; }
149 iterator begin() noexcept {
return data_.begin(); }
150 const_iterator begin() const noexcept {
return data_.begin(); }
151 const_iterator cbegin() const noexcept {
return data_.cbegin(); }
153 iterator end() noexcept {
return data_.end(); }
154 const_iterator end() const noexcept {
return data_.end(); }
155 const_iterator cend() const noexcept {
return data_.cend(); }
158 bool empty() const noexcept {
return data_.empty(); }
159 size_type size() const noexcept {
return data_.size(); }
162 void resize(size_type n) {
return data_.resize(n); }
163 void resize(size_type n,
const value_type& value) {
164 data_.resize(n, value);
167 void reserve(size_type new_cap) { data_.reserve(new_cap); }
175 if (this->size() != v.size())
return false;
178 this->begin(), this->end(), v.begin(),
179 [](Real val1, Real val2) { return OpenMD::equal(val1, val2); });
191 std::transform(this->begin(), this->end(), this->begin(),
192 [](Real val) {
return -val; });
200 std::transform(v1.begin(), v1.end(), this->begin(),
201 [](Real val) { return -val; });
209 std::transform(this->begin(), this->end(), v1.begin(), this->begin(),
210 [](Real val1, Real val2) { return val1 + val2; });
219 std::transform(v1.begin(), v1.end(), v2.begin(), this->begin(),
220 [](Real val1, Real val2) { return val1 + val2; });
229 std::transform(this->begin(), this->end(), v1.begin(), this->begin(),
230 [](Real val1, Real val2) { return val1 - val2; });
240 std::transform(v1.begin(), v1.end(), v2.begin(), this->begin(),
241 [](Real val1, Real val2) { return val1 - val2; });
250 std::transform(this->begin(), this->end(), this->begin(),
251 [s](Real val) {
return val * s; });
261 if (this->size() != v1.size()) this->resize(v1.size());
263 std::transform(v1.begin(), v1.end(), this->begin(),
264 [s](Real val) { return val * s; });
273 std::transform(this->begin(), this->end(), this->begin(),
274 [s](Real val) {
return val / s; });
284 if (this->size() != v1.size()) this->resize(v1.size());
286 std::transform(v1.begin(), v1.end(), this->begin(),
287 [s](Real val) { return val / s; });
315 void setZero() { std::fill(this->begin(), this->end(), 0); }
332 std::transform(this->begin(), this->end(), result.begin(),
333 [](Real val) { return std::abs(val); });
338 Real
max()
const {
return *std::max_element(this->begin(), this->end()); }
356 template<
class VectorType>
357 void getSubVector(size_type beginning, VectorType& v) {
358 assert(beginning + v.size() - 1 <= this->size());
360 for (size_type i {}; i < v.size(); ++i)
361 v(i) = (*this)[beginning + i];
365 std::vector<Real, Alloc> data_;
369 template<
typename Real>
382 template<
typename Real>
385 assert(v1.size() == v2.size());
397 template<
typename Real>
400 assert(v1.size() == v2.size());
412 template<
typename Real>
425 template<
typename Real>
438 template<
typename Real>
451 template<
typename Real>
456 assert(v1.size() == v2.size());
457 for (
typename DynamicVector<Real>::size_type i {}; i < v1.size(); i++)
458 tmp += v1[i] * v2[i];
469 template<
typename Real>
473 return tempDynamicVector.
length();
482 template<
typename Real>
492 template<
typename Real>
496 std::for_each(v.begin(), v.end() - 1,
497 [&strm](
auto elem) { strm << elem <<
", "; });
499 strm << *(v.end() - 1) <<
" ]";
DynamicVector(const allocator_type &alloc=allocator_type())
Default constructor creates no elements.
Dynamically-sized vector class.
DynamicVector< Real > & operator-=(const DynamicVector< Real > &v1)
DynamicVector< Real > abs() const
Returns a vector containing the absolute value of each element.
void setZero()
zero out the vector
Real max() const
Returns the largest element of this vector.
DynamicVector(size_type n, const value_type &value, const allocator_type &alloc=allocator_type())
Create a DynamicVector with copies of an exemplar element.
void negate(const DynamicVector< Real > &v1)
Sets the value of this vector to the negation of vector v1.
DynamicVector(const allocator_type &alloc=allocator_type())
Default constructor creates no elements.
void negate()
Negates the value of this vector in place.
void normalize()
Normalizes this vector in place.
void add(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
DynamicVector< Real > & operator+=(const DynamicVector< Real > &v1)
void sub(const DynamicVector< Real > &v1)
Sets the value of this vector to the difference of itself and v1 (*this -= v1).
DynamicVector(InputIterator first, InputIterator last, const allocator_type &alloc=allocator_type())
Create a DynamicVector using an iterator range.
void sub(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2).
void mul(Real s)
Sets the value of this vector to the scalar multiplication of itself (*this *= s).
DynamicVector(size_type n, const allocator_type &alloc=allocator_type())
Create a DynamicVector with default elements.
void div(Real s)
Sets the value of this vector to the scalar division of itself (*this /= s).
bool operator==(const DynamicVector< Real > &v)
Tests if this vetor is equal to other vector.
void div(const DynamicVector< Real > &v1, Real s)
Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s).
DynamicVector(std::initializer_list< value_type > init, const allocator_type &alloc=allocator_type())
Create a DynamicVector with the contents of an initializer_list.
Real lengthSquare()
Returns the squared length of this vector.
DynamicVector< Real > & operator*=(Real s)
Real length()
Returns the length of this vector.
bool isNormalized()
Tests if this vector is normalized.
void mul(const DynamicVector< Real > &v1, Real s)
Sets the value of this vector to the scalar multiplication of vector v1 (*this = s * v1).
void add(const DynamicVector< Real > &v1)
Sets the value of this vector to the sum of itself and v1 (*this += v1).
bool operator!=(const DynamicVector< Real > &v)
Tests if this vetor is not equal to other vector.
DynamicVector< Real > & operator/=(Real s)
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).