MatrixBaseOperators.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: MatrixBaseOperators.hpp 70 2006-08-01 18:36:21Z ehagen $"
00002 
00003 
00004 
00010 #ifndef GPSTK_MATRIX_BASE_OPERATORS_HPP
00011 #define GPSTK_MATRIX_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 <iomanip>
00037 
00038 namespace gpstk
00039 {
00040 
00043  
00045    template <class T, class E>
00046    std::ostream& operator<<(std::ostream& s, const ConstMatrixBase<T, E>& a) 
00047    {
00048       size_t i, j;
00049       std::ofstream savefmt;
00050       savefmt.copyfmt(s);
00051       for (i=0; i<a.rows(); i++)
00052       {
00053          for (j=0; j< a.cols(); j++) {
00054             s << std::setw(1) << ' ';
00055             s.copyfmt(savefmt);
00056             s << a(i,j);
00057          }
00058          if(i < a.rows()-1) s << std::endl;
00059       }
00060       return s;
00061    }
00062 
00066    template <class T, class BaseClass>
00067    BaseClass& ident(RefMatrixBase<T, BaseClass>& m)
00068       throw (MatrixException)
00069    {
00070       BaseClass& me = static_cast<BaseClass&>(m);
00071       if ( (me.rows() != me.cols()) || (me.cols() < 1) )
00072       {
00073          MatrixException e("invalid matrix dimensions for ident()");
00074          GPSTK_THROW(e);
00075       }
00076       m.assignFrom(T(0));
00077       size_t i;
00078       for (i = 0; i < me.rows(); i++)
00079          me(i,i) = T(1);
00080       return me;
00081    }
00082 
00086    template <class T, class BaseClass>
00087    inline T trace(const ConstMatrixBase<T, BaseClass>& m)
00088       throw (MatrixException)
00089    {
00090       if ((!m.isSquare()) || (m.rows() == 0))
00091       {
00092          MatrixException e("Invalid matrix for trace()");
00093          GPSTK_THROW(e);
00094       }
00095       size_t index = 0;
00096       T answer = m(index,index);
00097       for (index = 1; index < m.rows(); index++)
00098          answer += m(index,index);
00099       return answer;
00100    }
00101 
00105    template <class T, class BaseClass>
00106    inline T normF(const ConstMatrixBase<T, BaseClass>& m)
00107    {
00108       T sum(0);
00109       size_t i,j;
00110       for (i = 0; i < m.rows(); i++)
00111          for (j = 0; j < m.cols(); j++)
00112             sum += m(i,j) * m(i,j);
00113       return SQRT(sum);
00114    }
00115 
00119    template <class T, class BaseClass>
00120    inline T normCol(const ConstMatrixBase<T, BaseClass>& m)
00121    {
00122       T sum(0), tempSum;
00123       size_t i,j;
00124       for (i = 0; i < m.rows(); i++)
00125       {
00126          tempSum = T(0);
00127          for (j = 0; j < m.cols(); j++)
00128             tempSum += ABS(m(i,j));
00129          if (tempSum > sum)
00130             sum = tempSum;
00131       }
00132       return sum;
00133    }
00134 
00139    template <class T, class BaseClass>
00140    inline T slowDet(const ConstMatrixBase<T, BaseClass>& l)
00141    {
00142       if (!l.isSquare() || (l.rows() <= 1))
00143       {
00144          MatrixException e("Invalid matrix for det()");
00145          GPSTK_THROW(e);
00146       }
00147          // go recursion!
00148       if (l.rows() == 2)
00149          return l(0,0)*l(1,1) - l(0,1)*l(1,0);
00150       else
00151       {
00152             // use v[0,0] * det(minor matrix(0,0)) + 
00153             //     v[0,1] * det(minor matrix(0,1)) + ...
00154          size_t i;
00155          int sign;
00156          T det = 0;
00157          for (i = 0; i < l.rows(); i++)
00158          {
00159             sign = (i % 2) ? -1 : 1;
00160             if (l(0,i) != 0)
00161                det += sign * l(0,i) * slowDet(minorMatrix(l,0,i));
00162          }
00163          return det;
00164       }
00165    }
00166 
00168  
00169 }  // namespace
00170 
00171 #endif

Generated on Thu Feb 9 03:30:58 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1