Using libRmath from Perl scripts: Difference between revisions
From OpenWetWare
Jump to navigationJump to search
No edit summary |
No edit summary |
||
| Line 19: | Line 19: | ||
} | } | ||
__DATA__ | |||
__C__ | __C__ | ||
Latest revision as of 20:16, 3 May 2007
The following shows a Perl script that calls libRmath to calculate the Student's t-distribution. It takes each line of input and calculating the t-distribution P(T<t) for the first number with the second number (tab separated) giving the degrees of freedom.
It illustrates several points about how to use Inline::C, including the use of prefixes and library inclusion.
#!/usr/bin/perl
use strict;
use Inline C => "DATA",
LIBS => '-lRmath -lm',
PREFIX => 'R_';
while(<>) {
chomp;
my ($q, $df) = split /\t/, $_;
print pt($q, $df || 5), "\n";
}
__DATA__
__C__
#define MATHLIB_STANDALONE 1
#include <Rmath.h>
double R_pt(double q, int df) {
return pt(q,df,1,0);
}