From f769986d80474bbdaef6347ce25a400445357751 Mon Sep 17 00:00:00 2001 From: hg Date: Sun, 7 Aug 2016 12:58:04 +0200 Subject: [PATCH] verify method added --- main.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index fced142..93dfb75 100644 --- a/main.py +++ b/main.py @@ -9,14 +9,17 @@ import random startTime = 0 +lastTime = 0 def log(msg, reset=False): - global startTime + global startTime, lastTime currentTime = time.time() if reset: startTime = currentTime - print("%f: %s" % (currentTime - startTime, msg)) - + if lastTime == 0: + lastTime = currentTime + print("%f %f: %s" % (currentTime - startTime, currentTime - lastTime, msg)) + lastTime = currentTime def s_permutations(seq): items = [[]] @@ -100,6 +103,28 @@ def generate(order, lowerBound, upperBound): return matrix +def verify(m, v): + coeffs = m[:-1] + results = m[-1] + epsilons = [] + good = True + MAX_EPSILON = 1E-10 + worstEpsilon = 0.0 + + for i in range(len(results)): + res = 0.0 + for j in range(len(results)): + res += coeffs[j][i] * v[j] + epsilon = res - results[i] + if epsilon > MAX_EPSILON: + good = False + if abs(epsilon) > worstEpsilon: + worstEpsilon = abs(epsilon) + epsilons.append(res - results[i]) + + return (good, worstEpsilon, epsilons) + + #g = [[1.0,2.0,10.0], [5.0,3.0,11.0], [7.0,4.0,3.0], [8.0,9.0,13.0]] #g = [[599.1290806728277, 80.40433203454268, 615.5120772027384], [187.33506822036273, 663.7490700388757, 677.5997857592452], [956.929845461785, 456.8619830134234, 419.21973493934263], [61.824473951393344, 342.8555419523176, 216.32654519677496]] @@ -115,6 +140,9 @@ for i in range(2, 10+1): print(i) print(g) print(v) + + e = verify(g, v) + print(e) print