MetricMatrixCalc.h

Go to the documentation of this file.
00001 // $Id: MetricMatrixCalc.h 411 2007-11-09 17:31:45Z glandrum $
00002 //
00003 //  Copyright (C) 2003-2006 Rational Discovery LLC
00004 //
00005 //  @@ All Rights Reserved @@
00006 //
00007 #ifndef __RD_METRICMATRIXCAL_H__
00008 #define __RD_METRICMATRIXCAL_H__
00009 
00010 #include "MetricFuncs.h"
00011 #include <RDGeneral/Invariant.h>
00012 
00013 namespace RDDataManip {
00014   
00015   /*! \brief A generic metric matrix calculator (e.g similarity matrix or
00016    *         distance matrix) 
00017    *
00018    *  This templated class needs some explanation
00019    *    vectType is a container that can support [] operator 
00020    *    entryType is the type of entry that is returned by the [] operator
00021    *  Examples of the container include PySequenceHolder which is wrapper around 
00022    *  a python sequence objects like lists and tuples.
00023    *  Examples of the entryType include a sequence of double, floats, and ExplicitBitVects 
00024    *
00025    */
00026   template <class vectType, class entryType> class MetricMatrixCalc {
00027   public:
00028     /*! \brief Default Constructor
00029      *
00030      */
00031     MetricMatrixCalc() {};
00032     
00033     /*! \brief Set the metric function
00034      *
00035      * Set the pointer to the mertic funvtion to be used by the metric calculator
00036      *
00037      * ARGUMENTS:
00038      *
00039      *  mFunc - pointer to the metric funtion
00040      */
00041     void setMetricFunc(double (*mFunc)(const entryType &, const entryType &, unsigned int)) {
00042       dp_metricFunc = mFunc;
00043     }
00044 
00045     /*! \brief The calculator function
00046      *
00047      * ARGUMENTS:
00048      *
00049      *  descrips - vectType container with a entryType for each item
00050      *  nItems - the number of item in the descripts.
00051      *           In several cases this argument is irrelvant since vectType probably supports
00052      *           a size() member function, But we would like this interface to take for example 
00053      *           a double** and correctly parse the row and columns.
00054      *  dim - the dimension of the sequences
00055      *  distMat - pointer to an array to write the distance matrix to
00056      *            it is assumed that the right sized array has already be allocated.
00057      *
00058      * FIX: we can probably make this function create the correct sized distMat and return
00059      * it to the caller, but when pushing he result out to a python array not sure how to
00060      * avoid copy the entire distance matrix in that case
00061      *
00062      * RETURNS:
00063      * 
00064      *  pointer to a 1D array of doubles. Only the lower triangle elements are
00065      *  included in the array
00066      */
00067     void calcMetricMatrix(const vectType &descripts, unsigned int nItems, unsigned int dim,
00068                           double *distMat) {
00069       CHECK_INVARIANT(distMat, "invalid pointer to a distance matix");
00070       
00071       for (unsigned int i = 1; i < nItems; i++) {
00072         unsigned int itab = i*(i-1)/2;
00073         for (unsigned int j = 0; j < i; j++) {
00074           distMat[itab+j] = dp_metricFunc(descripts[i], descripts[j], dim);
00075         }
00076       }
00077     };
00078     
00079   private:
00080     // pointer to the metric function
00081     /*! \brief pointer to the metric function
00082      *
00083      * In several cases the last argument 'dim' should be irrelevant, 
00084      * For example when entryType is a bit vector the size is of the vector
00085      * or the dimension can be obtained by asking the bit vector itself. However
00086      * we woul like this interface to support other containers lines double* 
00087      * in which case the 'dim' value is useful in cumputing the metric.
00088      */  
00089     double (*dp_metricFunc)(const entryType &, const entryType &, unsigned int);
00090     
00091   };
00092 };
00093 
00094 #endif

Generated on Sat May 24 08:36:32 2008 for RDCode by  doxygen 1.5.3