Пример #1
0
    def Compute(self):
        n = 1
        m = max(self.power, 3) + 7
        manage.config.N, manage.config.M = n, m
        impl = manage.config.EXPR_IMPL
        manage.config.MAXPOWER = np.array([self.power])
        self.SetStartSymbols(1)
        target = None
        for h_iter in itertools.combinations(xrange(m), self.power):
            small_target = None
            expr_vector = np.zeros((1, m))
            for h in h_iter:
                expr_vector[0, h] = 1
            expr = impl([1], expr_vector)
            if target is None:
                target = expr
            else:
                target = target + expr
        target = np.array([[target]])
        matlab = '''n = 1;
m = 18;
A = randn(1, m);
sub = nchoosek(1:m, %s);
original = 0;
for i = 1:size(sub, 1)
  original = original + prod(A(sub(i, :)));
end
''' % self.power
        target = ExprMatrix(comp={THIS: ""},
                            expressions=target,
                            powers=manage.config.MAXPOWER)
        target = target.ElementwiseMultiply(120)
        target.comp[MATLAB] = matlab
        self.target_mat = target
Пример #2
0
    def testElementwiseMultiply(self):
        manage.config.MAXPOWER = np.array([10, 10])
        # a = x1 * x3
        # b = x1 * x2^2
        # c = x2^3 + x3^4
        a = ExprSymbolic([1], np.array([[1, 0, 1]]))
        b = ExprSymbolic([1], np.array([[1, 2, 0]]))
        c = ExprSymbolic([1, 1], np.array([[0, 3, 0], [0, 0, 4]]))
        expressions1 = np.array([[a, b], [c, a]])
        expressions2 = np.array([[a, a], [a, a]])
        # mat1:
        #  x1x3  |  x1 * x2^2
        #  x2^3 + x3^4 | x1x3
        mat1 = ExprMatrix('M1', expressions1, powers=np.array([1, 0]))
        mat2 = ExprMatrix('M2', expressions2, powers=np.array([0, 1]))

        mat1 = mat1.ElementwiseMultiply(mat2)
        self.assertEquals((2, 2), mat1.expressions.shape)

        elem = mat1.expressions[0, 0]
        self.assertEquals(1, elem.expr_vectors.shape[0])
        self.assertEquals(1, elem.quants[0])
        self.assertTrue((np.array([2, 0, 2]) == elem.expr_vectors).all())

        elem2 = mat1.expressions[1, 0]  # x1x3 * (x2^3 + x3^4)
        self.assertEquals(2, elem2.expr_vectors.shape[0])
        self.assertTrue((np.array([1, 3, 1]) == elem2.expr_vectors).any())
        self.assertTrue((np.array([1, 0, 5]) == elem2.expr_vectors).any())
        self.assertTrue((np.array([1, 1]) == elem2.quants).any())
  def testElementwiseMultiply(self):
    manage.config.MAXPOWER = np.array([10, 10])
    # a = x1 * x3
    # b = x1 * x2^2
    # c = x2^3 + x3^4
    a = ExprZp([1], np.array([[1, 0, 1]]))
    b = ExprZp([1], np.array([[1, 2, 0]]))
    c = ExprZp([1, 1], np.array([[0, 3, 0], [0, 0, 4]]))
    expressions1 = np.array([[a, b], [c, a]])
    expressions2 = np.array([[a, a], [a, a]])
    # mat1:
    #  x1x3  |  x1 * x2^2
    #  x2^3 + x3^4 | x1x3
    mat1 = ExprMatrix('M1', expressions1, powers=np.array([1, 0]))
    mat2 = ExprMatrix('M2', expressions2, powers=np.array([0, 1]))

    mat1.ElementwiseMultiply(mat2)
    self.assertEquals((2, 2), mat1.expressions.shape)