コード例 #1
0
def test_sympyissue_4499():
    # previously, this gave 16 constants
    from diofant.abc import a, b
    B = Function('B')
    G = Function('G')
    t = Tuple(*(a, a + Rational(1, 2), 2 * a, b, 2 * a - b + 1,
                (sqrt(z) / 2)**(-2 * a + 1) * B(2 * a - b, sqrt(z)) *
                B(b - 1, sqrt(z)) * G(b) * G(2 * a - b + 1),
                sqrt(z) * (sqrt(z) / 2)**(-2 * a + 1) * B(b, sqrt(z)) *
                B(2 * a - b, sqrt(z)) * G(b) * G(2 * a - b + 1),
                sqrt(z) * (sqrt(z) / 2)**(-2 * a + 1) * B(b - 1, sqrt(z)) *
                B(2 * a - b + 1, sqrt(z)) * G(b) * G(2 * a - b + 1),
                (sqrt(z) / 2)**(-2 * a + 1) * B(b, sqrt(z)) *
                B(2 * a - b + 1, sqrt(z)) * G(b) * G(2 * a - b + 1), 1, 0,
                Rational(1, 2), z / 2, -b + 1, -2 * a + b, -2 * a))
    c = cse(t)
    ans = ([(x0, 2 * a), (x1, -b), (x2, x1 + 1), (x3, x0 + x2), (x4, sqrt(z)),
            (x5, B(x0 + x1, x4)), (x6, G(b)), (x7, G(x3)), (x8, -x0),
            (x9, (x4 / 2)**(x8 + 1)), (x10, x6 * x7 * x9 * B(b - 1, x4)),
            (x11, x6 * x7 * x9 * B(b, x4)),
            (x12, B(x3,
                    x4))], [(a, a + Rational(1, 2), x0, b, x3, x10 * x5,
                             x11 * x4 * x5, x10 * x12 * x4, x11 * x12, 1, 0,
                             Rational(1, 2), z / 2, x2, b + x8, x8)])
    assert ans == c
コード例 #2
0
def test_undef_fcn_float_sympyissue_6938():
    f = Function('ceil')
    assert isinstance(f(0.3), Function)
    f = Function('sin')
    assert isinstance(f(0.3), Function)
    assert isinstance(f(pi).evalf(), Function)
    assert isinstance(f(x).evalf(subs={x: 1.2}), Function)
コード例 #3
0
def test_local_dict_symbol_to_fcn():
    x = Symbol('x')
    d = {'foo': Function('bar')}
    assert parse_expr('foo(x)', local_dict=d) == d['foo'](x)
    # XXX: bit odd, but would be error if parser left the Symbol
    d = {'foo': Symbol('baz')}
    assert parse_expr('foo(x)', local_dict=d) == Function('baz')(x)
コード例 #4
0
def test_functional_diffgeom_ch3():
    x0, y0 = symbols('x0, y0', extended_real=True)
    x, y, t = symbols('x, y, t', extended_real=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])

    s_field = f(R2.x, R2.y)
    v_field = b1(R2.x) * R2.e_x + b2(R2.y) * R2.e_y
    assert v_field.rcall(s_field).rcall(p_r).doit() == b1(x0) * Derivative(
        f(x0, y0), x0) + b2(y0) * Derivative(f(x0, y0), y0)

    assert R2.e_x(R2.r**2).rcall(p_r) == 2 * x0
    v = R2.e_x + 2 * R2.e_y
    s = R2.r**2 + 3 * R2.x
    assert v.rcall(s).rcall(p_r).doit() == 2 * x0 + 4 * y0 + 3

    circ = -R2.y * R2.e_x + R2.x * R2.e_y
    series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True)
    series_x, series_y = zip(*series)
    assert all(term == cos(t).taylor_term(i, t)
               for i, term in enumerate(series_x))
    assert all(term == sin(t).taylor_term(i, t)
               for i, term in enumerate(series_y))
