Numerical integration: Difference between revisions
m
→{{header|Perl 6}}: Added 'Promise' for concurrency
Artoria2e5 (talk | contribs) No edit summary |
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: Added 'Promise' for concurrency) |
||
Line 3,340:
=={{header|Perl 6}}==
The addition of <tt>'''Promise'''</tt>/<tt>'''await'''</tt>, in two places, allows for concurrent computation, and brings a significant speed-up in running time. Which is not to say that it makes this code fast, but it does make it less slow.
{{works with|rakudo|
<lang perl6>use MONKEY-SEE-NO-EVAL;
Line 3,380 ⟶ 3,378:
}
sub
my @r0;
say "\n$f\n in [$a..$b] / $n";▼
say ' rectangle method right: ', rightrect &f, $a, $b, $n;▼
my (@r1,@r2,@r3,@r4,@r5);
say ' rectangle method mid: ', midrect &f, $a, $b, $n;▼
my &f;
say 'composite trapezoidal rule: ', trapez &f, $a, $b, $n;▼
EVAL "&f = $f";
say ' quadratic simpsons rule: ', simpsons &f, $a, $b, $n;"▼
my $p1 = Promise.start( { @r1.push: ' rectangle method left: '~ leftrect(&f, $a, $b, $n).round($e) } );
▲
▲
▲
▲
await $p1, $p2, $p3, $p4, $p5;
@r0, @r1, @r2, @r3, @r4, @r5;
}
▲tryem '1 / *', 1, 100, 1000, log(100);
▲tryem '*.self', 0, 5_000, 5_000_000, 12_500_000;
▲tryem '*.self', 0, 6_000, 6_000_000, 18_000_000;</lang>
{{out}}
<lang>{ $_ ** 3 }
Line 3,410 ⟶ 3,413:
1 / *
in [1..100] / 1000
exact result: 4.
rectangle method left: 4.
rectangle method right: 4.
rectangle method mid: 4.
composite trapezoidal rule: 4.
quadratic simpsons rule: 4.
*.self
Line 3,435 ⟶ 3,438:
quadratic simpsons rule: 18000000</lang>
Note that these integrations are done with rationals rather than floats, so should be fairly precise (though of course with so few iterations they are not terribly accurate (except when they are)). Some of the sums do overflow into <tt>Num</tt> (floating point)--currently
=={{header|Phix}}==
|