Пример #1
0
def test_heurisch_hyperbolic():
    assert heurisch(sinh(x), x) == cosh(x)
    assert heurisch(cosh(x), x) == sinh(x)

    assert heurisch(x*sinh(x), x) == x*cosh(x) - sinh(x)
    assert heurisch(x*cosh(x), x) == x*sinh(x) - cosh(x)

    assert heurisch(
        x*asinh(x/2), x) == x**2*asinh(x/2)/2 + asinh(x/2) - x*sqrt(4 + x**2)/4
Пример #2
0
def test_heurisch_hyperbolic():
    assert heurisch(sinh(x), x) == cosh(x)
    assert heurisch(cosh(x), x) == sinh(x)

    assert heurisch(x * sinh(x), x) == x * cosh(x) - sinh(x)
    assert heurisch(x * cosh(x), x) == x * sinh(x) - cosh(x)

    assert heurisch(
        x * asinh(x / 2),
        x) == x**2 * asinh(x / 2) / 2 + asinh(x / 2) - x * sqrt(4 + x**2) / 4
Пример #3
0
def test_heurisch_function_derivative():
    # TODO: it looks like this used to work just by coincindence and
    # thanks to sloppy implementation. Investigate why this used to
    # work at all and if support for this can be restored.

    df = diff(f(x), x)

    assert heurisch(f(x) * df, x) == f(x)**2 / 2
    assert heurisch(f(x)**2 * df, x) == f(x)**3 / 3
    assert heurisch(df / f(x), x) == log(f(x))
Пример #4
0
def test_pmint_besselj():
    # TODO: in both cases heurisch() gives None. Wrong besselj() derivative?

    f = besselj(nu + 1, x)/besselj(nu, x)
    g = nu*log(x) - log(besselj(nu, x))

    assert simplify(heurisch(f, x) - g) == 0

    f = (nu*besselj(nu, x) - x*besselj(nu + 1, x))/x
    g = besselj(nu, x)

    assert simplify(heurisch(f, x) - g) == 0
Пример #5
0
def test_pmint_besselj():
    # TODO: in both cases heurisch() gives None. Wrong besselj() derivative?

    f = besselj(nu + 1, x) / besselj(nu, x)
    g = nu * log(x) - log(besselj(nu, x))

    assert heurisch(f, x) == g

    f = (nu * besselj(nu, x) - x * besselj(nu + 1, x)) / x
    g = besselj(nu, x)

    assert heurisch(f, x) == g
Пример #6
0
def test_heurisch_exp():
    assert heurisch(exp(x), x) == exp(x)
    assert heurisch(exp(-x), x) == -exp(-x)
    assert heurisch(exp(17*x), x) == exp(17*x) / 17
    assert heurisch(x*exp(x), x) == x*exp(x) - exp(x)
    assert heurisch(x*exp(x**2), x) == exp(x**2) / 2

    assert heurisch(exp(-x**2), x) is None

    assert heurisch(2**x, x) == 2**x/log(2)
    assert heurisch(x*2**x, x) == x*2**x/log(2) - 2**x*log(2)**(-2)

    assert heurisch(Integral(x**z*y, (y, 1, 2), (z, 2, 3)).function, x) == (x*x**z*y)/(z+1)
    assert heurisch(Sum(x**z, (z, 1, 2)).function, z) == x**z/log(x)
Пример #7
0
def test_heurisch_exp():
    assert heurisch(exp(x), x) == exp(x)
    assert heurisch(exp(-x), x) == -exp(-x)
    assert heurisch(exp(17 * x), x) == exp(17 * x) / 17
    assert heurisch(x * exp(x), x) == x * exp(x) - exp(x)
    assert heurisch(x * exp(x**2), x) == exp(x**2) / 2

    assert heurisch(exp(-x**2), x) is None

    assert heurisch(2**x, x) == 2**x / log(2)
    assert heurisch(x * 2**x, x) == x * 2**x / log(2) - 2**x * log(2)**(-2)

    assert heurisch(Integral(x**z * y, (y, 1, 2), (z, 2, 3)).function,
                    x) == (x * x**z * y) / (z + 1)
    assert heurisch(Sum(x**z, (z, 1, 2)).function, z) == x**z / log(x)