コード例 #5
0
def test_diff():
    f = Function('f')
    g = Function('g')
    assert simplify(g(x).diff(x)*f(x).diff(x) - f(x).diff(x)*g(x).diff(x)) == 0
    assert simplify(2*f(x)*f(x).diff(x) - diff(f(x)**2, x)) == 0
    assert simplify(diff(1/f(x), x) + f(x).diff(x)/f(x)**2) == 0
    assert simplify(f(x).diff(x, y) - f(x).diff(y, x)) == 0
コード例 #6
0
ファイル: test_repr.py プロジェクト: diofant/diofant
def test_Function():
    sT(Function('f')(x), "Function('f')(Symbol('x'))")
    # test unapplied Function
    sT(Function('f'), "Function('f')")

    sT(sin(x), "sin(Symbol('x'))")
    sT(sin, 'sin')
コード例 #7
0
def idiff(eq, y, x, n=1):
    """Return ``dy/dx`` assuming that ``eq == 0``.

    Parameters
    ==========

    y : the dependent variable or a list of dependent variables (with y first)
    x : the variable that the derivative is being taken with respect to
    n : the order of the derivative (default is 1)

    Examples
    ========

    >>> from diofant.abc import x, y, a
    >>> from diofant.geometry.util import idiff

    >>> circ = x**2 + y**2 - 4
    >>> idiff(circ, y, x)
    -x/y
    >>> idiff(circ, y, x, 2).simplify()
    -(x**2 + y**2)/y**3

    Here, ``a`` is assumed to be independent of ``x``:

    >>> idiff(x + a + y, y, x)
    -1

    Now the x-dependence of ``a`` is made explicit by listing ``a`` after
    ``y`` in a list.

    >>> idiff(x + a + y, [y, a], x)
    -Derivative(a, x) - 1

    See Also
    ========

    diofant.core.function.Derivative: represents unevaluated derivatives
    diofant.core.function.diff: explicitly differentiates wrt symbols

    """
    if is_sequence(y):
        dep = set(y)
        y = y[0]
    elif isinstance(y, (Dummy, Symbol)):
        dep = {y}
    else:
        raise ValueError("expecting x-dependent symbol(s) but got: %s" % y)

    f = {s: Function(s.name)(x) for s in eq.free_symbols if s != x and s in dep}
    dydx = Function(y.name)(x).diff(x)
    eq = eq.subs(f)
    derivs = {}
    for i in range(n):
        yp = solve(eq.diff(x), dydx)[0].subs(derivs)
        if i == n - 1:
            return yp.subs([(v, k) for k, v in f.items()])
        derivs[dydx] = yp
        eq = dydx - yp
        dydx = dydx.diff(x)
コード例 #8
0
def test_sympyissue_7687():
    f = Function('f')(x)
    ff = Function('f')(x)
    match_with_cache = f.match(ff)
    assert isinstance(f, type(ff))
    clear_cache()
    ff = Function('f')(x)
    assert isinstance(f, type(ff))
    assert match_with_cache == f.match(ff)
コード例 #9
0
ファイル: test_evalf.py プロジェクト: cbm755/diofant
def test_implemented_function_evalf():
    f = Function('f')
    f = implemented_function(f, lambda x: x + 1)
    assert str(f(x)) == "f(x)"
    assert str(f(2)) == "f(2)"
    assert f(2).evalf(strict=False) == 3
    assert f(x).evalf(strict=False) == f(x)
    f = implemented_function(Function('sin'), lambda x: x + 1)
    assert f(2).evalf() != sin(2)
    del f._imp_  # XXX: due to caching _imp_ would influence all other tests
