From e5f0d6016ba768c06b36d8b3d54f3ea700a4aa58 Mon Sep 17 00:00:00 2001 From: William Wang Date: Tue, 5 Jun 2012 01:23:11 -0400 Subject: stats: when applying an operation to two vectors sum the components first. Previously writing X/Y in a formula would result in: x[0]/y[0] + x[1]/y[1] In reality you want: (x[0] +x[1])/(y[0] + y[1]) --- src/base/statistics.hh | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/base') diff --git a/src/base/statistics.hh b/src/base/statistics.hh index c36f8f461..723c8bd9c 100644 --- a/src/base/statistics.hh +++ b/src/base/statistics.hh @@ -2289,9 +2289,31 @@ class BinaryNode : public Node total() const { const VResult &vec = this->result(); + const VResult &lvec = l->result(); + const VResult &rvec = r->result(); Result total = 0.0; - for (off_type i = 0; i < size(); i++) + Result lsum = 0.0; + Result rsum = 0.0; + Op op; + + assert(lvec.size() > 0 && rvec.size() > 0); + assert(lvec.size() == rvec.size() || + lvec.size() == 1 || rvec.size() == 1); + + /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */ + if (lvec.size() == rvec.size() && lvec.size() > 1) { + for (off_type i = 0; i < size(); ++i) { + lsum += lvec[i]; + rsum += rvec[i]; + } + return op(lsum, rsum); + } + + /** Otherwise divide each item by the divisor */ + for (off_type i = 0; i < size(); ++i) { total += vec[i]; + } + return total; } -- cgit v1.2.3