570 | | def integral(x, *args, **kwds): |
571 | | """ |
572 | | Returns an indefinite or definite integral of an object x. |
573 | | |
574 | | First call x.integral() and if that fails make an object and |
575 | | integrate it using Maxima, maple, etc, as specified by algorithm. |
576 | | |
577 | | For symbolic expression calls |
578 | | :func:`sage.calculus.calculus.integral` - see this function for |
579 | | available options. |
580 | | |
581 | | EXAMPLES:: |
582 | | |
583 | | sage: f = cyclotomic_polynomial(10) |
584 | | sage: integral(f) |
585 | | 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x |
586 | | |
587 | | :: |
588 | | |
589 | | sage: integral(sin(x),x) |
590 | | -cos(x) |
591 | | |
592 | | :: |
593 | | |
594 | | sage: y = var('y') |
595 | | sage: integral(sin(x),y) |
596 | | y*sin(x) |
597 | | |
598 | | :: |
599 | | |
600 | | sage: integral(sin(x), x, 0, pi/2) |
601 | | 1 |
602 | | sage: sin(x).integral(x, 0,pi/2) |
603 | | 1 |
604 | | sage: integral(exp(-x), (x, 1, oo)) |
605 | | e^(-1) |
606 | | |
607 | | Numerical approximation:: |
608 | | |
609 | | sage: h = integral(tan(x)/x, (x, 1, pi/3)); h |
610 | | integrate(tan(x)/x, x, 1, 1/3*pi) |
611 | | sage: h.n() |
612 | | 0.07571599101... |
613 | | |
614 | | Specific algorithm can be used for integration:: |
615 | | |
616 | | sage: integral(sin(x)^2, x, algorithm='maxima') |
617 | | 1/2*x - 1/4*sin(2*x) |
618 | | sage: integral(sin(x)^2, x, algorithm='sympy') |
619 | | -1/2*cos(x)*sin(x) + 1/2*x |
620 | | |
621 | | TESTS: |
622 | | |
623 | | A symbolic integral from :trac:`11445` that was incorrect in |
624 | | earlier versions of Maxima:: |
625 | | |
626 | | sage: f = abs(x - 1) + abs(x + 1) - 2*abs(x) |
627 | | sage: integrate(f, (x, -Infinity, Infinity)) |
628 | | 2 |
629 | | |
630 | | Another symbolic integral, from :trac:`11238`, that used to return |
631 | | zero incorrectly; with Maxima 5.26.0 one gets |
632 | | ``1/2*sqrt(pi)*e^(1/4)``, whereas with 5.29.1, and even more so |
633 | | with 5.33.0, the expression is less pleasant, but still has the |
634 | | same value. Unfortunately, the computation takes a very long time |
635 | | with the default settings, so we temporarily use the Maxima |
636 | | setting ``domain: real``:: |
637 | | |
638 | | sage: sage.calculus.calculus.maxima('domain: real') |
639 | | real |
640 | | sage: f = exp(-x) * sinh(sqrt(x)) |
641 | | sage: t = integrate(f, x, 0, Infinity); t # long time |
642 | | 1/4*sqrt(pi)*(erf(1) - 1)*e^(1/4) - 1/4*(sqrt(pi)*(erf(1) - 1) - sqrt(pi) + 2*e^(-1) - 2)*e^(1/4) + 1/4*sqrt(pi)*e^(1/4) - 1/2*e^(1/4) + 1/2*e^(-3/4) |
643 | | sage: t.canonicalize_radical() # long time |
644 | | 1/2*sqrt(pi)*e^(1/4) |
645 | | sage: sage.calculus.calculus.maxima('domain: complex') |
646 | | complex |
647 | | |
648 | | An integral which used to return -1 before maxima 5.28. See :trac:`12842`:: |
649 | | |
650 | | sage: f = e^(-2*x)/sqrt(1-e^(-2*x)) |
651 | | sage: integrate(f, x, 0, infinity) |
652 | | 1 |
653 | | |
654 | | This integral would cause a stack overflow in earlier versions of |
655 | | Maxima, crashing sage. See :trac:`12377`. We don't care about the |
656 | | result here, just that the computation completes successfully:: |
657 | | |
658 | | sage: y = (x^2)*exp(x) / (1 + exp(x))^2 |
659 | | sage: _ = integrate(y, x, -1000, 1000) |
660 | | |
661 | | """ |
662 | | if hasattr(x, 'integral'): |
663 | | return x.integral(*args, **kwds) |
664 | | else: |
665 | | from sage.symbolic.ring import SR |
666 | | return SR(x).integral(*args, **kwds) |
667 | | |
668 | | integrate = integral |
669 | | |
670 | | |