コード例 #10
0
ファイル: test_euler.py プロジェクト: goretkin/diofant
def test_euler_henonheiles():
    x = Function('x')
    y = Function('y')
    t = Symbol('t')
    L = sum(D(z(t), t)**2 / 2 - z(t)**2 / 2 for z in [x, y])
    L += -x(t)**2 * y(t) + y(t)**3 / 3
    assert euler(L, [x(t), y(t)], t) == [
        Eq(-2 * x(t) * y(t) - x(t) - D(x(t), t, t)),
        Eq(-x(t)**2 + y(t)**2 - y(t) - D(y(t), t, t))
    ]
コード例 #11
0
def test_sympyissue_7687():
    from diofant.core.function import Function
    from diofant.abc import x
    f = Function('f')(x)
    ff = Function('f')(x)
    match_with_cache = ff.matches(f)
    assert isinstance(f, type(ff))
    clear_cache()
    ff = Function('f')(x)
    assert isinstance(f, type(ff))
    assert match_with_cache == ff.matches(f)
コード例 #12
0
def test_match_deriv_bug1():
    n = Function('n')
    l = Function('l')

    p = Wild('p')

    e = diff(l(x), x)/x - diff(diff(n(x), x), x)/2 - \
        diff(n(x), x)**2/4 + diff(n(x), x)*diff(l(x), x)/4
    e = e.subs({n(x): -l(x)}).doit()
    t = x * exp(-l(x))
    t2 = t.diff(x, x) / t
    assert e.match((p * t2).expand()) == {p: -Rational(1, 2)}
コード例 #13
0
ファイル: test_exponential.py プロジェクト: goretkin/diofant
def test_log_product():
    from diofant.abc import n, m
    i, j = symbols('i,j', positive=True, integer=True)
    x, y = symbols('x,y', positive=True)
    from diofant.concrete import Product, Sum
    f, g = Function('f'), Function('g')
    assert simplify(log(Product(x**i,
                                (i, 1, n)))) == Sum(i * log(x), (i, 1, n))
    assert simplify(log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))) == \
            log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))

    expr = log(Product(-2, (n, 0, 4)))
    assert simplify(expr) == expr
コード例 #14
0
def test_Derivative_bug1():
    f = Function("f")
    x = abc.x
    a = Wild("a", exclude=[f(x)])
    b = Wild("b", exclude=[f(x)])
    eq = f(x).diff(x)
    assert eq.match(a * Derivative(f(x), x) + b) == {a: 1, b: 0}
コード例 #15
0
def test_functions():
    g = WildFunction('g')
    p = Wild('p')
    q = Wild('q')

    f = cos(5 * x)
    notf = x
    assert f.match(p * cos(q * x)) == {p: 1, q: 5}
    assert f.match(p * g) == {p: 1, g: cos(5 * x)}
    assert notf.match(g) is None

    F = WildFunction('F', nargs=2)
    assert F.nargs == FiniteSet(2)
    f = Function('f')
    assert f(x).match(F) is None

    F = WildFunction('F', nargs=(1, 2))
    assert F.nargs == FiniteSet(1, 2)

    # issue sympy/sympy#2711
    f = meijerg(((), ()), ((0, ), ()), x)
    a = Wild('a')
    b = Wild('b')

    assert f.find(a) == {
        0: 1,
        x: 1,
        meijerg(((), ()), ((0, ), ()), x): 1,
        (): 3,
        (0, ): 1,
        ((), ()): 1,
        ((0, ), ()): 1
    }
    assert f.find(a + b) == {0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1}
    assert f.find(a**2) == {x: 1, meijerg(((), ()), ((0, ), ()), x): 1}
コード例 #16
0
def test_sympyissue_4855():
    assert 1 / O(1) != O(1)
    assert 1 / O(x) != O(1 / x)
    assert 1 / O(x, (x, oo)) != O(1 / x, (x, oo))

    f = Function('f')
    assert 1 / O(f(x)) != O(1 / x)
コード例 #17
0
ファイル: test_sums_products.py プロジェクト: cbm755/diofant
def test_distribution_over_equality():
    f = Function('f')
    assert Product(Eq(x * 2, f(x)),
                   (x, 1, 3)).doit() == Eq(48,
                                           f(1) * f(2) * f(3))
    assert Sum(Eq(f(x), x**2), (x, 0, y)) == \
        Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
