def test_storage(self): self.tensor[5, 5] = 1 self.tensor[2, 7] = 2 assertTensorEqual( self.tensor, [[None] * 8, [None] * 8, [None] * 7 + [2], [None] * 8, [None] * 8, [None] * 5 + [1, None, None]])
def test_container(self): self.tensor[0, 0] = 1 self.tensor[2, 3] = 2 self.tensor[0, -1] = 3 assertTensorEqual(self.tensor, [[1, 0, 0, 3], [0, 0, 0, 0], [0, 0, 0, 2]])
def test_delete(self): self.tensor.update( nones_removed(nested_list_to_dict(self.slice_testcase))) assertTensorEqual(self.tensor, self.slice_testcase) del self.tensor[0, 0] assertTensorEqual(self.tensor, [[None, None, None], [None, 2, 3], [4, None, None], [None, 5, None]])
def test_orthonormality(self): identity = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] assertTensorEqual(self.u.T * self.u, identity) assertTensorEqual(self.v.T * self.v, identity)
def test_delete(self): self.tensor.update(nones_removed(nested_list_to_dict(self.slice_testcase))) assertTensorEqual(self.tensor, self.slice_testcase) del self.tensor[0,0] assertTensorEqual(self.tensor, [[None, None, None], [None, 2, 3 ], [4, None, None], [None, 5, None]])
def test_transpose(self): self.tensor[0, 0] = 1 self.tensor[1, 2] = 3 self.tensor[2, 0] = 4 self.tensor[3, 1] = 5 t = self.tensor.transpose() assertTensorEqual( t, [[1, None, 4, None], [None, None, None, 5], [None, 3, None, None]])
def testAdd(self): t1 = DictTensor(2) t2 = DictTensor(2) t1[0, 0] = 1 t1[1, 1] = 1 t1[1, 0] = 2 t2[2, 1] = 4 t2[1, 0] = 5 t3 = t1 + t2 assertTensorEqual(t3, [[1, None], [7, 1], [None, 4]])
def test_transpose(self): self.tensor[0, 0] = 1 self.tensor[1, 2] = 3 self.tensor[2, 0] = 4 self.tensor[3, 1] = 5 t = self.tensor.transpose() assertTensorEqual(t, [[1, None, 4, None], [None, None, None, 5], [None, 3, None, None]])
def test_storage(self): self.tensor[5, 5] = 1 self.tensor[2, 7] = 2 assertTensorEqual(self.tensor, [[None]*8, [None]*8, [None]*7 + [2], [None]*8, [None]*8, [None]*5 + [1, None, None]])
def test_combine_by_element(self): t1 = DictTensor(2) t2 = DictTensor(2) t1[1, 1] = 1 t1[1, 0] = 2 t2[1, 1] = 4 t2[0, 1] = 5 t3 = t1.combine_by_element(t2, lambda x, y: x + (2*y)) assertTensorEqual(t3, [[None, 10], [2, 9]]) # Make sure errors are raised when the tensors don't have the # same shape or number of dimensions t4 = DictTensor(2) t4[0, 2] = 3 t4[1, 0] = 5 self.assertRaises(IndexError, lambda: t1.combine_by_element(t4, lambda x, y: x + y)) t4 = DictTensor(3) self.assertRaises(IndexError, lambda: t1.combine_by_element(t4, lambda x, y: x + y))
def test_combine_by_element(self): t1 = DictTensor(2) t2 = DictTensor(2) t1[1, 1] = 1 t1[1, 0] = 2 t2[1, 1] = 4 t2[0, 1] = 5 t3 = t1.combine_by_element(t2, lambda x, y: x + (2 * y)) assertTensorEqual(t3, [[None, 10], [2, 9]]) # Make sure errors are raised when the tensors don't have the # same shape or number of dimensions t4 = DictTensor(2) t4[0, 2] = 3 t4[1, 0] = 5 self.assertRaises( IndexError, lambda: t1.combine_by_element(t4, lambda x, y: x + y)) t4 = DictTensor(3) self.assertRaises( IndexError, lambda: t1.combine_by_element(t4, lambda x, y: x + y))
def test_reconstructed(self): assertTensorEqual(self.svd.reconstructed, [[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 4, 0, 0, 0]]) assertTensorEqual(self.svd.reconstructed[1,:], [0, 0, 3, 0, 0]) assertTensorEqual(self.svd.reconstructed[:,2], [0, 3, 0, 0])
def test_decomposition(self): self.assertEqual(self.u.shape[0], self.tensor.shape[0]) self.assertEqual(len(self.svals), self.u.shape[1]) self.assertEqual(len(self.svals), self.v.shape[1]) self.assertEqual(self.v.shape[0], self.tensor.shape[1]) assertTensorEqual(self.u, [[0, 0, 1], [0, -1, 0], [0, 0, 0], [-1, 0, 0]], abs=True) assertTensorEqual(self.v, [[0, 0, sqrt(.2)], [-1, 0, 0], [0, -1, 0], [0, 0, 0], [0, 0, sqrt(.8)]], abs=True) assertTensorEqual(self.svals, [4, 3, sqrt(5)])
def test_incremental(self): self.assertEqual(self.incremental.u.shape[0], self.tensor.shape[0]) self.assertEqual(len(self.incremental.svals), self.incremental.u.shape[1]) self.assertEqual(len(self.incremental.svals), self.incremental.v.shape[1]) self.assertEqual(self.incremental.v.shape[0], self.tensor.shape[1]) assertTensorEqual(self.incremental.u, [[0, 0, 1], [0, 1, 0], [0, 0, 0], [1, 0, 0]]) assertTensorEqual(self.incremental.v, [[0, 0, sqrt(.2)], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 0, sqrt(.8)]]) assertTensorEqual(self.incremental.svals, [4, 3, sqrt(5)])
def test_slice(self): self.tensor.update( nones_removed(nested_list_to_dict(self.slice_testcase))) # Test end conditions: start index # is included in slice, end index is not slice = self.tensor[1:3, 0:2] assertTensorEqual(slice, [[None, 2], [4, None]]) # Test that slicing on some dims correctly # reduces the dimensionality of the tensor slice = self.tensor[3, :] assertTensorEqual(slice, [None, 5, None]) # Test the step parameter slice = self.tensor[1:4:2, :] assertTensorEqual(slice, [[None, 2, 3], [None, 5, None]])
def test_slice(self): self.tensor.update(nones_removed(nested_list_to_dict(self.slice_testcase))) # Test end conditions: start index # is included in slice, end index is not slice = self.tensor[1:3, 0:2] assertTensorEqual(slice, [[None, 2], [4, None]]) # Test that slicing on some dims correctly # reduces the dimensionality of the tensor slice = self.tensor[3, :] assertTensorEqual(slice, [None, 5, None]) # Test the step parameter slice = self.tensor[1:4:2, :] assertTensorEqual(slice, [[None, 2, 3], [None, 5, None]])
def test_result(): assertTensorEqual(tensor, normalize_expected_result)
def test_reconstructed(self): assertTensorEqual(self.svd.reconstructed, [[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 4, 0, 0, 0]]) assertTensorEqual(self.svd.reconstructed[1, :], [0, 0, 3, 0, 0]) assertTensorEqual(self.svd.reconstructed[:, 2], [0, 3, 0, 0])
def test_1D(self): tensor_1D = DictTensor(1) tensor_1D[2] = 1 assertTensorEqual(tensor_1D, [None, None, 1])
def testICdiv(self): t1 = tensor_from_nested_list([[2, 4], [6, 8]]) t1 /= 2 assertTensorEqual(t1, [[1, 2], [3, 4]])
def test_orthonormality(self): assertTensorEqual(self.u.T * self.u, numpy.eye(self.u.shape[1])) assertTensorEqual(self.v.T * self.v, numpy.eye(self.u.shape[1]))