# Ticket #3732: testintfailscases.py

File testintfailscases.py, 3.4 KB (added by was, 13 years ago)
Line
1"""
2# Some specific documented cases of failing integrals.
3# Tested on sage-3.0.4.rc0
4    FAILING INTEGRATION TESTS:
5    sage: var('a,b,q')
6    (a, b, q)
7
8# The first example of a failing integral involves
9# two variables, q and x, where the integral is
10# evaluated with respect to x.
11# First, the indefinite form -- no problem here
12    sage: integrate(1/sqrt(x-q), x)
13    2*sqrt(x - q)
14
15# Now, introduce upper and lower bounds -- Sage will
16# throw an error: Is  q-2  positive, negative, or zero?
17    sage: integrate(1/sqrt(x-q), x, 1, 2)
18    2*sqrt(2 - q) - 2*sqrt(1 - q)
19
20# Note that except for when the upper and lower bounds
21# are equal, we will receive an error every time.
22# The following two integrals return errors of
23# 'Is  q-1000  positive, negative, or zero?' and
24# 'Is  b-a  positive, negative, or zero?' respectively
25    sage: integrate(1/sqrt(x-q), x, 1, 1000)
26    2*sqrt(1000 - q) - 2*sqrt(1 - q)
27    sage: integrate(1/sqrt(x-q), x, a, b)
28    2*sqrt(b - q) - 2*sqrt(a - q)
29
30# As a quick fix to this problem, we can take the
31# general form and subtract f.subs(x=b) from
32# f.subs(x=a) ourselves:
33    sage: f = integrate(1/sqrt(x-q), x)
34    sage: f.subs(x=b)-f.subs(x=a)
35    2*sqrt(b - q) - 2*sqrt(a - q)
36
37# And voila! We have the correct expression without
38# having used 'assume'. Similar errors will arise
39# not only when q is under a radical, but when it is
40# in the denominator or inside a log, an arc-function
41# or any other function whose domain is restricted.
42# Here are some examples:
43
44# 1/(x-q) returns Is  b-a  positive, negative, or zero?
45    sage: integrate(1/(x-q), x, a, b)
46    log(b - q) - log(a -q)
47
48# This will evaluate using subs(...)
49    sage: f = integrate(1/(x-q), x); f.subs(x=b)-f.subs(x=a)
50    log(b - q) - log(a - q)
51
52# log(x-q) from a to b returns the same error
53    sage: integrate(log(q-x), x, a, b)
54    (b - q)*log(q - b) + (q - a)*log(q - a) - b + a
55
56# Use subs(...) to evaluate it
57    sage: f = integrate(log(q-x), x); f.subs(x=b)-f.subs(x=a)
58    (b - q)*log(q - b) + (q - a)*log(q - a) - b + a
59
60# Currently (as of 3.0.4.rc0), the only way to evaluate
61# this indefinite integral using Sage is with an assume
62# statement, which is most likely due to its piecewise
63# nature. Without assume, we again get an error.
64    sage: integrate(1/(q-x^2), x).rational_simplify()
65    -log(-(sqrt(q) - x)/(x + sqrt(q)))/(2*sqrt(q))
66
67# If q > 0:
68    sage: assume(q > 0)
69    sage: integrate(1/(q-x^2), x).rational_simplify()
70    -log(-(sqrt(q) - x)/(x + sqrt(q)))/(2*sqrt(q))
71    sage: forget(q > 0)
72
73# If q < 0:
74    sage: assume(q < 0)
75    sage: integrate(1/(q-x^2), x)
76    -arctan(x/sqrt(-q))/sqrt(-q)
77    sage: forget(q < 0)
78
79# I won't demonstrate it here, but even with assume(q ...)
80# one must still use f.subs(x=b)-f.subs(x=a) to evaluate the
81# the definite integral. Next, a non-piecewise, arcsine integral.
83    arcsin(x/abs(q))
84
85# With bounds, the integration fails
86    sage: integrate(1/sqrt(q^2-x^2), x, a, b).radical_simplify()
87    arcsin(b/abs(q)) - arcsin(a/abs(q))
88
89# Using f.subs(...):
90    f = integrate(1/sqrt(q^2-x^2), x).radical_simplify(); f.subs(x=b)-f.subs(x=a)
91    arcsin(b/abs(q)) - arcsin(a/abs(q))
92
93# Finally, with q as an exponent, Sage will also fail to
94# integrate, returning Is  q+1  zero or nonzero? The only
95# way to make Sage evaluate the integral at this point is
96# via an assume statement, as in assume(q > 0) or assume(q < 0)
97    sage: integrate(x^q, x)
98    x^(q + 1)/(q + 1)
99"""