コード例 #18
0
def test_Function():
    f = Function('f')
    fx = f(x)
    w = WildFunction('w')
    assert str(f) == 'f'
    assert str(fx) == 'f(x)'
    assert str(w) == 'w_'
コード例 #19
0
ファイル: test_str.py プロジェクト: goretkin/diofant
def test_Function():
    f = Function('f')
    fx = f(x)
    w = WildFunction('w')
    assert str(f) == "f"
    assert str(fx) == "f(x)"
    assert str(w) == "w_"
コード例 #20
0
ファイル: test_diff.py プロジェクト: naveensaigit/diofant
def test_diff():
    x, y = symbols('x, y')
    assert Rational(1, 3).diff(x) is Integer(0)
    assert I.diff(x) is Integer(0)
    assert pi.diff(x) is Integer(0)
    assert x.diff(x, 0) == x
    assert (x**2).diff(x, 2, x) == 0
    assert (x**2).diff(x, y, 0) == 2*x
    assert (x**2).diff(x, y) == 0
    pytest.raises(ValueError, lambda: x.diff(1, x))

    a = Symbol('a')
    b = Symbol('b')
    c = Symbol('c')
    p = Integer(5)
    e = a*b + b**p
    assert e.diff(a) == b
    assert e.diff(b) == a + 5*b**4
    assert e.diff(b).diff(a) == 1
    e = a*(b + c)
    assert e.diff(a) == b + c
    assert e.diff(b) == a
    assert e.diff(b).diff(a) == 1
    e = c**p
    assert e.diff(c, 6) == 0
    assert e.diff(c, 5) == 120
    e = c**2
    assert e.diff(c) == 2*c
    e = a*b*c
    assert e.diff(c) == a*b

    f = Function('f')
    assert f(x).diff(x, 2).diff(f(x).diff(x, 1)) == 0
コード例 #21
0
ファイル: test_cse.py プロジェクト: Blendify/diofant
def test_dont_cse_tuples():
    f = Function("f")
    g = Function("g")

    name_val, (expr,) = cse(Subs(f(x, y), (x, 0), (y, 1)) +
                            Subs(g(x, y), (x, 0), (y, 1)))

    assert name_val == []
    assert expr == (Subs(f(x, y), (x, 0), (y, 1))
                    + Subs(g(x, y), (x, 0), (y, 1)))

    name_val, (expr,) = cse(Subs(f(x, y), (x, 0), (y, x + y)) +
                            Subs(g(x, y), (x, 0), (y, x + y)))

    assert name_val == [(x0, x + y)]
    assert expr == Subs(f(x, y), (x, 0), (y, x0)) + Subs(g(x, y), (x, 0), (y, x0))
コード例 #22
0
def test_functional_diffgeom_ch2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    f = Function('f')

    assert (R2_p.point_to_coords(R2_r.point([x0, y0])) == Matrix(
        [sqrt(x0**2 + y0**2), atan2(y0, x0)]))
    assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) == Matrix(
        [r0 * cos(theta0), r0 * sin(theta0)]))

    assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix(
        [[cos(theta0), -r0 * sin(theta0)], [sin(theta0), r0 * cos(theta0)]])

    field = f(R2.x, R2.y)
    p1_in_rect = R2_r.point([x0, y0])
    p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)])
    assert field.rcall(p1_in_rect) == f(x0, y0)
    assert field.rcall(p1_in_polar) == f(x0, y0)

    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])
    assert R2.x(p_r) == x0
    assert R2.x(p_p) == r0 * cos(theta0)
    assert R2.r(p_p) == r0
    assert R2.r(p_r) == sqrt(x0**2 + y0**2)
    assert R2.theta(p_r) == atan2(y0, x0)

    h = R2.x * R2.r**2 + R2.y**3
    assert h.rcall(p_r) == x0 * (x0**2 + y0**2) + y0**3
    assert h.rcall(p_p) == r0**3 * sin(theta0)**3 + r0**3 * cos(theta0)
