def test_piecewise_simplification(): d2d3_11 = (c * d * TP(x, sp.Piecewise( (x, sp.Eq(k, 0)), (0, True))) + c * d * TP(sp.Piecewise( (x, sp.Eq(k, 0)), (0, True)), x) + d * TP(1, sp.Piecewise( (x, sp.Eq(k, 0)), (0, True))) + d * TP(sp.Piecewise((x, sp.Eq(k, 0)), (0, True)), 1)) assert texpand(d2d3_11) == 0 expr = sp.Sum( sp.Piecewise( (c * TP(xy**k, y * xy**(k - q - 1) * y) + TP(y * xy**(k - 1), y * xy**(k - q - 1) * y), sp.Eq(q, 0)), (0, True)), (q, 0, k - 1)) assert texpand(expr) == 0
def composition(d2, d1): """ Computes composition of d1 and d2, i.e. λx -> d2(d1(x)). """ product_rows = len(d2) product_cols = len(d1[0]) product = [[None] * product_cols for _ in range(product_rows)] for i in range(product_rows): for j in range(product_cols): row = d2[i] col = [d1[k][j] for k in range(len(d1))] product[i][j] = texpand(dot(row, col)) return product
def test_x_cubed_reduces_to_xy_k(): left = TP(x, y) * X right = X + TP(xy**k, xy**k) assert texpand(left * right) == texpand(texpand(left) * texpand(right))
def test_misc(): assert texpand(2 * x) == 0 assert texpand(y * x * y * x) == yx**2 assert texpand(d * xy**(k + 1)) == 0 assert texpand(x * y * xy**(k - 1)) == xy**k assert texpand(y * xy**(k - 1) * yx**(k - 1)) == 0
def test_sign_removal(): X = TP(x, 1) - TP(1, x) assert texpand(X) == TP(x, 1) + TP(1, x)
def test_zero_propagation(): assert texpand(TP(x, 0)) == 0 assert texpand(TP(0, y)) == 0 assert texpand(TP(xy**k, y) * TP(x, 1)) == 0 assert texpand(TP(x * c**2 * d * xy**k, y)) == 0
def ast_matrix(expr): expr_expanded = texpand(expr) col1 = basis col2 = [texpand(ast(expr_expanded, r)) for r in basis] return sp.Matrix([col1, col2]).T
def ast_table(expr): expr_expanded = texpand(expr) for r in basis: product = ast(expr_expanded, r) product = texpand(product) print('({}) ∗ {}\n = {}'.format(expr, r, product))