00001
00002
00003
00004
00005
00006
00007
00008 #ifndef VECTOR_N_H_INCLUDED
00009 #define VECTOR_N_H_INCLUDED
00010
00011 #define VECTOR_N_STACK
00012
00013 #include <math.h>
00014 #include "MVTools.h"
00015
00016
00017 #if !defined(VECTOR_N_STACK) && !defined(VECTOR_N_HEAP)
00018 #error Define VECTOR_N_STACK if you'd like Vector_n to use stack memory or \
00019 VECTOR_N_HEAP if you'd like Vector_n to use heap memory!
00020 #endif
00021
00022
00023
00024
00025
00026
00027
00028
00029 template <class T, size_t N>
00030 class Vector_n
00031 {
00032 public:
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 Vector_n()
00043 {
00044 #if defined(VECTOR_N_HEAP)
00045 content = new T[N];
00046 #endif
00047
00048 T zero = T();
00049 for (size_t i = 0; i < N; ++i)
00050 content[i] = zero;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 Vector_n(const T* v)
00060 {
00061 #if defined(VECTOR_N_HEAP)
00062 content = new T[N];
00063 #endif
00064
00065 for (size_t i = 0; i < N; ++i)
00066 content[i] = v[i];
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 Vector_n(const Vector_n<T, N>& v)
00076 {
00077 #if defined(VECTOR_N_HEAP)
00078 content = new T[N];
00079 #endif
00080
00081 *this = v;
00082 }
00083
00084
00085
00086
00087
00088
00089 ~Vector_n()
00090 {
00091 #if defined(VECTOR_N_HEAP)
00092 delete [] content;
00093 #endif
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 Vector_n<T, N>& operator=(const Vector_n<T, N>& v)
00103 {
00104 for (size_t i = 0; i < N; ++i)
00105 content[i] = v.content[i];
00106 return *this;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 Vector_n<T, N>& operator=(const T* v)
00116 {
00117 for (size_t i = 0; i < N; ++i)
00118 content[i] = v[i];
00119 return *this;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 void copyTo(T* v) const
00129 {
00130 const T* con = content;
00131 for (size_t i = 0; i < N; ++i, ++v, ++con)
00132 *v = *con;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142 const T& operator[](size_t i) const
00143 {
00144 return content[i];
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 T& operator[](size_t i)
00155 {
00156 return content[i];
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 Vector_n<T, N>& operator+=(const Vector_n<T, N>& v)
00166 {
00167 for (size_t i = 0; i < N; ++i)
00168 content[i] += v.content[i];
00169 return *this;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 Vector_n<T, N>& operator-=(const Vector_n<T, N>& v)
00179 {
00180 for (size_t i = 0; i < N; ++i)
00181 content[i] -= v.content[i];
00182 return *this;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 Vector_n<T, N>& operator*=(const T& s)
00192 {
00193 for (size_t i = 0; i < N; ++i)
00194 {
00195 content[i] *= s;
00196 if (MVTools::isNearInf(content[i]))
00197 {
00198 if (MVTools::isNearPosInf(content[i]))
00199 throw MVException(MVException::PosInfValue);
00200 else
00201 throw MVException(MVException::NegInfValue);
00202 }
00203 }
00204 return *this;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 Vector_n<T, N>& operator/=(const T& s)
00214 {
00215 if (MVTools::isNearZero(s))
00216 {
00217 if (MVTools::isNearNegZero(s))
00218 throw MVException(MVException::DivByNegZero);
00219 else
00220 throw MVException(MVException::DivByPosZero);
00221 }
00222
00223 for (size_t i = 0; i < N; ++i)
00224 {
00225 content[i] /= s;
00226 if (MVTools::isNearInf(content[i]))
00227 {
00228 if (MVTools::isNearPosInf(content[i]))
00229 throw MVException(MVException::PosInfValue);
00230 else
00231 throw MVException(MVException::NegInfValue);
00232 }
00233 }
00234 return *this;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243 T length()
00244 {
00245 T l = T();
00246 for (size_t i = 0; i < N; ++i)
00247 l += content[i]*content[i];
00248 return sqrt(l);
00249 }
00250
00251 private:
00252
00253
00254 #if defined(VECTOR_N_HEAP)
00255 T* content;
00256 #elif defined(VECTOR_N_STACK)
00257 T content[N];
00258 #endif
00259 };
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 template<class T, size_t N>
00270 Vector_n<T, N> operator+(const Vector_n<T, N>& v1,
00271 const Vector_n<T, N>& v2)
00272 {
00273 Vector_n<T, N> res(v1);
00274 res += v2;
00275 return res;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 template<class T, size_t N>
00287 Vector_n<T, N> operator-(const Vector_n<T, N>& v1,
00288 const Vector_n<T, N>& v2)
00289 {
00290 Vector_n<T, N> res(v1);
00291 res -= v2;
00292 return res;
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 template<class T, size_t N>
00304 Vector_n<T, N> operator*(const T& s,
00305 const Vector_n<T, N>& v)
00306 {
00307 Vector_n<T, N> res(v);
00308 res *= s;
00309 return res;
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 template<class T, size_t N>
00321 Vector_n<T, N> operator*(const Vector_n<T, N>& v,
00322 const T& s)
00323 {
00324 Vector_n<T, N> res(v);
00325 res *= s;
00326 return res;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 template <class T, size_t N>
00338 T operator*(const Vector_n<T, N>& v1,
00339 const Vector_n<T, N>& v2)
00340 {
00341 T res = T();
00342 for (size_t i = 0; i < N; ++i)
00343 res += v1[i]*v2[i];
00344
00345 if (MVTools::isNearInf(res))
00346 {
00347 if (MVTools::isNearPosInf(res))
00348 throw MVException(MVException::PosInfValue);
00349 else
00350 throw MVException(MVException::NegInfValue);
00351 }
00352 return res;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 template<class T, size_t N>
00364 Vector_n<T, N> operator/(const Vector_n<T, N>& v,
00365 const T& s)
00366 {
00367 Vector_n<T, N> res(v);
00368 res /= s;
00369 return res;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379 template<class T, size_t N>
00380 T length(const Vector_n<T, N>& v)
00381 {
00382 T l = T();
00383 for (size_t i = 0; i < N; ++i)
00384 l += v[i]*v[i];
00385 return sqrt(l);
00386 }
00387
00388 #endif
00389