discrete.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00038 #ifndef MATH_DISCRETE_H
00039 #define MATH_DISCRETE_H
00040 
00041 #include "fastlib/base/base.h"
00042 #include "fastlib/col/arraylist.h"
00043 
00044 #include <math.h>
00045 
00046 namespace math {
00050   COMPILER_FUNCTIONAL
00051   double Factorial(int d);
00052   
00061   COMPILER_FUNCTIONAL
00062   double BinomialCoefficient(int n, int k);
00063 
00075   void MakeIdentityPermutation(index_t size, index_t *array);
00076   
00085   inline void MakeIdentityPermutation(
00086       index_t size, ArrayList<index_t> *result) {
00087     result->Init(size);
00088     MakeIdentityPermutation(size, result->begin());
00089   }
00090   
00100   void MakeRandomPermutation(index_t size, index_t *array);
00101   
00108   inline void MakeRandomPermutation(
00109       index_t size, ArrayList<index_t> *result) {
00110     result->Init(size);
00111     MakeRandomPermutation(size, result->begin());
00112   }
00113 
00117   void MakeInversePermutation(index_t size,
00118       const index_t *original, index_t *reverse);
00119 
00123   inline void MakeInversePermutation(
00124       const ArrayList<index_t>& original, ArrayList<index_t> *reverse) {
00125     reverse->Init(original.size());
00126     MakeInversePermutation(original.size(), original.begin(), reverse->begin());
00127   }
00128   
00129   template<typename TAnyIntegerType>
00130   inline bool IsPowerTwo(TAnyIntegerType i) {
00131     return (i & (i - 1)) == 0;
00132   }
00133   
00139   inline unsigned IntLog2(unsigned i) {
00140     unsigned l;
00141     for (l = 0; (unsigned(1) << l) != i; l++) {
00142       DEBUG_ASSERT_MSG(l < 1024, "Taking IntLog2 of a non-power-of-2: %u.", i);
00143     }
00144     return l;
00145   }
00146 };
00147 
00148 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3