discrete.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00038 #include "fastlib/math/discrete.h"
00039
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
00084
00085
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 }