23 #include <gsl/gsl_vector.h>
24 #include <gsl/gsl_matrix.h>
25 #include <gsl/gsl_errno.h>
26 #include <gsl/gsl_sort_vector_double.h>
27 #include <gsl/gsl_permute_vector.h>
28 #include <gsl/gsl_randist.h>
45 static void generateSample(gsl_vector * samples,
unsigned int nb_samples,
double min,
double max,
double delta)
47 if(samples->size != nb_samples)
49 std::cerr <<
"[ERROR] RandomSample_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
54 for(
unsigned int i = 0 ; i < nb_samples ; i++)
55 gsl_vector_set(samples, i, min + floor((max - min)*rand()/
double(RAND_MAX)/delta) * delta );
66 static void generateSample(gsl_vector * samples,
unsigned int nb_samples,
double minx,
double maxx,
double deltax,
double miny,
double maxy,
double deltay)
68 if(samples->size != 2*nb_samples)
70 std::cerr <<
"[ERROR] RandomSample_2D : samples should be allocated to the size of the 2 times the requested number of samples" << std::endl;
73 gsl_vector_view vec_view = gsl_vector_subvector_with_stride (samples, 0, 2, nb_samples);
75 vec_view = gsl_vector_subvector_with_stride (samples, 1, 2, nb_samples);
87 static void generateSample(gsl_vector * samples,
unsigned int nb_samples,
double minx,
double maxx,
double deltax,
double miny,
double maxy,
double deltay,
double minz,
double maxz,
double deltaz)
89 if(samples->size != 3*nb_samples)
91 std::cerr <<
"[ERROR] RandomSample_2D : samples should be allocated to the size of the 2 times the requested number of samples" << std::endl;
94 gsl_vector_view vec_view = gsl_vector_subvector_with_stride (samples, 0, 3, nb_samples);
96 vec_view = gsl_vector_subvector_with_stride (samples, 1, 3, nb_samples);
98 vec_view = gsl_vector_subvector_with_stride (samples, 2, 3, nb_samples);
110 static void generateSample(gsl_vector * v, gsl_vector * indexes_samples,
unsigned int nb_samples)
112 if(indexes_samples->size != nb_samples)
114 std::cerr <<
"[ERROR] MaximumIndexes_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
117 if(nb_samples > v->size)
119 std::cerr <<
"[ERROR] MaximumIndexes_1D : the vector v must hold at least nb_samples elements" << std::endl;
123 gsl_permutation * p = gsl_permutation_alloc(v->size);
124 gsl_sort_vector_index(p,v);
125 for(
unsigned int i = 0 ; i < nb_samples ; i++)
126 gsl_vector_set(indexes_samples, i, gsl_permutation_get(p, p->size - i - 1));
127 gsl_permutation_free(p);
138 static void generateSample(gsl_matrix * m, gsl_vector * indexes_samples,
unsigned int nb_samples)
141 if(indexes_samples->size != 2*nb_samples)
143 std::cerr <<
"[ERROR] MaximumIndexes_2D : samples should be allocated to the size of 2 times the requested number of samples" << std::endl;
146 if(nb_samples > m->size1 * m->size2 )
148 std::cerr <<
"[ERROR] MaximumIndexes_2D : the vector v must hold at least nb_samples elements" << std::endl;
152 gsl_permutation * p = gsl_permutation_alloc(m->size1 * m->size2);
154 gsl_vector_view vec_view = gsl_vector_view_array(m->data,m->size1 * m->size2);
155 gsl_sort_vector_index(p,&vec_view.vector);
160 for(
unsigned int i = 0 ; i < nb_samples ; i++)
163 k = gsl_permutation_get(p,p->size - i - 1) / m->size2;
165 l = gsl_permutation_get(p,p->size - i - 1) % m->size2;
166 gsl_vector_set(indexes_samples, 2 * i, k);
167 gsl_vector_set(indexes_samples, 2 * i + 1, l);
170 gsl_permutation_free(p);
180 static void generateSample(gsl_vector * v, gsl_vector * indexes_samples,
unsigned int nb_samples)
182 if(indexes_samples->size != nb_samples)
184 std::cerr <<
"[ERROR] RandomSample_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
190 gsl_rng * r = gsl_rng_alloc(gsl_rng_default);
192 gsl_rng_set(r, time(NULL));
193 gsl_ran_discrete_t * ran_pre = gsl_ran_discrete_preproc(v->size, v->data);
195 for(
unsigned int i = 0 ; i < nb_samples ; i++)
196 gsl_vector_set(indexes_samples, i , gsl_ran_discrete(r, ran_pre));
198 gsl_ran_discrete_free(ran_pre);
209 static void generateSample(gsl_matrix * m, gsl_vector * indexes_samples,
unsigned int nb_samples)
211 if(indexes_samples->size != 2*nb_samples)
213 std::cerr <<
"[ERROR] RandomSample_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
221 gsl_vector * samples_tmp = gsl_vector_alloc(nb_samples);
222 gsl_vector_view vec_view = gsl_vector_view_array(m->data,m->size1 * m->size2);
226 for(
unsigned int i = 0 ; i < nb_samples ; i++)
228 k = int(gsl_vector_get(samples_tmp,i)) / m->size2;
229 l = int(gsl_vector_get(samples_tmp,i)) % m->size2;
230 gsl_vector_set(indexes_samples, 2*i, k);
231 gsl_vector_set(indexes_samples, 2*i+1, l);
234 gsl_vector_free(samples_tmp);
241 #endif // UKF_SAMPLES_H
Extract the indexes of the nb_samples highest values of a vector Be carefull, this function is extrac...
Definition: ukf_samples.h:107
static void generateSample(gsl_vector *samples, unsigned int nb_samples, double minx, double maxx, double deltax, double miny, double maxy, double deltay, double minz, double maxz, double deltaz)
Definition: ukf_samples.h:87
Generate 2D samples according to a uniform distribution and put them alternativaly at the odd/even po...
Definition: ukf_samples.h:63
static void generateSample(gsl_vector *v, gsl_vector *indexes_samples, unsigned int nb_samples)
Definition: ukf_samples.h:110
double min(double a, double b)
Definition: ukf_math.h:35
double max(double a, double b)
Definition: ukf_math.h:40
Extract the indexes of the nb_samples highest values of a matrix Be carefull, this function is extrac...
Definition: ukf_samples.h:135
Generate 2D samples according to a discrete matricial distribution.
Definition: ukf_samples.h:206
static void generateSample(gsl_vector *samples, unsigned int nb_samples, double min, double max, double delta)
Definition: ukf_samples.h:45
Generate 1D samples according to a uniform distribution :
Definition: ukf_samples.h:42
static void generateSample(gsl_matrix *m, gsl_vector *indexes_samples, unsigned int nb_samples)
Definition: ukf_samples.h:138
static void generateSample(gsl_vector *samples, unsigned int nb_samples, double minx, double maxx, double deltax, double miny, double maxy, double deltay)
Definition: ukf_samples.h:66
Generate 3D samples according to a uniform distribution and put them alternativaly at the odd/even po...
Definition: ukf_samples.h:84
static void generateSample(gsl_vector *v, gsl_vector *indexes_samples, unsigned int nb_samples)
Definition: ukf_samples.h:180
static void generateSample(gsl_matrix *m, gsl_vector *indexes_samples, unsigned int nb_samples)
Definition: ukf_samples.h:209
Generate 1D samples according to a discrete vectorial distribution.
Definition: ukf_samples.h:177