############################################################################

[main]

[main - data:]

# method
method: PINV

[main - header:]

/* header files */
\#include <methods/x_gen_source.h>

/* -- which tests for u-error should be performed (for development) -- */

\#define DEVEL FALSE    /* whether to run tests in development mode */
\#define ALL   TRUE

/* Remark:
   Setting ALL to FALSE is useful if a only a single validation test
   should be run. Thus the "if(ALL)" condition for that particular test
   has to be replaced by "if(TRUE)".
*/

\#define UERROR_STANDARDDIST  TRUE
\#define UERROR_QUASIPDF      TRUE
\#define UERROR_VANISHINGPDF  TRUE
\#define UERROR_TRUNCATED     TRUE
\#define UERROR_UNBOUNDEDPDF  TRUE
\#define UERROR_MULTIMODAL    TRUE
\#define UERROR_MULTIMODAL_VANISHINGPDF TRUE

/* orders */
static int UERROR_ORDER_MIN  =  3;
static int UERROR_ORDER_MAX  = 17;
static int UERROR_ORDER_STEP =  1;

/* smoothness */
static int SMOOTHNESS_MAX = 2;

/* u-resolutions */
static double UERROR_URESOLUTION_MAX = 1.e-8;
static double UERROR_URESOLUTION_MIN = 0.99e-13;
static double UERROR_URESOLUTION_STEP = 0.1;

/* we run all tests only in 'fullcheck' mode */
\#define set_uerror_tests() \
  if (!fullcheck) { \
     /* UERROR_ORDER_STEP =  3; */ \
     UERROR_URESOLUTION_MAX = 1.e-8; \
     UERROR_URESOLUTION_MIN = 0.99e-12; \
     UERROR_URESOLUTION_STEP = 0.01; \
  }
/* see also SKIPORDER in routine pinv_error_experiment() below */

/* sample size */
static const int UERROR_SAMPLESIZE = 100000;


/* -- constants -- */

\#define COMPARE_SAMPLE_SIZE   (500)
\#define VIOLATE_SAMPLE_SIZE   (20)

/* \#define SEED   (298346) */

\#ifndef M_PI
\#define M_PI       3.14159265358979323846264338328      /* pi */
\#endif


/* -- macros -- */

\#define set_debug_flag(par) \
   do { \
      if (fullcheck) unur_set_debug((par),~0u); \
      else           unur_set_debug((par),0x00010000u); \
   } while (0);


/* -- prototypes -- */

int pinv_error_experiment( UNUR_PAR *par, int samplesize, int UERROR_PDF, int UERROR_CDF);
int pinv_error_gen_experiment( UNUR_GEN *gen, 
                               double u_resolution, int order, int samplesize ); 

double neg_exponential_cdf( double x, const UNUR_DISTR *distr );
double neg_exponential_pdf( double x, const UNUR_DISTR *distr );

double neg_exponential_1_cdf( double x, const UNUR_DISTR *distr );
double neg_exponential_1_pdf( double x, const UNUR_DISTR *distr );

double sin_2_cdf( double x, const UNUR_DISTR *distr );
double sin_2_pdf( double x, const UNUR_DISTR *distr );

double sin_10_cdf( double x, const UNUR_DISTR *distr );
double sin_10_pdf( double x, const UNUR_DISTR *distr );




#############################################################################

[new]

[new - invalid NULL ptr: 
   distr = NULL; ]

~( distr );
	--> expected_NULL --> UNUR_ERR_NULL

[new - invalid distribution type:
   distr = unur_distr_discr_new(); ]

~( distr );
	 --> expected_NULL --> UNUR_ERR_DISTR_INVALID

[new - data missing in distribution object:
   distr = unur_distr_cont_new(); ]

/* pdf */
~( distr );
	--> expected_NULL --> UNUR_ERR_DISTR_REQUIRED

#############################################################################

[set]

[set - invalid NULL ptr: 
   /* double stp[] = {1.,2.,3.}; */
   par = NULL; ]