コード例 #23
0
def test_diff():
    assert Rational(1, 3).diff(x) is Integer(0)
    assert I.diff(x) is Integer(0)
    assert pi.diff(x) is Integer(0)
    assert x.diff((x, 0)) == x
    assert (x**2).diff((x, 2), x) == 0
    assert (x**2).diff(x, (y, 0)) == 2*x
    assert (x**2).diff(x, y) == 0
    pytest.raises(ValueError, lambda: x.diff((1, x)))

    p = Integer(5)
    e = a*b + b**p
    assert e.diff(a) == b
    assert e.diff(b) == a + 5*b**4
    assert e.diff(b).diff(a) == 1
    e = a*(b + c)
    assert e.diff(a) == b + c
    assert e.diff(b) == a
    assert e.diff(b).diff(a) == 1
    e = c**p
    assert e.diff((c, 6)) == 0
    assert e.diff((c, 5)) == 120
    e = c**2
    assert e.diff(c) == 2*c
    e = a*b*c
    assert e.diff(c) == a*b

    f = Function('f')
    assert f(x).diff((x, 2)).diff(f(x).diff((x, 1))) == 0
コード例 #24
0
ファイル: test_python.py プロジェクト: diofant/diofant
def test_python_keyword_symbol_name_escaping():
    # Check for escaping of keywords
    assert python(
        5 * Symbol('lambda')) == "lambda_ = Symbol('lambda')\ne = 5*lambda_"
    assert (
        python(5 * Symbol('lambda') + 7 * Symbol('lambda_')) ==
        "lambda__ = Symbol('lambda')\nlambda_ = Symbol('lambda_')\ne = 7*lambda_ + 5*lambda__"
    )
    assert (
        python(5 * Symbol('for') + Function('for_')(8)) ==
        "for__ = Symbol('for')\nfor_ = Function('for_')\ne = 5*for__ + for_(8)"
    )
    assert (
        python(5 * Symbol('for_') + Function('for')(8)) ==
        "for_ = Symbol('for_')\nfor__ = Function('for')\ne = 5*for_ + for__(8)"
    )
コード例 #25
0
def test__eval_product():
    from diofant.abc import i, n
    # issue 4809
    a = Function('a')
    assert product(2 * a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n))
    # issue 4810
    assert product(2**i, (i, 1, n)) == 2**(n / 2 + n**2 / 2)
コード例 #26
0
ファイル: test_str.py プロジェクト: cbm755/diofant
def test_Differential():
    tp = TensorProduct(R2.dx, R2.dy)
    assert sstr(LieDerivative(R2.e_x, tp)) == 'LieDerivative(e_x, TensorProduct(dx, dy))'

    g = Function('g')
    s_field = g(R2.x, R2.y)
    assert sstr(Differential(s_field)) == 'd(g(x, y))'
コード例 #27
0
ファイル: test_complexes.py プロジェクト: Blendify/diofant
def test_polarify():
    z = Symbol('z', polar=True)
    f = Function('f')
    ES = {}

    assert polarify(-1) == (polar_lift(-1), ES)
    assert polarify(1 + I) == (polar_lift(1 + I), ES)

    assert polarify(exp(x), subs=False) == exp(x)
    assert polarify(1 + x, subs=False) == 1 + x
    assert polarify(f(I) + x, subs=False) == f(polar_lift(I)) + x

    assert polarify(x, lift=True) == polar_lift(x)
    assert polarify(z, lift=True) == z
    assert polarify(f(x), lift=True) == f(polar_lift(x))
    assert polarify(1 + x, lift=True) == polar_lift(1 + x)
    assert polarify(1 + f(x), lift=True) == polar_lift(1 + f(polar_lift(x)))

    newex, subs = polarify(f(x) + z)
    assert newex.subs(subs) == f(x) + z

    mu = Symbol("mu")
    sigma = Symbol("sigma", positive=True)

    # Make sure polarify(lift=True) doesn't try to lift the integration
    # variable
    assert polarify(
        Integral(sqrt(2)*x*exp(-(-mu + x)**2/(2*sigma**2))/(2*sqrt(pi)*sigma),
                 (x, -oo, oo)), lift=True) == Integral(sqrt(2)*(sigma*exp_polar(0))**exp_polar(I*pi) *
                                                       exp((sigma*exp_polar(0))**(2*exp_polar(I*pi))*exp_polar(I*pi)*polar_lift(-mu + x) **
                                                           (2*exp_polar(0))/2)*exp_polar(0)*polar_lift(x)/(2*sqrt(pi)), (x, -oo, oo))
