def test_reshape_raises(): Ds = [8, 9, 10, 11] indices = [Index(U1Charge.random(-5, 5, Ds[n]), False) for n in range(4)] arr = ChargeArray.random(indices) with pytest.raises(ValueError): arr.reshape([64, 65]) arr2 = arr.reshape([72, 110]) with pytest.raises(ValueError): arr2.reshape([9, 8, 10, 11])
def test_ChargeArray_generic(dtype): Ds = [8, 9, 10, 11] indices = [Index(U1Charge.random(-5, 5, Ds[n]), False) for n in range(4)] arr = ChargeArray.random(indices, dtype=dtype) assert arr.ndim == 4 assert arr.dtype == dtype np.testing.assert_allclose(arr.shape, Ds) np.testing.assert_allclose(arr.flat_flows, [False, False, False, False]) for n in range(4): assert charge_equal(indices[n]._charges[0], arr.flat_charges[n]) assert arr.sparse_shape[n] == indices[n]
def test_transpose(): Ds = np.array([8, 9, 10, 11]) flows = [True, False, True, False] indices = [ Index(U1Charge.random(-5, 5, Ds[n]), flows[n]) for n in range(4) ] arr = ChargeArray.random(indices) order = [2, 1, 0, 3] arr2 = arr.transpose(order) np.testing.assert_allclose(Ds[order], arr2.shape) np.testing.assert_allclose(arr2._order, [[2], [1], [0], [3]]) np.testing.assert_allclose(arr2.flows, [[True], [False], [True], [False]])
def test_ChargeArray_init(): np.random.seed(10) D = 10 rank = 4 charges = [U1Charge.random(-5, 5, D) for _ in range(rank)] data = np.random.uniform(0, 1, size=D**rank) flows = np.random.choice([True, False], size=rank, replace=True) order = [[n] for n in range(rank)] arr = ChargeArray(data, charges, flows, order=order) np.testing.assert_allclose(data, arr.data) for c1, c2 in zip(charges, arr.charges): assert charge_equal(c1, c2[0]) for c1, c2 in zip(charges, arr._charges): assert charge_equal(c1, c2)
def test_transpose_data(): Ds = np.array([8, 9, 10, 11]) order = [2, 0, 1, 3] flows = [True, False, True, False] indices = [ Index(U1Charge.random(-5, 5, Ds[n]), flows[n]) for n in range(4) ] arr = ChargeArray.random(indices) data = np.ascontiguousarray(np.transpose(np.reshape(arr.data, Ds), order)) arr2 = arr.transpose(order).transpose_data() data3 = np.reshape(arr2.data, Ds[order]) np.testing.assert_allclose(data, data3) np.testing.assert_allclose(arr2.shape, Ds[order]) np.testing.assert_allclose(arr2._order, [[0], [1], [2], [3]]) np.testing.assert_allclose(arr2.flows, [[True], [True], [False], [False]])
def test_ChargeArray_reshape(dtype): Ds = [8, 9, 10, 11] indices = [Index(U1Charge.random(-5, 5, Ds[n]), False) for n in range(4)] arr = ChargeArray.random(indices, dtype=dtype) arr2 = arr.reshape([72, 110]) for n in range(2): for m in range(2): assert charge_equal(arr2.charges[n][m], indices[n * 2 + m].charges) np.testing.assert_allclose(arr2.shape, [72, 110]) np.testing.assert_allclose(arr2._order, [[0, 1], [2, 3]]) np.testing.assert_allclose(arr2.flows, [[False, False], [False, False]]) assert arr2.ndim == 2 arr3 = arr.reshape(Ds) for n in range(4): assert charge_equal(arr3.charges[n][0], indices[n].charges) np.testing.assert_allclose(arr3.shape, Ds) np.testing.assert_allclose(arr3._order, [[0], [1], [2], [3]]) np.testing.assert_allclose(arr3.flows, [[False], [False], [False], [False]]) assert arr3.ndim == 4
def test_transpose_reshape(): Ds = np.array([8, 9, 10, 11]) flows = [True, False, True, False] indices = [ Index(U1Charge.random(-5, 5, Ds[n]), flows[n]) for n in range(4) ] arr = ChargeArray.random(indices) arr2 = arr.transpose([2, 0, 1, 3]) arr3 = arr2.reshape([80, 99]) np.testing.assert_allclose(arr3.shape, [80, 99]) np.testing.assert_allclose(arr3._order, [[2, 0], [1, 3]]) np.testing.assert_allclose(arr3.flows, [[True, True], [False, False]]) arr4 = arr3.transpose([1, 0]) np.testing.assert_allclose(arr4.shape, [99, 80]) np.testing.assert_allclose(arr4._order, [[1, 3], [2, 0]]) np.testing.assert_allclose(arr4.flows, [[False, False], [True, True]]) arr5 = arr4.reshape([9, 11, 10, 8]) np.testing.assert_allclose(arr5.shape, [9, 11, 10, 8]) np.testing.assert_allclose(arr5._order, [[1], [3], [2], [0]]) np.testing.assert_allclose(arr5.flows, [[False], [False], [True], [True]])
def test_transpose_reshape_transpose_data(): Ds = np.array([8, 9, 10, 11]) flows = [True, False, True, False] indices = [ Index(U1Charge.random(-5, 5, Ds[n]), flows[n]) for n in range(4) ] arr = ChargeArray.random(indices) nparr = np.reshape(arr.data, Ds) arr2 = arr.transpose([2, 0, 1, 3]) nparr2 = nparr.transpose([2, 0, 1, 3]) arr3 = arr2.reshape([80, 99]) nparr3 = nparr2.reshape([80, 99]) arr4 = arr3.transpose([1, 0]) nparr4 = nparr3.transpose([1, 0]) arr5 = arr4.reshape([9, 11, 10, 8]) nparr5 = nparr4.reshape([9, 11, 10, 8]) np.testing.assert_allclose(arr3.transpose_data().data, np.ascontiguousarray(nparr3).flat) np.testing.assert_allclose(arr4.transpose_data().data, np.ascontiguousarray(nparr4).flat) np.testing.assert_allclose(arr5.transpose_data().data, np.ascontiguousarray(nparr5).flat)
def test_ChargeArray_todense(dtype): Ds = [8, 9, 10, 11] indices = [Index(U1Charge.random(-5, 5, Ds[n]), False) for n in range(4)] arr = ChargeArray.random(indices, dtype=dtype) np.testing.assert_allclose(arr.todense(), np.reshape(arr.data, Ds))