2010-04-29 22:29:35 -07:00
|
|
|
#!/usr/bin/env python
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
import unittest
|
2010-10-23 12:49:15 -07:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
from interp import baker
|
2011-09-17 14:39:01 -07:00
|
|
|
from interp.baker import Answer
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
|
2011-02-15 10:27:45 -08:00
|
|
|
class Test(unittest.TestCase):
|
2011-09-17 14:38:49 -07:00
|
|
|
def setUp(self):
|
|
|
|
self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1],
|
|
|
|
[0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
|
|
|
|
self.all_points = [
|
|
|
|
[0, 0], # 0
|
|
|
|
[1, 0], # 1
|
|
|
|
[1, 1], # 2
|
|
|
|
[0, 1], # 3
|
|
|
|
[1, -1], # 4
|
|
|
|
[0, -1], # 5
|
|
|
|
[-1, 1], # 6
|
|
|
|
[-1, 0], # 7
|
|
|
|
[-1, -1], # 8
|
|
|
|
]
|
|
|
|
self.q = [1, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
self.X = [0.5, 0.25]
|
|
|
|
self.accuracy = 8
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testImports(self):
|
|
|
|
import numpy
|
|
|
|
import scipy
|
|
|
|
import interp.grid as gv
|
|
|
|
import interp.baker as bv
|
2010-02-06 23:01:02 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
numpy.__version__
|
|
|
|
scipy.__version__
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
gv, bv
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testGetPhis(self):
|
|
|
|
X = [0, 0]
|
|
|
|
r = [[-1, -1], [0, 2], [1, -1]]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
result = baker.get_phis(X, r)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
right_answer = [1 / 3.0, 1 / 3.0, 1 / 3.0]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
for a, b in zip(result, right_answer):
|
|
|
|
self.assertAlmostEqual(a, b)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testGetPhis2(self):
|
|
|
|
X = [0.5, 0.25]
|
|
|
|
r = [[0, 0], [1, 0], [1, 1]]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
result = baker.get_phis(X, r)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
right_answer = [0.5, 0.25, 0.25]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
for a, b in zip(result, right_answer):
|
|
|
|
self.assertEqual(a, b)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testQlinear(self):
|
|
|
|
X = [0.5, 0.25]
|
|
|
|
r = [[0, 0], [1, 0], [1, 1]]
|
|
|
|
q = [1, 0, 0]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
phi, result = baker.qlinear(X, r, q)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
right_answer = 0.5
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
self.assertAlmostEqual(result, right_answer)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
def testRunBaker_linear(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
|
|
|
|
R, R_q = (self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
|
|
|
|
|
|
|
|
|
|
|
answer = baker.interpolate(self.X, R, R_q)
|
|
|
|
good_answer = Answer(qlin=0.5, final=None, error=None, abc={})
|
|
|
|
self.assertEqual(answer, good_answer)
|
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testRunBaker_1(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 3
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
R, R_q = (self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
S, S_q = (self.all_points[size_of_simplex:size_of_simplex \
|
|
|
|
+ extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
answer = baker.interpolate(self.X, R, R_q, S, S_q)
|
2011-01-26 21:52:39 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
a = answer.abc[0]
|
|
|
|
b = answer.abc[1]
|
|
|
|
c = answer.abc[2]
|
2011-01-26 21:52:39 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
self.assertEqual(sorted((a, b, c)), sorted((0, 0.0, 1 / 3.)))
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testRunBaker_2(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 4
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
R, R_q = (self.all_points[:size_of_simplex], self.q[:size_of_simplex])
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
S, S_q = (self.all_points[size_of_simplex:size_of_simplex \
|
|
|
|
+ extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
answer = baker.interpolate(self.X, R, R_q, S, S_q)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
a, b, c = sorted(answer.abc)
|
2011-09-17 14:38:49 -07:00
|
|
|
aa, bb, cc = sorted((2 / 3.0, 2 / 3.0, 1 / 3.0))
|
2010-01-31 20:10:37 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
self.assertAlmostEqual(a, aa)
|
|
|
|
self.assertAlmostEqual(b, bb)
|
|
|
|
self.assertAlmostEqual(c, cc)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testRunBaker_3(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 5
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
R, R_q = (self.all_points[:size_of_simplex], self.q[:size_of_simplex])
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
S, S_q = (self.all_points[size_of_simplex:size_of_simplex \
|
|
|
|
+ extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
2011-09-17 14:39:01 -07:00
|
|
|
answer = baker.interpolate(self.X, R, R_q, S, S_q)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
a = answer.abc[0]
|
|
|
|
b = answer.abc[1]
|
|
|
|
c = answer.abc[2]
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
a, b, c = sorted((a, b, c))
|
|
|
|
aa, bb, cc = sorted((13 / 14., 2 / 7., 15 / 14.))
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
self.assertAlmostEqual(a, aa)
|
|
|
|
self.assertAlmostEqual(b, bb)
|
|
|
|
self.assertAlmostEqual(c, cc)
|
2011-01-26 21:52:39 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
def testRunBaker_4(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 6
|
2011-01-26 21:52:39 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
R, R_q = (self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
|
|
|
S, S_q = (self.all_points[size_of_simplex:size_of_simplex \
|
|
|
|
+ extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
2011-09-17 14:39:01 -07:00
|
|
|
answer = baker.interpolate(self.X, R, R_q, S, S_q)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:39:01 -07:00
|
|
|
a = answer.abc[0]
|
|
|
|
b = answer.abc[1]
|
|
|
|
c = answer.abc[2]
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
a, b, c = sorted((a, b, c))
|
|
|
|
aa, bb, cc = sorted((48 / 53.0, 15 / 53.0, 54 / 53.0))
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
self.assertAlmostEqual(a, aa)
|
|
|
|
self.assertAlmostEqual(b, bb)
|
|
|
|
self.assertAlmostEqual(c, cc)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-09-17 14:38:49 -07:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(Test)
|
|
|
|
unittest.TextTestRunner(verbosity=3).run(suite)
|