LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiMeansFunctor.h

00001 /*
00002  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiMeansFunctor.h
00027  * authors ....: Jochen Wickel
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 25.9.2000
00030  * revisions ..: $Id: ltiMeansFunctor.h,v 1.8 2006/02/08 12:36:03 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_MEANS_FUNCTOR_H_
00034 #define _LTI_MEANS_FUNCTOR_H_
00035 
00036 #include "ltiStatisticsFunctor.h"
00037 #include "ltiVector.h"
00038 #include "ltiMatrix.h"
00039 
00040 namespace lti {
00041   /**
00042    * Functor which computes several kinds of means for
00043    * vectors and matrices.
00044    *
00045    * Be careful with the terms used here.  If you have following matrix
00046    *
00047    * \code
00048    * |  5  6  10  3 |
00049    * |  1 -2   4  5 |
00050    * |  3  2   1 -2 |
00051    * \endcode
00052    *
00053    * The meanOfRows means the mean of all rows, i.e. a row vector containing
00054    * (for the arithmetical mean) the sum of all row vectors divided by the
00055    * number of rows.  This is (3 2 5 2).
00056    *
00057    * The meanOfColumns means the mean of all columns, i.e. a column vector
00058    * containing (for the arithmetical mean) the sum of all column vectors
00059    * divided by the number of columns.  This is (6 2 1)^T.
00060    *
00061    * The parameter parameters::rowWise indicates that the matrix will be
00062    * seen as a set of row vectors (if true), or column vectors (false).
00063    *
00064    * @ingroup gStats
00065    */
00066   template <class T> class meansFunctor : public statisticsFunctor {
00067   public:
00068     /**
00069      * the parameters for the class meansFunctor
00070      */
00071     class parameters : public statisticsFunctor::parameters {
00072     public:
00073       /**
00074        * default constructor
00075        */
00076       parameters(): statisticsFunctor::parameters() {
00077         rowWise=true;
00078         geometric=false;
00079       };
00080 
00081       /**
00082        * copy constructor
00083        * @param other the parameters object to be copied
00084        */
00085       parameters(const parameters& other) 
00086         : statisticsFunctor::parameters() {
00087         copy(other);
00088       };
00089 
00090       /**
00091        * destructor
00092        */
00093       ~parameters() {};
00094 
00095       /**
00096        * returns name of this type
00097        */
00098       const char* getTypeName() const {
00099         return "meansFunctor::parameters";
00100       };
00101 
00102       /**
00103        * copy the contents of a parameters object
00104        * @param other the parameters object to be copied
00105        * @return a reference to this parameters object
00106        */
00107       parameters& copy(const parameters& other) {
00108 #     ifndef _LTI_MSC_6
00109         // MS Visual C++ 6 is not able to compile this...
00110         statisticsFunctor::parameters::copy(other);
00111 #     else
00112         // ...so we have to use this workaround.
00113         // Conditional on that, copy may not be virtual.
00114         statisticsFunctor::parameters& (statisticsFunctor::parameters::* p_copy)
00115           (const statisticsFunctor::parameters&) =
00116           statisticsFunctor::parameters::copy;
00117         (this->*p_copy)(other);
00118 #     endif
00119 
00120         rowWise=other.rowWise;
00121         geometric=other.geometric;
00122 
00123         return *this;
00124       };
00125 
00126       /**
00127        * returns a pointer to a clone of the parameters
00128        */
00129       virtual functor::parameters* clone() const {
00130         return new parameters(*this);
00131       };
00132 
00133       /**
00134        * If this flag is true, the mean computation will be row-wise, i.e.
00135        * the result will contain a sum of the rows. This is much faster
00136        * than column-wise, since data do not have to be copied.
00137        *
00138        * Default value: true
00139        */
00140       bool rowWise;
00141 
00142       /**
00143        * If this flag is true, the functor will compute the geometric
00144        * mean of the elements, i.e. pow(e1*e2*...*en,1/n)
00145        *
00146        * Default value: false
00147        */
00148       bool geometric;
00149     };
00150 
00151     /**
00152      * default constructor
00153      */
00154     meansFunctor();
00155 
00156     /**
00157      * copy constructor
00158      * @param other the object to be copied
00159      */
00160     meansFunctor(const meansFunctor& other);
00161 
00162     /**
00163      * destructor
00164      */
00165     virtual ~meansFunctor();
00166 
00167     /**
00168      * returns the name of this type ("meansFunctor")
00169      */
00170     virtual const char* getTypeName() const;
00171 
00172     /**
00173      * returns the mean of the given vector. The type of the mean
00174      * (arithmetic or geometric) is determined by the parameter
00175      * parameters::geometric
00176      * @param src vector<T> with the source data.
00177      * @return the result.
00178      */
00179     T apply(const vector<T>& src) const;
00180 
00181     /**
00182      * returns the geometric mean of the given vector. For convenience
00183      * we take the square root of the absolute value of the product of
00184      * all elements.
00185      * @param src vector<T> with the source data.
00186      * @return the geometric mean of the elements of src.
00187      */
00188     T geometricMean(const vector<T>& src) const;
00189 
00190     /**
00191      * returns the arithmetic mean of the given vector.
00192      * @param src vector<T> with the source data.
00193      * @return the arithmetic mean of the elements of src.
00194      */
00195     T arithmeticMean(const vector<T>& src) const;
00196 
00197     /**
00198      * computes the mean of the given vector. The type of the mean
00199      * (arithmetic or geometric) is determined by the parameter
00200      * parameters::geometric. For geometric mean we take the square
00201      * root of the absolute value of the product of all elements.
00202      * @param src vector<T> with the source data.
00203      * @param dest T where the result will be left.
00204      * @return a reference to the <code>dest</code>.
00205      */
00206     T &apply(const vector<T>& src, T& dest) const;
00207 
00208     /**
00209      * computes the arithmetic mean of the given vector.
00210      * @param src vector<T> with the source data.
00211      * @param dest T where the result will be left.
00212      * @return a reference to the <code>dest</code>.
00213      */
00214     T &arithmeticMean(const vector<T>& src, T& dest) const;
00215 
00216     /**
00217      * computes the geometric mean of the given vector. For convenience
00218      * we take the square root of the absolute value of the product of
00219      * all elements.
00220      * @param src vector<T> with the source data.
00221      * @param dest T where the result will be left.
00222      * @return a reference to the <code>dest</code>.
00223      */
00224     T &geometricMean(const vector<T>& src, T& dest) const;
00225 
00226     /**
00227      * The result of this function depends on the value of
00228      * parameters::rowWise. If this parameter is true, the functor
00229      * will compute a vector, whose elements contain each the mean of
00230      * one column of the matrix (the computes the mean of the rows,
00231      * where each row is a data point in n-dimensional space.  if
00232      * rowWise is false, the result vector contains the mean of the
00233      * columns of the matrix (each column a data point).  The type of
00234      * the mean (arithmetic or geometric) is determined by the value
00235      * of parameters::geometric. For the geometric mean we take the
00236      * square root of the absolute value of the product of all
00237      * elements.
00238      * @param src matrix<T> with the source data.
00239      * @param dest matrix<T> where the result will be left.
00240      * @return a reference to the <code>dest</code>.
00241      */
00242     vector<T>& apply(const matrix<T>& src, vector<T>& dest) const;
00243 
00244     /**
00245      * This function
00246      * will compute a vector, whose elements contain each the mean
00247      * of one column of the matrix (the method computes the mean of the rows,
00248      * where each row is a data point in n-dimensional space).
00249      * The type of the mean (arithmetic or geometric) is determined by
00250      * the value of parameters::geometric. For the geometric mean we take the
00251      * square root of the absolute value of the product of all
00252      * elements.
00253      * @param src matrix<T> with the source data.
00254      * @param dest matrix<T> where the result will be left.
00255      */
00256     void meanOfRows(const matrix<T>& src, vector<T>& dest) const;
00257 
00258     /**
00259      * This function
00260      * will compute a vector, whose elements contain each the geometric
00261      * mean
00262      * of one column of the matrix (the method computes the mean of the rows,
00263      * where each row is a data point in n-dimensional space).
00264      * @param src matrix<T> with the source data.
00265      * @param dest matrix<T> where the result will be left.
00266      */
00267     void geometricMeanOfRows(const matrix<T>& src, vector<T>& dest) const;
00268 
00269     /**
00270      * This function will compute a vector, whose elements contain
00271      * each the arithmetic mean of one column of the matrix (the
00272      * method computes the mean of the rows, where each row is a data
00273      * point in n-dimensional space).
00274      * @param src matrix<T> with the source data.
00275      * @param dest matrix<T> where the result will be left.
00276      */
00277     void arithmeticMeanOfRows(const matrix<T>& src, vector<T>& dest) const;
00278 
00279     /**
00280      * The result vector contains the mean of the columns of the
00281      * matrix (each column a data point).  The type of the mean
00282      * (arithmetic or geometric) is determined by the value of
00283      * parameters::geometric. For the geometric mean we take the
00284      * square root of the absolute value of the product of all
00285      * elements.
00286      * @param src matrix<T> with the source data.
00287      * @param dest matrix<T> where the result will be left.
00288      */
00289     void meanOfColumns(const matrix<T>& src, vector<T>& dest) const;
00290 
00291     /**
00292      * The result vector contains the arithmetic mean
00293      * of the columns of the matrix (each column a data point).
00294      * @param src matrix<T> with the source data.
00295      * @param dest matrix<T> where the result will be left.
00296      */
00297     void arithmeticMeanOfColumns(const matrix<T>& src, vector<T>& dest) const;
00298 
00299     /**
00300      * The result vector contains the geometric mean of the columns of
00301      * the matrix (each column a data point). For convenience we take
00302      * the square root of the absolute value of the product of all
00303      * elements.
00304      * @param src matrix<T> with the source data.
00305      * @param dest matrix<T> where the result will be left.
00306      */
00307     void geometricMeanOfColumns(const matrix<T>& src, vector<T>& dest) const;
00308 
00309     /**
00310      * copy data of "other" functor.
00311      * @param other the functor to be copied
00312      * @return a reference to this functor object
00313      */
00314     meansFunctor& copy(const meansFunctor& other);
00315 
00316     /**
00317      * returns a pointer to a clone of this functor.
00318      */
00319     virtual functor* clone() const;
00320 
00321     /**
00322      * returns used parameters
00323      */
00324     const parameters& getParameters() const;
00325   };
00326 }
00327 
00328 #endif

Generated on Sat Apr 10 15:25:50 2010 for LTI-Lib by Doxygen 1.6.1