#include "brick_statistic.h"


bool BrickStatisticMinMaxI::tick_body(double time) {
	if (inputs_count == 0) return true;
	min = max = inputs[0];
	for (int i = 1; i < inputs_count; ++i) {
		if (min > inputs[i]) min = inputs[i];
		if (max < inputs[i]) max = inputs[i];
	}
	outputs[Min] = min;
	outputs[Max] = max;
	return true;
}


bool BrickStatisticExpectation::tick_body(double time) {
	t += time - pt;
	pt = time;
	i.setInputValue(0, inputs[0]);
	i.tick(time);
	if (t == 0.) outputs[0] = 0.;
	else outputs[0] = i.output(0) / t;
	return true;
}


bool BrickStatisticVariance::tick_body(double time) {
	t += time - pt;
	pt = time;
	i0.setInputValue(0, inputs[0]);
	i0.tick(time);
	if (t == 0.) i1.setInputValue(0, 0.);
	else i1.setInputValue(0, sqr(inputs[0] - i0.output(0) / t));
	i1.tick(time);
	if (t == 0.) outputs[0] = 0.;
	else outputs[0] = i1.output(0) / t;
	return true;
}
