⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testrandom.cc

📁 一个用MATLAB语言编写的摄像机标定工具箱,内容丰富
💻 CC
字号:
//
// testrandom
//
// $Id: testrandom.cc,v 1.1.1.1 2001/02/28 00:28:38 cstolte Exp $
//

#include <sgl.h>
#include <gl/gl.h>
#include <unistd.h>
#include <iostream>
using namespace std;

int iteration;
Random *ran;

const int n_buckets = 300;
int first_pass_iters = 5000;
int second_pass_iters = 100000;

class sample_set {
  public:
    sample_set(const char *t);
    ~sample_set();

    void start_first_pass();
    void first_pass_sample(double s);
    void finish_first_pass();

    void add_sample(double s);
    void graph();

  private:
    int quantize_sample(double s) { return int((n_buckets * (s - low)) / (high - low)); }
    const char *title;
    double low, high;
    int *points;
};

static void test_disk();
static void test_vector1();
static void test_vector2();
static void test_3vec1();
static void test_3vec2();
static void test_uniform();
static void test_normal();
static void test_exponential();
static void test_beta();
static void test_gamma();
static void test_chi();
static void test_Fdist();
static void test_tdist();
static void test_geometric();
static void test_binomial();
static void test_poisson();

int main(int argc, char *argv[]) {
    int seed = getpid();
    int times = 2;
    bool forever = false;
    ArgParser ap;
    ap('1', "first-pass-iters", &first_pass_iters);
    ap('2', "second-pass-iters", &second_pass_iters);
    ap('n', "n-times-through", &times);
    ap('s', "seed", &seed);
    ap('f', "forever", &forever);
    ap.process("testrandom", argc, argv);
    ran = new Random(seed);

    if (forever == true) {
	times = -1;
    }
    cerr << "Starting test of _random. Seed = " << seed << 
	", iterations = " << times << endl;

    for (iteration = 0; iteration != times; ++iteration) {
	test_disk();
	test_vector1();
	test_vector2();
	test_3vec1();
	test_3vec2();
	test_normal();
	test_uniform();
	test_exponential();
	test_gamma();
	test_beta();
	test_chi();
	test_Fdist();
	test_tdist();
	test_geometric();
	test_binomial();
	test_poisson();
    }
    delete ran;
    sleep(20);
}

static void test_disk() {
    foreground();
    prefsize(512, 512);
    winopen("test disk");
    ortho2(-1.1, 1.1, -1.1, 1.1);
    viewport(0, 511, 0, 511);
    RGBmode();
    gconfig();
    clear();
    float c[3] = { 1, 0, 1 };
    c3f(c);
    circ(0, 0, 1);
    c[2] = 0;
    c3f(c);

    double pt[2];
    for (int i = 0; i < 3000; ++i) {
	ran->disk(&pt[0], &pt[1]);
	bgnpoint();
	v2d(pt);
	endpoint();
    }
}

static void test_vector1() {
    foreground();
    prefsize(512, 512);
    winopen("test vec2--a");
    ortho2(-1.1, 1.1, -1.1, 1.1);
    viewport(0, 511, 0, 511);
    RGBmode();
    gconfig();
    clear();
    float c[3] = { 1, 0, 1 };
    c3f(c);
    circ(0, 0, 1);
    c[2] = 0;
    c3f(c);

    double pt[2];
    for (int i = 0; i < 1000; ++i) {
	ran->vector2(&pt[0], &pt[1]);
	bgnline();
	v2d(pt);
	pt[0] = 0; pt[1] = 0;
	v2d(pt);
	endline();
    }
}

static void test_vector2() {
    foreground();
    prefsize(512, 512);
    winopen("test vec2--b");
    ortho2(-1.1, 1.1, -1.1, 1.1);
    viewport(0, 511, 0, 511);
    RGBmode();
    gconfig();
    clear();
    float c[3] = { 1, 0, 1 };
    c3f(c);
    circ(0, 0, 1);
    c[2] = 0;
    c3f(c);

    double pt[2];
    for (int i = 0; i < 1000; ++i) {
	Vector2 v = ran->vector2();
	pt[0] = v.u; pt[1] = v.v;
	bgnline();
	v2d(pt);
	pt[0] = 0; pt[1] = 0;
	v2d(pt);
	endline();
    }
}

