def test_multitensor_init(): """ Testing the generation of a multitensor object with random tensors """ a = np.random.random((5, 5)) b = np.random.random((4, 4)) c = np.random.random((3, 3)) at = Tensor(a, name='a') bt = Tensor(b, name='b') ct = Tensor(c, name='c') mt = MultiTensor([at, bt, ct]) assert len(mt.dual_basis) == 0 vec = np.vstack((at.vectorize(), bt.vectorize())) vec = np.vstack((vec, ct.vectorize())) assert np.allclose(vec, mt.vectorize_tensors())
def test_tensor_init(): """ Initialize a tensor and confirm that we have all values None Initialize a tensor and confirm that all values are equal to the correct values and iteration over the tensors are the correct values :return: """ test_tensor = Tensor() assert test_tensor.dim is None assert test_tensor.ndim is None assert test_tensor.data is None assert test_tensor.size is None assert test_tensor.basis is None a = np.arange(16).reshape((4, 4)) test_tensor = Tensor(a) assert np.allclose(test_tensor.data, a) assert test_tensor.size == 16 assert isinstance(test_tensor.basis, Bijection) a_triu = a[np.triu_indices_from(a)] a_tril = a[np.tril_indices_from(a)] counter = 0 for val, idx in test_tensor.utri_iterator(): assert val == a[tuple(idx)] assert val == a_triu[counter] counter += 1 assert counter == 4 * (4 + 1) / 2 counter = 0 for val, idx in test_tensor.ltri_iterator(): assert val == a[tuple(idx)] assert val == a_tril[counter] counter += 1 assert counter == 4 * (4 + 1) / 2 assert np.allclose(test_tensor.vectorize(), a.reshape((-1, 1), order='C'))