Repository: sympy/sympy
File: sympy/assumptions/tests/test_refine.py (Python)

Code before:
    mock_obj = MockExpr()
    assert refine(mock_obj)

def test_refine_issue_12724():
    expr1 = refine(Abs(x * y), Q.positive(x))
    expr2 = refine(Abs(x * y * z), Q.positive(x))
    assert expr1 == x * Abs(y)
    assert expr2 == x * Abs(y * z)
    y1 = Symbol('y1', real = True)
    expr3 = refine(Abs(x * y1**2 * z), Q.positive(x))
    assert expr3 == x * y1**2 * Abs(z)


def test_matrixelement():
    x = MatrixSymbol('x', 3, 3)
    i = Symbol('i', positive = True)
    j = Symbol('j', positive = True)
    assert refine(x[0, 1], Q.symmetric(x)) == x[0, 1]
    assert refine(x[1, 0], Q.symmetric(x)) == x[0, 1]
    assert refine(x[i, j], Q.symmetric(x)) == x[j, i]
    assert refine(x[j, i], Q.symmetric(x)) == x[j, i]


def test_sin_cos():
    n = Symbol('n')
    m = Symbol('m')
    assert refine(cos(n*pi/2), Q.odd(n)) == 0
    assert refine(cos(n*pi), Q.even(n)) == 1
    assert refine(cos(n*pi), Q.odd(n)) == -1
    assert refine(sin(n*pi), Q.integer(n)) == 0
    assert refine(sin(n*pi/2), Q.odd(n) & Q.even((n-1)/2)) == 1
    assert refine(sin(n*pi/2), Q.odd(n) & Q.odd((n-1)/2)) == -1
    assert refine(cos(n*pi), Q.integer(n)) == (-1)**n
    assert refine(sin(n*pi/2), Q.even(n)) == 0
    assert refine(cos(n*pi/2), Q.even(n)) == (-1)**(n/2)
    assert refine(sin(n*pi/2), Q.odd(n)) == (-1)**((n + 3)/2)
    assert refine(cos(n*pi/2), Q.odd(n)) == 0
    assert refine(sin(x + n*pi), Q.integer(n)) == ((-1)**n) * sin(x)
    assert refine(cos(x + n*pi), Q.integer(n)) == ((-1)**n) * cos(x)
    assert refine(sin(x + n*pi), Q.even(n)) == sin(x)
    assert refine(cos(x + n*pi), Q.even(n)) == cos(x)
    assert refine(sin(x + n*pi), Q.odd(n)) == -sin(x)
    assert refine(cos(x + n*pi), Q.odd(n)) == -cos(x)
    assert refine(sin(x + n*pi/2), Q.even(n)) == ((-1)**(n/2)) * sin(x)
    assert refine(cos(x + n*pi/2), Q.even(n)) == ((-1)**(n/2)) * cos(x)
    assert refine(sin(x + n*pi/2), Q.odd(n)) == ((-1)**((n + 3)/2)) * cos(x)
    assert refine(cos(x + n*pi/2), Q.odd(n)) == ((-1)**((n + 1)/2)) * sin(x)
    assert refine(sin(x + y + 2*n*pi), Q.integer(n)) == sin(x + y)
    assert refine(cos(x + y + 2*n*pi), Q.integer(n)) == cos(x + y)
    assert refine(cos(x + n*pi + m*pi / 2), Q.integer(n) & Q.even(m)) == \
        (-1)**(n + m / 2) * cos(x)

Review comment (suggestion):
You can do this in one line with: `n, m = symbols("n m")`

Though it could also make sense to only create `m` right before you use it. That way it's easier to see which cases use a single variable and which don't.

Code after:
    mock_obj = MockExpr()
    assert refine(mock_obj)

def test_refine_issue_12724():
    expr1 = refine(Abs(x * y), Q.positive(x))
    expr2 = refine(Abs(x * y * z), Q.positive(x))
    assert expr1 == x * Abs(y)
    assert expr2 == x * Abs(y * z)
    y1 = Symbol('y1', real = True)
    expr3 = refine(Abs(x * y1**2 * z), Q.positive(x))
    assert expr3 == x * y1**2 * Abs(z)


def test_matrixelement():
    x = MatrixSymbol('x', 3, 3)
    i = Symbol('i', positive = True)
    j = Symbol('j', positive = True)
    assert refine(x[0, 1], Q.symmetric(x)) == x[0, 1]
    assert refine(x[1, 0], Q.symmetric(x)) == x[0, 1]
    assert refine(x[i, j], Q.symmetric(x)) == x[j, i]
    assert refine(x[j, i], Q.symmetric(x)) == x[j, i]


def test_sin_cos():
    n = Symbol('n')
    assert refine(cos(n*pi/2), Q.odd(n)) == 0
    assert refine(cos(n*pi), Q.even(n)) == 1
    assert refine(cos(n*pi), Q.odd(n)) == -1
    assert refine(sin(n*pi), Q.integer(n)) == 0
    assert refine(sin(n*pi/2), Q.odd(n) & Q.even((n-1)/2)) == 1
    assert refine(sin(n*pi/2), Q.odd(n) & Q.odd((n-1)/2)) == -1
    assert refine(cos(n*pi), Q.integer(n)) == (-1)**n
    assert refine(sin(n*pi/2), Q.even(n)) == 0
    assert refine(cos(n*pi/2), Q.even(n)) == (-1)**(n/2)
    assert refine(sin(n*pi/2), Q.odd(n)) == (-1)**((n + 3)/2)
    assert refine(cos(n*pi/2), Q.odd(n)) == 0
    assert refine(sin(x + n*pi), Q.integer(n)) == ((-1)**n) * sin(x)
    assert refine(cos(x + n*pi), Q.integer(n)) == ((-1)**n) * cos(x)
    assert refine(sin(x + n*pi), Q.even(n)) == sin(x)
    assert refine(cos(x + n*pi), Q.even(n)) == cos(x)
    assert refine(sin(x + n*pi), Q.odd(n)) == -sin(x)
    assert refine(cos(x + n*pi), Q.odd(n)) == -cos(x)
    assert refine(sin(x - n*pi), Q.odd(n)) == -sin(x)
    assert refine(cos(x - n*pi), Q.even(n)) == cos(x)
    assert refine(sin(x + n*pi/2), Q.even(n)) == ((-1)**(n/2)) * sin(x)
    assert refine(cos(x + n*pi/2), Q.even(n)) == ((-1)**(n/2)) * cos(x)
    assert refine(sin(x + n*pi/2), Q.odd(n)) == ((-1)**((n + 3)/2)) * cos(x)
    assert refine(cos(x + n*pi/2), Q.odd(n)) == ((-1)**((n + 1)/2)) * sin(x)
    assert refine(sin(x - n*pi/2), Q.odd(n)) == ((-1)**((n + 3)/2)) * -cos(x)
    assert refine(cos(x - n*pi / 2), Q.even(n)) == ((-1)**(n/2)) * cos(x)
    assert refine(sin(x + y + 2*n*pi), Q.integer(n)) == sin(x + y)