def testMarginalizeBothAxis(self): a = ExprSymbolic([1], np.array([[1, 0, 0]])) b = ExprSymbolic([1], np.array([[0, 1, 0]])) c = ExprSymbolic([2], np.array([[0, 0, 1]])) expressions = np.array([[a, b, c], [a, b, c]], dtype=np.object) matrix = ExprMatrix('A', expressions, powers=np.array([1])) matrix = matrix.Marginalize(axis=None) self.assertEquals((1, 1), matrix.expressions.shape) self.assertEquals(3, len(matrix.expressions.item(0, 0).quants)) self.assertEquals([2, 2, 4], sorted(matrix.expressions.item(0, 0).quants))
def testSameHash(self): manage.config.MAXPOWER = np.array([10]) a = ExprSymbolic([1], np.array([[1, 0, 0, 0]])) b = ExprSymbolic([1], np.array([[0, 1, 0, 0]])) c = ExprSymbolic([1], np.array([[0, 0, 1, 0]])) d = ExprSymbolic([1], np.array([[0, 0, 0, 1]])) expressions = np.array([[a, b], [c, d]], dtype=np.object) matrix = ExprMatrix('A', expressions, powers=np.array([1])) Q = matrix.Marginalize(axis=0).Marginalize(axis=1) W = matrix.Marginalize(axis=1).Marginalize(axis=0) self.assertEquals(Q, W)
def testShapesAfterSumAreCorrec(self): a = ExprSymbolic([1], np.array([[1, 0, 0]])) b = ExprSymbolic([1], np.array([[0, 1, 0]])) expressions = np.array([[a, a, a], [b, b, b]]) matrix = ExprMatrix('M', expressions, powers=np.array([1])) # Exp matrix test self.assertEquals((2, 3), matrix.expressions.shape) m0 = copy.copy(matrix) m0 = m0.Marginalize(axis=0) self.assertEquals((1, 3), m0.expressions.shape) self.assertEquals(a + b, m0.expressions[0, 0]) self.assertEquals(a + b, m0.expressions[0, 1]) m1 = copy.copy(matrix) m1 = m1.Marginalize(axis=1) self.assertEquals((2, 1), m1.expressions.shape) self.assertEquals(a + a + a, m1.expressions[0, 0]) self.assertEquals(b + b + b, m1.expressions[1, 0])
def testAddOne(self): manage.config.MAXPOWER = np.array([10]) a = ExprSymbolic([5], np.array([[0, 0, 0, 0]])) expressions = np.array([[a]], dtype=np.object) matrix = ExprMatrix('A', expressions, powers=np.array([1])) self.assertTrue(matrix.Add(4).expressions[0, 0].quants.shape[0] == 1)
def testFindLinearCombinationWithMatrix(self): manage.config.MAXPOWER = np.array([10]) # mat1 mat2 target # x1 | x1x2 x1 | x1x2 3x1 | 3x1x2 # 2x3 | x2^2 x1 | x3 4x3+x1 | 2x2^2 + x3 mat1_11 = ExprSymbolic([1], np.array([[1, 0, 0]])) mat1_12 = ExprSymbolic([1], np.array([[1, 1, 0]])) mat1_21 = ExprSymbolic([2], np.array([[0, 0, 1]])) mat1_22 = ExprSymbolic([1], np.array([[0, 2, 0]])) mat2_11 = ExprSymbolic([1], np.array([[1, 0, 0]])) mat2_12 = ExprSymbolic([1], np.array([[1, 1, 0]])) mat2_21 = ExprSymbolic([1], np.array([[1, 0, 0]])) mat2_22 = ExprSymbolic([1], np.array([[0, 0, 1]])) target_11 = ExprSymbolic([3], np.array([[1, 0, 0]])) target_12 = ExprSymbolic([3], np.array([[1, 1, 0]])) target_21 = ExprSymbolic([4, 1], np.array([[0, 0, 1], [1, 0, 0]])) target_22 = ExprSymbolic([2, 1], np.array([[0, 2, 0], [0, 0, 1]])) mat1 = ExprMatrix("a", np.array([[mat1_11, mat1_12], [mat1_21, mat1_22]]), np.array([1])) mat2 = ExprMatrix("b", np.array([[mat2_11, mat2_12], [mat2_21, mat2_22]]), np.array([1])) target = ExprMatrix( "target", np.array([[target_11, target_12], [target_21, target_22]]), np.array([1])) expressions_list = [mat1, mat2] weights = ExprSymbolic.FindLinearCombination(expressions_list, target)[0] self.assertEquals(2, len(weights)) self.assertAlmostEqual(2.0, weights[0]) self.assertAlmostEqual(1.0, weights[1])
def testDiffHash(self): a = ExprSymbolic([F(2, 7), F(3, 11)], np.array([[1, 0, 0], [0, 1, 0]])) b = ExprSymbolic([F(2, 13), F(3, 19)], np.array([[1, 0, 0], [0, 1, 0]])) self.assertNotEquals(a, b)
def testHashIsComputed(self): a = ExprSymbolic([2, 3], np.array([[1, 2, 3], [4, 5, 6]])) b = ExprSymbolic([2, 2], np.array([[1, 2, 3], [4, 5, 6]])) self.assertNotEquals(a, b)