Пример #8
0
def test_heurisch_radicals():
    assert heurisch(1/sqrt(x), x) == 2*sqrt(x)
    assert heurisch(1/sqrt(x)**3, x) == -2/sqrt(x)
    assert heurisch(sqrt(x)**3, x) == 2*sqrt(x)**5/5

    assert heurisch(sin(x)*sqrt(cos(x)), x) == -2*sqrt(cos(x))**3/3
    y = Symbol('y')
    assert heurisch(sin(y*sqrt(x)), x) == 2/y**2*sin(y*sqrt(x)) - \
        2*sqrt(x)*cos(y*sqrt(x))/y
    assert heurisch_wrapper(sin(y*sqrt(x)), x) == Piecewise(
        (0, Eq(y, 0)),
        (-2*sqrt(x)*cos(sqrt(x)*y)/y + 2*sin(sqrt(x)*y)/y**2, True))
    y = Symbol('y', positive=True)
    assert heurisch_wrapper(sin(y*sqrt(x)), x) == 2/y**2*sin(y*sqrt(x)) - \
        2*sqrt(x)*cos(y*sqrt(x))/y
Пример #9
0
def test_heurisch_radicals():
    assert heurisch(1 / sqrt(x), x) == 2 * sqrt(x)
    assert heurisch(1 / sqrt(x)**3, x) == -2 / sqrt(x)
    assert heurisch(sqrt(x)**3, x) == 2 * sqrt(x)**5 / 5

    assert heurisch(sin(x) * sqrt(cos(x)), x) == -2 * sqrt(cos(x))**3 / 3
    y = Symbol('y')
    assert heurisch(sin(y*sqrt(x)), x) == 2/y**2*sin(y*sqrt(x)) - \
        2*sqrt(x)*cos(y*sqrt(x))/y
    assert heurisch_wrapper(sin(y * sqrt(x)), x) == Piecewise(
        (0, Eq(y, 0)),
        (-2 * sqrt(x) * cos(sqrt(x) * y) / y + 2 * sin(sqrt(x) * y) / y**2,
         True))
    y = Symbol('y', positive=True)
    assert heurisch_wrapper(sin(y*sqrt(x)), x) == 2/y**2*sin(y*sqrt(x)) - \
        2*sqrt(x)*cos(y*sqrt(x))/y
Пример #10
0
def test_pmint_WrightOmega():
    def omega(x):
        return LambertW(exp(x))

    f = (1 + omega(x) * (2 + cos(omega(x)) * (x + omega(x))))/(1 + omega(x))/(x + omega(x))
    g = log(x + LambertW(exp(x))) + sin(LambertW(exp(x)))

    assert heurisch(f, x) == g
Пример #11
0
def test_pmint_logexp():
    f = (1 + x + x*exp(x))*(x + log(x) + exp(x) - 1)/(x + log(x) + exp(x))**2/x
    g = log(x**2 + 2*x*exp(x) + 2*x*log(x) + exp(2*x) + 2*exp(x)*log(x) + log(x)**2)/2 + 1/(x + exp(x) + log(x))

    # TODO: Optimal solution is g = 1/(x + log(x) + exp(x)) + log(x + log(x) + exp(x)),
    # but Diofant requires a lot of guidance to properly simplify heurisch() output.

    assert ratsimp(heurisch(f, x)) == g
Пример #12
0
def test_pmint_WrightOmega():
    def omega(x):
        return LambertW(exp(x))

    f = (1 + omega(x) * (2 + cos(omega(x)) *
                         (x + omega(x)))) / (1 + omega(x)) / (x + omega(x))
    g = log(x + LambertW(exp(x))) + sin(LambertW(exp(x)))

    assert heurisch(f, x) == g
Пример #13
0
def test_pmint_logexp():
    f = (1 + x + x * exp(x)) * (x + log(x) + exp(x) - 1) / (x + log(x) +
                                                            exp(x))**2 / x
    g = log(x**2 + 2 * x * exp(x) + 2 * x * log(x) + exp(2 * x) +
            2 * exp(x) * log(x) + log(x)**2) / 2 + 1 / (x + exp(x) + log(x))

    # TODO: Optimal solution is g = 1/(x + log(x) + exp(x)) + log(x + log(x) + exp(x)),
    # but Diofant requires a lot of guidance to properly simplify heurisch() output.

    assert ratsimp(heurisch(f, x)) == g
