GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
norm.h
00001 /*
00002  * The GURLS Package in C++
00003  *
00004  * Copyright (C) 2011-1013, IIT@MIT Lab
00005  * All rights reserved.
00006  *
00007  * author:  M. Santoro
00008  * email:   msantoro@mit.edu
00009  * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  *
00015  *     * Redistributions of source code must retain the above
00016  *       copyright notice, this list of conditions and the following
00017  *       disclaimer.
00018  *     * Redistributions in binary form must reproduce the above
00019  *       copyright notice, this list of conditions and the following
00020  *       disclaimer in the documentation and/or other materials
00021  *       provided with the distribution.
00022  *     * Neither the name(s) of the copyright holders nor the names
00023  *       of its contributors or of the Massacusetts Institute of
00024  *       Technology or of the Italian Institute of Technology may be
00025  *       used to endorse or promote products derived from this software
00026  *       without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00031  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039  * POSSIBILITY OF SUCH DAMAGE.
00040  */
00041 
00042 #ifndef _GURLS_NORM_H_
00043 #define _GURLS_NORM_H_
00044 
00045 #include <cstring>
00046 #include <cmath>
00047 
00048 #include "gurls++/gvec.h"
00049 #include "gurls++/gmat2d.h"
00050 #include "gurls++/exceptions.h"
00051 
00052 #include "gurls++/optlist.h"
00053 
00054 namespace gurls
00055 {
00056 
00057 static const std::string L0norm = "l0";     
00058 static const std::string L1norm = "l1";     
00059 static const std::string L2norm = "l2";     
00060 static const std::string LInfnorm = "inf";  
00061 
00065 template<typename T>
00066 T norm(const gVec<T>& x, std::string type = "l2"){
00067     T nrm = 0;
00068     const T* ptr = x.getData();
00069 
00070     if (type.compare(L0norm) == 0){
00071         for(unsigned long i =0;i < x.getSize(); ++i){
00072             if (*ptr++ != 0)
00073                 nrm++;
00074         }
00075         return nrm;
00076     }else if (type.compare(L1norm) == 0) {
00077         for(unsigned long i =0; i < x.getSize(); ++i){
00078             nrm+= static_cast<T>(std::abs(*ptr++));
00079         }
00080         return nrm;
00081     }else if (type.compare(L2norm) == 0) {
00082         for(unsigned long i =0; i < x.getSize(); ++i, ++ptr){
00083             nrm+= std::pow(*ptr, 2);
00084         }
00085         return static_cast<T>(std::sqrt(nrm));
00086     }else if (type.compare(LInfnorm) == 0 ) {
00087         throw gException("Sorry. You required the computation of LInfnorm but this functionality has not been implemented yet!");
00088     }else {
00089         throw gException("Unknown norm type.");
00090     }
00091 
00092     return nrm;
00093 }
00094 
00098 template<typename T>
00099 T norm(const gMat2D<T>& A, std::string type = "l2"){
00100 
00101     throw gException("Sorry. You required to compute the norm of a matrix but this functionality has not been implemented yet!");
00102 }
00103 
00104 template<typename T>
00105 class NormL2;
00106 
00107 template<typename T>
00108 class NormZScore;
00109 
00110 template<typename T>
00111 class NormTestZScore;
00112 
00118 class BadNormCreation : public gException
00119 {
00120 public:
00121 
00125     BadNormCreation(const std::string& type): gException("Cannot create type " + type) {}
00126 };
00127 
00132 template<typename T>
00133 class Norm
00134 {
00135 public:
00136 
00144     virtual GurlsOptionsList* execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt) = 0;
00145 
00152     static Norm<T> *factory(const std::string& id) throw(BadNormCreation)
00153     {
00154         if(id == "l2")
00155             return new NormL2<T>;
00156         if(id == "zscore")
00157             return new NormZScore<T>;
00158         if(id == "testzscore")
00159             return new NormTestZScore<T>;
00160 
00161         throw BadNormCreation(id);
00162     }
00163 };
00164 
00165 }
00166 
00167 #endif // _GURLS_NORM_H_
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends