def test_eq(self): a0 = numpy.arange(6).reshape(2,3) a1 = ctf.array(a0) a2 = ctf.array(a0) self.assertTrue(ctf.all(a1==a2)) self.assertTrue(ctf.all(a0==a1)) a1[:] = 0 self.assertTrue(ctf.all(a1==0))
def test_ravel(self): a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(a1.ravel()==a0.ravel())) #self.assertTrue(ctf.all(a1.transpose(0,1,3,2).ravel()==a0.transpose(0,1,3,2).ravel())) self.assertTrue(ctf.all(a1.transpose(0,1,3,2).ravel()==a0.transpose(0,1,3,2).ravel())) self.assertTrue(ctf.all(a1.transpose(0,2,1,3).ravel()==a0.transpose(0,2,1,3).ravel())) self.assertTrue(ctf.all(a1.transpose(3,2,0,1).ravel()==a0.transpose(3,2,0,1).ravel())) self.assertTrue(ctf.all(a1.transpose(2,1,0,3).ravel()==a0.transpose(2,1,0,3).ravel()))
def test_diagonal(self): a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.astensor([0,4,8]))) self.assertTrue(ctf.all(a1 == ctf.diagonal(numpy.arange(9).reshape(3,3)))) try: a1.diagonal() except ValueError: # a1 needs to be 2d array pass
def test_abs(self): a0 = numpy.arange(2., 5.) a1 = ctf.from_nparray(a0) self.assertTrue(ctf.all(ctf.abs(a1) == ctf.abs(a0))) self.assertTrue(ctf.all(ctf.abs(a1) == numpy.abs(a0))) try: a1 = a1 + 1j self.assertAlmostEqual(ctf.abs(a1).sum(), numpy.abs(a1.to_nparray()).sum(), 14) except AttributeError: pass
def test_hstack(self): a1 = ctf.astensor(numpy.ones(4)) a2 = ctf.astensor(numpy.ones(5)) self.assertTrue(ctf.hstack((a1, a2)).shape == (9,)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((2,5))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) self.assertTrue(ctf.hstack((a1, a2)).dtype == numpy.complex128) a1 = numpy.ones((2,4)) a2 = ctf.astensor(numpy.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) na2 = numpy.ones((2,5))+0j self.assertTrue(ctf.all(ctf.hstack((a1, a2)) == numpy.hstack((a1,na2)))) a1 = ctf.astensor(numpy.ones(4)) self.assertTrue(ctf.hstack((a1, 1.5)).shape == (5,)) a1 = ctf.astensor(numpy.ones((2,4,2))) a2 = ctf.astensor(numpy.ones((2,5,2))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9,2))
def test_conj(self): a0 = ctf.zeros((2,3)) self.assertTrue(ctf.conj(a0).dtype == numpy.double) self.assertTrue(a0.conj().dtype == numpy.double) a0 = ctf.zeros((2,3), dtype=numpy.complex) self.assertTrue(ctf.conj(a0).dtype == numpy.complex128) self.assertTrue(a0.conj().dtype == numpy.complex128) a0[:] = 1j a0 = a0.conj() self.assertTrue(ctf.all(a0 == -1j))
def test_transpose(self): a1 = ctf.zeros((2,3)) self.assertTrue(a1.transpose().shape == (3,2)) a1 = ctf.zeros((2,3,4,5)) self.assertTrue(a1.transpose().shape == (5,4,3,2)) a1 = ctf.zeros((2,3)) self.assertTrue(a1.T().shape == (3,2)) a1 = ctf.zeros((2,3,4,5)) self.assertTrue(a1.T().shape == (5,4,3,2)) a1 = ctf.zeros((2,3,4,5)) self.assertTrue(a1.transpose((0,2,1,-1)).shape == (2,4,3,5)) self.assertTrue(ctf.transpose(a1, (0,2,1,-1)).shape == (2,4,3,5)) self.assertTrue(a1.transpose(0,-1,2,1).shape == (2,5,4,3)) self.assertTrue(a1.transpose(0,-2,1,-1).shape == (2,4,3,5)) self.assertTrue(a1.transpose(-3,-2,0,-1).shape == (3,4,2,5)) self.assertTrue(a1.transpose(-3,0,-1,2).shape == (3,2,5,4)) self.assertTrue(a1.transpose(-3,-2,-1,-4).shape == (3,4,5,2)) # The case which does not change the data ordering in memory. # It does not need create new tensor. a2 = a1.transpose(0,1,2,3) a2[:] = 1 self.assertTrue(ctf.all(a2 == 1)) a0 = ctf.zeros((1,1,3)) a2 = a0.transpose(1,0,2) a0[:] = 1 self.assertTrue(ctf.all(a0 == 1)) a1 = ctf.zeros((2,3,4,5)) with self.assertRaises(ValueError): a1.transpose((1,2)) with self.assertRaises(ValueError): a1.transpose((0,2,1,2)) with self.assertRaises(ValueError): a1.transpose((0,4,1,2))
def test_astensor(self): # astensor converts python object to ctf tensor a0 = ctf.astensor((1,2,3)) a0 = ctf.astensor([1,2.,3]) a0 = ctf.astensor([(1,2), (3,4)]) a0 = ctf.astensor(numpy.arange(3)) a0 = ctf.astensor([numpy.array((1,2)), numpy.array((3,4))+1j]) a1 = ctf.astensor(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) a0 = numpy.asarray(a1) # self.assertTrue(ctf.asarray(a0).__class__ == ctf.astensor(a0).__class__) a0 = ctf.astensor([1,2.,3], dtype='D') self.assertTrue(a0.dtype == numpy.complex128) with self.assertRaises(TypeError): ctf.astensor([1j,2j], dtype='d') a0 = numpy.arange(4.).reshape(2,2) a1 = ctf.to_nparray(ctf.from_nparray(a0)) self.assertTrue(ctf.all(a0==a1)) try: a1 = ctf.from_nparray(a1).to_nparray() self.assertTrue(ctf.all(a0==a1)) except AttributeError: pass a0 = ctf.from_nparray(numpy.arange(3)) a1 = ctf.from_nparray(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) a0 = numpy.arange(6).reshape(2,3) a1 = ctf.array(a0) self.assertTrue(ctf.all(a0==a1)) self.assertTrue(ctf.all(a1==a0)) a1 = ctf.array(a0, copy=False) self.assertTrue(ctf.all(a1==0))
def test_transpose_astensor(self): a0 = numpy.arange(6).reshape(2,3) a1 = ctf.astensor(a0.T) #self.assertTrue(ctf.all(a1==a0)) a1 = ctf.astensor(a1.T()) self.assertTrue(ctf.all(a1==a0)) a0 = numpy.arange(120).reshape(2,3,4,5) a1 = a0.transpose(0,1,3,2) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(0,2,1,3) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(3,2,0,1) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(2,1,0,3) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(a1.transpose(0,1,3,2)==a0.transpose(0,1,3,2))) self.assertTrue(ctf.all(a1.transpose(0,2,1,3)==a0.transpose(0,2,1,3))) self.assertTrue(ctf.all(a1.transpose(3,2,0,1)==a0.transpose(3,2,0,1))) self.assertTrue(ctf.all(a1.transpose(2,1,0,3)==a0.transpose(2,1,0,3)))
def test_transpose_reshape(self): a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) a0 = a0.transpose(3,0,2,1) a1 = a1.transpose(3,0,2,1) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2)))
def test_diag(self): a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.diag(a0))) self.assertTrue(ctf.all(ctf.diag(a1) == numpy.diag(numpy.arange(9).reshape(3,3).diagonal())))
def test_eye(self): a0 = ctf.identity(4) a1 = ctf.eye(4) self.assertTrue(ctf.all(a0==a1)) a1 = ctf.eye(4, dtype=numpy.complex128) self.assertTrue(a1.dtype == numpy.complex128)
def test_reshape(self): a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2))) with self.assertRaises(ValueError): a1.reshape((1,2))
def test_sp_reshape(self): a1 = ctf.tensor((2,3),sp=True) a1.fill_sp_random(0.,1.,.5) a0 = a1.to_nparray() self.assertTrue(ctf.all(ctf.reshape(a1,(2,3)) ==a0.reshape(2,3))) self.assertTrue(ctf.all(ctf.reshape(a1,(2,1,3)) ==a0.reshape(2,1,3))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,2,3)) ==a0.reshape(1,2,3))) a1 = ctf.tensor((3,1,2),sp=True) a1.fill_sp_random(0.,1.,.5) a0 = a1.to_nparray() self.assertTrue(ctf.all(ctf.reshape(a1,(2,3)) ==a0.reshape(2,3))) a1 = ctf.tensor((2,3,4,5),sp=True) a1.fill_sp_random(0.,1.,.5) a0 = a1.to_nparray() self.assertTrue(ctf.all(ctf.reshape(a1,(2,3,4,5)) ==a0.reshape(2,3,4,5))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2))) with self.assertRaises(ValueError): a1.reshape((1,2))
def test_reshape(self): a = ctf.random.random((1,10,10,10)) self.assertTrue(ctf.all(a.reshape((10,100)) == a.to_nparray().reshape((10,100)))) base_shapes = [(2,3,4,5),(3,10,4),(1,3,10,4),(2,3,4,1,5)] for shape in base_shapes: a0 = numpy.arange(120).reshape(shape) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(ctf.reshape(a1,(2,3,4,5)) ==a0.reshape(2,3,4,5))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,3,10,4))==a0.reshape(1,3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,3,1,10,4))==a0.reshape(1,3,1,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,3,1,1,10,4))==a0.reshape(1,3,1,1,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4,1))==a0.reshape(3,10,4,1))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2))) with self.assertRaises(ValueError): a1.reshape((1,2))