示例#1
0
def test_expand_frac():
    assert expand((x + y)*y/x/(x + 1), frac=True) == \
        (x*y + y**2)/(x**2 + x)
    assert expand((x + y)*y/x/(x + 1), numer=True) == \
        (x*y + y**2)/(x*(x + 1))
    assert expand((x + y)*y/x/(x + 1), denom=True) == \
        y*(x + y)/(x**2 + x)
    eq = (x + 1)**2/y
    assert expand_numer(eq, multinomial=False) == eq
def test_issues_5919_6830():
    # issue 5919
    n = -1 + 1 / x
    z = n / x / (-n)**2 - 1 / n / x
    assert expand(
        z) == 1 / (x**2 - 2 * x + 1) - 1 / (x - 2 + 1 / x) - 1 / (-x + 1)

    # issue 6830
    p = (1 + x)**2
    assert expand_multinomial(
        (1 + x * p)**2) == (x**2 * (x**4 + 4 * x**3 + 6 * x**2 + 4 * x + 1) +
                            2 * x * (x**2 + 2 * x + 1) + 1)
    assert expand_multinomial(
        (1 + (y + x) * p)**2) == (2 * ((x + y) * (x**2 + 2 * x + 1)) +
                                  (x**2 + 2 * x * y + y**2) *
                                  (x**4 + 4 * x**3 + 6 * x**2 + 4 * x + 1) + 1)
    A = Symbol('A', commutative=False)
    p = (1 + A)**2
    assert expand_multinomial(
        (1 + x * p)**2) == (x**2 * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) +
                            2 * x * (1 + 2 * A + A**2) + 1)
    assert expand_multinomial(
        (1 + (y + x) * p)**2) == ((x + y) * (1 + 2 * A + A**2) * 2 +
                                  (x**2 + 2 * x * y + y**2) *
                                  (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) + 1)
    assert expand_multinomial((1 + (y + x) * p)**3) == (
        (x + y) * (1 + 2 * A + A**2) * 3 + (x**2 + 2 * x * y + y**2) *
        (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) * 3 +
        (x**3 + 3 * x**2 * y + 3 * x * y**2 + y**3) *
        (1 + 6 * A + 15 * A**2 + 20 * A**3 + 15 * A**4 + 6 * A**5 + A**6) + 1)
    # unevaluate powers
    eq = (Pow((x + 1) * ((A + 1)**2), 2, evaluate=False))
    # - in this case the base is not an Add so no further
    #   expansion is done
    assert expand_multinomial(eq) == \
        (x**2 + 2*x + 1)*(1 + 4*A + 6*A**2 + 4*A**3 + A**4)
    # - but here, the expanded base *is* an Add so it gets expanded
    eq = (Pow(((A + 1)**2), 2, evaluate=False))
    assert expand_multinomial(eq) == 1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4

    # coverage
    def ok(a, b, n):
        e = (a + I * b)**n
        return verify_numerically(e, expand_multinomial(e))

    for a in [2, S.Half]:
        for b in [3, S(1) / 3]:
            for n in range(2, 6):
                assert ok(a, b, n)

    assert expand_multinomial((x + 1 + O(z))**2) == \
        1 + 2*x + x**2 + O(z)
    assert expand_multinomial((x + 1 + O(z))**3) == \
        1 + 3*x + 3*x**2 + x**3 + O(z)

    assert expand_multinomial(3**(x + y + 3)) == 27 * 3**(x + y)