Пример #14
0
def test_pmint_rat():
    # TODO: heurisch() is off by a constant: -3/4. Possibly different permutation
    # would give the optimal result?

    def drop_const(expr, x):
        if expr.is_Add:
            return Add(*[ arg for arg in expr.args if arg.has(x) ])
        else:
            return expr

    f = (x**7 - 24*x**4 - 4*x**2 + 8*x - 8)/(x**8 + 6*x**6 + 12*x**4 + 8*x**2)
    g = (4 + 8*x**2 + 6*x + 3*x**3)/(x**5 + 4*x**3 + 4*x) + log(x)

    assert drop_const(ratsimp(heurisch(f, x)), x) == g
Пример #15
0
def test_heurisch_hacking():
    assert (heurisch(sqrt(1 + 7*x**2), x, hints=[]) ==
            x*sqrt(1 + 7*x**2)/2 + sqrt(7)*asinh(sqrt(7)*x)/14)
    assert (heurisch(sqrt(1 - 7*x**2), x, hints=[]) ==
            x*sqrt(1 - 7*x**2)/2 + sqrt(7)*asin(sqrt(7)*x)/14)

    assert (heurisch(1/sqrt(1 + 7*x**2), x, hints=[]) ==
            sqrt(7)*asinh(sqrt(7)*x)/7)
    assert (heurisch(1/sqrt(1 - 7*x**2), x, hints=[]) ==
            sqrt(7)*asin(sqrt(7)*x)/7)

    assert (heurisch(exp(-7*x**2), x, hints=[]) == sqrt(7*pi)*erf(sqrt(7)*x)/14)

    assert heurisch(1/sqrt(9 - 4*x**2), x, hints=[]) == asin(2*x/3)/2
    assert heurisch(1/sqrt(9 + 4*x**2), x, hints=[]) == asinh(2*x/3)/2

    assert heurisch(li(x), x, hints=[]) == x*li(x) - Ei(2*log(x))
Пример #16
0
def test_pmint_rat():
    # TODO: heurisch() is off by a constant: -3/4. Possibly different permutation
    # would give the optimal result?

    def drop_const(expr, x):
        if expr.is_Add:
            return Add(*[arg for arg in expr.args if arg.has(x)])
        else:
            return expr

    f = (x**7 - 24 * x**4 - 4 * x**2 + 8 * x - 8) / (x**8 + 6 * x**6 +
                                                     12 * x**4 + 8 * x**2)
    g = (4 + 8 * x**2 + 6 * x + 3 * x**3) / (x**5 + 4 * x**3 + 4 * x) + log(x)

    assert drop_const(ratsimp(heurisch(f, x)), x) == g
Пример #17
0
def test_heurisch_fractions():
    assert heurisch(1/x, x) == log(x)
    assert heurisch(1/(2 + x), x) == log(x + 2)
    assert heurisch(1/(x + sin(y)), x) == log(x + sin(y))

    # Up to a constant, where C = 5*pi*I/12, Mathematica gives identical
    # result in the first case. The difference is because diofant changes
    # signs of expressions without any care.
    # XXX ^ ^ ^ is this still correct?
    assert heurisch(5*x**5/(
        2*x**6 - 5), x) in [5*log(2*x**6 - 5) / 12, 5*log(-2*x**6 + 5) / 12]
    assert heurisch(5*x**5/(2*x**6 + 5), x) == 5*log(2*x**6 + 5) / 12

    assert heurisch(1/x**2, x) == -1/x
    assert heurisch(-1/x**5, x) == 1/(4*x**4)
Пример #18
0
def test_heurisch_fractions():
    assert heurisch(1 / x, x) == log(x)
    assert heurisch(1 / (2 + x), x) == log(x + 2)
    assert heurisch(1 / (x + sin(y)), x) == log(x + sin(y))

    # Up to a constant, where C = 5*pi*I/12, Mathematica gives identical
    # result in the first case. The difference is because diofant changes
    # signs of expressions without any care.
    # XXX ^ ^ ^ is this still correct?
    assert heurisch(5 * x**5 / (2 * x**6 - 5), x) in [
        5 * log(2 * x**6 - 5) / 12, 5 * log(-2 * x**6 + 5) / 12
    ]
    assert heurisch(5 * x**5 / (2 * x**6 + 5), x) == 5 * log(2 * x**6 + 5) / 12

    assert heurisch(1 / x**2, x) == -1 / x
    assert heurisch(-1 / x**5, x) == 1 / (4 * x**4)