コード例 #28
0
ファイル: test_sums_products.py プロジェクト: cbm755/diofant
def test_Sum_doit():
    f = Function('f')
    assert Sum(n * Integral(a**2), (n, 0, 2)).doit() == a**3
    assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \
        3*Integral(a**2)
    assert summation(n * Integral(a**2), (n, 0, 2)) == 3 * Integral(a**2)

    # test nested sum evaluation
    s = Sum(Sum(Sum(2, (z, 1, n + 1)), (y, x + 1, n)), (x, 1, n))
    assert 0 == (s.doit() - n * (n + 1) * (n - 1)).factor()

    assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3
    assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \
        3*Piecewise((1, And(Integer(1) <= k, k <= 3)), (0, True))
    assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \
        f(1) + f(2) + f(3)
    assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \
        Sum(Piecewise((f(n), And(Le(0, n), n < oo)), (0, True)), (n, 1, oo))
    l = Symbol('l', integer=True, positive=True)
    assert Sum(f(l)*Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == \
        Sum(f(l), (l, 1, oo))

    # issue sympy/sympy#2597
    nmax = symbols('N', integer=True, positive=True)
    pw = Piecewise((1, And(Integer(1) <= n, n <= nmax)), (0, True))
    assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax))
コード例 #29
0
ファイル: test_complexes.py プロジェクト: diofant/diofant
def test_conjugate():
    a = Symbol('a', extended_real=True)
    b = Symbol('b', imaginary=True)
    assert conjugate(a) == a
    assert conjugate(I * a) == -I * a
    assert conjugate(b) == -b
    assert conjugate(I * b) == I * b
    assert conjugate(a * b) == -a * b
    assert conjugate(I * a * b) == I * a * b

    x = Symbol('x')
    y = Symbol('y')
    assert conjugate(conjugate(x)) == x
    assert conjugate(x + y) == conjugate(x) + conjugate(y)
    assert conjugate(x - y) == conjugate(x) - conjugate(y)
    assert conjugate(x * y) == conjugate(x) * conjugate(y)
    assert conjugate(x / y) == conjugate(x) / conjugate(y)
    assert conjugate(-x) == -conjugate(x)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert conjugate(z).diff(z) == Derivative(conjugate(z), z)

    # issue sympy/sympy#4754
    x = Symbol('x', extended_real=True)
    y = Symbol('y', imaginary=True)
    f = Function('f')
    assert (f(x).conjugate()).diff(x) == (f(x).diff(x)).conjugate()
    assert (f(y).conjugate()).diff(y) == -(f(y).diff(y)).conjugate()
コード例 #30
0
def test_sympyissue_6920():
    e = [cos(x) + I*sin(x), cos(x) - I*sin(x),
         cosh(x) - sinh(x), cosh(x) + sinh(x)]
    ok = [exp(I*x), exp(-I*x), exp(-x), exp(x)]
    # wrap in f to show that the change happens wherever ei occurs
    f = Function('f')
    assert [simplify(f(ei)).args[0] for ei in e] == ok