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

[main]

[main - data:]

# method (for information only: the program scans the file name)
method: MCORR

[main - header:]

/* prototypes */

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

\#include<utils/matrix_source.h>
\#include<utils/vector_source.h>

int mcorr_eigenvalues_test(int dim);

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

[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


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

[set]

[set - invalid NULL ptr: 
   double *eigenvalues = NULL;
   par = NULL; ]

~_eigenvalues( par, eigenvalues )
	--> expected_setfailed --> UNUR_ERR_NULL


[set - invalid NULL ptr: 
   double *eigenvalues = NULL;
   distr = unur_distr_correlation(4); 
   par = unur_mcorr_new(distr); ]

~_eigenvalues( par, eigenvalues )
	--> expected_setfailed --> UNUR_ERR_NULL

unur_par_free(par);


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

~_eigenvalues( par, eigenvalues )
        --> expected_setfailed --> UNUR_ERR_PAR_INVALID

unur_par_free(par);


[set - invalid parameters:
   double eigenvalues[] = {1,-2,3};
   distr = unur_distr_correlation(4); 
   par = unur_mcorr_new(distr); ]

~_eigenvalues( par, eigenvalues )
        --> expected_setfailed --> UNUR_ERR_PAR_SET

unur_par_free(par);


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

# [get]

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

# [chg]

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

[init]

[init - invalid NULL ptr: 
   par = NULL; ]

unur_init( par );
	--> expected_NULL --> UNUR_ERR_NULL


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

[reinit]

[reinit - exists:
   distr = unur_distr_correlation(3); 
   par = unur_mcorr_new(distr);
   gen = unur_init( par ); <-- ! NULL ]

unur_reinit( gen );
	--> expected_reinit

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

[sample]

[sample - compare clone:
   UNUR_GEN *clone;
   distr = unur_distr_correlation(4); 
   par = NULL;
   gen = NULL; ]

/* original generator object */
par = unur_mcorr_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 clone:
   UNUR_GEN *clone;
   double eigenvalues[] = {1.,2.,3.,4.};
   par = NULL;
   gen = NULL;
   distr = unur_distr_correlation(4); ]

/* original generator object */
par = unur_mcorr_new(distr);
unur_mcorr_set_eigenvalues( par, eigenvalues );
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_correlation(4); 
   par = NULL;
   gen = NULL; ]

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

/* reinit */
unur_reinit(gen);
        -->compare_sequence_gen

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

[sample - compare reinit:
   double eigenvalues[] = {1.,2.,3.,4.};
   distr = unur_distr_correlation(4); 
   par = NULL;
   gen = NULL; ]

/* original generator object */
par = unur_mcorr_new(distr);
unur_mcorr_set_eigenvalues( par, eigenvalues );
gen = unur_init(par);
        -->compare_sequence_gen_start

/* reinit */
unur_reinit(gen);
        -->compare_sequence_gen


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

# [validate]

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

[special]

[special - decl:]
  int errorsum = 0;
  int dim;
  
[special - start:]

  /* test for eigenvalues */
  printf("\ntest eigenvalues"); 
  fprintf(TESTLOG,"\nTest eigenvalues:\n"); 

  for (dim=2; dim<10; dim++) {
    if (mcorr_eigenvalues_test(dim)) errorsum++;
  }

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

[verbatim]

int
mcorr_eigenvalues_test(int dim) 
/* returns 0 if everything is OK, 1 otherwise */
{
  UNUR_DISTR *corr_distr;
  UNUR_PAR   *corr_par;
  UNUR_GEN   *corr_gen;

  int i;
  int err = 0;
  
  double *mean;
  double *corr;
  double *eigenvalues, *eigenvalues_set;
  double *eigenvectors;

  mean = _unur_vector_new(dim);
  corr = _unur_vector_new(dim*dim);
  eigenvalues_set = _unur_vector_new(dim);
  eigenvalues = _unur_vector_new(dim);
  eigenvectors = _unur_vector_new(dim*dim);

  /* setting eigenvalues to const*{1,2,...,dim} with sum=dim */	  
  for (i=0; i<dim; i++) eigenvalues_set[i]=2*(1+i)/(1.+dim);	  

  corr_distr=unur_distr_correlation(dim);
  corr_par=unur_mcorr_new(corr_distr);
  unur_mcorr_set_eigenvalues(corr_par, eigenvalues_set);

  corr_gen=unur_init(corr_par);
  unur_sample_matr(corr_gen, corr);

  /* calculating eigenvectors of generated correlation matric */
  _unur_matrix_eigensystem(dim, corr, eigenvalues, eigenvectors);

  fprintf(TESTLOG,"dim = %d ...\t", dim);

  for (i=0; i<dim; i++)
    if (_unur_FP_cmp(eigenvalues[i], eigenvalues_set[i], 100*UNUR_SQRT_DBL_EPSILON))
       /* calculation of eigenvalues is more sensitive against round-off errors */
       err++;

  if (err) {
    fprintf(TESTLOG,"failed\n");
    for (i=0; i<dim; i++)
      fprintf(TESTLOG,"\t%g  \t(instead of %g, diff = %g)\n",
         eigenvalues[i],eigenvalues_set[i],
	 eigenvalues[i] - eigenvalues_set[i]);
  }	
  else {
    fprintf(TESTLOG,"ok\n");
  }

  free(mean); 
  free(corr);
  free(eigenvectors);
  free(eigenvalues);
  free(eigenvalues_set);
  
  unur_distr_free(corr_distr);
  unur_free(corr_gen);

  return err;
}

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