Numerical integration: Difference between revisions
Content added Content deleted
Artoria2e5 (talk | contribs) No edit summary |
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: Added 'Promise' for concurrency) |
||
Line 3,340: | Line 3,340: | ||
=={{header|Perl 6}}== |
=={{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| |
{{works with|rakudo|2018-09}} |
||
<lang perl6>use MONKEY-SEE-NO-EVAL; |
<lang perl6>use MONKEY-SEE-NO-EVAL; |
||
Line 3,380: | Line 3,378: | ||
} |
} |
||
sub |
sub integrate($f, $a, $b, $n, $exact) { |
||
my @r0; |
|||
⚫ | |||
my $e = 0.000001; |
|||
⚫ | |||
say ' exact result: ', $exact; |
|||
@r0.push: ' exact result: '~ $exact.round($e); |
|||
⚫ | |||
my (@r1,@r2,@r3,@r4,@r5); |
|||
⚫ | |||
my &f; |
|||
⚫ | |||
EVAL "&f = $f"; |
|||
⚫ | |||
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; |
|||
} |
} |
||
.say for integrate '{ $_ ** 3 }', 0, 1, 100, 0.25; say ''; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |
||
<lang>{ $_ ** 3 } |
<lang>{ $_ ** 3 } |
||
Line 3,410: | Line 3,413: | ||
1 / * |
1 / * |
||
in [1..100] / 1000 |
in [1..100] / 1000 |
||
exact result: 4. |
exact result: 4.60517 |
||
rectangle method left: 4. |
rectangle method left: 4.654991 |
||
rectangle method right: 4. |
rectangle method right: 4.556981 |
||
rectangle method mid: 4. |
rectangle method mid: 4.604763 |
||
composite trapezoidal rule: 4. |
composite trapezoidal rule: 4.605986 |
||
quadratic simpsons rule: 4. |
quadratic simpsons rule: 4.60517 |
||
*.self |
*.self |
||
Line 3,435: | Line 3,438: | ||
quadratic simpsons rule: 18000000</lang> |
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 Num (floating point)--currently |
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 Rakudo allows 64-bit denominators--but at least all of the interval arithmetic is exact. |
||
=={{header|Phix}}== |
=={{header|Phix}}== |