Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How does Math::GSL accept data?

by hda (Chaplain)
on May 18, 2009 at 15:52 UTC ( [id://764662]=perlquestion: print w/replies, xml ) Need Help??

hda has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise monks!

I am trying to use Math::GSL::histogram to construct an histogram with logarithmic bins. What I want to do is to read the data from an array or a PDL piddle. However, Math::GSL seems to require a "stream", i.e. a file to read data from. I tried to save my data to a file in several formats but I could not manage to get Math::GSL to read it. I am obviously doing something wrong and I will appreciate any hint from you!

Here is my code, it terminates with segment fault:

my $fo = gsl_fopen('myfile', 'w'); # sets the output file my $fi = gsl_fopen( $input_stream ); # here is where I can't understan +d how it works my $h = gsl_histogram_alloc (5); gsl_histogram_fscanf($fi, $h); my $range = [ 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0 ]; gsl_histogram_set_ranges($h, $range, 4); gsl_histogram_fprintf($fo, $h, "%g", "%g"); gsl_fclose($fi); gsl_fclose($fo);


Thanks in advance!

Replies are listed 'Best First'.
Re: How does Math::GSL accept data?
by Anonymous Monk on May 18, 2009 at 16:35 UTC
      Hi! thanks for your answer, but unfortunately I cannot make it to work. Here is the beggining of my data:

      1 3 25 1 5 1 3 1 2 2 3 3 3 25 1 1 5 2 4500 1 1 1 1 5 5 8000 1 1 5 500 1 1 1 5 2 1 1 1000 1000 1000 1000 5000 1 25 6000 500 25 25 9000 1 1 5 1000 6000 25 10 5 5 5 5 1000 9540 1000 5 1 5 500 2500 2 5 5 5 ...

      Is just a sequence of integers, stored in a plain text file called "data".

      Now, this is my code:

      my $fi = gsl_fopen('data', 'r'); my $fo = gsl_fopen('hist', 'w'); my $h = gsl_histogram_alloc(6); my $range = [ 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0 ]; gsl_histogram_fread($fi, $h); gsl_histogram_set_ranges($h,$range,7); gsl_histogram_fprintf($fo, $h, "%3.1f", "%3.1f"); gsl_fclose($fo); gsl_fclose($fi);

      What I need is to produce a histogram with 6 logarithmic bins (see the $range variable in the code). When I run this code I get no errors, and the result is:

      1.0 10.0 0.0
      10.0 100.0 0.0
      100.0 1000.0 0.0
      1000.0 10000.0 0.0
      10000.0 100000.0 0.0
      100000.0 0.0 0.0


      So, either no data is read or something of what I have no idea is going on. In the documentation one finds:

      gsl_histogram_fread($stream, $h) - This function reads into the histogram $h from the open stream $stream, which has been opened by the gsl_fopen function from the Math::GSL module, in binary format. The histogram $h must be preallocated with the correct size since the function uses the number of bins in $h to determine how many bytes to read. The return value is 0 for success and $GSL_EFAILED if there was a problem reading from the file. The data is assumed to have been written in the native binary format on the same architecture.

      But I find it a bit complicated to understand if a text file can be read or not. Oh wise monks! any hint will be appreciated!
        Well, I don't know Perl, but I do know GSL. Statemens in your code are in a wrong order. 1) Allocate 6 bins gsl_histogram* h=gsl_histogram_alloc(6); 2) Set the ranges. 6 bins require 7 points. (you have 6) double ranges7={1.,2.,3.,4.,5.,6.,7.}; gsl_histogram_set_ranges(h,ranges,7); 3) And only after initialization one can use the histogram 4) In C one should free the histogram after using it. No idea about Perl.
Re: How does Math::GSL accept data?
by etj (Priest) on Jun 03, 2022 at 21:52 UTC
    gsl_histogram is currently not part of the PDL GSL bindings. I would have expected either PDL::Basic's hist or PDL::NDBin to accommodate user-provided ranges, in line with your requirement, but it seems neither of them do. Pull requests to rectify any or all of those would be most welcome!

      I didn't investigate what's wrong with OP's code and how to use Math::GSL for the task, but

      I would have expected either PDL::Basic's hist or PDL::NDBin to accommodate user-provided ranges, in line with your requirement, but it seems neither of them do.

      I think you are wrong here, see Variable-width bins

      pdl> p$data = pdl qw/ 1 3 25 1 5 1 3 1 2 2 3 3 3 25 1 1 5 2 4500 1 1 1 + 1 5 5 8 000 1 1 5 500 1 1 1 5 2 1 1 1000 1000 1000 1000 5000 1 25 60 +00 500 25 25 9000 1 1 5 1000 6000 25 10 5 5 5 5 1000 9540 1000 5 1 5 +500 2500 2 5 5 5/ [1 3 25 1 5 1 3 1 2 2 3 3 3 25 1 1 5 2 4500 1 1 1 1 5 5 8000 1 1 5 500 + 1 1 1 5 2 1 1 1000 1000 1000 1000 5000 1 25 6000 500 25 25 9000 1 1 +5 1000 6000 25 10 5 5 5 5 1000 9540 1000 5 1 5 500 2500 2 5 5 5] pdl> $binner = PDL::NDBin->new(axes=>[[x => grid => 10 ** sequence 6]] +) pdl> $binner->feed(x => $data) pdl> $binner->process pdl> p $binner->output->{histogram} [47 7 3 15 0] pdl>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764662]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2025-06-12 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.