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

Hi, I'm trying to get the perl module PDL::GSLSF::GAMMA working but am having some difficulty. As I understand it, the function "gsl_sf_gamma" should return the gamma function "Gamma(x)". For example, in R:
> gamma(0.4) [1] 2.21816
However, I can't seem to replicate this using PDL:
#!/usr/bin/perl use PDL; use PDL::GSLSF::GAMMA; my $out = gsl_sf_gamma(0.4); print "$out\n";
Output is "1.63493983016443e-14". I'm clearly doing something wrong, please excuse my ignorance. Any help greatly appreciated.

Replies are listed 'Best First'.
Re: Using PDL::GSLSF::GAMMA
by Khen1950fx (Canon) on Aug 09, 2012 at 14:47 UTC
    Try this:
    #!/usr/bin/perl -l BEGIN { $| = 1; $^W = 1; } use autodie; use PDL::LiteF; use common::sense; BEGIN { use PDL::Config; if ( $PDL::Config{'WITH_GSL'} ) { eval " use PDL::GSLSF::GAMMA "; unless ($@) { return; } else { print "Houston, we have a problem: $!"; } } } my $num = 0.4; my ($f, $e) = gsl_sf_gamma($num); print $f;
Re: Using PDL::GSLSF::GAMMA
by syphilis (Archbishop) on Aug 09, 2012 at 14:50 UTC
    I get the same (wrong) result as you.
    Either there's a bug in the implementation, or there's a bug in the documentation. Either way, unless someone here comes up with a better idea, I'd report it to the pdl mailing list.

    Another option would be to submit a bug report to the PDL bug tracker on Sourceforge ... if your browser is up to handling something as shitful as Sourceforge and you can find the PDL bug tracker. (Sorry, I don't even have a link.)

    Math::GSL seems to get it right:
    C:\>perl -MMath::GSL::SF -e "print Math::GSL::SF::gsl_sf_gamma(0.4)" 2.21815954375769 C:\>
    I guess that means the error is in the PDL implementation.

    UPDATE: Khen1950fx's approach seems to work ok. But where is this documented ?

    Cheers,
    Rob
Re: Using PDL::GSLSF::GAMMA
by Anonymous Monk on Aug 09, 2012 at 15:08 UTC
    Thanks! The reply by Khen1950fx led me to conclude that this works - the only difference being the presence of round brackets...
    #!/usr/bin/perl use PDL; use PDL::GSLSF::GAMMA; my ($out) = gsl_sf_gamma(0.4); print "$out\n";
Re: Using PDL::GSLSF::GAMMA
by etj (Priest) on May 22, 2022 at 20:06 UTC
    This is a bit of a general problem with PDL operations that return more than one result (for instance, I'm seeing this in the nearly-finished PDL::OpenCV, and I consider it somewhat of a problem), and I'm very open to ideas on a way forward.
    pdl> ? gsl_sf_gamma Module PDL::GSLSF::GAMMA gsl_sf_gamma Signature: (double x(); double [o]y(); double [o]e()) Gamma(x), x not a negative integer
    Note the two [o] parameters (i.e. outputs); the second one is the error, which here was a very small number. If you call the op in scalar context, you get the last output. In this case, that's arguably the least useful one, as shown by the OP's confusion. One idea is to just throw an exception for a multi-output op called in scalar context. Anyone got a view?