Ticket #9427: trac_9427-fricas-integrate.patch

File trac_9427-fricas-integrate.patch, 4.3 KB (added by whuss, 11 years ago)
  • sage/symbolic/integration/external.py

    # HG changeset patch
    # User Wilfried Huss <huss@finanz.math.tugraz.at>
    # Date 1278319493 -7200
    # Node ID 8969d5160bff2c7ff3c993c7dbee74ed0c966ca7
    # Parent  fbb071b96ba95a830c0a561a00ad78a977142b00
    Implement algorithm="fricas" for integration
    
    diff -r fbb071b96ba9 -r 8969d5160bff sage/symbolic/integration/external.py
    a b  
    7474        return ans
    7575    except TypeError:
    7676        raise ValueError, "Unable to parse: %s" % mexpr
     77
     78def fricas_integrator(expression, v, a=None, b=None):
     79    """
     80        sage: from sage.symbolic.integration.external import fricas_integrator  # optional -- requires fricas
     81        sage: fricas_integrator(sin(x), x)                                      # optional -- requires fricas
     82        -cos(x)
     83        sage: fricas_integrator(cos(x), x)                                      # optional -- requires fricas
     84        sin(x)
     85        sage: fricas_integrator(1/(x^2-2), x, 0, 1)                             # optional -- requires fricas
     86        1/4*(log(3*sqrt(2) - 4) - log(sqrt(2)))*sqrt(2)
     87        sage: fricas_integrator(1/(x^2+6), x, -oo, oo)                          # optional -- requires fricas
     88        1/6*pi*sqrt(6)
     89    """
     90    if not isinstance(expression, Expression):
     91        expression = SR(expression)
     92    if a is None:
     93        result = expression._fricas_().integrate(v)
     94    else:
     95        import sage.rings.infinity
     96        if a == sage.rings.infinity.PlusInfinity():
     97            a = "%plusInfinity"
     98        elif a == sage.rings.infinity.MinusInfinity():
     99            a = "%minusInfinity"
     100        if b == sage.rings.infinity.PlusInfinity():
     101            b = "%plusInfinity"
     102        elif b == sage.rings.infinity.MinusInfinity():
     103            b = "%minusInfinity"
     104
     105        result = expression._fricas_().integrate("%s=%s..%s" % (v, a, b))
     106    locals = dict([(str(v), v) for v in expression.variables()])
     107    if str(result) == "potentialPole":
     108        raise ValueError, "The integrand has a potential pole in the integration interval"
     109    parsed_result = result.unparsed_input_form()
     110    import sage.misc.sage_eval
     111    try:
     112        return sage.misc.sage_eval.sage_eval(parsed_result, locals=locals)
     113    except:
     114        raise ValueError, "Unable to parse: %s" % parsed_result
     115 No newline at end of file
  • sage/symbolic/integration/integral.py

    diff -r fbb071b96ba9 -r 8969d5160bff sage/symbolic/integration/integral.py
    a b  
    2828available_integrators['maxima'] = external.maxima_integrator
    2929available_integrators['sympy'] = external.sympy_integrator
    3030available_integrators['mathematica_free'] = external.mma_free_integrator
     31available_integrators['fricas'] = external.fricas_integrator
    3132
    3233######################################################
    3334#
     
    281282       - 'sympy' - use sympy (also in Sage)
    282283               
    283284       - 'mathematica_free' - use http://integrals.wolfram.com/
     285
     286       - 'fricas' - use fricas (the optional fricas spkg has to be installed)
    284287 
    285288     EXAMPLES::
    286289
     
    437440        sage: integral(e^(-x^2),(x, 0, 0.1))       
    438441        0.0562314580091*sqrt(pi)
    439442
     443    An example of an integral that fricas can integrate, but the default integrator cannot::
     444
     445        sage: f(x) = sqrt(x+sqrt(1+x^2))/x
     446        sage: integrate(f(x), x, algorithm="fricas")            # optional -- require fricas
     447        2*sqrt(x + sqrt(x^2 + 1)) + log(sqrt(x + sqrt(x^2 + 1)) - 1) - log(sqrt(x + sqrt(x^2 + 1)) + 1) - 2*arctan(sqrt(x + sqrt(x^2 + 1)))
     448
     449    The following definite integral is calculated wrong with the default integrator::
     450
     451        sage: f(x) = (x^4 - 3*x^2 + 6) / (x^6 - 5*x^4 + 5*x^2 + 4)
     452        sage: integrate(f(x), x, 1, 2)
     453        0
     454
     455    Both fricas and sympy give the correct result::
     456
     457        sage: integrate(f(x), x, 1, 2, algorithm = "fricas")    # optional -- requires fricas
     458        -1/2*pi + arctan(1/2) + arctan(2) + arctan(5) + arctan(8)
     459        sage: integrate(f(x), x, 1, 2, algorithm = "sympy")
     460        -1/2*pi + arctan(1/2) + arctan(2) + arctan(5) + arctan(8)
     461
    440462    ALIASES: integral() and integrate() are the same.
    441463
    442464    EXAMPLES: Here is example where we have to use assume::