Numerical integration: Difference between revisions
→{{header|Raku}}: Move some calculations out of hot loops, more efficient looping constructs, arguably less idiomatic, but less horribly slow
SqrtNegInf (talk | contribs) m (→{{header|Sidef}}: Fix link: Perl 6 --> Raku) |
Thundergnat (talk | contribs) (→{{header|Raku}}: Move some calculations out of hot loops, more efficient looping constructs, arguably less idiomatic, but less horribly slow) |
||
Line 4,183:
sub leftrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
loop (my $i = $a; $i <= $end; $i += $h) { $sum += f($i) }
$h * $sum;
}
sub rightrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
loop (my $i = $a+$h; $i <= $b; $i += $h) { $sum += f($i) }
$h * $sum;
}
sub midrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
my $sum = 0;
my ($start, $end) = $a+$h/2, $b-$h/2;
loop (my $i = $start; $i <= $end; $i += $h) { $sum += f($i) }
$h * $sum;
}
sub trapez(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
my $partial-sum
my ($
loop (my $i = $start; $i <= $end; $i += $h) { $partial-sum += f($i) * 2 }
$h / 2 * ( f($a) + f($b) + $partial-sum );
}
sub simpsons(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
my $h2 = $h/2;
my ($start, $end) = $a+$h, $b-$h;
my $sum1 = f($a + $h2);
my $sum2 = 0;
loop (my $i = $start; $i <= $end; $i += $h) {
$
▲ $sum2 += f($_);
}
($h / 6) * (f($a) + f($b) + 4*$sum1 + 2*$sum2);
}
sub integrate($f, $a, $b, $n, $exact) {
▲ my @r0;
my $e = 0.000001;
my (
my &f;
EVAL "&f = $f";
my $p1 = Promise.start( {
my $p2 = Promise.start( {
my $p3 = Promise.start( {
my $p4 = Promise.start( {
my $p5 = Promise.start( {
await $p1, $p2, $p3, $p4, $p5;
}
.say for integrate '{ $_ ** 3 }', 0, 1, 100, 0.25; say '';
.say for integrate '1 / *', 1, 100, 1000, log(100); say '';
|