Пример #19
0
def test_heurisch_hacking():
    assert (heurisch(sqrt(1 + 7 * x**2), x,
                     hints=[]) == x * sqrt(1 + 7 * x**2) / 2 +
            sqrt(7) * asinh(sqrt(7) * x) / 14)
    assert (heurisch(sqrt(1 - 7 * x**2), x,
                     hints=[]) == x * sqrt(1 - 7 * x**2) / 2 +
            sqrt(7) * asin(sqrt(7) * x) / 14)

    assert (heurisch(1 / sqrt(1 + 7 * x**2), x,
                     hints=[]) == sqrt(7) * asinh(sqrt(7) * x) / 7)
    assert (heurisch(1 / sqrt(1 - 7 * x**2), x,
                     hints=[]) == sqrt(7) * asin(sqrt(7) * x) / 7)

    assert (heurisch(exp(-7 * x**2), x,
                     hints=[]) == sqrt(7 * pi) * erf(sqrt(7) * x) / 14)

    assert heurisch(1 / sqrt(9 - 4 * x**2), x, hints=[]) == asin(2 * x / 3) / 2
    assert heurisch(1 / sqrt(9 + 4 * x**2), x,
                    hints=[]) == asinh(2 * x / 3) / 2

    assert heurisch(li(x), x, hints=[]) == x * li(x) - Ei(2 * log(x))
Пример #20
0
def test_heurisch_hacking():
    assert heurisch(sqrt(1 + 7*x**2), x, hints=[]) == \
        x*sqrt(1 + 7*x**2)/2 + sqrt(7)*asinh(sqrt(7)*x)/14
    assert heurisch(sqrt(1 - 7*x**2), x, hints=[]) == \
        x*sqrt(1 - 7*x**2)/2 + sqrt(7)*asin(sqrt(7)*x)/14

    assert heurisch(1/sqrt(1 + 7*x**2), x, hints=[]) == \
        sqrt(7)*asinh(sqrt(7)*x)/7
    assert heurisch(1/sqrt(1 - 7*x**2), x, hints=[]) == \
        sqrt(7)*asin(sqrt(7)*x)/7

    assert heurisch(exp(-7*x**2), x, hints=[]) == \
        sqrt(7*pi)*erf(sqrt(7)*x)/14

    assert heurisch(1/sqrt(9 - 4*x**2), x, hints=[]) == \
        asin(2*x/3)/2

    assert heurisch(1/sqrt(9 + 4*x**2), x, hints=[]) == \
        asinh(2*x/3)/2
Пример #21
0
def test_heurisch_special():
    assert heurisch(erf(x), x) == x*erf(x) + exp(-x**2)/sqrt(pi)
    assert heurisch(exp(-x**2)*erf(x), x) == sqrt(pi)*erf(x)**2 / 4
Пример #22
0
def test_heurisch_special():
    assert heurisch(erf(x), x) == x * erf(x) + exp(-x**2) / sqrt(pi)
    assert heurisch(exp(-x**2) * erf(x), x) == sqrt(pi) * erf(x)**2 / 4
Пример #23
0
def test_heurisch_symbolic_coeffs():
    assert heurisch(1 / (x + y), x) == log(x + y)
    assert heurisch(1 / (x + sqrt(2)), x) == log(x + sqrt(2))
    assert simplify(diff(heurisch(log(x + y + z), y), y)) == log(x + y + z)
