1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/************ ranliptest - an example of usage ************************
* of ranlip library using static linking
*
* begin : May 9 2004
* version : 1.0
* copyright : (C) 2004 by Gleb Beliakov
* email : gleb@deakin.edu.au
*
*
*
* This example shows the usage of the class interface to access
* ranlip library. For the class interface, we derive a
* new class from CRanLib (declared in ranlip.h) , called
* MyRnumGen, with the member Distribution(), which implements
* the desired distribution density function. The subsequent calls
* to the members of CRanLip are as per documentation of this class
*
*
*
* Copyright (C) 2004 Gleb Beliakov (gleb@deakin.edu.au)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;
#include <math.h>
#include <ranlip/ranlip.h>
class MyRnumGen:public CRanLip {
public:
virtual double Distribution(double* p) ;
};
double MyRnumGen::Distribution(double* p)
{ // example: multivariate normal distribution
double r;
r=0;
for(int j=0;j<Dimension;j++)
r += p[j]*p[j];
return exp(-r);
}
int main(int argc, char *argv[])
{
int dim=3;
int i,j;
MyRnumGen MyGen;
int timesgen=10000;
double *P=(double*) malloc(dim*sizeof(double));
double *a, *b;
a=(double*) malloc(dim*sizeof(double));
b=(double*) malloc(dim*sizeof(double));
for(i=0;i<dim;i++) {a[i]=-1; b[i]=1;}
MyGen.Init(dim,a,b);
MyGen.PrepareHatFunction(20,8,2);
MyGen.Seed(10);
// generation step
for(j=0;j<timesgen;j++)
MyGen.RandomVec(P);
MyGen.SavePartition("partition.txt");
MyGen.FreeMem();
MyGen.Init(dim,a,b);
MyGen.PrepareHatFunctionAuto(20,8);
MyGen.Seed(10);
// generation step
for(j=0;j<timesgen;j++)
MyGen.RandomVec(P);
cout <<"Computed Lipschitz const: "<< MyGen.Lipschitz << endl;
cout<<"Acceptance ratio: "<< (double)timesgen/(double)MyGen.count_total <<endl;
cout<<"errors due to low Lipschitz constant: "<<MyGen.count_errors<< endl;
MyGen.LoadPartition("partition.txt");
// generation step
for(j=0;j<timesgen;j++)
MyGen.RandomVec(P);
cout<< "Loading saved hat function..."<<endl;
cout<<"Acceptance ratio: "<< (double)timesgen/(double)MyGen.count_total <<endl;
cout<<"errors due to low Lipschitz constant: "<<MyGen.count_errors<< endl;
return 0;
}
|