def test_tensordot________________01(): larray = np.random.rand(5,5) rarray = np.random.rand(5,5) tarray = np.tensordot(larray, rarray, axes=([1],[0])) a1 = ax.PartitionedAxis((5,5)) L = tn.TiledTensor((a1,a1)) R = tn.TiledTensor((a1,a1)) L[0:5,0:5] = L[5:10,5:10] = larray R[0:5,0:5] = R[5:10,5:10] = rarray T = tn.tensordot(L, R, axis_keys=([1],[0])) assert (T[0: 5,0: 5] == tarray).all() assert (T[5:10,5:10] == tarray).all()
def water_mp2_script(scfwfn, mints): nirrep = scfwfn.nirrep() nsopi = tuple(scfwfn.nsopi()[h] for h in range(nirrep)) nso = scfwfn.nso() import axis as ax c1_axis = ax.PartitionedAxis(nso) c2v_axis = ax.PartitionedAxis(nsopi) import tensor as tn U = tn.TiledTensor((c2v_axis, c1_axis)) symmetrizer = mints.petite_list().sotoao() for h in range(nirrep): offset = c2v_axis.get_partition_start(h) for i in range(symmetrizer.rows(h)): for j in range(symmetrizer.cols(h)): U[i+offset, j] = symmetrizer.get(h, i, j) C = tn.TiledTensor((c2v_axis, c2v_axis)) mo_coeffs = scfwfn.Ca() for h in range(nirrep): offset = c2v_axis.get_partition_start(h) for i in range(mo_coeffs.rows(h)): for j in range(mo_coeffs.cols(h)): C[i+offset, j+offset] = mo_coeffs.get(h, i, j) import numpy as np c2v_g = tn.TiledTensor((c2v_axis, c2v_axis, c2v_axis, c2v_axis)) c1_g = tn.TiledTensor(( c1_axis, c1_axis, c1_axis, c1_axis)) c1_g[0:7,0:7,0:7,0:7] = np.array(mints.ao_eri()) g1 = tn.tensordot(U, c1_g, axis_keys=([1],[3])) g2 = tn.tensordot(U, g1, axis_keys=([1],[3])) g1 = tn.tensordot(U, g2, axis_keys=([1],[3])) c2v_g = tn.tensordot(U, g1, axis_keys=([1],[3])) import numpy.linalg as la sh = SymmetryHelper(BINARY_IRREPS["C2v"], [0, 1, 2, 3])
def test_tensordot________________02(): a2 = ax.PartitionedAxis(2) a3 = ax.PartitionedAxis((1,2)) a4 = ax.PartitionedAxis((2,1,1)) a5 = ax.PartitionedAxis((3,2)) a6 = ax.PartitionedAxis((3,0,1,2)) larray = np.random.rand(2,3,4,5,6) rarray = np.random.rand(6,4,5,3,3) tarray = np.tensordot(larray, rarray, axes=([1,2,3,4],[3,1,2,0])) L = tn.TiledTensor((a2,a3,a4,a5,a6)) R = tn.TiledTensor((a6,a4,a5,a3,a3)) for coord in it.product(*(range(dim) for dim in larray.shape)): L[coord] = larray[coord] for coord in it.product(*(range(dim) for dim in rarray.shape)): R[coord] = rarray[coord] T = tn.tensordot(L, R, axis_keys=([1,2,3,4],[3,1,2,0])) for coord in it.product(*(range(dim) for dim in tarray.shape)): assert abs(T[coord] - tarray[coord]) < 1e-13
a5 = ax.PartitionedAxis((3,2)) a6 = ax.PartitionedAxis((3,0,1,2)) larray = np.random.rand(2,3,4,5,6) rarray = np.random.rand(6,4,5,3,3) tarray = np.tensordot(larray, rarray, axes=([1,2,3,4],[3,1,2,0])) L = tn.TiledTensor((a2,a3,a4,a5,a6)) R = tn.TiledTensor((a6,a4,a5,a3,a3)) for coord in it.product(*(range(dim) for dim in larray.shape)): L[coord] = larray[coord] for coord in it.product(*(range(dim) for dim in rarray.shape)): R[coord] = rarray[coord] T = tn.tensordot(L, R, axis_keys=([1,2,3,4],[3,1,2,0])) for coord in it.product(*(range(dim) for dim in tarray.shape)): assert abs(T[coord] - tarray[coord]) < 1e-13 if __name__ == "__main__": larray = np.random.rand(5,5) rarray = np.random.rand(5,5) tarray = np.tensordot(larray, rarray, axes=([1],[0])) print tarray a1 = ax.PartitionedAxis((5,5)) L = tn.TiledTensor((a1,a1)) R = tn.TiledTensor((a1,a1)) L[0:5,0:5] = L[5:10,5:10] = larray R[0:5,0:5] = R[5:10,5:10] = rarray T = tn.tensordot(L, R, axis_keys=([1],[0])) print T