~_order( par, 3 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_smoothness( par, 1 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_u_resolution( par, 1.e-8 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_use_upoints( par, TRUE );
	--> expected_setfailed --> UNUR_ERR_NULL

~_usepdf( par );
	--> expected_setfailed --> UNUR_ERR_NULL

~_usecdf( par );
	--> expected_setfailed --> UNUR_ERR_NULL

~_boundary(par,1.,3.);
	--> expected_setfailed --> UNUR_ERR_NULL

~_searchboundary(par, TRUE, TRUE )
	--> expected_setfailed --> UNUR_ERR_NULL

~_max_intervals(par,1000)
	--> expected_setfailed --> UNUR_ERR_NULL

~_extra_testpoints(par,10)
	--> expected_setfailed --> UNUR_ERR_NULL

~_keepcdf(par,TRUE)
	--> expected_setfailed --> UNUR_ERR_NULL


[set - invalid parameter object:
   /* double stp[] = {1.,2.,3.}; */
   distr = unur_distr_normal(NULL,0);
   par = unur_arou_new(distr); ]

~_order( par, 3 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_smoothness( par, 1 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_u_resolution( par, 1.e-8 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_use_upoints( par, TRUE );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_usepdf( par );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_usecdf( par );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_boundary(par,1.,3.);
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_searchboundary(par, TRUE, TRUE )
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_max_intervals(par,1000)
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_extra_testpoints(par,10)
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_keepcdf(par,TRUE)
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

unur_par_free(par);


[set - invalid parameters:
   distr = unur_distr_normal(NULL,0);
   par = unur_pinv_new(distr); ]

~_order( par, 30 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_order( par, 1 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_smoothness( par, 4 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_boundary(par,1.,-3.);
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_boundary(par,1.,UNUR_INFINITY);
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_max_intervals(par,1)
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_max_intervals(par,10000000)
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_extra_testpoints(par,-1)
	--> expected_setfailed --> UNUR_ERR_PAR_SET

unur_par_free(par);


[set - invalid parameters:
   UNUR_PAR *par;    
   UNUR_GEN *gen;    
   distr = unur_distr_normal(NULL,0); ]

par = unur_pinv_new(distr);
~_order( par, 4 );
~_smoothness( par, 1 );
gen = unur_init(par);
	--> none --> UNUR_ERR_GENERIC

unur_free(gen);
par = unur_pinv_new(distr);
~_order( par, 4 );
~_smoothness( par, 2 );
gen = unur_init(par);
	--> none --> UNUR_ERR_GENERIC

unur_free(gen);
par = unur_pinv_new(distr);
~_order( par, 6 );
~_smoothness( par, 2 );
gen = unur_init(par);
	--> none --> UNUR_ERR_GENERIC

unur_free(gen);


[set - missing CDF:
   distr = unur_distr_cont_new();
   unur_distr_cont_set_pdf(distr, neg_exponential_pdf);
   par = unur_pinv_new(distr); ]

~_usecdf( par );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

unur_par_free(par);


[set - missing PDF:
   distr = unur_distr_cont_new();
   unur_distr_cont_set_cdf(distr, neg_exponential_cdf);
   par = unur_pinv_new(distr); ]

~_usepdf( par );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

unur_par_free(par);


[set - missing PDF:
   UNUR_PAR *par;    
   UNUR_GEN *gen;    
   distr = unur_distr_cont_new();
   unur_distr_cont_set_cdf(distr, neg_exponential_cdf); ]

par = unur_pinv_new(distr);
~_usecdf( par );
~_smoothness( par, 1 );
gen = unur_init(par);
	--> none --> UNUR_ERR_DISTR_REQUIRED

unur_free(gen);
par = unur_pinv_new(distr);
~_usecdf( par );
~_smoothness( par, 2 );
gen = unur_init(par);
	--> none --> UNUR_ERR_DISTR_REQUIRED

unur_free(gen);
unur_distr_cont_set_pdf(distr, neg_exponential_pdf);
par = unur_pinv_new(distr);
~_usecdf( par );
~_smoothness( par, 2 );
gen = unur_init(par);
	--> none --> UNUR_ERR_DISTR_REQUIRED

unur_free(gen);


#############################################################################

[get]

[get - invalid generator object:
   distr = unur_distr_normal(NULL,0);
   par = unur_srou_new(distr);
   gen = unur_init(par); <-- ! NULL ]

~_n_intervals(gen);
        --> expected_zero --> UNUR_ERR_GEN_INVALID


#############################################################################

#[chg]

#############################################################################

# [init]

#############################################################################

#[reinit]

#[reinit - exists:
#   distr = unur_distr_normal(NULL,0);
#   par = unur_pinv_new(distr);
#   gen = unur_init( par ); <-- ! NULL ]
#
#unur_reinit( gen );
#	--> expected_reinit

#############################################################################

[sample]

[sample - invalid NULL ptr: 
   gen = NULL; ]

unur_pinv_eval_approxinvcdf(gen,0.5);
	--> expected_INFINITY --> UNUR_ERR_NULL

#.....................................................................

[sample - invalid generator object:
   distr = unur_distr_normal(NULL,0);
   par = unur_srou_new(distr);
   gen = unur_init(par); <-- ! NULL ]

unur_pinv_eval_approxinvcdf(gen,0.5);
	--> expected_INFINITY --> UNUR_ERR_GEN_INVALID


[sample - invalid domain:
   distr = unur_distr_normal(NULL,0);
   par = unur_pinv_new(distr);
   unur_set_debug(par,0);
   gen = unur_init( par ); <-- ! NULL ]

unur_pinv_eval_approxinvcdf(gen,1.5);
	--> expected_INFINITY --> UNUR_ERR_DOMAIN

unur_pinv_eval_approxinvcdf(gen,-0.5);
	--> expected_negINFINITY --> UNUR_ERR_DOMAIN

#.....................................................................

[sample - cdf table removed:
   distr = unur_distr_normal(NULL,0);
   par = unur_pinv_new(distr);
   unur_set_debug(par,0);
   gen = unur_init( par ); <-- ! NULL ]

unur_pinv_eval_approxcdf(gen,1.5);
	--> expected_INFINITY --> UNUR_ERR_GENERIC

#.....................................................................

[sample - check approximate cdf:
   int i, cdffailed=0;
   double x,u,d;
   gen = unur_str2gen("cont;logpdf='5-x';domain=(0,infinity)&method=pinv;keepcdf=on;debug=0"); ]

for (i=1; i<1000; i++) {
    x = 5.*i/1000.;
    u = unur_pinv_eval_approxcdf(gen,x);
    d = fabs(u - (1-exp(-x)));
    if (d > 1.e-11) { cdffailed = 1; }}
cdffailed;
	--> expected_zero --> UNUR_SUCCESS

#.....................................................................

[sample - compare clone:
   UNUR_GEN *clone;
   distr = unur_distr_normal(NULL,0); 
   par = NULL;
   gen = NULL; ]

/* original generator object */
par = unur_pinv_new(distr);
gen = unur_init(par);
        -->compare_sequence_gen_start

/* clone */
clone = unur_gen_clone(gen);
unur_free(gen);
gen = clone;
        -->compare_sequence_gen

#.....................................................................
#
#[sample - compare reinit:
#   distr = unur_distr_normal(NULL,0); 
#   par = NULL;
#   gen = NULL; ]
#
#/* original generator object */
#par = unur_pinv_new(distr);
#gen = unur_init(par);
#        -->compare_sequence_gen_start
#
#/* reinit */
#unur_reinit(gen);
#        -->compare_sequence_gen

#.....................................................................

[sample - compare stringparser:
   double fpar[] = {3.,4.};
   distr = NULL;
   par = NULL;
   gen = NULL; ]


distr = unur_distr_gamma(fpar,2);
unur_distr_cont_set_domain(distr,2.,UNUR_INFINITY);
par = unur_pinv_new(distr);
set_debug_flag(par);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,4.); domain = (2,infinity) & \
    method = pinv; debug = 0x1" );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_normal(NULL,0);
unur_distr_cont_set_domain(distr,2.,UNUR_INFINITY);
par = unur_pinv_new(distr);
unur_pinv_set_order(par,10);
unur_pinv_set_u_resolution(par,1.e-6);
unur_pinv_set_boundary(par,1.,1000.);
unur_pinv_set_max_intervals(par,1000);
set_debug_flag(par);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "normal; domain = (2,inf) & \
         method = pinv; order = 10; u_resolution = 1.e-6; \
         boundary = (1,1000); max_intervals = 1000; debug = 0x1" );
	-->compare_sequence_gen


#############################################################################

[validate]

[validate - generators:]

# default
par[0] = unur_pinv_new(@distr@);


[validate - distributions:]

# Normal distributions
distr[0] = unur_distr_normal(NULL,0);

# Normal distributions with quasi density
distr[1] = unur_distr_multPDF( distr[0], 100. );

# distributions with negative PDF
distr[2] = unur_str2distr( "cont; pdf='x/(1+x*x*x*x)'; center=1" );

distr[3] = unur_str2distr( "cont; pdf='-x/(1+x*x*x*x)'; center=-1" );

# distributions with almost vanishing PDF
distr[4] = unur_str2distr( "cont; pdf='cos(2*x)'; cdf='(1+sin(2*x))/2'; center=0; domain=(-0.7853981633974482, 0.7853981633974482)" );

distr[5] = unur_str2distr( "cont; pdf='1-x^2+1e-16'; cdf='( 2-(x^2-3)*x) / 4'; center=0; domain=(-1, 1)" );


# number of distributions: 3


[validate - test chi2:]

#  + ... pass test
#  0 ... fail to initialize generator
#  - ... fail test
#  . ... do not run test
#  # ... comment
#
# generators:
#  [0]  ... default 
#
#	0	# distribution
#-----------------------------
<0>	+	# normal ()
<1>	+	# normal ()
<2>	0	# PDF(x) < 0
<3>	0	# PDF(x) < 0
<4>	+	# PDF(x) \approx 0
<5>	+	# PDF(x) \approx 0


#############################################################################

[special]

[special - decl:]

  int samplesize = UERROR_SAMPLESIZE;
  int errorsum = 0;
  double fpar[4];

  UNUR_DISTR *distr, *qdistr;
  UNUR_PAR   *par;

[special - start:]

  /* set u-error tests */
  set_uerror_tests();

  /* test for maximal u-error */
  printf("\n[test maximal u-error]"); 
  fprintf(TESTLOG,"\n* Test maximal u-error *\n"); 

## ----------------------------------------------------
if (UERROR_STANDARDDIST) { /* --------------------------- */

  printf("\nStandard distributions:\n"); 
  fprintf(TESTLOG,"\n--- Standard distributions --- \n\n"); 

  ## --- Normal ---
if (ALL) {
  printf(" normal");
  distr = unur_distr_normal(NULL,0);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}
	
  ## --- Cauchy ---
if (ALL) {
  printf(" cauchy");
  distr = unur_distr_cauchy(NULL,0);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Exponential - Gamma(1) ---
if (ALL) {
  printf(" exponential");
  distr = unur_distr_exponential(NULL,0);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(0.9) ---
if (ALL) {
  printf(" gamma(0.9)");
  fpar[0] = 0.9;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(1.02) ---
if (ALL) {
  printf(" gamma(1.02)");
  fpar[0] = 1.02;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(1.5) ---
if (ALL) {
  printf(" gamma(1.5)");
  fpar[0] = 1.5;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(2) ---
if (ALL) {
  printf(" gamma(2)");
  fpar[0] = 2.;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  unur_pinv_set_boundary(par,0., 1.e100);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(5) ---
if (ALL) {
  printf(" gamma(5)");
  fpar[0] = 5.;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  unur_pinv_set_boundary(par,0., 1.e100);
##  unur_pinv_set_searchboundary(par,FALSE,TRUE);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- beta(2,3) ---
if (ALL) {
  printf(" beta(2,3)");
  fpar[0] = 2;
  fpar[1] = 3;
  distr = unur_distr_beta(fpar,2);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gumbel - ExtremeI ---
if (ALL) {
  printf(" Gumbel");
  distr = unur_distr_extremeI(NULL,0);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Shifted Normal ---
if (ALL) {
  printf(" shifted normal");
  fpar[0] = 200.;
  fpar[1] = 1.;
  distr = unur_distr_normal(fpar,2);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");

  printf(" shifted normal");
  fpar[0] = -200;
  fpar[1] = 1.;
  distr = unur_distr_normal(fpar,2);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Lognormal ---
if (0) {
  printf(" lognormal(2,0.5)");
  fpar[0] = 2.;
  fpar[1] = 0.5;
  distr = unur_distr_lognormal(fpar,2);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");

  printf(" lognormal(2.0.5,4.)");
  fpar[0] = 2.;
  fpar[1] = 0.5;
  fpar[2] = 4.;
  distr = unur_distr_lognormal(fpar,3);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}
	
} /* endif (UERROR_STANDARDDIST) */
## ----------------------------------------------------
if (UERROR_QUASIPDF) { /* --------------------------- */

  printf("\nDistributions with quasi-densities:\n"); 
  fprintf(TESTLOG,"\n--- Distributions with quasi-densities --- \n\n"); 

if (ALL) {
  distr = unur_distr_normal(NULL,0);

  printf(" (normal*1e10)");
  qdistr = unur_distr_multPDF( distr, 1.e10 );
  par = unur_pinv_new(qdistr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(qdistr);
  fprintf(TESTLOG,"\n");

  printf(" (normal*1e-10)");
  qdistr = unur_distr_multPDF( distr, 1.e-10 );
  par = unur_pinv_new(qdistr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(qdistr);
  fprintf(TESTLOG,"\n");

  unur_distr_free(distr);
}

} /* endif (UERROR_QUASIPDF) */
## ----------------------------------------------------
if (UERROR_VANISHINGPDF) { /* --------------------------- */

  printf("\nDistributions with vanishing PDF:\n"); 
  fprintf(TESTLOG,"\n--- Distributions with vanishing PDF --- \n\n"); 

  ## --- Exponential ---
if (ALL) {
  printf(" (exponential_on_(-oo,oo))");
  distr = unur_distr_exponential(NULL,0);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_set_name(distr,"exponential_(-oo,oo)");
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" (exponential(1,1)_on_(-oo,oo))");
  fpar[0] = 1.;
  fpar[1] = 1.;
  distr = unur_distr_exponential(fpar,2);
  unur_distr_cont_set_center(distr,2.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_set_name(distr,"exponential(1,1)_(-oo,oo)");
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" (exponential(1,1)_on_(-oo,oo))");
  fpar[0] = 1.;
  fpar[1] = 1.;
  distr = unur_distr_exponential(fpar,2);
  unur_distr_cont_set_center(distr,2.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_cont_set_center(distr,2.);
  unur_distr_set_name(distr,"exponential(1,1)_(-oo,oo)");
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" (exponential(1,-1)_on_(-oo,oo))");
  fpar[0] = 1.;
  fpar[1] = -1.;
  distr = unur_distr_exponential(fpar,2);
  unur_distr_cont_set_center(distr,2.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_set_name(distr,"exponential(1,-1)_(-oo,oo)");
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" (exponential(0.1,0.1)_on_(-oo,oo))");
  fpar[0] = 0.1;
  fpar[1] = 0.1;
  distr = unur_distr_exponential(fpar,2);
  unur_distr_cont_set_center(distr,2.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_set_name(distr,"exponential(0.1,0.1)_(-oo,oo)");
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" (exponential(1000,1000)_on_(-oo,oo))");
  fpar[0] = 1000;
  fpar[1] = 1000;
  distr = unur_distr_exponential(fpar,2);
  unur_distr_cont_set_center(distr,1000.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_set_name(distr,"exponential(1000,1000)_(-oo,oo)");
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- "Negative" exponential ---
if (ALL) {
  printf(" neg_exponential");
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"neg_exponential");
  unur_distr_cont_set_cdf( distr, neg_exponential_cdf );
  unur_distr_cont_set_pdf( distr, neg_exponential_pdf );
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" neg_exponential_1");
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"neg_exponential_1");
  unur_distr_cont_set_cdf( distr, neg_exponential_1_cdf );
  unur_distr_cont_set_pdf( distr, neg_exponential_1_pdf );
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_cont_set_center(distr,-2.);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(2) ---
if (ALL) {
  printf(" gamma(2)");
  fpar[0] = 2.;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  par = unur_pinv_new(distr);
  unur_pinv_set_boundary(par,0., 1.e100);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(5) ---
if (ALL) {
  printf(" gamma(5)");
  fpar[0] = 5.;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  par = unur_pinv_new(distr);
  unur_pinv_set_boundary(par,0., 1.e100);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(1.02) ---
if (ALL) {
  printf(" gamma(1.02)");
  fpar[0] = 1.02;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

} /* endif (UERROR_VANISHINGPDF) */
## ----------------------------------------------------
if (UERROR_TRUNCATED) { /* --------------------------- */

  printf("\nTruncated Distributions:\n"); 
  fprintf(TESTLOG,"\n--- Truncated Distributions --- \n\n"); 

  ## --- Normal ---
if (ALL) {
  printf(" truncated normal[3,inf]");
  distr = unur_distr_normal(NULL,0);
  unur_distr_cont_set_domain(distr,3.,UNUR_INFINITY);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL && DEVEL) {
  printf(" truncated normal[15,inf]");
  distr = unur_distr_normal(NULL,0);
  unur_distr_cont_set_domain(distr,15.,UNUR_INFINITY);
  par = unur_pinv_new(distr);
  /* using CDF does not work here. */
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

} /* endif (UERROR_TRUNCATED) */
## ----------------------------------------------------
if (UERROR_UNBOUNDEDPDF) { /* --------------------------- */

  printf("\nDistributions with unbounded PDF:\n"); 
  fprintf(TESTLOG,"\n--- Distributions with unbounded PDF --- \n\n"); 

  ## --- Gamma(0.2) ---
if (ALL) {
  printf(" gamma(0.2)");
  fpar[0] = 0.2;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  unur_pinv_set_extra_testpoints(par,50);
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- Gamma(0.5) ---
if (ALL) {
  printf(" gamma(0.5)");
  fpar[0] = 0.5;
  distr = unur_distr_gamma(fpar,1);
  unur_distr_cont_set_center(distr,1.);
  par = unur_pinv_new(distr);
  unur_pinv_set_extra_testpoints(par,40);
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

} /* endif (UERROR_UNBOUNDEDPDF) */
## ----------------------------------------------------
if (UERROR_MULTIMODAL) { /* --------------------------- */

  printf("\nMultimodal distributions:\n"); 
  fprintf(TESTLOG,"\n--- Multimodal distributions --- \n\n"); 

  ## --- sin-2-example ---
if (ALL) {
  printf(" sin-2-example");
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"sin-2-example");
  unur_distr_cont_set_cdf( distr, sin_2_cdf );
  unur_distr_cont_set_pdf( distr, sin_2_pdf );
  unur_distr_cont_set_domain( distr, -1., 1. );
  unur_distr_cont_set_center(distr,0.25);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- sin-10-example ---
if (ALL) {
  printf(" sin-10-example");
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"sin-10-example");
  unur_distr_cont_set_cdf( distr, sin_10_cdf );
  unur_distr_cont_set_pdf( distr, sin_10_pdf );
  unur_distr_cont_set_domain( distr, -5., 5. );
  unur_distr_cont_set_center(distr,0.25);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

} /* endif (UERROR_MULTIMODAL) */
## ----------------------------------------------------
if (UERROR_MULTIMODAL_VANISHINGPDF) { /* --------------------------- */

  printf("\nMultimodal distributions with vanishing PDF:\n"); 
  fprintf(TESTLOG,"\n--- Multimodal distributions with vanishing PDF --- \n\n"); 

  ## --- sin-0-example ---
if (ALL) {
  printf(" sin-2-example");fflush(stdout);
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"sin-0-example");
  unur_distr_cont_set_cdf( distr, sin_2_cdf );
  unur_distr_cont_set_pdf( distr, sin_2_pdf );
  unur_distr_cont_set_domain( distr, -2., 2. );
  unur_distr_cont_set_center(distr,0.25);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

if (ALL) {
  printf(" sin-2-example");fflush(stdout);
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"sin-0-example");
  unur_distr_cont_set_cdf( distr, sin_2_cdf );
  unur_distr_cont_set_pdf( distr, sin_2_pdf );
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_cont_set_center(distr,0.25);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE,TRUE);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

  ## --- sin-10-example ---
if (ALL) {
  printf(" sin-10-example");
  distr = unur_distr_cont_new();
  unur_distr_set_name(distr,"sin-10-example");
  unur_distr_cont_set_cdf( distr, sin_10_cdf );
  unur_distr_cont_set_pdf( distr, sin_10_pdf );
  unur_distr_cont_set_domain(distr,-UNUR_INFINITY,UNUR_INFINITY);
  unur_distr_cont_set_center(distr,0.25);
  par = unur_pinv_new(distr);
  errorsum += pinv_error_experiment(par,samplesize,TRUE, DEVEL);
  unur_distr_free(distr);
  fprintf(TESTLOG,"\n");
}

} /* endif (UERROR_MULTIMODAL_VANISHINGPDF) */
## ----------------------------------------------------

 
/*---------------------------------------------------------------------------*/

  /* test finished */
  FAILED = (errorsum < 2) ? 0 : 1;


#############################################################################

[verbatim]


#############################################################################
#
# routines for special tests
#
#############################################################################

/*****************************************************************************/
/* run unur_test_u_error for a particular generator object and print result  */

int
pinv_error_gen_experiment( UNUR_GEN *gen,       /* generator object */
			   double u_resolution, /* maximal tolerated u-error */
			   int order,           /* order of polynomial */
			   int samplesize )     /* sample size for error experiment */
     /* returns 0 if maxerror < u_resolution, errorcode otherwise */
{   
  int i, nfpar;
  const double *fpar;     
  const UNUR_DISTR *distr;
  const char *genid;
  int score;

  /* check for invalid NULL pointer */
  if (gen==NULL) {
    fprintf(TESTLOG,"\n--setup failed !! --\n");
    printf("(!!+)"); fflush(stdout);
    return 100;
  }

  /* get data about generator object */
  distr = unur_get_distr(gen);
  genid = unur_get_genid(gen);

  /* print data about distribution */
  fprintf(TESTLOG,"%s: %s distribution",genid,unur_distr_get_name(distr));
  nfpar = unur_distr_cont_get_pdfparams(distr,&fpar);
  if (nfpar) { 
     fprintf(TESTLOG," with parameters ");
     for(i=0;i<nfpar;i++) fprintf(TESTLOG," | %g",fpar[i]);
     fprintf(TESTLOG," |");
  }
  fprintf(TESTLOG,":\n");

  /* print chosen parameter for method PINV and for experiment */
  fprintf(TESTLOG,"%s:   setting: order = %d, u-error <= %g, samplesize = %d\n",
	  genid,order,u_resolution,samplesize);
  /* number of intervals */
  fprintf(TESTLOG,"%s:   \#intervals = %d", genid, unur_pinv_get_n_intervals(gen));
  /* number of function calls */
  if (get_counter_pdf() > 0) {
    fprintf(TESTLOG,"\t\#PDF calls = %d\t(per interval = %d)",
	    get_counter_pdf(), 
	    ( (unur_pinv_get_n_intervals(gen)!=0) ? 
	      get_counter_pdf() / unur_pinv_get_n_intervals(gen) : -1 ) );
  }
  if (get_counter_logpdf() > 0) {
    fprintf(TESTLOG,"\t\#logPDF calls = %d\t(per interval = %d)",
	    get_counter_logpdf(), 
	    ( (unur_pinv_get_n_intervals(gen)!=0) ? 
	      get_counter_logpdf() / unur_pinv_get_n_intervals(gen) : -1 ) );
  }
  if (get_counter_cdf() > 0) {
    fprintf(TESTLOG,"\t\#CDF calls = %d\t(per interval = %d)",
	    get_counter_cdf(), 
	    ( (unur_pinv_get_n_intervals(gen)!=0) ? 
	      get_counter_cdf() / unur_pinv_get_n_intervals(gen) : -1 ) );
  }
  fprintf(TESTLOG,"\n");

  /* run test */
  score = run_validate_u_error( TESTLOG, gen, distr, u_resolution, samplesize );

  /* return score */
  fprintf(TESTLOG,"\n"); fflush(TESTLOG);
  return score;

} /* end of pinv_error_gen_experiment() */

/*****************************************************************************/
/* run experiment for a particular distribution object                       */

int
pinv_error_experiment( UNUR_PAR *par,     /* parameter object */
		       int samplesize,    /* samplesize for error experiment */
		       int UERROR_PDF,   /* if TRUE, check variant usePDF */
		       int UERROR_CDF)   /* if TRUE, check variant useCDF */
     /* returns 0 if maxerror < u_resolution, errorcode otherwise */
{ 
  UNUR_PAR *par_clone;  /* working copy of parameter object */
  UNUR_GEN *gen;        /* generator object */
  
  int order = 3;
  int smooth = 0;
  double u_resolution = 1.e-8;  /* maximal tolerated u-error */
  int errorsum = 0;

  int variant;
  
  /* check for invalid NULL pointer */
  if (par==NULL) {
    printf(" NULL! ");
    return 1000;
  }

  /* -- loop smoothness -- */
  for (smooth=0; smooth <= SMOOTHNESS_MAX; smooth++) {
   
    /* -- loop variante -- */
    for (variant=0; variant <= 1; ++variant) {
      /* variant==0 --> use PDF */
      /* variant==1 --> use CDF */
      
      switch (variant) {
	
      case 0: /* run variant PDF+Lobatto */
	if (UERROR_PDF) printf("<PDF:%d>",smooth);
	else continue;
	break;
	
      case 1: /* run variant CDF */
	if (UERROR_CDF) printf("<CDF:%d>",smooth); 
	else continue;
	break;
	
      default:
	continue;
      }
      
      /* -- loop order -- */
      for ( order=UERROR_ORDER_MIN; order<=UERROR_ORDER_MAX; order+=UERROR_ORDER_STEP) {   
	
	/* prevent warning from invalid order */
	if (smooth==1 && order % 2 != 1) continue;
	if (smooth==2 && order % 3 != 2) continue;

	/* SKIPORDER:
	 * when we do not run in fullcheck mode, we skip some orders. */
	if (!fullcheck) {
	  switch (smooth) {
	  case 0:
	    if (! (order==3 || order==5 || order==8 || order==12 || order==17) )
	      continue;
	    break;
	  case 1:
	    if (order==7 || order==11 || order==15)
	      continue;
	    break;
	  case 2:
	    if (order == 14) 
	      continue;
	    break;
	  }
	}

	/* -- loop u-resolution -- */
	for ( u_resolution = UERROR_URESOLUTION_MAX; 
	      u_resolution > UERROR_URESOLUTION_MIN; u_resolution *= UERROR_URESOLUTION_STEP ) {

	  if (!fullcheck) {
	    if (smooth==2 && u_resolution < 0.99e-10)
	      /* setup fails for some truncated distributions. */
	      /* so we skip it when not run in fullcheck mode. */
	      continue;
	  }
	  
	  /* make a working copy */
	  par_clone = _unur_par_clone(par);
	  
	  /* set variant */
	  switch (variant) {
	  case 0: unur_pinv_set_usepdf(par_clone); break;
	  case 1: unur_pinv_set_usecdf(par_clone); break;
	  }
	  
	  /* maximum number of subintervals */
	  /**/   if (order <= 3 && u_resolution < 1.e-12) {
	    unur_pinv_set_max_intervals(par_clone,100000);
	  } else if (u_resolution < 1.e13) {
	    unur_pinv_set_max_intervals(par_clone,100000);
	  }
	  
	  /* start counter for PDF calls */
	  start_counter_fcalls(par_clone);
	  
	  /* create generator object */
	  unur_pinv_set_order(par_clone,order);
	  unur_pinv_set_smoothness(par_clone,smooth);
	  unur_pinv_set_u_resolution(par_clone,u_resolution);
	  set_debug_flag(par_clone);
	  gen = unur_init(par_clone);
	  
	  /* run test */
	  errorsum += pinv_error_gen_experiment(gen, u_resolution, order, samplesize);
	  
	  /* clear memory */
	  unur_free(gen);
	  stop_counter_fcalls();
	  
	} /* -- end loop u-resolution -- */
	
      } /* -- end end loop order -- */
      
    } /* -- end loop variant */
    
  } /* -- end loop smoothness -- */

  unur_par_free(par);
  
  return errorsum;
} /* end of pinv_error_experiment() */

/*****************************************************************************/

#############################################################################
#
# PDFs and CDF for test distributions
#
#############################################################################

/*---------------------------------------------------------------------------*/
/* "negative" exponential distribution                                       */
/*                                                                           */
/*          /  exp(x) if x <= 0                                              */
/*  f(x) = <                                                                 */
/*          \  0      otherwise                                              */
/*                                                                           */
double neg_exponential_pdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  if (x>0.)
    return 0.;
  else
    return (exp(x));
}

double neg_exponential_cdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  if (x>0.)
    return 1.;
  else
    return (exp(x));
}

/*---------------------------------------------------------------------------*/
/* "shifted negative" exponential distribution                               */
/*                                                                           */
/*          /  exp(x-s) if x <= s                                            */
/*  f(x) = <                                                                 */
/*          \  0      otherwise                                              */
/*                                                                           */

\#define shift (-1.)

double neg_exponential_1_pdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  x = x-shift;

  if (x>0.)
    return 0.;
  else
    return (exp(x));
}

double neg_exponential_1_cdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  x = x-shift;

  if (x>=0.)
    return 1.;
  else
    return (exp(x));
}

\#undef shift


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*          /  0.05 + 0.45*(1 +sin(2 Pi x))  if |x| <= 1                     */
/*  f(x) = <                                                                 */
/*          \  0        otherwise                                            */
/*                                                                           */
double sin_2_pdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  if (fabs(x) > 1.)
    return 0.;
  else
    return (0.05 + 0.45*(1.+sin(2.*M_PI*x)));
} /* end of sin_pdf() */

/* The CDF of our distribution:                                  */
double sin_2_cdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  if (x < -1.)
    return 0.;
  else if(x<=1.)
    return (0.05*(x+1) + 0.9*(1.+2.*M_PI*(1+x)-cos((2.*M_PI)*x))/(4.*M_PI));
  else return 1.;
} /* end of sin_cdf() */


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*          /  0.05 + 0.45*(1 +sin(2 Pi x))  if |x| <= 5                     */
/*  f(x) = <                                                                 */
/*          \  0        otherwise                                            */
/*                                                                           */
double sin_10_pdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  if (fabs(x) > 5.)
    return 0.;
  else
    return (0.05 + 0.45*(1.+sin(2.*M_PI*x)));
} /* end of sin_pdf() */

/* The CDF of our distribution:                                  */
double sin_10_cdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
  if (x < -5.)
    return 0.;
  else if(x<=5.)
    return ((x/10.+0.5) + 0.09/(2*M_PI)*(cos(10*M_PI)-cos(2*M_PI*x)));
  else return 1.;
} /* end of sin_cdf() */

/*---------------------------------------------------------------------------*/
