?? math_util.cc
字號:
/* $Id: math_util.cc,v 1.3 2006-08-09 15:20:54 jonathan Exp $ * Jonathan Ledlie, Harvard University. * Copyright 2005. All rights reserved. */#include <assert.h>#include "math_util.h"double poisson (double mean){ double u; while ((u = randPct()) == 0.) ; return (-mean) * log(u);};int poisson (int mean){ return (int)(poisson((double)mean));}double mean (vector<double> v) { double sum = 0.; for (int i = 0; i < v.size(); i++) { sum += v[i]; } return sum/(double)v.size();}double square (double x){ return x*x;}double stddev (vector<double> v, double mean) { double sum = 0.; if (v.size() <= 1) return 0.; for (int i = 0; i < v.size(); i++) { sum += pow(v[i]-mean,2); } return sqrt (sum/(v.size()-1));}double percentile (vector<double> v, double percentile) { //cout << "size " << v.size() << " pct " << percentile << endl; if (v.size() == 0) { return 0; } if (v.size() == 1) { return v[0]; } sort (v.begin(), v.end()); assert (percentile >= 0 && percentile <= 1); int index = (int)(rint (percentile * v.size())); //cout << "size " << v.size() << " pct " << percentile //<< " index " << index << " v[i] " << v[index] << endl; return v[index];}//返回0-1之間的隨機數(shù)double randPct () { double ret = Rand (); assert (ret >= 0. && ret < 1); return ret;}// Returns a number (a,b]int unifRand (int a, int b){ assert (a >= 0 && b >= 0); int diff = ABS ((a-b)); int randValue = (int)(Rand()*(double)diff); return (int)(a + randValue);}// Returns a list of numbers (a,b]vector<int> unifRands (int count, int a, int b){ vector<int> rands; rands.reserve (count); for (int i = 0; i < count; i++) { rands.push_back (unifRand (a, b)); } return rands;}double log16 (double x){ const double logOf16 = log(16.); return log(x)/logOf16;}double log8 (double x){ const double logOf8 = log(8.); return log(x)/logOf8;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -