VectorBaseOperators.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: VectorBaseOperators.hpp 3140 2012-06-18 15:03:02Z susancummins $"
00002 
00003 
00004 
00010 #ifndef GPSTK_VECTOR_BASE_OPERATORS_HPP
00011 #define GPSTK_VECTOR_BASE_OPERATORS_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 #include <fstream> // for copyfmt
00036 #include <vector>
00037 #include <iomanip>
00038 
00039 // to solve the conflict with windows.h
00040 #if defined(min)
00041 #undef min
00042 #endif
00043 
00044 #if defined(max)
00045 #undef max
00046 #endif
00047 
00048 namespace gpstk
00049 {
00050 
00053  
00055    template <class T, class E>
00056    std::ostream& operator<<(std::ostream& s, const ConstVectorBase<T, E>& a) 
00057    {
00058       std::ofstream savefmt;
00059       savefmt.copyfmt(s);
00060       size_t i;
00061       for (i=0; i< a.size(); i++) {
00062          s << std::setw(1) << ' ';
00063          s.copyfmt(savefmt);
00064          s << a[i];
00065       }
00066       return s;
00067    }
00068 
00070    template <class T, class BaseClass>
00071    inline T sum(const ConstVectorBase<T, BaseClass>& l)
00072    { 
00073       T total(0);
00074       size_t i;
00075       for (i = 0; i < l.size(); i++)
00076          total += l[i];
00077       return total;
00078    }
00079 
00081    template <class T, class BaseClass>
00082    inline T minabs(const ConstVectorBase<T, BaseClass>& l) throw (VectorException)
00083    { 
00084       if (l.size() == 0)
00085       {
00086          VectorException e("Can't find the minabs of an empty vector");
00087          GPSTK_THROW(e);
00088       }
00089       T min = l[0];
00090       size_t i;
00091       for (i = 1; i < l.size(); i++)
00092          if (ABS(l[i]) < ABS(min)) 
00093             min = l[i];
00094       return min;
00095    }
00096 
00098    template <class T, class BaseClass>
00099    inline T min(const ConstVectorBase<T, BaseClass>& l) throw (VectorException)
00100    { 
00101       if (l.size() == 0)
00102       {
00103          VectorException e("Can't find the min of an empty vector");
00104          GPSTK_THROW(e);
00105       }
00106       T min = l[0];
00107       size_t i;
00108       for (i = 1; i < l.size(); i++)
00109          if (l[i] < min) 
00110             min = l[i];
00111       return min;
00112    }
00113 
00115    template <class T, class BaseClass>
00116    inline T maxabs(const ConstVectorBase<T, BaseClass>& l)
00117    {
00118       if (l.size() == 0)
00119       {
00120          VectorException e("Can't find the maxabs of an empty vector");
00121          GPSTK_THROW(e);
00122       }
00123       T max = l[0];
00124       size_t i;
00125       for (i = 1; i < l.size(); i++)
00126          if (ABS(l[i]) > ABS(max)) 
00127             max = l[i];
00128       return max;
00129    }
00130 
00132    template <class T, class BaseClass>
00133    inline T max(const ConstVectorBase<T, BaseClass>& l)
00134    {
00135       if (l.size() == 0)
00136       {
00137          VectorException e("Can't find the max of an empty vector");
00138          GPSTK_THROW(e);
00139       }
00140       T max = l[0];
00141       size_t i;
00142       for (i = 1; i < l.size(); i++)
00143          if (l[i] > max) 
00144             max = l[i];
00145       return max;
00146    }
00147 
00149    template <class T, class BaseClass, class BaseClass2> 
00150    inline T dot(const ConstVectorBase<T, BaseClass>& l, 
00151          const ConstVectorBase<T, BaseClass2>& r) 
00152    {
00153       T sum(0);
00154       size_t i,n=(l.size() > r.size() ? r.size() : l.size());
00155       for (i = 0; i < n; i++)
00156       {
00157          sum += l[i] * r[i];
00158       }
00159       return sum;
00160    } 
00161 
00163    template <class T, class BaseClass> 
00164    inline T dot(const ConstVectorBase<T, BaseClass>& l, const T r) 
00165    {
00166       T sum(0);
00167       size_t i;
00168       for (i = 0; i < l.size(); i++)
00169       {
00170          sum += l[i] * r;
00171       }
00172       return sum;
00173    }
00174 
00176    template <class T, class BaseClass> 
00177    inline T dot(const T l, const ConstVectorBase<T, BaseClass>& r) 
00178    {
00179       T sum(0);
00180       size_t i;
00181       for (i = 0; i < r.size(); i++)
00182       {
00183          sum += l * r[i];
00184       }
00185       return sum;
00186    }
00187 
00189    template <class T, class BaseClass> 
00190    inline T norm(const ConstVectorBase<T, BaseClass>& v) 
00191    {
00192       T mag=T(0);
00193       if(v.size()==0) return mag;
00194       mag = ABS(v(0));
00195       for(size_t i=1; i<v.size(); i++) {
00196          if(mag > ABS(v(i)))
00197             mag *= SQRT(T(1)+(v(i)/mag)*(v(i)/mag));
00198          else if(ABS(v(i)) > mag)
00199             mag = ABS(v(i))*SQRT(T(1)+(mag/v(i))*(mag/v(i)));
00200          else
00201             mag *= SQRT(T(2));
00202       }
00203       return mag;
00204    } 
00205 
00207    template <class T, class BaseClass, class BaseClass2> 
00208    inline T Minkowski(const ConstVectorBase<T, BaseClass>& v, 
00209          const ConstVectorBase<T, BaseClass2>& w) 
00210    {
00211       if (v.size()<4 || w.size()<4)
00212       {
00213          VectorException e("Minkowski requires vector length 4");
00214          GPSTK_THROW(e);
00215       }
00216       return (v(0)*w(0)+v(1)*w(1)+v(2)*w(2)-v(3)*w(3));
00217    }
00218 
00220    template <class T, class BaseClass1, class BaseClass2>
00221    inline T cosVec(const ConstVectorBase<T, BaseClass1>& a,
00222                 const ConstVectorBase<T, BaseClass2>& b)
00223    {
00224       T na=norm(a), nb=norm(b), c(0);
00225       size_t i,n=(b.size() > a.size() ? a.size() : b.size());
00226       for(i=0; i<n; i++) c += (a(i)/na)*(b(i)/nb);
00227       return c;
00228    }
00229 
00230 // shortwire equality operators - compares each individual
00231 // element in the vector but returns one 'true' or 'false'
00232 // for the whole comparison.  note this only compares
00233 // the smaller of the size of the two vectors
00234 #define VecShortwireComparisonOperator(func, op) \
00235  \
00236 template <class T, class BaseClass, class BaseClass2>  \
00237 inline bool func(const ConstVectorBase<T, BaseClass>& l,  \
00238        const ConstVectorBase<T, BaseClass2>& r)  \
00239 {  \
00240    size_t len = (l.size() < r.size()) ? l.size() : r.size(); \
00241    size_t i; \
00242    for(i = 0; i < len; i++) \
00243       if ( !(l[i] op r[i]) ) \
00244          return false; \
00245    return true; \
00246 }  \
00247  \
00248 template <class T, class BaseClass>  \
00249 inline bool func(const ConstVectorBase<T, BaseClass>& l, const T r)  \
00250 { \
00251    size_t len = l.size(); \
00252    size_t i; \
00253    for(i = 0; i < len; i++) \
00254       if ( !(l[i] op r) ) \
00255          return false; \
00256    return true; \
00257 } \
00258  \
00259 template <class T, class BaseClass>  \
00260 inline bool func(const T l, const ConstVectorBase<T, BaseClass>& r)  \
00261 {  \
00262    size_t len = r.size(); \
00263    size_t i; \
00264    for(i = 0; i < len; i++) \
00265       if ( !(l op r[i]) ) \
00266          return false; \
00267    return true; \
00268 }
00269 
00270 VecShortwireComparisonOperator(eq, ==)
00271    VecShortwireComparisonOperator(ne, !=)
00272    VecShortwireComparisonOperator(lt, <)
00273    VecShortwireComparisonOperator(gt, >)
00274    VecShortwireComparisonOperator(ge, >=)
00275    VecShortwireComparisonOperator(le, <=)
00276 
00278 
00279 }  // namespace gpstk
00280  
00281 #endif // GPSTK_VECTOR_BASE_OPERATORS_HPP

Generated on Sat May 18 03:31:12 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1