Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

Vector.hh

Go to the documentation of this file.
00001 #ifndef VECTOR_TMPL_HEADER
00002 #define VECTOR_TMPL_HEADER
00003 
00004 // ==== TEMPLATE HEADER Vector.hh ====
00005 
00011 // 12-Jul-1999. Andras Aszodi
00012 
00013 // ---- COMPATIBILITY NOTES ----
00014 
00015 // Exception support is assumed.
00016 // This module depends on the Standard Template Library.
00017  
00018 // ---- COMPILER COMMAND LINE MACROS ----
00019 
00020 // Define the macro INCLUDE_TMPL_DEFS if your compiler
00021 // does not support automatic template source inclusion.
00022 
00023 // ---- STANDARD HEADERS ----
00024 
00025 #include <stdlib.h>
00026 #include <math.h>
00027 
00028 #ifdef _STANDARD_C_PLUS_PLUS
00029 #include <iostream>
00030 #include <iomanip>
00031 #include <vector>
00032 #include <algorithm>
00033 #include <numeric>
00034 #include <functional>
00035     using namespace std;
00036 #else
00037 #include <iostream.h>
00038 #include <iomanip.h>
00039 #include <vector.h>
00040 #include <algo.h>
00041 #include <function.h>
00042 #endif
00043 
00044 // ---- LIBRARY HEADERS ----
00045 
00046 #include "Rangedimexc.hh"       // linear algebra exceptions
00047 
00048 // ==== CLASSES ====
00049 
00050 namespace RazorBack {
00051 
00058 template <class T_>
00059 class Vector_
00060 {
00061     // data
00062     private:
00063     
00064     vector<T_> Vec;     // the STL container holding the coordinates
00065     size_t Dim; // the dimension
00066     bool Ftnidx;        // true for FORTRAN-style [1..N] indexing
00067     bool Check;         // true when range checking is enabled.
00068     
00069     // methods
00070     public:
00071     
00072     // constructors and destructor
00073     
00078     explicit Vector_(size_t N=3, const T_& X=T_(0)):
00079         Vec(!N? 3: N, X), Dim(!N? 3: N), 
00080         Ftnidx(false), Check(false) {}
00081     
00086     explicit Vector_(const vector<T_>& V): 
00087             Vec(V), Dim(Vec.size()), Ftnidx(false), Check(false)
00088     {
00089         if (V.empty()) dim(3);
00090     }
00091     
00096     Vector_(const T_* Arr, size_t Len):
00097             Vec(Len), Dim(Len), Ftnidx(false), Check(false)
00098     {
00099         for (register unsigned int i=0; i<Len; i++)
00100             Vec[i]=Arr[i];
00101     }
00102     
00104     virtual ~Vector_() {}
00105     
00111     const T_& operator[](size_t Idx) const { return Vec[Idx]; }
00112     T_& operator[](size_t Idx) { return Vec[Idx]; }
00113     const T_& operator()(size_t Idx) const;
00114     T_& operator()(size_t Idx);
00115     
00125     bool ftn_idx() const { return Ftnidx; }
00126     bool ftn_idx(bool F) { bool Of=Ftnidx; Ftnidx=F; return Of; }
00127     
00132     bool range_check() const { return Check; }
00133     bool range_check(bool C) { bool Oc=Check; Check=C; return Oc; }
00134     
00136     Vector_<T_>& set_values(const T_& X=T_(0));
00137     
00143     size_t dim() const { return(Dim); }
00144     size_t dim(size_t D);
00145     
00147     typename vector<T_>::const_iterator begin() const { return Vec.begin(); }
00148     typename vector<T_>::iterator begin() { return Vec.begin(); }
00149     typename vector<T_>::const_iterator end() const { return Vec.end(); }
00150     typename vector<T_>::iterator end() { return Vec.end(); }
00151     
00157     Vector_<T_>& operator+=(const T_& X);
00158     Vector_<T_>& operator-=(const T_& X);
00159     Vector_<T_>& operator*=(const T_& X);
00160     Vector_<T_> operator*(const T_& X) const;
00161     Vector_<T_>& operator/=(const T_& X);
00162     Vector_<T_> operator/(const T_& X) const;
00163     
00168     Vector_<T_>& operator+=(const Vector_<T_>& V);
00169     Vector_<T_> operator+(const Vector_<T_>& V) const;
00170     Vector_<T_>& operator-=(const Vector_<T_>& V);
00171     Vector_<T_> operator-(const Vector_<T_>& V) const;
00172     T_ operator*(const Vector_<T_>& V) const;
00173     
00175     
00182     T_ vec_len2() const { return((*this) * (*this)); }
00183     T_ vec_len() const { return(sqrt(vec_len2())); }
00184 
00191     T_ vec_norm();
00192     
00194     
00202     void transform(unary_function<T_, T_> Unaryfunc)
00203     {
00204         std::transform(Vec.begin(), Vec.end(), Vec.begin(), Unaryfunc);
00205     }
00206     
00208     void read(istream& In);
00209 };
00210 // END OF CLASS Vector_
00211 
00212 // ---- OUT-OF-CLASS FUNCTIONS ----
00213 
00214 // These were originally friend templates but EGCS 1.1.2
00215 // did not like them. 12-Jul-1999.
00216 
00220 template <class T_>
00221 Vector_<T_> operator*(const T_& X, const Vector_<T_>& V);       // see Bug Notes
00222 
00228 template <class T_>
00229 Vector_<T_> cross_prod(const Vector_<T_>& V1, const Vector_<T_>& V2); // see Bug Notes
00230 
00236 template <class T_>
00237 T_ vec_len2(const Vector_<T_>& V) { return(V*V); }
00238 
00243 template <class T_>
00244 T_ vec_len(const Vector_<T_>& V) { return(sqrt(V*V)); }
00245 
00250 template <class T_>
00251 T_ diff_len2(const Vector_<T_>& V1, const Vector_<T_>& V2);     // see Bug notes
00252 
00253     // I/O
00254 
00266 template <class T_>
00267 istream& operator>>(istream& In, Vector_<T_>& V)
00268 {
00269     V.read(In);
00270     return In;
00271 }
00272 
00274 template <class T_>
00275 ostream& operator<<(ostream& Out, const Vector_<T_>& V);
00276 
00277 // ---- TYPEDEFS ----
00278 
00280 typedef Vector_<double> Vecdbl_;
00281 typedef Vector_<float> Vecflt_;
00282 
00283 } // RazorBack
00284 
00285 #ifdef INCLUDE_TMPL_DEFS
00286 #include "Vector.cc"
00287 #endif
00288 
00289 // ==== END OF TEMPLATE HEADER Vector.hh ====
00290 
00291 #endif  // VECTOR_TMPL_HEADER

Generated at Wed Aug 21 09:33:22 2002 for The Razorback C++ Library: Linear Algebra by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001