FirstIndexLast

[help-gsl] need help - pass gsl_histogram as output of void function (2008-09-23)

From: chadia kanaan <chadiakanaan@........>
Subject: need help - pass gsl_histogram as output of void function
Date: Tue, 23 Sep 2008 05:33:27 +0000 (GMT)
To: help-gsl@gnu.org

Hi everybody,

Do not expect, brilliant question, I am a poor beginner in C and in gsl as well...

Here is my little problem :
In my code, I have spread many GSL_vectors next to random-generators in order to pick up their output. However, I need , as matter of organization, to gather all the stored data at the end , get their histograms and their pdf_histogram as well.
I have written below a void function make_hist_and_pdf that should create the histogram h and the Pdf P after receiving as arguments the GSL vector "input vector" and the size and ranges ( lower and upper ) of the histogram.
the function will be called many times and I expect it returns h1, h2,...p1,p2..at each call
1,2...

My question is : should I redefine again the histogram and pdf structures in order to pass it in functions ?? because, when I compile it, i get an "undeclared" h1,p1 ... !ÂÂ
My aim is to minimize as possible repetition in the code, otherwise, I am aware of the easy way :) .

Thanks for your help,
Chad

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_histogram.h>
#define zmin 0.
#define zmax 6.

void make_hist_and_pdf (&h, &p, size_t n, double lo_range, double up_range, double input_vector)
{

gsl_histogram *h = gsl_histogram_alloc (n);
gsl_histogram_pdf *p = gsl_histogram_pdf_alloc
(n);
gsl_histogram_set_ranges_uniform (h, lo_range, up_range);
gsl_histogram_increment (h, input_vector);
gsl_histogram_pdf_init (p, h);
}

int
main (void)
{

int i, nb_bins = 10;
double z;

const gsl_rng_type *T;ÂÂ
gsl_rng *r;ÂÂÂÂÂÂÂÂÂÂÂÂÂ
gsl_rng_env_setup();ÂÂÂÂ
T = gsl_rng_ranlxd2;ÂÂÂÂ
r = gsl_rng_alloc(T);Â
ÂÂÂÂÂ
gsl_vector *v_ptr1 = gsl_vector_alloc (nb_bins);

for (i=0; i <= nb_bins; i++)
ÂÂÂÂ {
Â
ÂÂÂÂÂÂ z = zmin-(gsl_rng_uniform(r)*(zmin-zmax));
ÂÂÂÂÂÂ gsl_vector_set(v_ptr1, i, z);
ÂÂÂÂÂÂ make_hist_and_pdf (h1, p1, nb_bins, zmin, zmax, gsl_vector_get (v_ptr1, i));
ÂÂÂÂ
}
ÂÂ
ÂÂÂÂÂ
gsl_rng_free(r);
gsl_vector_free(v_ptr1);
gsl_histogram_free (h1);
gsl_histogram_pdf_free (p1);

exit (0);

}