Пример #24
0
    def _eval_integral(self,
                       f,
                       x,
                       meijerg=None,
                       risch=None,
                       conds='piecewise'):
        """
        Calculate the anti-derivative to the function f(x).

        The following algorithms are applied (roughly in this order):

        1. Simple heuristics (based on pattern matching and integral table):

           - most frequently used functions (e.g. polynomials, products of trig functions)

        2. Integration of rational functions:

           - A complete algorithm for integrating rational functions is
             implemented (the Lazard-Rioboo-Trager algorithm).  The algorithm
             also uses the partial fraction decomposition algorithm
             implemented in apart() as a preprocessor to make this process
             faster.  Note that the integral of a rational function is always
             elementary, but in general, it may include a RootSum.

        3. Full Risch algorithm:

           - The Risch algorithm is a complete decision
             procedure for integrating elementary functions, which means that
             given any elementary function, it will either compute an
             elementary antiderivative, or else prove that none exists.
             Currently, part of transcendental case is implemented, meaning
             elementary integrals containing exponentials, logarithms, and
             (soon!) trigonometric functions can be computed.  The algebraic
             case, e.g., functions containing roots, is much more difficult
             and is not implemented yet.

           - If the routine fails (because the integrand is not elementary, or
             because a case is not implemented yet), it continues on to the
             next algorithms below.  If the routine proves that the integrals
             is nonelementary, it still moves on to the algorithms below,
             because we might be able to find a closed-form solution in terms
             of special functions.  If risch=True, however, it will stop here.

        4. The Meijer G-Function algorithm:

           - This algorithm works by first rewriting the integrand in terms of
             very general Meijer G-Function (meijerg in Diofant), integrating
             it, and then rewriting the result back, if possible.  This
             algorithm is particularly powerful for definite integrals (which
             is actually part of a different method of Integral), since it can
             compute closed-form solutions of definite integrals even when no
             closed-form indefinite integral exists.  But it also is capable
             of computing many indefinite integrals as well.

           - Another advantage of this method is that it can use some results
             about the Meijer G-Function to give a result in terms of a
             Piecewise expression, which allows to express conditionally
             convergent integrals.

           - Setting meijerg=True will cause integrate() to use only this
             method.

        5. The Heuristic Risch algorithm:

           - This is a heuristic version of the Risch algorithm, meaning that
             it is not deterministic.  This is tried as a last resort because
             it can be very slow.  It is still used because not enough of the
             full Risch algorithm is implemented, so that there are still some
             integrals that can only be computed using this method.  The goal
             is to implement enough of the Risch and Meijer G-function methods
             so that this can be deleted.
        """
        from diofant.integrals.deltafunctions import deltaintegrate
        from diofant.integrals.heurisch import heurisch, heurisch_wrapper
        from diofant.integrals.rationaltools import ratint
        from diofant.integrals.risch import risch_integrate

        if risch:
            try:
                return risch_integrate(f, x, conds=conds)
            except NotImplementedError:
                return

        # if it is a poly(x) then let the polynomial integrate itself (fast)
        #
        # It is important to make this check first, otherwise the other code
        # will return a diofant expression instead of a Polynomial.
        #
        # see Polynomial for details.
        if isinstance(f, Poly) and not meijerg:
            return f.integrate(x)

        # Piecewise antiderivatives need to call special integrate.
        if f.func is Piecewise:
            return f._eval_integral(x)

        # let's cut it short if `f` does not depend on `x`
        if not f.has(x):
            return f * x

        # try to convert to poly(x) and then integrate if successful (fast)
        poly = f.as_poly(x)

        if poly is not None and not meijerg:
            return poly.integrate().as_expr()

        if risch is not False:
            try:
                result, i = risch_integrate(f,
                                            x,
                                            separate_integral=True,
                                            conds=conds)
            except NotImplementedError:
                pass
            else:
                if i:
                    # There was a nonelementary integral. Try integrating it.
                    return result + i.doit(risch=False)
                else:
                    return result

        # since Integral(f=g1+g2+...) == Integral(g1) + Integral(g2) + ...
        # we are going to handle Add terms separately,
        # if `f` is not Add -- we only have one term

        # Note that in general, this is a bad idea, because Integral(g1) +
        # Integral(g2) might not be computable, even if Integral(g1 + g2) is.
        # For example, Integral(x**x + x**x*log(x)).  But many heuristics only
        # work term-wise.  So we compute this step last, after trying
        # risch_integrate.  We also try risch_integrate again in this loop,
        # because maybe the integral is a sum of an elementary part and a
        # nonelementary part (like erf(x) + exp(x)).  risch_integrate() is
        # quite fast, so this is acceptable.
        parts = []
        args = Add.make_args(f)
        for g in args:
            coeff, g = g.as_independent(x)

            # g(x) = const
            if g is S.One and not meijerg:
                parts.append(coeff * x)
                continue

            # g(x) = expr + O(x**n)
            order_term = g.getO()

            if order_term is not None:
                h = self._eval_integral(g.removeO(), x)

                if h is not None:
                    parts.append(coeff *
                                 (h + self.func(order_term, *self.limits)))
                    continue

                # NOTE: if there is O(x**n) and we fail to integrate then there is
                # no point in trying other methods because they will fail anyway.
                return

            #               c
            # g(x) = (a*x+b)
            if g.is_Pow and not g.exp.has(x) and not meijerg:
                a = Wild('a', exclude=[x])
                b = Wild('b', exclude=[x])

                M = g.base.match(a * x + b)

                if M is not None:
                    if g.exp == -1:
                        h = log(g.base)
                    elif conds != 'piecewise':
                        h = g.base**(g.exp + 1) / (g.exp + 1)
                    else:
                        h1 = log(g.base)
                        h2 = g.base**(g.exp + 1) / (g.exp + 1)
                        h = Piecewise((h1, Eq(g.exp, -1)), (h2, True))

                    parts.append(coeff * h / M[a])
                    continue

            #        poly(x)
            # g(x) = -------
            #        poly(x)
            if g.is_rational_function(x) and not meijerg:
                parts.append(coeff * ratint(g, x))
                continue

            if not meijerg:
                # g(x) = Mul(trig)
                h = trigintegrate(g, x, conds=conds)
                if h is not None:
                    parts.append(coeff * h)
                    continue

                # g(x) has at least a DiracDelta term
                h = deltaintegrate(g, x)
                if h is not None:
                    parts.append(coeff * h)
                    continue

                # Try risch again.
                if risch is not False:
                    try:
                        h, i = risch_integrate(g,
                                               x,
                                               separate_integral=True,
                                               conds=conds)
                    except NotImplementedError:
                        h = None
                    else:
                        if i:
                            h = h + i.doit(risch=False)

                        parts.append(coeff * h)
                        continue

                # fall back to heurisch
                try:
                    if conds == 'piecewise':
                        h = heurisch_wrapper(g, x, hints=[])
                    else:
                        h = heurisch(g, x, hints=[])
                except PolynomialError:
                    # XXX: this exception means there is a bug in the
                    # implementation of heuristic Risch integration
                    # algorithm.
                    h = None
            else:
                h = None

            if meijerg is not False and h is None:
                # rewrite using G functions
                try:
                    h = meijerint_indefinite(g, x)
                except NotImplementedError:
                    from diofant.integrals.meijerint import _debug
                    _debug('NotImplementedError from meijerint_definite')
                    res = None
                if h is not None:
                    parts.append(coeff * h)
                    continue

            # if we failed maybe it was because we had
            # a product that could have been expanded,
            # so let's try an expansion of the whole
            # thing before giving up; we don't try this
            # at the outset because there are things
            # that cannot be solved unless they are
            # NOT expanded e.g., x**x*(1+log(x)). There
            # should probably be a checker somewhere in this
            # routine to look for such cases and try to do
            # collection on the expressions if they are already
            # in an expanded form
            if not h and len(args) == 1:
                f = f.expand(mul=True, deep=False)
                if f.is_Add:
                    # Note: risch will be identical on the expanded
                    # expression, but maybe it will be able to pick out parts,
                    # like x*(exp(x) + erf(x)).
                    return self._eval_integral(f,
                                               x,
                                               meijerg=meijerg,
                                               risch=risch,
                                               conds=conds)

            if h is not None:
                parts.append(coeff * h)
            else:
                return

        return Add(*parts)
