easykf-2.04
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
ukf_samples.h
Go to the documentation of this file.
1 /* ukf_samples.h
2  *
3  * Copyright (C) 2011-2014 Jeremy Fix
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef UKF_SAMPLES_H
21 #define UKF_SAMPLES_H
22 
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>
29 
30 #include <time.h>
31 
32 #include <iostream>
33 
34 namespace ukf
35 {
36  namespace samples
37  {
43  {
44  public:
45  static void generateSample(gsl_vector * samples, unsigned int nb_samples, double min, double max, double delta)
46  {
47  if(samples->size != nb_samples)
48  {
49  std::cerr << "[ERROR] RandomSample_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
50  return;
51  }
52 
53 
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 );
56  }
57  };
58 
64  {
65  public:
66  static void generateSample(gsl_vector * samples, unsigned int nb_samples, double minx, double maxx, double deltax, double miny, double maxy, double deltay)
67  {
68  if(samples->size != 2*nb_samples)
69  {
70  std::cerr << "[ERROR] RandomSample_2D : samples should be allocated to the size of the 2 times the requested number of samples" << std::endl;
71  return;
72  }
73  gsl_vector_view vec_view = gsl_vector_subvector_with_stride (samples, 0, 2, nb_samples);
74  RandomSample_1D::generateSample(&vec_view.vector, nb_samples, minx, maxx, deltax);
75  vec_view = gsl_vector_subvector_with_stride (samples, 1, 2, nb_samples);
76  RandomSample_1D::generateSample(&vec_view.vector, nb_samples, miny, maxy, deltay);
77  }
78  };
79 
85  {
86  public:
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)
88  {
89  if(samples->size != 3*nb_samples)
90  {
91  std::cerr << "[ERROR] RandomSample_2D : samples should be allocated to the size of the 2 times the requested number of samples" << std::endl;
92  return;
93  }
94  gsl_vector_view vec_view = gsl_vector_subvector_with_stride (samples, 0, 3, nb_samples);
95  RandomSample_1D::generateSample(&vec_view.vector, nb_samples, minx, maxx, deltax);
96  vec_view = gsl_vector_subvector_with_stride (samples, 1, 3, nb_samples);
97  RandomSample_1D::generateSample(&vec_view.vector, nb_samples, miny, maxy, deltay);
98  vec_view = gsl_vector_subvector_with_stride (samples, 2, 3, nb_samples);
99  RandomSample_1D::generateSample(&vec_view.vector, nb_samples, minz, maxz, deltaz);
100  }
101  };
102 
108  {
109  public:
110  static void generateSample(gsl_vector * v, gsl_vector * indexes_samples, unsigned int nb_samples)
111  {
112  if(indexes_samples->size != nb_samples)
113  {
114  std::cerr << "[ERROR] MaximumIndexes_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
115  return;
116  }
117  if(nb_samples > v->size)
118  {
119  std::cerr << "[ERROR] MaximumIndexes_1D : the vector v must hold at least nb_samples elements" << std::endl;
120  return;
121  }
122 
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);
128  }
129  };
130 
136  {
137  public:
138  static void generateSample(gsl_matrix * m, gsl_vector * indexes_samples, unsigned int nb_samples)
139  {
140 
141  if(indexes_samples->size != 2*nb_samples)
142  {
143  std::cerr << "[ERROR] MaximumIndexes_2D : samples should be allocated to the size of 2 times the requested number of samples" << std::endl;
144  return;
145  }
146  if(nb_samples > m->size1 * m->size2 )
147  {
148  std::cerr << "[ERROR] MaximumIndexes_2D : the vector v must hold at least nb_samples elements" << std::endl;
149  return;
150  }
151 
152  gsl_permutation * p = gsl_permutation_alloc(m->size1 * m->size2);
153  // We cast the matrix in a vector ordered in row-major and get the permutation to order the data by increasing value
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);
156 
157  // The indexes stored in p are in row-major order
158  // we then convert the nb_samples first indexes into matrix indexes and copy them in indexes_samples
159  int k,l;
160  for(unsigned int i = 0 ; i < nb_samples ; i++)
161  {
162  // Line
163  k = gsl_permutation_get(p,p->size - i - 1) / m->size2;
164  // Column
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);
168  }
169 
170  gsl_permutation_free(p);
171  }
172  };
173 
178  {
179  public:
180  static void generateSample(gsl_vector * v, gsl_vector * indexes_samples, unsigned int nb_samples)
181  {
182  if(indexes_samples->size != nb_samples)
183  {
184  std::cerr << "[ERROR] RandomSample_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
185  return;
186  }
187 
188  // Generating nb_samples samples from an array of weights for each index is easily done in the gsl with gsl_ran_discrete_* methods
189  // the weights v do not need to add up to one
190  gsl_rng * r = gsl_rng_alloc(gsl_rng_default);
191  // Init the random seed
192  gsl_rng_set(r, time(NULL));
193  gsl_ran_discrete_t * ran_pre = gsl_ran_discrete_preproc(v->size, v->data);
194 
195  for(unsigned int i = 0 ; i < nb_samples ; i++)
196  gsl_vector_set(indexes_samples, i , gsl_ran_discrete(r, ran_pre));
197 
198  gsl_ran_discrete_free(ran_pre);
199  gsl_rng_free(r);
200  }
201  };
202 
207  {
208  public:
209  static void generateSample(gsl_matrix * m, gsl_vector * indexes_samples, unsigned int nb_samples)
210  {
211  if(indexes_samples->size != 2*nb_samples)
212  {
213  std::cerr << "[ERROR] RandomSample_1D : samples should be allocated to the size of the requested number of samples" << std::endl;
214  return;
215  }
216 
217  // To generate 2D samples according to a matrix of weights
218  // we simply cast the matrix into a vector, ask DistributionSample_1D to do the job
219  // and then convert the linear row-major indexes back into 2D indexes
220 
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);
223  DistributionSample_1D::generateSample(&vec_view.vector, samples_tmp, nb_samples);
224 
225  int k,l;
226  for(unsigned int i = 0 ; i < nb_samples ; i++)
227  {
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);
232  }
233 
234  gsl_vector_free(samples_tmp);
235  }
236  };
237  }
238 }
239 
240 
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