def test_indexing(self): big = ch.Ch(np.arange(60).reshape((10, 6))) little = big[1:3, 3:6] self.assertTrue( np.max(np.abs(little.r - np.array([[9, 10, 11], [15, 16, 17]]))) == 0) little = big[5] self.assertTrue(np.max(np.abs(little.r - np.arange(30, 36))) == 0) self.assertTrue( np.max( np.abs( sp.coo_matrix(little.dr_wrt(big)).col - np.arange(30, 36))) == 0) little = big[2, 3] self.assertTrue(little.r[0] == 15.0) little = big[2, 3:5] self.assertTrue(np.max(np.abs(little.r - np.array([15, 16]))) == 0.) _ = little.dr_wrt(big) # Tests assignment through reorderings aa = ch.arange(4 * 4 * 4).reshape((4, 4, 4))[:3, :3, :3] aa[0, 1, 2] = 100 self.assertTrue(aa[0, 1, 2].r[0] == 100) # Tests assignment through reorderings (NaN's are a special case) aa = ch.arange(9).reshape((3, 3)) aa[1, 1] = np.nan self.assertTrue(np.isnan(aa.r[1, 1])) self.assertFalse(np.isnan(aa.r[0, 0]))
def test_tensordot(self): an = np.arange(60.).reshape(3, 4, 5) bn = np.arange(24.).reshape(4, 3, 2) cn = np.tensordot(an, bn, axes=([1, 0], [0, 1])) ac = ch.arange(60.).reshape(3, 4, 5) bc = ch.arange(24.).reshape(4, 3, 2) cc = ch.tensordot(ac, bc, axes=([1, 0], [0, 1])) cc.r cc.dr_wrt(ac) cc.dr_wrt(bc)
def test_dr_wrt_selection(self): aa = ch.arange(10, 20) bb = ch.arange(1, 11) cc = aa * bb + aa + bb + 2 dr0 = cc.dr_wrt(aa[4:6]) dr1 = cc.dr_wrt(aa)[:, 4:6] self.assertTrue((dr0 - dr1).nnz == 0) dr0 = cc.dr_wrt(bb[5:8]) dr1 = cc.dr_wrt(bb)[:, 5:8] self.assertTrue((dr0 - dr1).nnz == 0)
def test_nested_concatenate(self): aa = ch.arange(3) bb = ch.arange(4) cc = ch.arange(5) result = ch.concatenate((ch.concatenate((aa, bb)), cc)) self.assertTrue(result.m0 is aa) self.assertTrue(result.m1 is bb) self.assertTrue(result.m2 is cc) self.assertTrue(result.dr_wrt(aa).nnz > 0) self.assertTrue(result.dr_wrt(bb).nnz > 0) self.assertTrue(result.dr_wrt(cc).nnz > 0)