📄 btl_numeric_vector.h
字号:
numeric_vector<T> result(*this); const_iterator i; iterator j; for (i=v.begin(), j=result.begin(); i!=v.end(); i++, j++) *j += *i; return result; }//............................................................................. /**#: [Description="Vector addition and assignment."]*/ numeric_vector<T>& operator+=(const numeric_vector<T> &v) { #if defined(BTL_DEBUG_VERSION) if (v.size() != size()) WARNING("The numeric_vectors must be the same size for increment"); #endif iterator i; const_iterator j; for (i=vec.begin(), j=v.begin(); i!=vec.end(); i++, j++) *i += *j; return *this; }//............................................................................. /**#: [Description="Vector subtraction."]*/ numeric_vector<T> operator-(const numeric_vector<T> &v) const { #if defined(BTL_DEBUG_VERSION) if (v.size() != size()) WARNING("The numeric_vectors must be the same size for subtraction"); #endif numeric_vector<T> result = *this; const_iterator i; iterator j; for (i=v.begin(), j=result.begin(); i!=v.end(); i++, j++) *j -= *i; return result; }//............................................................................. /**#: [Description="Vector negation (unary minus operator)."]*/ numeric_vector<T> operator-() const { numeric_vector<T> negthis = *this; for (iterator i=negthis.begin(); i!=negthis.end(); i++) *i = -(*i); return negthis; }//............................................................................. /**#: [Description="Vector subtraction and assignment."]*/ numeric_vector<T>& operator-=(const numeric_vector<T> &v) { #if defined(BTL_DEBUG_VERSION) if (v.size() != size()) WARNING("The numeric_vectors must be the same size for decrement"); #endif iterator i; const_iterator j; for (i=vec.begin(), j=v.begin(); i!=vec.end(); i++, j++) *i -= *j; return *this; }//............................................................................. /**#: [Description="Add a scalar to each element in the numeric_vector."]*/ numeric_vector<T> operator+(const value_type &s) const { numeric_vector<T> result = *this; for (iterator i=result.begin(); i!=result.end(); i++) *i += s; return result; } //............................................................................. /**#: [Description="Add and assign a scalar to each element in the numeric_vector."]*/ numeric_vector<T>& operator+=(const value_type &s) { for (iterator i=vec.begin(); i!=vec.end(); i++) *i += s; return *this; } //............................................................................. /**#: [Description="Subtract a scalar from each element in the numeric_vector."]*/ numeric_vector<T> operator-(const value_type &s) const { numeric_vector<T> result = *this; for (iterator i=result.begin(); i!=result.end(); i++) *i -= s; return result; } //............................................................................. /**#: [Description="Subtract and assign a scalar from each element in the numeric_vector."]*/ numeric_vector<T>& operator-=(const value_type &s) { for (iterator i=vec.begin(); i!=vec.end(); i++) *i -= s; return *this; } //............................................................................. /**#: [Description="Vector scalar/dot product."]*/ value_type operator*(const numeric_vector<T> &v) const { #if defined(BTL_DEBUG_VERSION) if (v.size() != size()) WARNING("The input numeric_vector<T> must be the same size as this numeric_vector"); #endif value_type product = value_type(0); const_iterator i,j; for (i=vec.begin(), j=v.begin(); i!=vec.end(); i++, j++) product += *i * *j; return product; }//............................................................................. /**#: [Description="Vector cross product."]*/ numeric_vector<T> operator%(const numeric_vector<T> &v) const { // This works out the cross product of two vectors. These vectors must // be of size 3. #if defined(BTL_DEBUG_VERSION) if (size() != 3 || v.size() != 3) WARNING("Both numeric_vectors must be of size 3 for a valid cross product"); #endif numeric_vector<T> result; result[0] = vec[1]*v[2] - vec[2]*v[1]; result[1] = vec[2]*v[0] - vec[0]*v[2]; result[2] = vec[0]*v[1] - vec[1]*v[0]; return result; }//............................................................................. /**#: [Description="Vector multiplication by a scalar."]*/ numeric_vector<T> operator*(const value_type& y) const { numeric_vector<T> result = *this; for (iterator i=result.begin(); i!=result.end(); i++) *i *= y; return result; }//............................................................................. /**#: [Description="Vector multiplication by a scalar and assignment."]*/ numeric_vector<T>& operator*=(const value_type& y) { for (iterator i=vec.begin(); i!=vec.end(); i++) *i *= y; return *this; }//............................................................................. /**#: [Description="Vector division by a scalar."]*/ numeric_vector<T> operator/(const value_type& y) const { #if defined(BTL_DEBUG_VERSION) if (y == 0.0) WARNING("Divide by zero"); #endif BTL_REAL y_reciprocal = 1.0 / y; numeric_vector<T> result = *this; for (iterator i=result.begin(); i!=result.end(); i++) *i *= y_reciprocal; return result; }//............................................................................. /**#: [Description="Vector division by a scalar and assignment."]*/ numeric_vector<T>& operator/=(const value_type& y) { #if defined(BTL_DEBUG_VERSION) if (y == 0.0) WARNING("Divide by zero"); #endif BTL_REAL y_reciprocal = 1.0 / y; for (iterator i=vec.begin(); i!=vec.end(); i++) *i *= y_reciprocal; return *this; }//............................................................................. /**#: [Description="Equality operator"]*/ bool operator==(const numeric_vector<T>& v) const { iterator i; const_iterator j; for (i=vec.begin(), j=v.begin(); i!=vec.end(); i++, j++) { if (*i != *j) return false; } return true; }//............................................................................. /**#: [Description="Postmultiplication of a Vector by a BTL_Matrix. e.g. matrix m; numeric_vector v1,v2; ... v2 = v1 * m; "] [Restrictions="The size of v must equal the number of rows in the Matrix."]*/// friend numeric_vector<T>// operator*(matrix<T>& m) // currently matrix not recognised as a type// {// #if defined(BTL_DEBUG_VERSION)// if (vec.size() != m.num_rows())// FATAL_ERROR("The size of the input numeric_vector<T> must equal the number of rows"// " in this Matrix");// #endif// numeric_vector<T> result(vec.size());// const_iterator i;// iterator k; // const_iterator j = m.begin();// for (i = vec.begin(), k = result.begin(); i < vec.end(); i++, k++)// for (size_type l = 0; l < m.num_cols(); l++, j++)// *k += *i * *j;// return result;// }};_BTL_END_NAMESPACE#endif // numeric_vector.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -