Example #1
0
def test_atoms():
    assert sorted(list(x.atoms())) == [x]
    assert sorted(list((1+x).atoms())) == sorted([1, x])

    assert sorted(list((1+2*cos(x)).atoms(Symbol))) == [x]
    assert sorted(list((1+2*cos(x)).atoms(Symbol,Number))) == sorted([1, 2, x])

    assert sorted(list((2*(x**(y**x))).atoms())) == sorted([2, x, y])

    assert sorted(list(Rational(1,2).atoms())) == [S.Half]
    assert sorted(list(Rational(1,2).atoms(Symbol))) == []

    assert sorted(list(sin(oo).atoms(oo))) == [oo]

    assert sorted(list(Poly(0, x).atoms())) == [S.Zero]
    assert sorted(list(Poly(1, x).atoms())) == [S.One]

    assert sorted(list(Poly(x, x).atoms())) == [x]
    assert sorted(list(Poly(x, x, y).atoms())) == [x]
    assert sorted(list(Poly(x + y, x, y).atoms())) == sorted([x, y])
    assert sorted(list(Poly(x + y, x, y, z).atoms())) == sorted([x, y])
    assert sorted(list(Poly(x + y*t, x, y, z).atoms())) == sorted([t, x, y])

    assert list((I*pi).atoms(NumberSymbol)) == [pi]
    assert sorted((I*pi).atoms(NumberSymbol, I)) == \
           sorted((I*pi).atoms(I,NumberSymbol)) == [pi, I]


    assert exp(exp(x)).atoms(exp) == set([exp(exp(x)), exp(x)])
    assert (1 + x*(2 + y)+exp(3 + z)).atoms(Add) == set(
                                                   [1 + x*(2 + y)+exp(3 + z),
                                                    2 + y,
                                                    3 + z])

    # issue 3033
    f = Function('f')
    e = (f(x) + sin(x) + 2)
    assert e.atoms(AppliedUndef) == \
        set([f(x)])
    assert e.atoms(AppliedUndef, Function) == \
        set([f(x), sin(x)])
    assert e.atoms(Function) == \
        set([f(x), sin(x)])
    assert e.atoms(AppliedUndef, Number) == \
        set([f(x), S(2)])
    assert e.atoms(Function, Number) == \
        set([S(2), sin(x), f(x)])
Example #2
0
def test_atoms():
    assert sorted(list(x.atoms())) == [x]
    assert sorted(list((1 + x).atoms())) == sorted([1, x])

    assert sorted(list((1 + 2 * cos(x)).atoms(Symbol))) == [x]
    assert sorted(list((1 + 2 * cos(x)).atoms(Symbol,
                                              Number))) == sorted([1, 2, x])

    assert sorted(list((2 * (x**(y**x))).atoms())) == sorted([2, x, y])

    assert sorted(list(Rational(1, 2).atoms())) == [S.Half]
    assert sorted(list(Rational(1, 2).atoms(Symbol))) == []

    assert sorted(list(sin(oo).atoms(oo))) == [oo]

    assert sorted(list(Poly(0, x).atoms())) == [S.Zero]
    assert sorted(list(Poly(1, x).atoms())) == [S.One]

    assert sorted(list(Poly(x, x).atoms())) == [x]
    assert sorted(list(Poly(x, x, y).atoms())) == [x]
    assert sorted(list(Poly(x + y, x, y).atoms())) == sorted([x, y])
    assert sorted(list(Poly(x + y, x, y, z).atoms())) == sorted([x, y])
    assert sorted(list(Poly(x + y * t, x, y, z).atoms())) == sorted([t, x, y])

    assert list((I * pi).atoms(NumberSymbol)) == [pi]
    assert sorted((I*pi).atoms(NumberSymbol, I)) == \
           sorted((I*pi).atoms(I,NumberSymbol)) == [pi, I]

    assert exp(exp(x)).atoms(exp) == set([exp(exp(x)), exp(x)])
    assert (1 + x * (2 + y) + exp(3 + z)).atoms(Add) == set(
        [1 + x * (2 + y) + exp(3 + z), 2 + y, 3 + z])

    # issue 3033
    f = Function('f')
    e = (f(x) + sin(x) + 2)
    assert e.atoms(AppliedUndef) == \
        set([f(x)])
    assert e.atoms(AppliedUndef, Function) == \
        set([f(x), sin(x)])
    assert e.atoms(Function) == \
        set([f(x), sin(x)])
    assert e.atoms(AppliedUndef, Number) == \
        set([f(x), S(2)])
    assert e.atoms(Function, Number) == \
        set([S(2), sin(x), f(x)])