示例#3
0
def test_issues_5919_6830():
    # issue 5919
    n = -1 + 1 / x
    z = n / x / (-n) ** 2 - 1 / n / x
    assert expand(z) == 1 / (x ** 2 - 2 * x + 1) - 1 / (x - 2 + 1 / x) - 1 / (-x + 1)

    # issue 6830
    p = (1 + x) ** 2
    assert expand_multinomial((1 + x * p) ** 2) == (
        x ** 2 * (x ** 4 + 4 * x ** 3 + 6 * x ** 2 + 4 * x + 1) + 2 * x * (x ** 2 + 2 * x + 1) + 1
    )
    assert expand_multinomial((1 + (y + x) * p) ** 2) == (
        2 * ((x + y) * (x ** 2 + 2 * x + 1))
        + (x ** 2 + 2 * x * y + y ** 2) * (x ** 4 + 4 * x ** 3 + 6 * x ** 2 + 4 * x + 1)
        + 1
    )
    A = Symbol("A", commutative=False)
    p = (1 + A) ** 2
    assert expand_multinomial((1 + x * p) ** 2) == (
        x ** 2 * (1 + 4 * A + 6 * A ** 2 + 4 * A ** 3 + A ** 4) + 2 * x * (1 + 2 * A + A ** 2) + 1
    )
    assert expand_multinomial((1 + (y + x) * p) ** 2) == (
        (x + y) * (1 + 2 * A + A ** 2) * 2
        + (x ** 2 + 2 * x * y + y ** 2) * (1 + 4 * A + 6 * A ** 2 + 4 * A ** 3 + A ** 4)
        + 1
    )
    assert expand_multinomial((1 + (y + x) * p) ** 3) == (
        (x + y) * (1 + 2 * A + A ** 2) * 3
        + (x ** 2 + 2 * x * y + y ** 2) * (1 + 4 * A + 6 * A ** 2 + 4 * A ** 3 + A ** 4) * 3
        + (x ** 3 + 3 * x ** 2 * y + 3 * x * y ** 2 + y ** 3)
        * (1 + 6 * A + 15 * A ** 2 + 20 * A ** 3 + 15 * A ** 4 + 6 * A ** 5 + A ** 6)
        + 1
    )
    # unevaluate powers
    eq = Pow((x + 1) * ((A + 1) ** 2), 2, evaluate=False)
    # - in this case the base is not an Add so no further
    #   expansion is done
    assert expand_multinomial(eq) == (x ** 2 + 2 * x + 1) * (1 + 4 * A + 6 * A ** 2 + 4 * A ** 3 + A ** 4)
    # - but here, the expanded base *is* an Add so it gets expanded
    eq = Pow(((A + 1) ** 2), 2, evaluate=False)
    assert expand_multinomial(eq) == 1 + 4 * A + 6 * A ** 2 + 4 * A ** 3 + A ** 4

    # coverage
    def ok(a, b, n):
        e = (a + I * b) ** n
        return verify_numerically(e, expand_multinomial(e))

    for a in [2, S.Half]:
        for b in [3, S(1) / 3]:
            for n in range(2, 6):
                assert ok(a, b, n)

    assert expand_multinomial((x + 1 + O(z)) ** 2) == 1 + 2 * x + x ** 2 + O(z)
    assert expand_multinomial((x + 1 + O(z)) ** 3) == 1 + 3 * x + 3 * x ** 2 + x ** 3 + O(z)

    assert expand_multinomial(3 ** (x + y + 3)) == 27 * 3 ** (x + y)
def test_expand_non_commutative():
    A = Symbol('A', commutative=False)
    B = Symbol('B', commutative=False)
    C = Symbol('C', commutative=False)
    a = Symbol('a')
    b = Symbol('b')
    i = Symbol('i', integer=True)
    n = Symbol('n', negative=True)
    m = Symbol('m', negative=True)
    p = Symbol('p', polar=True)
    np = Symbol('p', polar=False)

    assert (C * (A + B)).expand() == C * A + C * B
    assert (C * (A + B)).expand() != A * C + B * C
    assert ((A + B)**2).expand() == A**2 + A * B + B * A + B**2
    assert ((A +
             B)**3).expand() == (A**2 * B + B**2 * A + A * B**2 + B * A**2 +
                                 A**3 + B**3 + A * B * A + B * A * B)
    # issue 6219
    assert ((a * A * B * A**-1)**2).expand() == a**2 * A * B**2 / A
    # Note that (a*A*B*A**-1)**2 is automatically converted to a**2*(A*B*A**-1)**2
    assert ((a * A * B *
             A**-1)**2).expand(deep=False) == a**2 * (A * B * A**-1)**2
    assert ((a * A * B * A**-1)**2).expand() == a**2 * (A * B**2 * A**-1)
    assert ((a * A * B *
             A**-1)**2).expand(force=True) == a**2 * A * B**2 * A**(-1)
    assert ((a * A * B)**2).expand() == a**2 * A * B * A * B
    assert ((a * A)**2).expand() == a**2 * A**2
    assert ((a * A * B)**i).expand() == a**i * (A * B)**i
    assert ((a * A *
             (B *
              (A * B / A)**2))**i).expand() == a**i * (A * B * A * B**2 / A)**i
    # issue 6558
    assert (A * B * (A * B)**-1).expand() == A * B * (A * B)**-1
    assert ((a * A)**i).expand() == a**i * A**i
    assert ((a * A * B * A**-1)**3).expand() == a**3 * A * B**3 / A
    assert ((a*A*B*A*B/A)**3).expand() == \
        a**3*A*B*(A*B**2)*(A*B**2)*A*B*A**(-1)
    assert ((a*A*B*A*B/A)**-3).expand() == \
        a**-3*(A*B*(A*B**2)*(A*B**2)*A*B*A**(-1))**-1
    assert ((a * b * A * B *
             A**-1)**i).expand() == a**i * b**i * (A * B / A)**i
    assert ((a * (a * b)**i)**i).expand() == a**i * a**(i**2) * b**(i**2)
    e = Pow(Mul(a, 1 / a, A, B, evaluate=False), S(2), evaluate=False)
    assert e.expand() == A * B * A * B
    assert sqrt(a * (A * b)**i).expand() == sqrt(a * b**i * A**i)
    assert (sqrt(-a)**a).expand() == sqrt(-a)**a
    assert expand((-2 * n)**(i / 3)) == 2**(i / 3) * (-n)**(i / 3)
    assert expand(
        (-2 * n * m)**(i / a)) == (-2)**(i / a) * (-n)**(i / a) * (-m)**(i / a)
    assert expand((-2 * a * p)**b) == 2**b * p**b * (-a)**b
    assert expand((-2 * a * np)**b) == 2**b * (-a * np)**b
    assert expand(sqrt(A * B)) == sqrt(A * B)
    assert expand(sqrt(-2 * a * b)) == sqrt(2) * sqrt(-a * b)