Пример #25
0
def test_pmint_erf():
    f = exp(-x**2)*erf(x)/(erf(x)**3 - erf(x)**2 - erf(x) + 1)
    g = sqrt(pi)*log(erf(x) - 1)/8 - sqrt(pi)*log(erf(x) + 1)/8 - sqrt(pi)/(4*erf(x) - 4)

    assert ratsimp(heurisch(f, x)) == g
Пример #26
0
def test_pmint_trig():
    f = (x - tan(x)) / tan(x)**2 + tan(x)
    g = -x**2 / 2 - x / tan(x) + log(tan(x)**2 + 1) / 2

    assert heurisch(f, x) == g
Пример #27
0
def test_pmint_erf():
    f = exp(-x**2) * erf(x) / (erf(x)**3 - erf(x)**2 - erf(x) + 1)
    g = sqrt(pi) * log(erf(x) - 1) / 8 - sqrt(pi) * log(erf(x) + 1) / 8 - sqrt(
        pi) / (4 * erf(x) - 4)

    assert ratsimp(heurisch(f, x)) == g
Пример #28
0
def test_heurisch_function():
    assert heurisch(f(x), x) is None
Пример #29
0
def test_sympyissue_3609():
    assert heurisch(1/(x * (1 + log(x)**2)), x) == I*log(log(x) + I)/2 - \
        I*log(log(x) - I)/2
Пример #30
0
def test_heurisch_symbolic_coeffs():
    assert heurisch(1/(x + y), x) == log(x + y)
    assert heurisch(1/(x + sqrt(2)), x) == log(x + sqrt(2))
    assert simplify(diff(heurisch(log(x + y + z), y), y)) == log(x + y + z)
