def test_inv_raises(): num_charges = 1 np.random.seed(10) R = 3 D = 10 charge = BaseCharge( np.random.randint(-5, 6, (num_charges, D), dtype=np.int16), charge_types=[U1Charge] * num_charges) A = BlockSparseTensor.random([Index(charge, False) for n in range(R)], (-0.5, 0.5)) with pytest.raises(ValueError): inv(A)
def test_inv(dtype, num_charges): np.random.seed(10) R = 2 D = 10 charge = BaseCharge(np.random.randint(-5, 6, (num_charges, D), dtype=np.int16), charge_types=[U1Charge] * num_charges) flows = [True, False] A = BlockSparseTensor.random([Index(charge, flows[n]) for n in range(R)], (-0.5, 0.5), dtype=dtype) invA = inv(A) left_eye = invA @ A blocks, _, shapes = _find_diagonal_sparse_blocks(left_eye.flat_charges, left_eye.flat_flows, 1) for n, block in enumerate(blocks): t = np.reshape(left_eye.data[block], shapes[:, n]) assert np.linalg.norm(t - np.eye(t.shape[0], t.shape[1])) < 1E-12 right_eye = A @ invA blocks, _, shapes = _find_diagonal_sparse_blocks(right_eye.flat_charges, right_eye.flat_flows, 1) for n, block in enumerate(blocks): t = np.reshape(right_eye.data[block], shapes[:, n]) assert np.linalg.norm(t - np.eye(t.shape[0], t.shape[1])) < 1E-12
def test_eig_prod(dtype, Ds, num_charges): np.random.seed(10) R = len(Ds) charges = [ BaseCharge( np.random.randint(-5, 6, (num_charges, Ds[n]), dtype=np.int16), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [False] * R inds = [Index(charges[n], flows[n]) for n in range(R)] A = BlockSparseTensor.random( inds + [i.copy().flip_flow() for i in inds], dtype=dtype) dims = np.prod(Ds) A = A.reshape([dims, dims]) E, V = eig(A) A_ = V @ diag(E) @ inv(V) np.testing.assert_allclose(A.data, A_.data)