6 _vec(std::vector<T>(n)) {
17 for (i = 0; i < n; i++)
18 _vec.push_back(vec[i]);
22 _vec(std::vector<T>(n)) {
24 std::fill(
_vec.begin(),
_vec.end(), value);
45 for (i = 0; i < size(); i++)
59 for (i = 0; i < _vec.size(); i++)
60 sum[i] = _vec[i]+vec[i];
72 for (i = 0; i < _vec.size(); i++)
84 for (i = 0; i < _vec.size(); i++)
85 diff[i] = _vec[i]-vec[i];
97 for (i = 0; i < _vec.size(); i++)
109 for (i = 0; i < _vec.size(); i++)
110 prod += _vec[i]*vec[i];
120 for (i = 0; i < _vec.
size(); i++)
121 prod[i] = factor*_vec[i];
130 for (i = 0; i < _vec.size(); i++)
142 for (i = 0; i < _vec.
size(); i++)
143 div[i] = _vec[i]/factor;
155 for (i = 0; i < _vec.size(); i++)
164 for (i = 0; i < vec.
size(); i++)
165 prod[i] = factor*vec[i];
182 template<
typename T> std::ostream& operator<<(std::ostream& os, const Vector<T>& vec) {
186 for (i = 0; i < vec.
size()-1; i++)
187 os << vec[i] <<
", ";
188 os << vec.
back() <<
"]";
201 for (i = 0; i < _vec.size(); i++)
202 mag2 += _vec[i]*_vec[i];
215 for (i = 0; i < _vec.size(); i++)
216 prod += _vec[i]*vec[i];
224 if ( vec.
size() != 3 || vec.
size() != _vec.size() )
226 "The vectors are not of dimension 3",
227 "Vector<T>::cross"));
231 cross[0] = _vec[1]*vec[2]-_vec[2]*vec[1];
232 cross[1] = _vec[2]*vec[0]-_vec[0]*vec[2];
233 cross[2] = _vec[0]*vec[1]-_vec[1]*vec[0];
259 _vec.push_back(element);
263 _vec.push_front(element);
T mag2() const
Returns the square of the magnitude of the vector.
Definition: Vector-inl.hh:197
T dot(const Vector &vec) const
Returns the dot product with another vecror.
Definition: Vector-inl.hh:207
void IsNonZero(const std::string &location, const std::string &what, const T &value)
Asserts that a value is non zero.
Definition: Assert.hh:2
Vector operator/(const T &factor) const
Overload the vector ratio with a scalar.
Definition: Vector-inl.hh:134
Object that can host any type of data in a vector arrangement.
Definition: Vector.hh:17
T mag() const
Returns the magnitude of the vector.
Definition: Vector-inl.hh:192
const T & front() const
Returns the first element of the vector.
Definition: Vector-inl.hh:242
~Vector()
Destructor.
Definition: Vector-inl.hh:31
void operator+=(const Vector &vec)
Overload the vector sum.
Definition: Vector-inl.hh:65
void push_front(const T &element)
Adds element at the back.
Definition: Vector-inl.hh:262
const T & operator[](const size_t i) const
Overload the subscript operator, return a value.
Definition: Vector-inl.hh:170
std::vector< T > _vec
Underlying std::vector.
Definition: Vector.hh:120
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
const T & back() const
Returns the last element of the vector.
Definition: Vector-inl.hh:238
const size_t size() const
Returns dimension of the vector.
Definition: Vector-inl.hh:266
Vector cross(const Vector &vec) const
Returns the cross product with another vecror.
Definition: Vector-inl.hh:221
const std::vector< T > & std() const
Returns the vector as an std::vector.
Definition: Vector-inl.hh:270
void resize(const size_t n)
Resizes the vector to chosen dimension.
Definition: Vector-inl.hh:254
Vector operator+(const Vector &vec) const
Overload the vector sum.
Definition: Vector-inl.hh:51
void operator-=(const Vector &vec)
Overload the v substraction.
Definition: Vector-inl.hh:90
Vector()
Default constructor, initilizes an empty vector of dimension 0.
Definition: Vector-inl.hh:1
Vector & operator=(const Vector &vec)
Equality operator.
Definition: Vector-inl.hh:33
T operator*(const Vector &vec) const
Overload the vector product (dot product)
Definition: Vector-inl.hh:101
T norm() const
Returns the norm of the vector.
Definition: Vector-inl.hh:246
Vector operator-() const
Overload the unary minus for vector algebra.
Definition: Vector-inl.hh:41
void operator/=(const T &factor)
Overload the vector ratio with a scalar.
Definition: Vector-inl.hh:148
T norm2() const
Returns the square of the norm of the vector.
Definition: Vector-inl.hh:250
void IsWithinBounds(const std::string &location, const size_t n, const size_t i)
Asserts that the index that is accessed is within bounds.
Definition: Assert.cc:5
void push_back(const T &element)
Adds element at the back.
Definition: Vector-inl.hh:258
void SameSize(const std::string &location, const std::string &what, const std::vector< T > &va, const std::vector< T > &vb)
Asserts that two vectors have the same size.
Definition: Assert.hh:83
void operator*=(const T &factor)
Overload the vector product with a scalar.
Definition: Vector-inl.hh:126