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

Code before:
    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_trig():
    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)) == S.NegativeOne**n
    assert refine(sin(n*pi/2), Q.even(n)) == 0
    assert refine(cos(n*pi/2), Q.even(n)) == S.NegativeOne**(n/2)
    assert refine(sin(n*pi/2), Q.odd(n)) == S.NegativeOne**((n + 3)/2)
    assert refine(cos(n*pi/2), Q.odd(n)) == 0
    assert refine(sin(x + n*pi), Q.integer(n)) == (S.NegativeOne**n) * sin(x)
    assert refine(cos(x + n*pi), Q.integer(n)) == (S.NegativeOne**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)) == (S.NegativeOne**(n/2)) * sin(x)
    assert refine(cos(x + n*pi/2), Q.even(n)) == (S.NegativeOne**(n/2)) * cos(x)
    assert refine(sin(x + n*pi/2), Q.odd(n)) == (S.NegativeOne**((n + 3)/2)) * cos(x)
    assert refine(cos(x + n*pi/2), Q.odd(n)) == (S.NegativeOne**((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)


Review comment (suggestion):
It would be better to just do:
```python
assert refine(cos(n*pi), Q.integer(n)) == (-1)**n
```

Code after:
    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)
    assert refine(cos(x + y + 2*n*pi), Q.integer(n)) == cos(x + y)
    assert refine(sin(x + n*pi), Q.zero(n)) == sin(x)
    assert refine(sin(x + n*pi), Q.zero(-n)) == sin(x)
    assert refine(cos(x + n*pi/2), Q.integer(n)) == cos(x + n*pi/2)
    assert refine(cos(x + y + n*pi/2), Q.integer(n)) == cos(x + y + n*pi/2)
    m = Symbol('m')