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