def test_svd_raises(): np.random.seed(10) dtype = np.float64 Ds = [10, 11, 12] R = len(Ds) charges = [ BaseCharge(np.random.randint(-5, 6, (Ds[n], 1)), charge_types=[U1Charge]) for n in range(R) ] flows = [True] * R A = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) with pytest.raises(NotImplementedError): svd(A, full_matrices=False, compute_uv=False)
def test_svd_singvals(dtype, Ds, R1, num_charges): np.random.seed(10) R = len(Ds) charges = [ BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R A = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) d1 = np.prod(Ds[:R1]) d2 = np.prod(Ds[R1:]) A = A.reshape([d1, d2]) _, S1, _ = svd(A, full_matrices=False) S2 = svd(A, full_matrices=False, compute_uv=False) np.testing.assert_allclose(S1.data, S2.data) Sdense = np.linalg.svd(A.todense(), compute_uv=False) np.testing.assert_allclose(np.sort(Sdense[Sdense > 1E-15]), np.sort(S2.data[S2.data > 0.0]))
def test_svd_prod(dtype, Ds, R1, num_charges): np.random.seed(10) R = len(Ds) charges = [ BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R A = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) d1 = np.prod(Ds[:R1]) d2 = np.prod(Ds[R1:]) A = A.reshape([d1, d2]) U, S, V = svd(A, full_matrices=False) A_ = U @ diag(S) @ V assert A_.dtype == A.dtype np.testing.assert_allclose(A.data, A_.data) for n in range(len(A._charges)): assert charge_equal(A_._charges[n], A._charges[n])
ham_init.reshape([chib] * 6)) # initialize tensors u = [0] * n_levels w = [0] * n_levels ham = [0] * (n_levels + 1) rho = [0] * (n_levels + 1) # --- Martin: is there a way of directly defining blocksparse `eye` matrices? eye_mat = np.eye(chib**2, chib**2).reshape(chib, chib, chib, chib) u[0] = BT.fromdense([ind_chib1, ind_chib1, ind_chib0, ind_chib0], eye_mat) # ----------------- # --- Martin: is there a way of directly defining blocksparse isometries? w_temp = BT.randn([ind_chib1, ind_chib1, ind_chi0], dtype=np.float64) ut, st, vt = BLA.svd(w_temp.reshape([chib**2, chi]), full_matrices=False) w[0] = (ut @ vt).reshape([chib, chib, chi]) # ----------------- ham_temp = (ham_init - bias * np.eye(chib**3)).reshape([chib] * 6) ham[0] = BT.fromdense([ind_chib1] * 3 + [ind_chib0] * 3, ham_temp) rho[0] = BT.randn([ind_chib0] * 3 + [ind_chib1] * 3, dtype=np.float64) for z in range(1, n_levels): eye_mat = np.eye(chim**2, chi**2).reshape(chi, chi, chim, chim) u[z] = BT.fromdense([ind_chi1, ind_chi1, ind_chim0, ind_chim0], eye_mat) w_temp = BT.randn([ind_chim1, ind_chim1, ind_chi0], dtype=np.float64) ut, st, vt = BLA.svd(w_temp.reshape([chim**2, chi]), full_matrices=False) w[z] = (ut @ vt).reshape([chim, chim, chi])
def orthog_sym(arr, pivot=1): arr_size = arr.shape arr = arr.reshape([np.prod(arr_size[:pivot]), np.prod(arr_size[pivot:])]) utemp, _, vtemph = BLA.svd(arr, full_matrices=False) return (utemp @ vtemph).reshape(arr_size)