Re: Simple Integration using Perl
by atcroft (Abbot) on Jan 28, 2002 at 20:30 UTC
|
My first suggestion would be Chapter 16 (Numerical Analysis) of Mastering Algorithms with Perl by Orwant, Hietaniemi, and Macdonald (from O'Reilly).
Beyond that, there are a number of books that include sections on scientific calculations (they escape my memory as I write this), of which at the very least the algorithms might prove helpful. | [reply] |
|
|
I quite agree. If you're trying to do numerical integration in Perl (and if your trying to do quite a number of other things), _Mastering Algorithms with Perl_ is a great book.
However, doing algebraic integration is rather beyond it's scope. It is, in fact, quite difficult. To quote:
If it's symbolic computation you're looking for, this chapter won't help -- but Ulrich Pfeifer's Math::ematica module will. Ulrich's module is freely available from CPAN, but requires the not-free Mathematica package to operate. There are, unfortunately, no free symbolic math packages available for Perl. Yet.
PS: Please excuse my terrible spelling.
TACCTGTTTGAGTGTAACAATCATTCGCTCGGTGTATCCATCTTTG
ACACAATGAATCTTTGACTCGAACAATCGTTCGGTCGCTCCGACGC
| [reply] |
Re: Simple Integration using Perl
by BazB (Priest) on Jan 28, 2002 at 20:38 UTC
|
I found Math::Integral::Romberg - might be of some use.
Otherwise, might be time to take a standard formula for this sort of calculation and implement it in Perl from scratch - as suggested by atcroft.
| [reply] |
Re: Simple Integration using Perl
by strat (Canon) on Jan 28, 2002 at 20:10 UTC
|
Please, could you be a little more verbose and tell exactly what you want to do?
Best regards,
perl -e "$_=*F=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print" | [reply] [d/l] |
|
|
at this moment I am trying to gain
knowledge (or how to) do integration
using perl, so I suppose we can start out
with something really simple like
integrating sin(x) from 0 to pi.
| [reply] |
|
|
First, grab a perl reference (e.g. The Camel or The Llama) if you haven't already got at least a smattering of perl knowledge. Secondly, do a search for Math modules at CPAN. This should get you started.. then come back with more specific questions if you need more help. Good luck.
| [reply] |
Re: Simple Integration using Perl
by strat (Canon) on Jan 28, 2002 at 20:36 UTC
|
What is your input data, and how shell the output be? Formulas, Graphs, ...?
Best regards,
perl -e "$_=*F=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print" | [reply] [d/l] |
Re: Simple Integration using Perl
by TGI (Parson) on Jan 29, 2002 at 00:25 UTC
|
Since a symbolic approach has been clearly shot down, I thought I'd suggest using the trapezoidal rule. Divide your interval into n even parts. Calculate the value of the function for each point that marks a boundary between parts of your interval. Use the values of these points to construct a series of trapezoids, bounded on the bottom with the x-axis. Then find the sum of the areas of the trapezoids.
A better approximation can be found by splitting the interval into uneven parts, basing your decision on the second derivative of the function. That is, you want to have more divisions in areas where the slope of the function changes a lot, and fewer divisions where the slope remains stable. The actual algorithm to implement this is left as an exercise for the reader. It's probably cheaper to just add more n when using the above approach, than to try and calculate all those derivatives, anyway.
Update: dragonchild- When you're holding a hammer, everything looks like a nail.
TGI says moo
| [reply] |
|
|
If you didn't want to use the smarter Math::Integral::Romberg and you are rolling your own, then it makes a lot of sense to use Simpson's method instead. Almost as little work, and you actually take into account the second derivative information, and make the third derivative irrelevant by a nice piece of symmetry.
# Takes a function, start point, end point, and number of intervals.
# Returns a numerical approximation of the integral using Simpsons Rul
+e.
sub integrate_simpson {
my ($func, $start, $end, $n) = @_;
my $width = ($end - $start)/$n;
my $mid_sum = sum(map {$func->($start + ($_ - 0.5)*$width)} 1..$n);
my $int_sum = sum(map {$func->($start + $_ *$width)} 1..$n-1);
my $first = $func->($start);
my $last = $func->($end);
($first + $last + 4*$mid_sum + 2*$int_sum) * $width / 6;
}
sub sum {
my $sum = shift;
$sum += shift while @_;
return $sum;
}
# And to test
print integrate_simpson(sub {$_[0]**3}, 0, 1, 9)
| [reply] [d/l] |
|
|
Of course, the very fact that one is thinking about doing heavy computational activities within Perl instead of (correctly) farming it out to a computational language like C or FORTRAN should be a tip-off that one has the wrong tool for the right job.
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
| [reply] |