Пример #31
0
def test_pmint_LambertW():
    f = LambertW(x)
    g = x*LambertW(x) - x + x/LambertW(x)

    assert heurisch(f, x) == g
Пример #32
0
def test_RR():
    # Make sure the algorithm does the right thing if the ring is RR. See
    # issue sympy/sympy#8685.
    assert heurisch(sqrt(1 + 0.25*x**2), x, hints=[]) == \
        0.5*x*sqrt(0.25*x**2 + 1) + 1.0*asinh(0.5*x)
Пример #33
0
def test_heurisch_function():
    assert heurisch(f(x), x) is None
Пример #34
0
def test_sympyissue_3609():
    assert heurisch(1/(x * (1 + log(x)**2)), x) == I*log(log(x) + I)/2 - \
        I*log(log(x) - I)/2
Пример #35
0
def test_atom_bug():
    from diofant import meijerg
    from diofant.integrals.heurisch import heurisch
    assert heurisch(meijerg([], [], [1], [], x), x) is None
Пример #36
0
def test_heurisch_trigonometric():
    assert heurisch(sin(x), x) == -cos(x)
    assert heurisch(pi*sin(x) + 1, x) == x - pi*cos(x)

    assert heurisch(cos(x), x) == sin(x)
    assert heurisch(tan(x), x) in [
        log(1 + tan(x)**2)/2,
        log(tan(x) + I) + I*x,
        log(tan(x) - I) - I*x,
    ]

    assert heurisch(sin(x)*sin(y), x) == -cos(x)*sin(y)
    assert heurisch(sin(x)*sin(y), y) == -cos(y)*sin(x)

    # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test
    assert heurisch(sin(x)*cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2]
    assert heurisch(cos(x)/sin(x), x) == log(sin(x))

    assert heurisch(x*sin(7*x), x) == sin(7*x) / 49 - x*cos(7*x) / 7
    assert heurisch(1/pi/4 * x**2*cos(x), x) == 1/pi/4*(x**2*sin(x) -
                                                        2*sin(x) + 2*x*cos(x))

    assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \
        + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)
Пример #37
0
def test_heurisch_trigonometric():
    assert heurisch(sin(x), x) == -cos(x)
    assert heurisch(pi * sin(x) + 1, x) == x - pi * cos(x)

    assert heurisch(cos(x), x) == sin(x)
    assert heurisch(tan(x), x) in [
        log(1 + tan(x)**2) / 2,
        log(tan(x) + I) + I * x,
        log(tan(x) - I) - I * x,
    ]

    assert heurisch(sin(x) * sin(y), x) == -cos(x) * sin(y)
    assert heurisch(sin(x) * sin(y), y) == -cos(y) * sin(x)

    assert heurisch(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2]
    assert heurisch(cos(x) / sin(x), x) == log(sin(x))

    assert heurisch(x * sin(7 * x), x) == sin(7 * x) / 49 - x * cos(7 * x) / 7
    assert heurisch(
        1 / pi / 4 * x**2 * cos(x),
        x) == 1 / pi / 4 * (x**2 * sin(x) - 2 * sin(x) + 2 * x * cos(x))

    assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \
        + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)

    assert heurisch(1 / sin(1 / x) / x**2, x) == -log(tan(1 / x / 2))
Пример #38
0
def test_heurisch_mixed():
    assert heurisch(sin(x)*exp(x), x) == exp(x)*sin(x)/2 - exp(x)*cos(x)/2
