June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
73
Task/Active-object/D/active-object.d
Normal file
73
Task/Active-object/D/active-object.d
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import core.thread;
|
||||
import std.datetime;
|
||||
import std.math;
|
||||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
auto func = (double t) => sin(cast(double) PI * t);
|
||||
Integrator integrator = new Integrator(func);
|
||||
Thread.sleep(2000.msecs);
|
||||
|
||||
integrator.setFunc(t => 0.0);
|
||||
Thread.sleep(500.msecs);
|
||||
|
||||
integrator.stop();
|
||||
writeln(integrator.getOutput());
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrates input function K over time
|
||||
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
|
||||
*/
|
||||
public class Integrator {
|
||||
public alias Function = double function (double);
|
||||
|
||||
private SysTime start;
|
||||
private shared bool running;
|
||||
|
||||
private Function func;
|
||||
private shared double t0;
|
||||
private shared double v0;
|
||||
private shared double sum = 0.0;
|
||||
|
||||
public this(Function func) {
|
||||
this.start = Clock.currTime();
|
||||
setFunc(func);
|
||||
new Thread({
|
||||
integrate();
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void setFunc(Function func) {
|
||||
this.func = func;
|
||||
v0 = func(0.0);
|
||||
t0 = 0.0;
|
||||
}
|
||||
|
||||
public double getOutput() {
|
||||
return sum;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
private void integrate() {
|
||||
running = true;
|
||||
while (running) {
|
||||
Thread.sleep(1.msecs);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
private void update() {
|
||||
import core.atomic;
|
||||
|
||||
Duration t1 = (Clock.currTime() - start);
|
||||
double v1 = func(t1.total!"msecs");
|
||||
double rect = (t1.total!"msecs" - t0) * (v0 + v1) / 2;
|
||||
atomicOp!"+="(this.sum, rect);
|
||||
t0 = t1.total!"msecs";
|
||||
v0 = v1;
|
||||
}
|
||||
}
|
||||
40
Task/Active-object/Julia/active-object.julia
Normal file
40
Task/Active-object/Julia/active-object.julia
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
mutable struct Integrator
|
||||
func::Function
|
||||
runningsum::Float64
|
||||
dt::Float64
|
||||
running::Bool
|
||||
function Integrator(f::Function, dt::Float64)
|
||||
this = new()
|
||||
this.func = f
|
||||
this.runningsum = 0.0
|
||||
this.dt = dt
|
||||
this.running = false
|
||||
return this
|
||||
end
|
||||
end
|
||||
|
||||
function run(integ::Integrator, lastval::Float64 = 0.0)
|
||||
lasttime = time()
|
||||
while integ.running
|
||||
sleep(integ.dt)
|
||||
newtime = time()
|
||||
measuredinterval = newtime - lasttime
|
||||
newval = integ.func(measuredinterval)
|
||||
integ.runningsum += (lastval + newval) * measuredinterval / 2.0
|
||||
lasttime = newtime
|
||||
lastval = newval
|
||||
end
|
||||
end
|
||||
|
||||
start!(integ::Integrator) = (integ.running = true; @async run(integ))
|
||||
stop!(integ) = (integ.running = false)
|
||||
f1(t) = sin(2π * t)
|
||||
f2(t) = 0.0
|
||||
|
||||
it = Integrator(f1, 0.00001)
|
||||
start!(it)
|
||||
sleep(2.0)
|
||||
it.func = f2
|
||||
sleep(0.5)
|
||||
v2 = it.runningsum
|
||||
println("After 2.5 seconds, integrator value was $v2")
|
||||
69
Task/Active-object/Kotlin/active-object.kotlin
Normal file
69
Task/Active-object/Kotlin/active-object.kotlin
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
// version 1.2.0
|
||||
|
||||
import kotlin.math.*
|
||||
|
||||
typealias Function = (Double) -> Double
|
||||
|
||||
/**
|
||||
* Integrates input function K over time
|
||||
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
|
||||
*/
|
||||
class Integrator {
|
||||
private val start: Long
|
||||
private @Volatile var running = false
|
||||
private lateinit var func: Function
|
||||
private var t0 = 0.0
|
||||
private var v0 = 0.0
|
||||
private var sum = 0.0
|
||||
|
||||
constructor(func: Function) {
|
||||
start = System.nanoTime()
|
||||
setFunc(func)
|
||||
Thread(this::integrate).start()
|
||||
}
|
||||
|
||||
fun setFunc(func: Function) {
|
||||
this.func = func
|
||||
v0 = func(0.0)
|
||||
t0 = 0.0
|
||||
}
|
||||
|
||||
fun getOutput() = sum
|
||||
|
||||
fun stop() {
|
||||
running = false
|
||||
}
|
||||
|
||||
private fun integrate() {
|
||||
running = true
|
||||
while (running) {
|
||||
try {
|
||||
Thread.sleep(1)
|
||||
update()
|
||||
}
|
||||
catch(e: InterruptedException) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun update() {
|
||||
val t1 = (System.nanoTime() - start) / 1.0e9
|
||||
val v1 = func(t1)
|
||||
val rect = (t1 - t0) * (v0 + v1) / 2.0
|
||||
sum += rect
|
||||
t0 = t1
|
||||
v0 = v1
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val integrator = Integrator( { sin(PI * it) } )
|
||||
Thread.sleep(2000)
|
||||
|
||||
integrator.setFunc( { 0.0 } )
|
||||
Thread.sleep(500)
|
||||
|
||||
integrator.stop()
|
||||
println(integrator.getOutput())
|
||||
}
|
||||
41
Task/Active-object/Lingo/active-object-1.lingo
Normal file
41
Task/Active-object/Lingo/active-object-1.lingo
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
property _sum
|
||||
property _func
|
||||
property _timeLast
|
||||
property _valueLast
|
||||
property _ms0
|
||||
property _updateTimer
|
||||
|
||||
on new (me, func)
|
||||
if voidP(func) then func = "0.0"
|
||||
me._sum = 0.0
|
||||
-- update frequency: 100/sec (arbitrary)
|
||||
me._updateTimer = timeout().new("update", 10, #_update, me)
|
||||
me.input(func)
|
||||
return me
|
||||
end
|
||||
|
||||
on stop (me)
|
||||
me._updateTimer.period = 0 -- deactivates timer
|
||||
end
|
||||
|
||||
-- func is a term (as string) that might contain "t" and is evaluated at runtime
|
||||
on input (me, func)
|
||||
me._func = func
|
||||
me._ms0 = _system.milliseconds
|
||||
me._timeLast = 0.0
|
||||
t = 0.0
|
||||
me._valueLast = value(me._func)
|
||||
end
|
||||
|
||||
on output (me)
|
||||
return me._sum
|
||||
end
|
||||
|
||||
on _update (me)
|
||||
now = _system.milliseconds - me._ms0
|
||||
t = now/1000.0
|
||||
val = value(me._func)
|
||||
me._sum = me._sum + (me._valueLast+val)*(t - me._timeLast)/2
|
||||
me._timeLast = t
|
||||
me._valueLast = val
|
||||
end
|
||||
19
Task/Active-object/Lingo/active-object-2.lingo
Normal file
19
Task/Active-object/Lingo/active-object-2.lingo
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
global gIntegrator
|
||||
|
||||
-- entry point
|
||||
on startMovie
|
||||
gIntegrator = script("Integrator").new("sin(PI * t)")
|
||||
timeout().new("timer", 2000, #step1)
|
||||
end
|
||||
|
||||
on step1 (_, timer)
|
||||
gIntegrator.input("0.0")
|
||||
timer.timeoutHandler = #step2
|
||||
timer.period = 500
|
||||
end
|
||||
|
||||
on step2 (_, timer)
|
||||
gIntegrator.stop()
|
||||
put gIntegrator.output()
|
||||
timer.forget()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue