vendredi 26 janvier 2018

segmentation fault using nested vectors

I stumbled upon the following code segment somewhere to create a list of random numbers in a certain interval:

#include <vector>
#include <iostream>
#include <math.h>
struct gen_rand_pos{
  double factor;
  public:
    gen_rand_pos(double r=1.0): factor(r/RAND_MAX)
    {}
    double operator()(){
        return rand()*factor;
    }
};

int main(){
  int N = 5;
  std::vector<double> result(N);
  std::generate_n(std::back_inserter(result), N, gen_rand_pos(1.0));
  std::cout << result[0] << std::endl;
}

It works perfectly fine. I tried to take this one step further and do the same, but this time creating a list(vector) of random unit vectors, uniformly distributed on the sphere. Here is my go at this:

double l2_norm(std::vector<double> const& u) {
    double accum = 0.;
    for (double x : u) {
        accum += x * x;
    }
    return sqrt(accum);
}

struct gen_rand_vec{
  double factor;
  public:
    gen_rand_vec(): factor(2.0/RAND_MAX)
    {}
  std::vector<double> operator()(){
    std::vector<double> result = {rand()*factor-1,rand()*factor-1,rand()*factor-1}; // uniform in each component
    double norm = l2_norm(result);
    std::transform(result.begin(), result.end(), result.begin(),
           std::bind1st(std::multiplies<float>(),1/norm)); // normalize the vector
    return result;
  }
};

However if I now try to construct it in the same manner:

int main(){
  int N = 5;
  std::vector<std::vector<double>> result2(N);
  std::generate_n(std::back_inserter(result2), N, gen_rand_vec());
  std::cout << result2[0][0] <<std::endl;
}

I get a segmentation error. Is the problem lying in the back_inserter or am I doing something completely wrong? I can't figure out. I checked the construction of the single vectors and they work just fine, so the mistake has to lie somewhere after that.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire