def test_piecewise_within_sum():
    expr_1 = 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))
    expr_2 = c * TP(xy**k,
                    y * xy**(k - 1) * y) + TP(y * xy**(k - 1),
                                              y * xy**(k - 1) * y)
    assert Transformer_sum_of_piecewise_with_only_zero.match_transform(
        expr_1) == expr_2
Beispiel #2
0
def expand_tp(expr):
    l, r = expr.args
    l = expand_with_transformers_impl(l)
    r = expand_with_transformers_impl(r)
    if l == 0 or r == 0:
        return S.Zero

    if isinstance(l, sp.Add):
        expr = sp.Add(*[TP(arg, r) for arg in l.args])
        return expand_with_transformers_impl(expr)
    if isinstance(r, sp.Add):
        expr = sp.Add(*[TP(l, arg) for arg in r.args])
        return expand_with_transformers_impl(expr)

    lc, lnc = split_commutative(l) if isinstance(l, sp.Mul) else ([S.One], [l])
    rc, rnc = split_commutative(r) if isinstance(r, sp.Mul) else ([S.One], [r])

    lnc = sp.Mul(*lnc)
    rnc = sp.Mul(*rnc)

    commutative = sp.Mul(*(lc + rc))
    tp = TP(lnc, rnc)

    return commutative * tp
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
import sympy as sp
from my_algebra import x, y, xy, yx, k, c, d
from tensor_product import TP

q = sp.symbols('q')

Y = TP(y, 1) + TP(1, y)
X = TP(x, 1) + TP(1, x)

phi1 = sp.Sum(TP(yx**q, xy**(k - 1 - q)), (q, 0, k - 1))
phi2 = X + sp.Sum(TP(y * xy**q, y * xy**(k - 2 - q)), (q, 0, k - 2))

rho = TP(x * yx**(k - 1), 1) + TP(1, x * yx**(k - 1))
psi = TP(yx**(k - 1), xy**(k - 1))
tau = Y * X
sigma = X * phi1

lbda = TP(x, 1) * phi1
mu = sp.Sum(TP((x * y)**q, y * (x * y)**(k - 1 - q)), (q, 0, k - 1))
omega = TP((1 + c * x) * yx**(k - 1), xy**k)

A = c**2 * d * omega
B = d * TP(1 + c * x, y) * X
C = (+c**2 * TP(y * xy**(k - 1), x * yx**(k - 1)) +
     c**3 * TP(x * yx**(k - 1), xy**k))
D = (+c * Y * TP(x, x) + c * TP(x**2, y) + d * TP(x * yx**(k - 1), x))

E = (+d * TP(x * yx**(k - 1), 1) + c**2 * d * omega)
F = d * TP(1, y)
G = c**2 * omega
H = c * TP(y * xy**(k - 1), xy**(k - 1))
def test_sums():
    d0d1_00_1 = (d * sp.Sum(TP(xy**q, xy**(k - q)), (q, 0, k - 1)) +
                 d * sp.Sum(TP(xy**(q + 1), xy**(k - q - 1)), (q, 0, k - 1)) +
                 d * TP(1, xy**k) + d * TP(xy**k, 1))
    d0d1_00_2 = (d * sp.Sum(TP(xy**q, xy**(k - q)), (q, 0, k - 1)) +
                 d * sp.Sum(TP(xy**q, xy**(k - q)),
                            (q, 1, k)) + d * TP(1, xy**k) + d * TP(xy**k, 1))
    d0d1_00_3 = (d * sp.Sum(2 * TP(xy**q, xy**(k - q)), (q, 1, k - 1)) +
                 2 * d * TP(1, xy**k) + 2 * d * TP(xy**k, 1))
    assert Transformer_overlapping_sums_i_plus_1.match_transform(
        d0d1_00_1) == d0d1_00_2
    assert Transformer_overlapping_sums_by_one.match_transform(
        d0d1_00_2) == d0d1_00_3
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_sum_simplification():
    assert sp.Sum(TP(yx * xy**i, yx**(k - i)), (i, 0, k - 1)) == TP(yx, xy**k)
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
Beispiel #10
0
 def transform(v1: TP, v2: TP):
     l = v1.args[0] * v2.args[0]
     r = v2.args[1] * v1.args[1]
     return [TP(l, r)]
Beispiel #11
0
 def transform(v: sp.Pow):
     tp = v.args[0]
     l = tp.args[0] * tp.args[0]
     r = tp.args[1] * tp.args[1]
     return TP(l, r)