VectorBaseOperators.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: VectorBaseOperators.hpp 70 2006-08-01 18:36:21Z ehagen $"
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 namespace gpstk
00040 {
00041 
00044  
00046    template <class T, class E>
00047    std::ostream& operator<<(std::ostream& s, const ConstVectorBase<T, E>& a) 
00048    {
00049       std::ofstream savefmt;
00050       savefmt.copyfmt(s);
00051       size_t i;
00052       for (i=0; i< a.size(); i++) {
00053          s << std::setw(1) << ' ';
00054          s.copyfmt(savefmt);
00055          s << a[i];
00056       }
00057       return s;
00058    }
00059 
00061    template <class T, class BaseClass>
00062    inline T sum(const ConstVectorBase<T, BaseClass>& l)
00063    { 
00064       T total(0);
00065       size_t i;
00066       for (i = 0; i < l.size(); i++)
00067          total += l[i];
00068       return total;
00069    }
00070 
00072    template <class T, class BaseClass>
00073    inline T minabs(const ConstVectorBase<T, BaseClass>& l) throw (VectorException)
00074    { 
00075       if (l.size() == 0)
00076       {
00077          VectorException e("Can't find the minabs of an empty vector");
00078          GPSTK_THROW(e);
00079       }
00080       T min = l[0];
00081       size_t i;
00082       for (i = 1; i < l.size(); i++)
00083          if (ABS(l[i]) < ABS(min)) 
00084             min = l[i];
00085       return min;
00086    }
00087 
00089    template <class T, class BaseClass>
00090    inline T min(const ConstVectorBase<T, BaseClass>& l) throw (VectorException)
00091    { 
00092       if (l.size() == 0)
00093       {
00094          VectorException e("Can't find the min of an empty vector");
00095          GPSTK_THROW(e);
00096       }
00097       T min = l[0];
00098       size_t i;
00099       for (i = 1; i < l.size(); i++)
00100          if (l[i] < min) 
00101             min = l[i];
00102       return min;
00103    }
00104 
00106    template <class T, class BaseClass>
00107    inline T maxabs(const ConstVectorBase<T, BaseClass>& l)
00108    {
00109       if (l.size() == 0)
00110       {
00111          VectorException e("Can't find the maxabs of an empty vector");
00112          GPSTK_THROW(e);
00113       }
00114       T max = l[0];
00115       size_t i;
00116       for (i = 1; i < l.size(); i++)
00117          if (ABS(l[i]) > ABS(max)) 
00118             max = l[i];
00119       return max;
00120    }
00121 
00123    template <class T, class BaseClass>
00124    inline T max(const ConstVectorBase<T, BaseClass>& l)
00125    {
00126       if (l.size() == 0)
00127       {
00128          VectorException e("Can't find the max of an empty vector");
00129          GPSTK_THROW(e);
00130       }
00131       T max = l[0];
00132       size_t i;
00133       for (i = 1; i < l.size(); i++)
00134          if (l[i] > max) 
00135             max = l[i];
00136       return max;
00137    }
00138 
00140    template <class T, class BaseClass, class BaseClass2> 
00141    inline T dot(const ConstVectorBase<T, BaseClass>& l, 
00142          const ConstVectorBase<T, BaseClass2>& r) 
00143    {
00144       T sum(0);
00145       size_t i,n=(l.size() > r.size() ? r.size() : l.size());
00146       for (i = 0; i < n; i++)
00147       {
00148          sum += l[i] * r[i];
00149       }
00150       return sum;
00151    } 
00152 
00154    template <class T, class BaseClass> 
00155    inline T dot(const ConstVectorBase<T, BaseClass>& l, const T r) 
00156    {
00157       T sum(0);
00158       size_t i;
00159       for (i = 0; i < l.size(); i++)
00160       {
00161          sum += l[i] * r;
00162       }
00163       return sum;
00164    }
00165 
00167    template <class T, class BaseClass> 
00168    inline T dot(const T l, const ConstVectorBase<T, BaseClass>& r) 
00169    {
00170       T sum(0);
00171       size_t i;
00172       for (i = 0; i < r.size(); i++)
00173       {
00174          sum += l * r[i];
00175       }
00176       return sum;
00177    }
00178 
00180    template <class T, class BaseClass> 
00181    inline T norm(const ConstVectorBase<T, BaseClass>& v) 
00182    {
00183       T mag=T(0);
00184       if(v.size()==0) return mag;
00185       mag = ABS(v(0));
00186       for(size_t i=1; i<v.size(); i++) {
00187          if(mag > ABS(v(i)))
00188             mag *= SQRT(T(1)+(v(i)/mag)*(v(i)/mag));
00189          else if(ABS(v(i)) > mag)
00190             mag = ABS(v(i))*SQRT(T(1)+(mag/v(i))*(mag/v(i)));
00191          else
00192             mag *= SQRT(T(2));
00193       }
00194       return mag;
00195    } 
00196 
00198    template <class T, class BaseClass, class BaseClass2> 
00199    inline T Minkowski(const ConstVectorBase<T, BaseClass>& v, 
00200          const ConstVectorBase<T, BaseClass2>& w) 
00201    {
00202       if (v.size()<4 || w.size()<4)
00203       {
00204          VectorException e("Minkowski requires vector length 4");
00205          GPSTK_THROW(e);
00206       }
00207       return (v(0)*w(0)+v(1)*w(1)+v(2)*w(2)-v(3)*w(3));
00208    }
00209 
00211    template <class T, class BaseClass1, class BaseClass2>
00212    inline T cosVec(const ConstVectorBase<T, BaseClass1>& a,
00213                 const ConstVectorBase<T, BaseClass2>& b)
00214    {
00215       T na=norm(a), nb=norm(b), c(0);
00216       size_t i,n=(b.size() > a.size() ? a.size() : b.size());
00217       for(i=0; i<n; i++) c += (a(i)/na)*(b(i)/nb);
00218       return c;
00219    }
00220 
00221 // shortwire equality operators - compares each individual
00222 // element in the vector but returns one 'true' or 'false'
00223 // for the whole comparison.  note this only compares
00224 // the smaller of the size of the two vectors
00225 #define VecShortwireComparisonOperator(func, op) \
00226  \
00227 template <class T, class BaseClass, class BaseClass2>  \
00228 inline bool func(const ConstVectorBase<T, BaseClass>& l,  \
00229        const ConstVectorBase<T, BaseClass2>& r)  \
00230 {  \
00231    size_t len = (l.size() < r.size()) ? l.size() : r.size(); \
00232    size_t i; \
00233    for(i = 0; i < len; i++) \
00234       if ( !(l[i] op r[i]) ) \
00235          return false; \
00236    return true; \
00237 }  \
00238  \
00239 template <class T, class BaseClass>  \
00240 inline bool func(const ConstVectorBase<T, BaseClass>& l, const T r)  \
00241 { \
00242    size_t len = l.size(); \
00243    size_t i; \
00244    for(i = 0; i < len; i++) \
00245       if ( !(l[i] op r) ) \
00246          return false; \
00247    return true; \
00248 } \
00249  \
00250 template <class T, class BaseClass>  \
00251 inline bool func(const T l, const ConstVectorBase<T, BaseClass>& r)  \
00252 {  \
00253    size_t len = r.size(); \
00254    size_t i; \
00255    for(i = 0; i < len; i++) \
00256       if ( !(l op r[i]) ) \
00257          return false; \
00258    return true; \
00259 }
00260 
00261 VecShortwireComparisonOperator(eq, ==)
00262    VecShortwireComparisonOperator(ne, !=)
00263    VecShortwireComparisonOperator(lt, <)
00264    VecShortwireComparisonOperator(gt, >)
00265    VecShortwireComparisonOperator(ge, >=)
00266    VecShortwireComparisonOperator(le, <=)
00267 
00269 
00270 }  // namespace gpstk
00271  
00272 #endif // GPSTK_VECTOR_BASE_OPERATORS_HPP

Generated on Thu Jul 29 03:30:56 2010 for GPS ToolKit Software Library by  doxygen 1.3.9.1