00001
00002 #ifndef IG_STAT_VECTOR_H
00003 #define IG_STAT_VECTOR_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <cmath>
00027
00028 #ifndef IG_STAT_MATRIX_H
00029 #include "stat_matrix.h"
00030 #endif
00031
00032 namespace CwMtx
00033 {
00034
00035 template <unsigned r, class T = double>
00036 class CWTSVector : public CWTSMatrix<r, 1, T>
00037 {
00038 public:
00039 CWTSVector(): CWTSMatrix<r, 1, T>() {}
00040 CWTSVector(const CWTSMatrix<r, 1, T>& mat): CWTSMatrix<r, 1, T>(mat) {}
00041 CWTSVector(const CWTSVector& vec): CWTSMatrix<r, 1, T>(vec) {}
00042
00043 ~CWTSVector() {}
00044
00045 T & operator [](unsigned irow);
00046 const T & operator [](unsigned irow) const;
00047
00048 CWTSVector operator +(const CWTSVector &) const;
00049 CWTSVector operator -(const CWTSVector &) const;
00050 CWTSVector operator -() const;
00051 CWTSVector operator *(const T &) const;
00052 CWTSVector operator /(const T &value) const;
00053
00054 T operator *(const CWTSVector &) const;
00055
00056
00057 CWTSVector & operator =(const CWTSVector &vec);
00058 CWTSVector & operator +=(const CWTSVector &vec);
00059 CWTSVector & operator -=(const CWTSVector &vec);
00060 CWTSVector & operator *=(const T &value);
00061 CWTSVector & operator /=(const T &value);
00062
00063
00064 T operator !() const { return (*this).norm(); }
00065
00066 T norm() const;
00067
00068 CWTSVector unit() const { return (*this)/norm(); }
00069
00070
00071 void makeUnit() { (*this) /= norm(); }
00072
00073 template <unsigned r2>
00074 void storeAtRow(unsigned, const CWTSVector<r2, T> &);
00075 };
00076
00077 template <unsigned r, class T>
00078 inline T &
00079 CWTSVector<r, T>::operator [](unsigned irow)
00080 {
00081 return this->CWTSMatrix<r, 1, T>::operator[](irow)[0];
00082 }
00083
00084 template <unsigned r, class T>
00085 inline const T &
00086 CWTSVector<r, T>::operator [](unsigned irow) const
00087 {
00088 return this->CWTSMatrix<r, 1, T>::operator[](irow)[0];
00089 }
00090
00091
00092 template <unsigned r, class T>
00093 inline CWTSVector<r, T> &
00094 CWTSVector<r, T>::operator =(const CWTSVector<r, T> &vec)
00095 {
00096 return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator=(vec));
00097 }
00098
00099 template <unsigned r, class T>
00100 inline CWTSVector<r, T> &
00101 CWTSVector<r, T>::operator +=(const CWTSVector<r, T> &vec)
00102 {
00103 return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator+=(vec));
00104 }
00105
00106 template <unsigned r, class T>
00107 inline CWTSVector<r, T> &
00108 CWTSVector<r, T>::operator -=(const CWTSVector<r, T> &vec)
00109 {
00110 return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator-=(vec));
00111 }
00112
00113 template <unsigned r, class T>
00114 inline CWTSVector<r, T> &
00115 CWTSVector<r, T>::operator *=(const T &value)
00116 {
00117 return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator*=(value));
00118 }
00119
00120 template <unsigned r, class T>
00121 inline CWTSVector<r, T> &
00122 CWTSVector<r, T>::operator /=(const T &value)
00123 {
00124 return (*this) *= CWTSUnity<T>()/value;
00125 }
00126
00127 template <unsigned r, class T>
00128 CWTSVector<r, T>
00129 CWTSVector<r, T>::operator +(const CWTSVector<r, T> &vec) const
00130 {
00131 return CWTSVector<r, T>(*this) += vec;
00132 }
00133
00134 template <unsigned r, class T>
00135 CWTSVector<r, T>
00136 CWTSVector<r, T>::operator -(const CWTSVector<r, T> &vec) const
00137 {
00138 return CWTSVector<r, T>(*this) -= vec;
00139 }
00140
00141 template <unsigned r, class T>
00142 CWTSVector<r, T>
00143 CWTSVector<r, T>::operator -() const
00144 {
00145 return (*this)*(CWTSZero<T>() - CWTSUnity<T>());
00146 }
00147
00148 template <unsigned r, class T>
00149 CWTSVector<r, T>
00150 CWTSVector<r, T>::operator *(const T &value) const
00151 {
00152 return CWTSVector<r, T>(*this) *= value;
00153 }
00154
00155 template <unsigned r, class T>
00156 CWTSVector<r, T>
00157 CWTSVector<r, T>::operator /(const T &value) const
00158 {
00159 return (*this)*(CWTSUnity<T>()/value);
00160 }
00161
00162 template <unsigned r, class T>
00163 T
00164 CWTSVector<r, T>::operator *(const CWTSVector<r, T> &vec) const
00165 {
00166 T elemResult = CWTSZero<T>();
00167
00168 for (unsigned irow = 0; irow < r; ++irow)
00169 {
00170 elemResult += (*this)[irow]*vec[irow];
00171 }
00172
00173 return elemResult;
00174 }
00175
00176
00177 template <unsigned r, class T>
00178 T
00179 CWTSVector<r, T>::norm() const
00180 {
00181 T elemResult = CWTSZero<T>();
00182
00183 elemResult = (*this)*(*this);
00184
00185 return sqrt(elemResult);
00186 }
00187
00188 template <unsigned r, class T>
00189 template <unsigned r2>
00190 inline void
00191 CWTSVector<r, T>::storeAtRow(unsigned irowStart,
00192 const CWTSVector<r2, T> &vec)
00193 {
00194 CWTSMatrix<r, 1, T>::storeAtPosition(irowStart, 0, vec);
00195 }
00196
00197
00198
00199 template <unsigned r, class T>
00200 inline CWTSVector<r, T>
00201 operator *(const T &value, const CWTSVector<r, T> &vec)
00202 {
00203 return vec*value;
00204 }
00205
00206
00207 template <unsigned r, unsigned c, class T>
00208 CWTSVector<r, T>
00209 operator *(const CWTSMatrix<r, c, T> &mat, const CWTSVector<r, T> &vec)
00210 {
00211 CWTSVector<r, T> vecResult;
00212
00213 for (unsigned irow = 0; irow < r; ++irow)
00214 {
00215 vecResult[irow] = CWTSZero<T>();
00216
00217 for (unsigned icol = 0; icol < c; ++icol)
00218 {
00219 vecResult[irow] += mat[irow][icol]*vec[icol];
00220 }
00221 }
00222
00223 return vecResult;
00224 }
00225
00226
00227 template <unsigned r, class T>
00228 inline T
00229 norm(const CWTSVector<r, T> &vec)
00230 {
00231 return vec.norm();
00232 }
00233
00234
00235 template <unsigned r, class T>
00236 inline CWTSVector<r, T>
00237 sgn(const CWTSVector<r, T> &vec)
00238 {
00239 return vec.unit();
00240 }
00241 }
00242
00243 #endif // IG_STAT_VECTOR_H