46 lines
916 B
Raku
46 lines
916 B
Raku
class Integrator {
|
|
has $.f is rw = sub ($t) { 0 };
|
|
has $.now is rw;
|
|
has $.value is rw = 0;
|
|
has $.integrator is rw;
|
|
|
|
method init() {
|
|
self.value = &(self.f)(0);
|
|
self.integrator = Thread.new(
|
|
:code({
|
|
loop {
|
|
my $t1 = now;
|
|
self.value += (&(self.f)(self.now) + &(self.f)($t1)) * ($t1 - self.now) / 2;
|
|
self.now = $t1;
|
|
sleep .001;
|
|
}
|
|
}),
|
|
:app_lifetime(True)
|
|
).run
|
|
}
|
|
|
|
method Input (&f-of-t) {
|
|
self.f = &f-of-t;
|
|
self.now = now;
|
|
self.init;
|
|
}
|
|
|
|
method Output { self.value }
|
|
}
|
|
|
|
my $a = Integrator.new;
|
|
|
|
$a.Input( sub ($t) { sin(2 * π * .5 * $t) } );
|
|
|
|
say "Initial value: ", $a.Output;
|
|
|
|
sleep 2;
|
|
|
|
say "After 2 seconds: ", $a.Output;
|
|
|
|
$a.Input( sub ($t) { 0 } );
|
|
|
|
sleep .5;
|
|
|
|
say "f(0): ", $a.Output;
|