discrete.cc

Go to the documentation of this file.
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 #include "fastlib/math/discrete.h"
00039 //#include "discrete.h"
00040 
00041 #include <stdlib.h>
00042 
00043 double math::BinomialCoefficient(int n, int k) {
00044   int n_k = n - k;
00045   double nchsk = 1;
00046   int i;
00047   
00048   if(k > n || k < 0) {
00049     return 0;
00050   }
00051   
00052   if(k < n_k) {
00053     k = n_k;
00054     n_k = n - k;
00055   }
00056   
00057   for(i = 1; i <= n_k; i++) {
00058     nchsk *= (++k);
00059     nchsk /= i;
00060   }
00061   return nchsk;
00062 }
00063 
00064 double math::Factorial(int d) {
00065   double v = 1;
00066   
00067   DEBUG_ASSERT(d >= 0);
00068   
00069   for (int i = 2; i <= d; i++) {
00070     v *= i;
00071   }
00072   
00073   return v;
00074 }
00075 
00076 void math::MakeIdentityPermutation(index_t size, index_t *array) {
00077   for (index_t i = 0; i < size; i++) {
00078     array[i] = i;
00079   }
00080 }
00081 
00082 void math::MakeRandomPermutation(index_t size, index_t *array) {
00083   // Regular permutation algorithm.
00084   // This is cache inefficient for large sizes; large caches might
00085   // warrant a more sophisticated blocked algorithm.
00086   
00087   if (unlikely(size == 0)) {
00088     return;
00089   }
00090   
00091   array[0] = 0;
00092   
00093   for (index_t i = 1; i < size; i++) {
00094     index_t victim = rand() % i;
00095     array[i] = array[victim];
00096     array[victim] = i;
00097   }
00098 }
00099 
00100 void math::MakeInversePermutation(index_t size,
00101     const index_t *original, index_t *reverse) {
00102   for (index_t i = 0; i < size; i++) {
00103     reverse[original[i]] = i;
00104   }
00105 }
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3