static void test_3vec1() {

}

static void test_3vec2() {

}

static void test_uniform() {
    sample_set uni("uniform");
    uni.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	uni.first_pass_sample(ran->uniform());
    uni.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	uni.add_sample(ran->uniform());
    uni.graph();
}

static void test_normal() {
    sample_set norm("normal");
    norm.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	norm.first_pass_sample(ran->normal(1., .5));
    norm.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	norm.add_sample(ran->normal(1., .5));
    norm.graph();
}

static void test_exponential() {
    double mu = -5 + 10 * ran->uniform();
    sample_set norm("exponential");
    norm.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	norm.first_pass_sample(ran->exponential(mu));
    norm.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	norm.add_sample(ran->exponential(mu));
    norm.graph();
}

static void test_beta() {
    double a = -5 + 10 * ran->uniform();
    double b = -5 + 10 * ran->uniform();
    sample_set ss("beta");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->beta(a, b));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->beta(a, b));
    ss.graph();
}

static void test_gamma() {
    double a = -5 + 10 * ran->uniform();
    sample_set ss("gamma");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->gamma(a));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->gamma(a));
    ss.graph();
}

static void test_chi() {
    double nu = -5 + 10 * ran->uniform();
    sample_set ss("chi");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->chi(nu));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->chi(nu));
    ss.graph();
}

static void test_Fdist() {
    double nu1 = -5 + 10 * ran->uniform();
    double nu2 = -5 + 10 * ran->uniform();
    sample_set ss("Fdist");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->Fdist(nu1, nu2));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->Fdist(nu1, nu2));
    ss.graph();
}

static void test_tdist() {
    double nu = -5 + 10 * ran->uniform();
    sample_set ss("tdist");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->tdist(nu));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->tdist(nu));
    ss.graph();
}

static void test_geometric() {
    double p = -5 + 10 * ran->uniform();
    sample_set ss("geometric");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->geometric(p));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->geometric(p));
    ss.graph();
}

static void test_binomial() {
    double p = -5 + 10 * ran->uniform();
    int trials = ran->integer(30);
    sample_set ss("binomial");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->binomial(p, trials));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->binomial(p, trials));
    ss.graph();
}

static void test_poisson() {
    double mu = -5 + 10 * ran->uniform();
    sample_set ss("poisson");
    ss.start_first_pass();
    for (int i = 0; i < first_pass_iters; ++i)
	ss.first_pass_sample(ran->poisson(mu));
    ss.finish_first_pass();
    for (i = 0; i < second_pass_iters; ++i)
	ss.add_sample(ran->poisson(mu));
    ss.graph();
}

//
// sample_set
//

sample_set::sample_set(const char *t) {
    title = t;
    points = new int[n_buckets];
    for (int i = 0; i < n_buckets; ++i)
	points[i] = 0;
}
 
sample_set::~sample_set() {
    delete[] points;
}

void
sample_set::start_first_pass() {
    low = 1000000;
    high = -low;
}

void
sample_set::first_pass_sample(double s) {
    if (s < low)
	low = s;
    if (s > high)
	high = s;
}

void 
sample_set::finish_first_pass() {
    if (low > 0)
	low /= 1.2;
    else
	low *= 1.2;
    if (high > 0)
	high *= 1.2;
    else
	high /= 1.2;
}

void
sample_set::add_sample(double s) {
    int i = quantize_sample(s);
    if (i >= 0 && i < n_buckets)
	++points[i];
    else
	cerr << "sample " << s << " out of range for " << title << endl;
}

void
sample_set::graph() {
    int max_hits = 0;
    for (int i = 0; i < n_buckets; ++i)
	if (points[i] > max_hits)
	    max_hits = points[i];

    foreground();
    prefsize(2 * n_buckets, 2 * n_buckets);
    winopen((String)title);
    ortho2(0, n_buckets, 0, 1.1 * max_hits);
    viewport(0, 2 * n_buckets - 1, 0, 2 * n_buckets - 1);
    RGBmode();
    gconfig();
    clear();

    float col[3] = { 0, 1, .2 };
    c3f(col);
    bgnline();
    double pt[2];
    for (i = 0; i < n_buckets; ++i) {
	pt[0] = i;
	pt[1] = points[i];
	v2d(pt);
    }
    endline();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -