def test_derivs(self): ns = range(1, 6) for ee, d in (self.e, 1e-6), (self.p, 1e-6): xd = self.x + [[0, 0, 0], [d, 0, 0], [0, d, 0], [0, 0, d]] for n in ns: p = utils.expand_tensor(ee.potential(self.x, n))[0] pd = utils.expand_tensor(ee.potential(xd, n-1)) pd = (pd[1:] - pd[0])/d nptest.assert_allclose(p, pd, rtol=d, atol=1e-14, err_msg="ee=%s, n=%i" % (ee, n))
def test_derivs(self): ns = range(1, 6) d = 1e-7 xd = self.x + [[0, 0, 0], [d, 0, 0], [0, d, 0], [0, 0, d]] for n in ns: p = utils.expand_tensor(self.e.potential(self.x, n))[0] pd = utils.expand_tensor(self.e.potential(xd, n-1)) pd = (pd[1:] - pd[0])/d for k, (i, j) in enumerate(zip(p.ravel(), pd.ravel())): nptest.assert_allclose(i, j, rtol=d, atol=d/100, err_msg="n=%i, k=%i" % (n,k))
def test_expand_tensor(self): a = np.array([1, 2, 3.])[None, :] nptest.assert_equal(utils.expand_tensor(a), a) b = np.array([1, 2, 3, 4, 5])[None, :] b1 = np.array([1, 2, 3, 2, 4, 5, 3, 5, -5] # triu ).reshape((1, 3, 3)) nptest.assert_equal(utils.expand_tensor(b), b1) c = np.random.random(5) ti, tj = np.triu_indices(3) ce = utils.expand_tensor(c[None, :])[0, ti, tj] nptest.assert_equal(ce[:5], c) nptest.assert_equal(ce[5], -c[0]-c[3])
def test_expand_tensor_trace(self): d = np.random.random(5)[None, :] de = utils.expand_tensor(d) nptest.assert_almost_equal(de[0].trace(), 0) d = np.random.random(7)[None, :] de = utils.expand_tensor(d) nptest.assert_almost_equal(de[0].trace(), np.zeros((3))) d = np.random.random(9)[None, :] de = utils.expand_tensor(d) nptest.assert_almost_equal(de[0].trace(), np.zeros((3,3))) d = np.random.random(11)[None, :] de = utils.expand_tensor(d) nptest.assert_almost_equal(de[0].trace(), np.zeros((3,3,3)))
def test_derivs(self): ns = range(1, 6) for ee, d in (self.e, 1e-6), (self.p, 1e-6): xd = self.x + [[0, 0, 0], [d, 0, 0], [0, d, 0], [0, 0, d]] for n in ns: p = utils.expand_tensor(ee.potential(self.x, n))[0] pd = utils.expand_tensor(ee.potential(xd, n - 1)) pd = (pd[1:] - pd[0]) / d nptest.assert_allclose(p, pd, rtol=d, atol=1e-14, err_msg="ee=%s, n=%i" % (ee, n))
def test_derivs(self): ns = range(1, 6) d = 1e-7 xd = self.x + [[0, 0, 0], [d, 0, 0], [0, d, 0], [0, 0, d]] for n in ns: p = utils.expand_tensor(self.e.potential(self.x, n))[0] pd = utils.expand_tensor(self.e.potential(xd, n - 1)) pd = (pd[1:] - pd[0]) / d for k, (i, j) in enumerate(zip(p.ravel(), pd.ravel())): nptest.assert_allclose(i, j, rtol=d, atol=d / 100, err_msg="n=%i, k=%i" % (n, k))
def test_expand_select_tensor(self): for n in 3, 5, 7, 9, 11: d = np.random.random(n)[None, :] de = utils.expand_tensor(d) ds = utils.select_tensor(de) nptest.assert_equal(d, ds)