52#ifndef MATH_DYNAMICVECTOR_HPP
53#define MATH_DYNAMICVECTOR_HPP
58#include <initializer_list>
70 template<
typename Real,
typename Alloc = std::allocator<Real>>
73 using value_type = Real;
74 using allocator_type = Alloc;
75 using VectorType = std::vector<Real, Alloc>;
76 using size_type =
typename VectorType::size_type;
77 using difference_type =
typename VectorType::difference_type;
78 using reference =
typename VectorType::reference;
79 using const_reference =
typename VectorType::const_reference;
80 using pointer =
typename VectorType::pointer;
81 using const_pointer =
typename VectorType::const_pointer;
82 using iterator =
typename VectorType::iterator;
83 using const_iterator =
typename VectorType::const_iterator;
84 using reverse_iterator =
typename VectorType::reverse_iterator;
85 using const_reverse_iterator =
typename VectorType::const_reverse_iterator;
91 explicit DynamicVector(
const allocator_type& alloc = allocator_type()) :
103 const allocator_type& alloc = allocator_type()) :
104 data_(n, value, alloc) {}
115 const allocator_type& alloc = allocator_type()) :
124 template<
typename InputIterator>
126 const allocator_type& alloc = allocator_type()) :
127 data_(first, last, alloc) {}
135 const allocator_type& alloc = allocator_type()) :
136 data_(init, alloc) {}
139 reference operator[](size_type i) {
return data_[i]; }
140 const_reference operator[](size_type i)
const {
return data_[i]; }
142 reference operator()(size_type i) {
return data_[i]; }
143 const_reference operator()(size_type i)
const {
return data_[i]; }
146 iterator begin() noexcept {
return data_.begin(); }
147 const_iterator begin() const noexcept {
return data_.begin(); }
148 const_iterator cbegin() const noexcept {
return data_.cbegin(); }
150 iterator end() noexcept {
return data_.end(); }
151 const_iterator end() const noexcept {
return data_.end(); }
152 const_iterator cend() const noexcept {
return data_.cend(); }
155 bool empty() const noexcept {
return data_.empty(); }
156 size_type size() const noexcept {
return data_.size(); }
159 void resize(size_type n) {
return data_.resize(n); }
160 void resize(size_type n,
const value_type& value) {
161 data_.resize(n, value);
164 void reserve(size_type new_cap) { data_.reserve(new_cap); }
172 if (this->size() != v.size())
return false;
175 this->begin(), this->end(), v.begin(),
176 [](Real val1, Real val2) { return OpenMD::equal(val1, val2); });
188 std::transform(this->begin(), this->end(), this->begin(),
189 [](Real val) {
return -val; });
197 std::transform(v1.begin(), v1.end(), this->begin(),
198 [](Real val) { return -val; });
206 std::transform(this->begin(), this->end(), v1.begin(), this->begin(),
207 [](Real val1, Real val2) { return val1 + val2; });
216 std::transform(v1.begin(), v1.end(), v2.begin(), this->begin(),
217 [](Real val1, Real val2) { return val1 + val2; });
226 std::transform(this->begin(), this->end(), v1.begin(), this->begin(),
227 [](Real val1, Real val2) { return val1 - val2; });
237 std::transform(v1.begin(), v1.end(), v2.begin(), this->begin(),
238 [](Real val1, Real val2) { return val1 - val2; });
247 std::transform(this->begin(), this->end(), this->begin(),
248 [s](Real val) {
return val * s; });
258 if (this->size() != v1.size()) this->resize(v1.size());
260 std::transform(v1.begin(), v1.end(), this->begin(),
261 [s](Real val) { return val * s; });
270 std::transform(this->begin(), this->end(), this->begin(),
271 [s](Real val) {
return val / s; });
281 if (this->size() != v1.size()) this->resize(v1.size());
283 std::transform(v1.begin(), v1.end(), this->begin(),
284 [s](Real val) { return val / s; });
312 void setZero() { std::fill(this->begin(), this->end(), 0); }
342 template<
class VectorType>
343 void getSubVector(size_type beginning, VectorType& v) {
344 assert(beginning + v.size() - 1 <= this->size());
346 for (size_type i {}; i < v.size(); ++i)
347 v(i) = (*this)[beginning + i];
351 std::vector<Real, Alloc> data_;
355 template<
typename Real>
368 template<
typename Real>
371 assert(v1.size() == v2.size());
383 template<
typename Real>
386 assert(v1.size() == v2.size());
398 template<
typename Real>
411 template<
typename Real>
424 template<
typename Real>
437 template<
typename Real>
442 assert(v1.size() == v2.size());
443 for (
typename DynamicVector<Real>::size_type i {}; i < v1.size(); i++)
444 tmp += v1[i] * v2[i];
455 template<
typename Real>
459 return tempDynamicVector.
length();
468 template<
typename Real>
478 template<
typename Real>
482 std::for_each(v.begin(), v.end() - 1,
483 [&strm](
auto elem) { strm << elem <<
", "; });
485 strm << *(v.end() - 1) <<
" ]";
Dynamically-sized vector class.
DynamicVector< Real > & operator-=(const DynamicVector< Real > &v1)
void setZero()
zero out the 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).