def test_conj_solve(self): x,y = np.exp(1.), np.exp(2.+1j) d,w = {'x*y_':x*y.conjugate(), 'x':x}, {} for k in d: w[k] = 1. ls = linsolve.LogProductSolver(d,w,sparse=self.sparse) sol = ls.solve() for k in sol: self.assertAlmostEqual(sol[k], eval(k))
def test_solve(self): x,y,z = np.exp(1.), np.exp(2.), np.exp(3.) keys = ['x*y*z', 'x*y', 'y*z'] d,w = {}, {} for k in keys: d[k],w[k] = eval(k), 1. ls = linsolve.LogProductSolver(d,w,sparse=self.sparse) sol = ls.solve() for k in sol: self.assertAlmostEqual(sol[k], eval(k))
def test_solve(self): x, y, z = np.exp(1. + 1j), np.exp(2. + 2j), np.exp(3. + 3j) keys = ['x*y*z', 'x*y', 'y*z'] d, w = {}, {} for k in keys: d[k], w[k] = eval(k), 1. ls = linsolve.LogProductSolver(d, w, sparse=self.sparse) sol = ls.solve() for k in sol: np.testing.assert_almost_equal(sol[k], eval(k))
def test_init(self): x,y,z = np.exp(1.), np.exp(2.), np.exp(3.) keys = ['x*y*z', 'x*y', 'y*z'] d,w = {}, {} for k in keys: d[k],w[k] = eval(k), 1. ls = linsolve.LogProductSolver(d,w,sparse=self.sparse) for k in ls.ls_phs.data: np.testing.assert_equal(ls.ls_phs.data[k], 0) x,y,z = 1.,2.,3. for k in ls.ls_amp.data: np.testing.assert_equal(eval(k), ls.ls_amp.data[k])
def test_dtype(self): for dtype in (np.float32, np.float64, np.complex64, np.complex128): x,y,z = np.exp(1.), np.exp(2.), np.exp(3.) keys = ['x*y*z', 'x*y', 'y*z'] d,w = {}, {} for k in keys: d[k] = eval(k).astype(dtype) w[k] = np.float32(1.) ls = linsolve.LogProductSolver(d,w,sparse=self.sparse) sol = ls.solve() for k in sol: self.assertEqual(sol[k].dtype, dtype)
def test_conj(self): x,y = 1+1j, 2+2j d,w = {}, {} d['x*y_'] = x * y.conjugate() d['x_*y'] = x.conjugate() * y d['x*y'] = x * y d['x_*y_'] = x.conjugate() * y.conjugate() for k in d: w[k] = 1. ls = linsolve.LogProductSolver(d,w,sparse=self.sparse) self.assertEqual(len(ls.ls_amp.data), 4) for k in ls.ls_amp.data: self.assertEqual(eval(k), 3+3j) # make sure they are all x+y self.assertTrue(k.replace('1','-1') in ls.ls_phs.data)
def test_no_abs_phs_solve(self): x,y,z = 1.+1j, 2.+2j, 3.+3j d,w = {'x*y_':x*y.conjugate(), 'x*z_':x*z.conjugate(), 'y*z_':y*z.conjugate()}, {} for k in list(d.keys()): w[k] = 1. ls = linsolve.LogProductSolver(d,w,sparse=self.sparse) sol = ls.solve() x,y,z = sol['x'], sol['y'], sol['z'] self.assertAlmostEqual(np.angle(x*y.conjugate()), 0.) self.assertAlmostEqual(np.angle(x*z.conjugate()), 0.) self.assertAlmostEqual(np.angle(y*z.conjugate()), 0.) # check projection of degenerate mode self.assertAlmostEqual(np.angle(x), 0.) self.assertAlmostEqual(np.angle(y), 0.) self.assertAlmostEqual(np.angle(z), 0.)
def test_conj(self): x, y = 1 + 1j, 2 + 2j d, w = {}, {} d['x*y_'] = x * y.conjugate() d['x_*y'] = x.conjugate() * y d['x*y'] = x * y d['x_*y_'] = x.conjugate() * y.conjugate() for k in d: w[k] = 1. ls = linsolve.LogProductSolver(d, w, sparse=self.sparse) assert len(ls.ls_amp.data) == 4 for k in ls.ls_amp.data: assert eval(k) == 3 + 3j # make sure they are all x+y assert k.replace('1', '-1') in ls.ls_phs.data
def test_dtype(self): for dtype in (np.float32, np.float64, np.complex64, np.complex128): x, y, z = np.exp(1.), np.exp(2.), np.exp(3.) keys = ['x*y*z', 'x*y', 'y*z'] d, w = {}, {} for k in keys: d[k] = eval(k).astype(dtype) w[k] = np.float32(1.) ls = linsolve.LogProductSolver(d, w, sparse=self.sparse) # some ridiculousness to avoid "The exact solution is x = 0" prints save_stdout = sys.stdout sys.stdout = io.StringIO() sol = ls.solve() sys.stdout = save_stdout for k in sol: assert sol[k].dtype == dtype
def test_no_abs_phs_solve(self): x, y, z = 1. + 1j, 2. + 2j, 3. + 3j d, w = { 'x*y_': x * y.conjugate(), 'x*z_': x * z.conjugate(), 'y*z_': y * z.conjugate() }, {} for k in list(d.keys()): w[k] = 1. ls = linsolve.LogProductSolver(d, w, sparse=self.sparse) # some ridiculousness to avoid "The exact solution is x = 0" prints save_stdout = sys.stdout sys.stdout = io.StringIO() sol = ls.solve() sys.stdout = save_stdout x, y, z = sol['x'], sol['y'], sol['z'] np.testing.assert_almost_equal(np.angle(x * y.conjugate()), 0.) np.testing.assert_almost_equal(np.angle(x * z.conjugate()), 0.) np.testing.assert_almost_equal(np.angle(y * z.conjugate()), 0.) # check projection of degenerate mode np.testing.assert_almost_equal(np.angle(x), 0.) np.testing.assert_almost_equal(np.angle(y), 0.) np.testing.assert_almost_equal(np.angle(z), 0.)