def test_prune_constants(self): """Test pruning constants from constraints. """ x = Variable(2) A = np.matrix("1 2; 3 4") constraints = (A * x <= 2).canonical_form[1] pruned = prune_constants(constraints) prod = mul(pruned[0].expr, {}) self.assertItemsAlmostEqual(prod, np.zeros(A.shape[0])) # Test no-op constraints = (0 * x <= 2).canonical_form[1] pruned = prune_constants(constraints) prod = mul(pruned[0].expr, {x.id: 1}) self.assertItemsAlmostEqual(prod, np.zeros(A.shape[0]))
def constr_mul(constraints, var_dict, vec_size, is_abs): """Multiplies a vector by the matrix implied by the constraints. Parameters ---------- constraints : list A list of linear constraints. var_dict : dict A dictionary mapping variable id to value. vec_size : int The length of the product vector. is_abs : bool Multiply by the absolute value of the matrix? """ product = np.zeros(vec_size) offset = 0 for constr in constraints: result = mul(constr.expr, var_dict, is_abs) rows, cols = constr.size for col in range(cols): # Handle scalars separately. if np.isscalar(result): product[offset:offset + rows] = result else: product[offset:offset + rows] = np.squeeze(result[:, col]) offset += rows return product
def get_constraint_tensors(constraints): """Get expression for Ax + b.""" A_exprs = [constr.expr for constr in tree_mat.prune_constants(constraints)] b = vstack( [vec(tf.constant(-tree_mat.mul(constr.expr, {}), dtype=tf.float32)) for constr in constraints]) return A_exprs, b
def constr_mul(constraints, var_dict, vec_size): """Multiplies a vector by the matrix implied by the constraints. Parameters ---------- constraints : list A list of linear constraints. var_dict : dict A dictionary mapping variable id to value. vec_size : int The length of the product vector. """ product = np.zeros(vec_size) offset = 0 for constr in constraints: result = mul(constr.expr, var_dict) rows, cols = constr.size for col in range(cols): # Handle scalars separately. if np.isscalar(result): product[offset:offset+rows] = result else: product[offset:offset+rows] = np.squeeze(result[:, col]) offset += rows return product
def constr_mul(constraints, var_dict, vec_size, p=None): """Multiplies a vector by the matrix implied by the constraints. Parameters ---------- constraints : list A list of linear constraints. var_dict : dict A dictionary mapping variable id to value. vec_size : int The length of the product vector. p : int The p-norm being approximated. """ product = np.zeros(vec_size) offset = 0 for constr in constraints: result = mul(constr.expr, var_dict, p) rows, cols = constr.size # Handle scalars separately. if np.isscalar(result): product[offset:offset + rows * cols] = result else: flattened = np.reshape(result, rows * cols, order='F') product[offset:offset + rows * cols] = flattened offset += rows * cols return product
def constr_mul(constraints, var_dict, vec_size, p=None): """Multiplies a vector by the matrix implied by the constraints. Parameters ---------- constraints : list A list of linear constraints. var_dict : dict A dictionary mapping variable id to value. vec_size : int The length of the product vector. p : int The p-norm being approximated. """ product = np.zeros(vec_size) offset = 0 for constr in constraints: result = mul(constr.expr, var_dict, p) rows, cols = constr.size # Handle scalars separately. if np.isscalar(result): product[offset:offset+rows*cols] = result else: flattened = np.reshape(result, rows*cols, order='F') product[offset:offset+rows*cols] = flattened offset += rows*cols return product
def get_constraint_tensors(constraints): """Get expression for Ax + b.""" A_exprs = [constr.expr for constr in tree_mat.prune_constants(constraints)] b = vstack([ vec(tf.constant(-tree_mat.mul(constr.expr, {}), dtype=tf.float32)) for constr in constraints ]) return A_exprs, b
def test_pmul(self): """Test the p mul method. """ n = 2 ones = np.mat(np.ones((n, n))) # Multiplication x = Variable(n, n) A = np.matrix("-1 2; -3 4") expr = (A * x).canonical_form[0] val_dict = {x.id: ones} for p in [1, 2, 3]: result = mul(expr, val_dict, p) self.assertItemsEqual( np.array(result)[:, 0], numpy.linalg.norm(A, p, axis=1)) result_dict = tmul(expr, ones, p) assert (np.array(result_dict[x.id])[:, 0] == numpy.linalg.norm( A, p, axis=0)).all() # Multiplication with promotion. t = Variable() A = np.matrix("1 -2; -3 -4") expr = (A * t).canonical_form[0] ones = np.mat(np.ones((n, n))) val_dict = {t.id: 2} for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, np.abs(A) * 2) result_dict = tmul(expr, result, p) output = pnorm_mat_mul(A.T, result, p) final = np.linalg.norm(np.diag(output), p) assert np.allclose(result_dict[t.id], final) # Addition y = Variable(n, n) expr = (y + x).canonical_form[0] val_dict = {x.id: 2 * np.ones((n, n)), y.id: np.ones((n, n))} for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, np.power((2**p + 1) * ones, 1.0 / p)) result_dict = tmul(expr, result, p) assert (result_dict[y.id] == result).all() assert (result_dict[x.id] == result).all() abs_A = np.abs(A) val_dict = {x.id: abs_A, y.id: abs_A} # Indexing expr = (x[:, 0] + y[:, 1]).canonical_form[0] for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose( result, np.power( np.power(abs_A[:, 0], p) + np.power(abs_A[:, 1], p), 1.0 / p)) result_dict = tmul(expr, result, p) mat = ones mat[:, 0] = result mat[:, 1] = 0 assert (result_dict[x.id] == mat).all() # Negation val_dict = {x.id: abs_A} expr = (-x).canonical_form[0] for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, abs_A) result_dict = tmul(expr, result, p) assert np.allclose(result_dict[x.id], abs_A) # Transpose expr = x.T.canonical_form[0] for p in [1, 2, 3]: val_dict = {x.id: abs_A} result = mul(expr, val_dict, p) assert np.allclose(result, abs_A.T) result_dict = tmul(expr, result, p) assert np.allclose(result_dict[x.id], abs_A) # Convolution x = Variable(3) f = np.matrix(np.array([1, 2, 3])).T g = np.array([0, 1, 0.5]) expr = conv(f, x).canonical_form[0] val_dict = {x.id: g} for p in [1, 2, 3]: result = mul(expr, val_dict, p) f_conv_g = np.power( conv(np.power(f, p), np.power(g, p)).value, 1.0 / p) self.assertItemsAlmostEqual(result, f_conv_g) value = np.array(range(5)) result_dict = tmul(expr, value, p) toep = LA.toeplitz(np.array([1, 0, 0]), np.array([1, 2, 3, 0, 0])) x_val = pnorm_mat_mul(toep, np.matrix(value).T, p) self.assertItemsAlmostEqual(result_dict[x.id], x_val) # Elementwise multiplication. n = 2 x = Variable(n, n) A = np.matrix("1 -2; -3 4").A expr = (mul_elemwise(A, x)).canonical_form[0] val_dict = {x.id: np.ones((n, n))} for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert (result == np.abs(A)).all() result_dict = tmul(expr, result, p) self.assertItemsAlmostEqual(result_dict[x.id], A * A) for p in [1, 2, 3]: # Reshape. x = Variable(3, 2) expr = (reshape(x, 1, 6)).canonical_form[0] orig = np.array([[1, 2, 3], [4, 5, 6]]) val_dict = {x.id: orig} result = mul(expr, val_dict, p) assert (result == np.matrix([[1, 4, 2, 5, 3, 6]])).all() result_dict = tmul(expr, result, p) assert np.allclose(np.matrix(result_dict[x.id]), np.matrix([[1., 4., 2.], [5., 3., 6.]]).T) for p in [1, 2, 3]: # Vstack. x = Variable(2, 3) y = Variable(1, 3) expr = (vstack(x, y)).canonical_form[0] orig_x = np.array([[1, 2, 3], [4, 5, 6]]) orig_y = np.array([[7, 8, 9]]) val_dict = {x.id: orig_x, y.id: orig_y} result = mul(expr, val_dict, p) assert (result == np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])).all() result_dict = tmul(expr, result, p) assert np.allclose(np.matrix(result_dict[x.id]), np.array([[1., 2., 3.], [4., 5., 6.]])) assert np.allclose(np.matrix(result_dict[y.id]), np.array([[7., 8., 9.]]))
def test_mul(self): """Test the mul method. """ n = 2 ones = np.mat(np.ones((n, n))) # Multiplication x = Variable(n, n) A = np.matrix("1 2; 3 4") expr = (A * x).canonical_form[0] val_dict = {x.id: ones} result = mul(expr, val_dict) assert (result == A * ones).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A.T * A * ones).all() # Multiplication with promotion. t = Variable() A = np.matrix("1 2; 3 4") expr = (A * t).canonical_form[0] val_dict = {t.id: 2} result = mul(expr, val_dict) assert (result == A * 2).all() result_dict = tmul(expr, result) total = 0 for i in range(A.shape[0]): for j in range(A.shape[1]): total += A[i, j] * result[i, j] assert (result_dict[t.id] == total) # Addition y = Variable(n, n) expr = (y + A * x).canonical_form[0] val_dict = {x.id: np.ones((n, n)), y.id: np.ones((n, n))} result = mul(expr, val_dict) assert (result == A * ones + ones).all() result_dict = tmul(expr, result) assert (result_dict[y.id] == result).all() assert (result_dict[x.id] == A.T * result).all() val_dict = {x.id: A, y.id: A} # Indexing expr = (x[:, 0] + y[:, 1]).canonical_form[0] result = mul(expr, val_dict) assert (result == A[:, 0] + A[:, 1]).all() result_dict = tmul(expr, result) mat = ones mat[:, 0] = result mat[:, 1] = 0 assert (result_dict[x.id] == mat).all() # Negation val_dict = {x.id: A} expr = (-x).canonical_form[0] result = mul(expr, val_dict) assert (result == -A).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Transpose expr = x.T.canonical_form[0] val_dict = {x.id: A} result = mul(expr, val_dict) assert (result == A.T).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Convolution x = Variable(3) f = np.matrix(np.array([1, 2, 3])).T g = np.array([0, 1, 0.5]) f_conv_g = np.array([0., 1., 2.5, 4., 1.5]) expr = conv(f, x).canonical_form[0] val_dict = {x.id: g} result = mul(expr, val_dict) self.assertItemsAlmostEqual(result, f_conv_g) value = np.array(range(5)) result_dict = tmul(expr, value) toep = LA.toeplitz(np.array([1, 0, 0]), np.array([1, 2, 3, 0, 0])) x_val = toep.dot(value) self.assertItemsAlmostEqual(result_dict[x.id], x_val) # Elementwise multiplication. x = Variable(n, n) A = np.matrix("1 -2; -3 4").A expr = (mul_elemwise(A, x)).canonical_form[0] val_dict = {x.id: np.ones((n, n))} result = mul(expr, val_dict) assert (result == A).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A * A).all() # Reshape. x = Variable(3, 2) expr = (reshape(x, 1, 6)).canonical_form[0] orig = np.array([[1, 2, 3], [4, 5, 6]]) val_dict = {x.id: orig} result = mul(expr, val_dict) assert (result == np.matrix([[1, 4, 2, 5, 3, 6]])).all() result_dict = tmul(expr, result) assert (np.matrix(result_dict[x.id]) == np.matrix([[1, 4, 2], [5, 3, 6]]).T).all() # Vstack. x = Variable(2, 3) y = Variable(1, 3) expr = (vstack(x, y)).canonical_form[0] orig_x = np.array([[1, 2, 3], [4, 5, 6]]) orig_y = np.array([[7, 8, 9]]) val_dict = {x.id: orig_x, y.id: orig_y} result = mul(expr, val_dict) assert (result == np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])).all() result_dict = tmul(expr, result) print np.matrix(result_dict[x.id]) assert (np.matrix(result_dict[x.id]) == np.array([[1, 2, 3], [4, 5, 6]])).all() assert (np.matrix(result_dict[y.id]) == np.array([[7, 8, 9]])).all()
def test_mul(self): """Test the mul method. """ n = 2 ones = np.mat(np.ones((n, n))) # Multiplication x = Variable(n, n) A = np.matrix("1 2; 3 4") expr = (A * x).canonical_form[0] val_dict = {x.id: ones} result = mul(expr, val_dict) assert (result == A * ones).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A.T * A * ones).all() # Multiplication with promotion. t = Variable() A = np.matrix("1 2; 3 4") expr = (A * t).canonical_form[0] val_dict = {t.id: 2} result = mul(expr, val_dict) assert (result == A * 2).all() result_dict = tmul(expr, result) total = 0 for i in range(A.shape[0]): for j in range(A.shape[1]): total += A[i, j] * result[i, j] assert (result_dict[t.id] == total) # Addition y = Variable(n, n) expr = (y + A * x).canonical_form[0] val_dict = {x.id: np.ones((n, n)), y.id: np.ones((n, n))} result = mul(expr, val_dict) assert (result == A * ones + ones).all() result_dict = tmul(expr, result) assert (result_dict[y.id] == result).all() assert (result_dict[x.id] == A.T * result).all() val_dict = {x.id: A, y.id: A} # Indexing expr = (x[:, 0] + y[:, 1]).canonical_form[0] result = mul(expr, val_dict) assert (result == A[:, 0] + A[:, 1]).all() result_dict = tmul(expr, result) mat = ones mat[:, 0] = result mat[:, 1] = 0 assert (result_dict[x.id] == mat).all() # Negation val_dict = {x.id: A} expr = (-x).canonical_form[0] result = mul(expr, val_dict) assert (result == -A).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Transpose expr = x.T.canonical_form[0] val_dict = {x.id: A} result = mul(expr, val_dict) assert (result == A.T).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Convolution x = Variable(3) f = np.matrix(np.array([1, 2, 3])).T g = np.array([0, 1, 0.5]) f_conv_g = np.array([0., 1., 2.5, 4., 1.5]) expr = conv(f, x).canonical_form[0] val_dict = {x.id: g} result = mul(expr, val_dict) self.assertItemsAlmostEqual(result, f_conv_g) value = np.array(range(5)) result_dict = tmul(expr, value) toep = LA.toeplitz(np.array([1, 0, 0]), np.array([1, 2, 3, 0, 0])) x_val = toep.dot(value) self.assertItemsAlmostEqual(result_dict[x.id], x_val)
def test_mul(self): """Test the mul method. """ n = 2 ones = np.mat(np.ones((n, n))) # Multiplication x = Variable(n, n) A = np.matrix("1 2; 3 4") expr = (A * x).canonical_form[0] val_dict = {x.id: ones} result = mul(expr, val_dict) assert (result == A * ones).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A.T * A * ones).all() # Multiplication with promotion. t = Variable() A = np.matrix("1 2; 3 4") expr = (A * t).canonical_form[0] val_dict = {t.id: 2} result = mul(expr, val_dict) assert (result == A * 2).all() result_dict = tmul(expr, result) total = 0 for i in range(A.shape[0]): for j in range(A.shape[1]): total += A[i, j] * result[i, j] assert result_dict[t.id] == total # Addition y = Variable(n, n) expr = (y + A * x).canonical_form[0] val_dict = {x.id: np.ones((n, n)), y.id: np.ones((n, n))} result = mul(expr, val_dict) assert (result == A * ones + ones).all() result_dict = tmul(expr, result) assert (result_dict[y.id] == result).all() assert (result_dict[x.id] == A.T * result).all() val_dict = {x.id: A, y.id: A} # Indexing expr = (x[:, 0] + y[:, 1]).canonical_form[0] result = mul(expr, val_dict) assert (result == A[:, 0] + A[:, 1]).all() result_dict = tmul(expr, result) mat = ones mat[:, 0] = result mat[:, 1] = 0 assert (result_dict[x.id] == mat).all() # Negation val_dict = {x.id: A} expr = (-x).canonical_form[0] result = mul(expr, val_dict) assert (result == -A).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Transpose expr = x.T.canonical_form[0] val_dict = {x.id: A} result = mul(expr, val_dict) assert (result == A.T).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Convolution x = Variable(3) f = np.matrix(np.array([1, 2, 3])).T g = np.array([0, 1, 0.5]) f_conv_g = np.array([0.0, 1.0, 2.5, 4.0, 1.5]) expr = conv(f, x).canonical_form[0] val_dict = {x.id: g} result = mul(expr, val_dict) self.assertItemsAlmostEqual(result, f_conv_g) value = np.array(range(5)) result_dict = tmul(expr, value) toep = LA.toeplitz(np.array([1, 0, 0]), np.array([1, 2, 3, 0, 0])) x_val = toep.dot(value) self.assertItemsAlmostEqual(result_dict[x.id], x_val)
def test_pmul(self): """Test the p mul method. """ n = 2 ones = np.mat(np.ones((n, n))) # Multiplication x = Variable(n, n) A = np.matrix("-1 2; -3 4") expr = (A*x).canonical_form[0] val_dict = {x.id: ones} for p in [1, 2, 3]: result = mul(expr, val_dict, p) self.assertItemsEqual(np.array(result)[:,0], numpy.linalg.norm(A, p, axis=1)) result_dict = tmul(expr, ones, p) assert (np.array(result_dict[x.id])[:,0] == numpy.linalg.norm(A, p, axis=0)).all() # Multiplication with promotion. t = Variable() A = np.matrix("1 -2; -3 -4") expr = (A*t).canonical_form[0] ones = np.mat(np.ones((n, n))) val_dict = {t.id: 2} for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, np.abs(A)*2) result_dict = tmul(expr, result, p) output = pnorm_mat_mul(A.T, result, p) final = np.linalg.norm(np.diag(output), p) assert np.allclose(result_dict[t.id], final) # Addition y = Variable(n, n) expr = (y + x).canonical_form[0] val_dict = {x.id: 2*np.ones((n, n)), y.id: np.ones((n, n))} for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, np.power((2**p + 1)*ones, 1.0/p)) result_dict = tmul(expr, result, p) assert (result_dict[y.id] == result).all() assert (result_dict[x.id] == result).all() abs_A = np.abs(A) val_dict = {x.id: abs_A, y.id: abs_A} # Indexing expr = (x[:, 0] + y[:, 1]).canonical_form[0] for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, np.power(np.power(abs_A[:, 0], p) + np.power(abs_A[:, 1], p), 1.0/p) ) result_dict = tmul(expr, result, p) mat = ones mat[:, 0] = result mat[:, 1] = 0 assert (result_dict[x.id] == mat).all() # Negation val_dict = {x.id: abs_A} expr = (-x).canonical_form[0] for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert np.allclose(result, abs_A) result_dict = tmul(expr, result, p) assert np.allclose(result_dict[x.id], abs_A) # Transpose expr = x.T.canonical_form[0] for p in [1, 2, 3]: val_dict = {x.id: abs_A} result = mul(expr, val_dict, p) assert np.allclose(result, abs_A.T) result_dict = tmul(expr, result, p) assert np.allclose(result_dict[x.id], abs_A) # Convolution x = Variable(3) f = np.matrix(np.array([1, 2, 3])).T g = np.array([0, 1, 0.5]) expr = conv(f, x).canonical_form[0] val_dict = {x.id: g} for p in [1, 2, 3]: result = mul(expr, val_dict, p) f_conv_g = np.power(conv(np.power(f, p), np.power(g, p)).value, 1.0/p) self.assertItemsAlmostEqual(result, f_conv_g) value = np.array(range(5)) result_dict = tmul(expr, value, p) toep = LA.toeplitz(np.array([1,0,0]), np.array([1, 2, 3, 0, 0])) x_val = pnorm_mat_mul(toep, np.matrix(value).T, p) self.assertItemsAlmostEqual(result_dict[x.id], x_val) # Elementwise multiplication. n = 2 x = Variable(n, n) A = np.matrix("1 -2; -3 4").A expr = (mul_elemwise(A, x)).canonical_form[0] val_dict = {x.id: np.ones((n, n))} for p in [1, 2, 3]: result = mul(expr, val_dict, p) assert (result == np.abs(A)).all() result_dict = tmul(expr, result, p) self.assertItemsAlmostEqual(result_dict[x.id], A*A) for p in [1, 2, 3]: # Reshape. x = Variable(3, 2) expr = (reshape(x, 1, 6)).canonical_form[0] orig = np.array([[1,2,3], [4, 5, 6]]) val_dict = {x.id: orig} result = mul(expr, val_dict) assert (result == np.matrix([[1, 4, 2, 5, 3, 6]])).all() result_dict = tmul(expr, result) assert (np.matrix(result_dict[x.id]) == np.matrix([[1,4,2], [5, 3, 6]]).T).all()
def test_mul(self): """Test the mul method. """ n = 2 ones = np.mat(np.ones((n, n))) # Multiplication x = Variable(n, n) A = np.matrix("1 2; 3 4") expr = (A*x).canonical_form[0] val_dict = {x.id: ones} result = mul(expr, val_dict) assert (result == A*ones).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A.T*A*ones).all() # Multiplication with promotion. t = Variable() A = np.matrix("1 2; 3 4") expr = (A*t).canonical_form[0] val_dict = {t.id: 2} result = mul(expr, val_dict) assert (result == A*2).all() result_dict = tmul(expr, result) total = 0 for i in range(A.shape[0]): for j in range(A.shape[1]): total += A[i, j]*result[i, j] assert (result_dict[t.id] == total) # Addition y = Variable(n, n) expr = (y + A*x).canonical_form[0] val_dict = {x.id: np.ones((n, n)), y.id: np.ones((n, n))} result = mul(expr, val_dict) assert (result == A*ones + ones).all() result_dict = tmul(expr, result) assert (result_dict[y.id] == result).all() assert (result_dict[x.id] == A.T*result).all() val_dict = {x.id: A, y.id: A} # Indexing expr = (x[:, 0] + y[:, 1]).canonical_form[0] result = mul(expr, val_dict) assert (result == A[:, 0] + A[:, 1]).all() result_dict = tmul(expr, result) mat = ones mat[:, 0] = result mat[:, 1] = 0 assert (result_dict[x.id] == mat).all() # Negation val_dict = {x.id: A} expr = (-x).canonical_form[0] result = mul(expr, val_dict) assert (result == -A).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Transpose expr = x.T.canonical_form[0] val_dict = {x.id: A} result = mul(expr, val_dict) assert (result == A.T).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A).all() # Convolution x = Variable(3) f = np.matrix(np.array([1, 2, 3])).T g = np.array([0, 1, 0.5]) f_conv_g = np.array([ 0., 1., 2.5, 4., 1.5]) expr = conv(f, x).canonical_form[0] val_dict = {x.id: g} result = mul(expr, val_dict) self.assertItemsAlmostEqual(result, f_conv_g) value = np.array(range(5)) result_dict = tmul(expr, value) toep = LA.toeplitz(np.array([1,0,0]), np.array([1, 2, 3, 0, 0])) x_val = toep.dot(value) self.assertItemsAlmostEqual(result_dict[x.id], x_val) # Elementwise multiplication. x = Variable(n, n) A = np.matrix("1 -2; -3 4").A expr = (mul_elemwise(A, x)).canonical_form[0] val_dict = {x.id: np.ones((n, n))} result = mul(expr, val_dict) assert (result == A).all() result_dict = tmul(expr, result) assert (result_dict[x.id] == A*A).all() # Reshape. x = Variable(3, 2) expr = (reshape(x, 1, 6)).canonical_form[0] orig = np.array([[1,2,3], [4, 5, 6]]) val_dict = {x.id: orig} result = mul(expr, val_dict) assert (result == np.matrix([[1, 4, 2, 5, 3, 6]])).all() result_dict = tmul(expr, result) assert (np.matrix(result_dict[x.id]) == np.matrix([[1,4,2], [5, 3, 6]]).T).all() # Vstack. x = Variable(2, 3) y = Variable(1, 3) expr = (vstack(x, y)).canonical_form[0] orig_x = np.array([[1,2,3], [4, 5, 6]]) orig_y = np.array([[7, 8, 9]]) val_dict = {x.id: orig_x, y.id: orig_y} result = mul(expr, val_dict) assert (result == np.array([[1,2,3], [4, 5, 6], [7, 8, 9]])).all() result_dict = tmul(expr, result) print np.matrix(result_dict[x.id]) assert (np.matrix(result_dict[x.id]) == np.array([[1,2,3], [4, 5, 6]])).all() assert (np.matrix(result_dict[y.id]) == np.array([[7, 8, 9]])).all()