示例#5
0
def test_expand_non_commutative():
    A = Symbol("A", commutative=False)
    B = Symbol("B", commutative=False)
    C = Symbol("C", commutative=False)
    a = Symbol("a")
    b = Symbol("b")
    i = Symbol("i", integer=True)
    n = Symbol("n", negative=True)
    m = Symbol("m", negative=True)
    p = Symbol("p", polar=True)
    np = Symbol("p", polar=False)

    assert (C * (A + B)).expand() == C * A + C * B
    assert (C * (A + B)).expand() != A * C + B * C
    assert ((A + B) ** 2).expand() == A ** 2 + A * B + B * A + B ** 2
    assert ((A + B) ** 3).expand() == (
        A ** 2 * B + B ** 2 * A + A * B ** 2 + B * A ** 2 + A ** 3 + B ** 3 + A * B * A + B * A * B
    )
    # issue 6219
    assert ((a * A * B * A ** -1) ** 2).expand() == a ** 2 * A * B ** 2 / A
    # Note that (a*A*B*A**-1)**2 is automatically converted to a**2*(A*B*A**-1)**2
    assert ((a * A * B * A ** -1) ** 2).expand(deep=False) == a ** 2 * (A * B * A ** -1) ** 2
    assert ((a * A * B * A ** -1) ** 2).expand() == a ** 2 * (A * B ** 2 * A ** -1)
    assert ((a * A * B * A ** -1) ** 2).expand(force=True) == a ** 2 * A * B ** 2 * A ** (-1)
    assert ((a * A * B) ** 2).expand() == a ** 2 * A * B * A * B
    assert ((a * A) ** 2).expand() == a ** 2 * A ** 2
    assert ((a * A * B) ** i).expand() == a ** i * (A * B) ** i
    assert ((a * A * (B * (A * B / A) ** 2)) ** i).expand() == a ** i * (A * B * A * B ** 2 / A) ** i
    # issue 6558
    assert (A * B * (A * B) ** -1).expand() == A * B * (A * B) ** -1
    assert ((a * A) ** i).expand() == a ** i * A ** i
    assert ((a * A * B * A ** -1) ** 3).expand() == a ** 3 * A * B ** 3 / A
    assert ((a * A * B * A * B / A) ** 3).expand() == a ** 3 * A * B * (A * B ** 2) * (A * B ** 2) * A * B * A ** (-1)
    assert ((a * A * B * A * B / A) ** -3).expand() == a ** -3 * (
        A * B * (A * B ** 2) * (A * B ** 2) * A * B * A ** (-1)
    ) ** -1
    assert ((a * b * A * B * A ** -1) ** i).expand() == a ** i * b ** i * (A * B / A) ** i
    assert ((a * (a * b) ** i) ** i).expand() == a ** i * a ** (i ** 2) * b ** (i ** 2)
    e = Pow(Mul(a, 1 / a, A, B, evaluate=False), S(2), evaluate=False)
    assert e.expand() == A * B * A * B
    assert sqrt(a * (A * b) ** i).expand() == sqrt(a * b ** i * A ** i)
    assert (sqrt(-a) ** a).expand() == sqrt(-a) ** a
    assert expand((-2 * n) ** (i / 3)) == 2 ** (i / 3) * (-n) ** (i / 3)
    assert expand((-2 * n * m) ** (i / a)) == (-2) ** (i / a) * (-n) ** (i / a) * (-m) ** (i / a)
    assert expand((-2 * a * p) ** b) == 2 ** b * p ** b * (-a) ** b
    assert expand((-2 * a * np) ** b) == 2 ** b * (-a * np) ** b
    assert expand(sqrt(A * B)) == sqrt(A * B)
    assert expand(sqrt(-2 * a * b)) == sqrt(2) * sqrt(-a * b)
示例#6
0
def test_expand_log():
    t = Symbol('t', positive=True)
    # after first expansion, -2*log(2) + log(4); then 0 after second
    assert expand(log(t**2) - log(t**2/4) - 2*log(2)) == 0