Пример #39
0
def test_heurisch_hacking():
    assert (heurisch(sqrt(1 + 7 * x**2), x,
                     hints=[]) == x * sqrt(1 + 7 * x**2) / 2 +
            sqrt(7) * asinh(sqrt(7) * x) / 14)
    assert (heurisch(sqrt(1 - 7 * x**2), x,
                     hints=[]) == x * sqrt(1 - 7 * x**2) / 2 +
            sqrt(7) * asin(sqrt(7) * x) / 14)

    assert heurisch(sqrt(y * x**2 - 1), x, hints=[]) is None

    assert (heurisch(1 / sqrt(1 + 7 * x**2), x,
                     hints=[]) == sqrt(7) * asinh(sqrt(7) * x) / 7)
    assert (heurisch(1 / sqrt(1 - 7 * x**2), x,
                     hints=[]) == sqrt(7) * asin(sqrt(7) * x) / 7)

    assert heurisch(exp(-7 * x**2), x,
                    hints=[]) == sqrt(7 * pi) * erf(sqrt(7) * x) / 14
    assert heurisch(exp(2 * x**2), x,
                    hints=[]) == sqrt(2) * sqrt(pi) * erfi(sqrt(2) * x) / 4

    assert (heurisch(exp(2 * x**2 - 3 * x), x,
                     hints=[]) == sqrt(2) * sqrt(pi) *
            erfi(sqrt(2) * x - 3 * sqrt(2) / 4) / (4 * E**Rational(9, 8)))

    assert heurisch(1 / sqrt(9 - 4 * x**2), x, hints=[]) == asin(2 * x / 3) / 2
    assert heurisch(1 / sqrt(9 + 4 * x**2), x,
                    hints=[]) == asinh(2 * x / 3) / 2

    assert heurisch(li(x), x, hints=[]) == x * li(x) - Ei(2 * log(x))
    assert heurisch(li(log(x)), x, hints=[]) is None

    assert (heurisch(sqrt(1 + x), x,
                     hints=[x, sqrt(1 + x)]) == 2 * x * sqrt(x + 1) / 3 +
            2 * sqrt(x + 1) / 3)
Пример #40
0
def test_heurisch_mixed():
    assert heurisch(sin(x) * exp(x),
                    x) == exp(x) * sin(x) / 2 - exp(x) * cos(x) / 2
Пример #41
0
def test_pmint_LambertW():
    f = LambertW(x)
    g = x * LambertW(x) - x + x / LambertW(x)

    assert heurisch(f, x) == g
Пример #42
0
def test_atom_bug():
    assert heurisch(meijerg([], [], [1], [], x), x) is None
Пример #43
0
def test_RR():
    # Make sure the algorithm does the right thing if the ring is RR. See
    # issue sympy/sympy#8685.
    assert heurisch(sqrt(1 + 0.25*x**2), x, hints=[]) == \
        0.5*x*sqrt(0.25*x**2 + 1) + 1.0*asinh(0.5*x)
Пример #44
0
def test_pmint_trig():
    f = (x - tan(x)) / tan(x)**2 + tan(x)
    g = -x**2/2 - x/tan(x) + log(tan(x)**2 + 1)/2

    assert heurisch(f, x) == g
Пример #45
0
def test_heurisch_polynomials():
    assert heurisch(1, x) == x
    assert heurisch(x, x) == x**2 / 2
    assert heurisch(x**17, x) == x**18 / 18
Пример #46
0
def test_heurisch_polynomials():
    assert heurisch(1, x) == x
    assert heurisch(x, x) == x**2/2
    assert heurisch(x**17, x) == x**18/18
Пример #47
0
def test_heurisch_log():
    assert heurisch(log(x), x) == x * log(x) - x
    assert heurisch(log(3 * x), x) == -x + x * log(3) + x * log(x)
    assert heurisch(log(x**2),
                    x) in [x * log(x**2) - 2 * x, 2 * x * log(x) - 2 * x]
Пример #48
0
def test_heurisch_log():
    assert heurisch(log(x), x) == x*log(x) - x
    assert heurisch(log(3*x), x) == -x + x*log(3) + x*log(x)
    assert heurisch(log(x**2), x) in [x*log(x**2) - 2*x, 2*x*log(x) - 2*x]
Пример #49
0
def test_heurisch_trigonometric():
    assert heurisch(sin(x), x) == -cos(x)
    assert heurisch(pi * sin(x) + 1, x) == x - pi * cos(x)

    assert heurisch(cos(x), x) == sin(x)
    assert heurisch(tan(x), x) in [
        log(1 + tan(x)**2) / 2,
        log(tan(x) + I) + I * x,
        log(tan(x) - I) - I * x,
    ]

    assert heurisch(sin(x) * sin(y), x) == -cos(x) * sin(y)
    assert heurisch(sin(x) * sin(y), y) == -cos(y) * sin(x)

    # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test
    assert heurisch(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2]
    assert heurisch(cos(x) / sin(x), x) == log(sin(x))

    assert heurisch(x * sin(7 * x), x) == sin(7 * x) / 49 - x * cos(7 * x) / 7
    assert heurisch(
        1 / pi / 4 * x**2 * cos(x),
        x) == 1 / pi / 4 * (x**2 * sin(x) - 2 * sin(x) + 2 * x * cos(x))

    assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \
        + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)
Пример #50
0
def test_atom_bug():
    assert heurisch(meijerg([], [], [1], [], x), x) is None