Active object: Difference between revisions

→‎{{header|Lua}}: added Lua solution
m (→‎Phix: comment -> use new requires() builtin)
(→‎{{header|Lua}}: added Lua solution)
Line 1,578:
{{out}}
<pre>-- 0.0004</pre>
 
=={{header|Lua}}==
Pure/native Lua is not multithreaded, so this task should perhaps be marked "omit from|Lua" if following the implicit ''intent'' of the task. However, the explicit ''wording'' of the task does not seem to require a multithreaded solution. Perhaps this is ''cheating'', but I thought it might interest the reader to see the integrator portion nonetheless, so it is demonstrated using a mock sampling method at various intervals (to ''simulate'' multithreaded updates).
<lang lua>local seconds = os.clock
 
local integrator = {
new = function(self, fn)
return setmetatable({fn=fn,t0=seconds(),v0=0,sum=0,nup=0},self)
end,
update = function(self)
self.t1 = seconds()
self.v1 = self.fn(self.t1)
self.sum = self.sum + (self.v0 + self.v1) * (self.t1 - self.t0) / 2
self.t0, self.v0, self.nup = self.t1, self.v1, self.nup+1
end,
input = function(self, fn) self.fn = fn end,
output = function(self) return self.sum end,
}
integrator.__index = integrator
 
-- "fake multithreaded sleep()"
-- waits for "duration" seconds calling "f" at every "interval" seconds
local function sample(duration, interval, f)
local now = seconds()
local untilwhen, nextinterval = now+duration, now+interval
f()
repeat
if seconds() >= nextinterval then f() nextinterval=nextinterval+interval end
until seconds() >= untilwhen
end
 
local pi, sin = math.pi, math.sin
local ks = function(t) return sin(2.0*pi*0.5*t) end
local kz = function(t) return 0 end
local intervals = { 0.5, 0.25, 0.1, 0.05, 0.025, 0.01, 0.005, 0.0025, 0.001 }
for _,interval in ipairs(intervals) do
local i = integrator:new(ks)
sample(2.0, interval, function() i:update() end)
i:input(kz)
sample(0.5, interval, function() i:update() end)
print(string.format("sampling interval: %f, %5d updates over 2.5s total = %.15f", interval, i.nup, i:output()))
end</lang>
{{out}}
<pre>sampling interval: 0.500000, 6 updates over 2.5s total = -0.003628054395752
sampling interval: 0.250000, 11 updates over 2.5s total = 0.003994231784540
sampling interval: 0.100000, 25 updates over 2.5s total = 0.001891527454886
sampling interval: 0.050000, 51 updates over 2.5s total = 0.023521980508657
sampling interval: 0.025000, 101 updates over 2.5s total = -0.000573259909112
sampling interval: 0.010000, 250 updates over 2.5s total = 0.003001745575344
sampling interval: 0.005000, 501 updates over 2.5s total = -0.000415541052666
sampling interval: 0.002500, 999 updates over 2.5s total = -0.001480800340644
sampling interval: 0.001000, 2493 updates over 2.5s total = 0.000362576907805</pre>
 
=={{header|Mathematica}}==
Anonymous user