Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/symbolic/integration/integral.py
8818 views
1
"""
2
Symbolic Integration
3
"""
4
5
##############################################################################
6
#
7
# Copyright (C) 2009 Golam Mortuza Hossain <[email protected]>
8
# Copyright (C) 2010 Burcin Erocal <[email protected]>
9
#
10
# Distributed under the terms of the GNU General Public License (GPL v2+)
11
# http://www.gnu.org/licenses/
12
#
13
##############################################################################
14
15
from sage.symbolic.ring import SR, is_SymbolicVariable
16
from sage.symbolic.function import BuiltinFunction, Function
17
18
##################################################################
19
# Table of available integration routines
20
##################################################################
21
22
# Add new integration routines to the dictionary below. This will make them
23
# accessible with the 'algorithm' keyword parameter of top level integrate().
24
available_integrators = {}
25
26
import sage.symbolic.integration.external as external
27
available_integrators['maxima'] = external.maxima_integrator
28
available_integrators['sympy'] = external.sympy_integrator
29
available_integrators['mathematica_free'] = external.mma_free_integrator
30
31
######################################################
32
#
33
# Class implementing symbolic integration
34
#
35
######################################################
36
37
class IndefiniteIntegral(BuiltinFunction):
38
def __init__(self):
39
"""
40
Class to represent an indefinite integral.
41
42
EXAMPLES::
43
44
sage: from sage.symbolic.integration.integral import indefinite_integral
45
sage: indefinite_integral(log(x), x) #indirect doctest
46
x*log(x) - x
47
sage: indefinite_integral(x^2, x)
48
1/3*x^3
49
sage: indefinite_integral(4*x*log(x), x)
50
2*x^2*log(x) - x^2
51
sage: indefinite_integral(exp(x), 2*x)
52
2*e^x
53
54
"""
55
# The automatic evaluation routine will try these integrators
56
# in the given order. This is an attribute of the class instead of
57
# a global variable in this module to enable customization by
58
# creating a subclasses which define a different set of integrators
59
self.integrators = [external.maxima_integrator]
60
61
BuiltinFunction.__init__(self, "integrate", nargs=2)
62
63
def _eval_(self, f, x):
64
"""
65
EXAMPLES::
66
67
sage: from sage.symbolic.integration.integral import indefinite_integral
68
sage: indefinite_integral(exp(x), x) # indirect doctest
69
e^x
70
sage: indefinite_integral(exp(x), x^2)
71
2*(x - 1)*e^x
72
"""
73
# Check for x
74
if not is_SymbolicVariable(x):
75
if len(x.variables()) == 1:
76
nx = x.variables()[0]
77
f = f*x.diff(nx)
78
x = nx
79
else:
80
return None
81
82
# we try all listed integration algorithms
83
for integrator in self.integrators:
84
res = integrator(f, x)
85
try:
86
return integrator(f, x)
87
except NotImplementedError:
88
pass
89
return None
90
91
def _tderivative_(self, f, x, diff_param=None):
92
"""
93
EXAMPLES::
94
95
sage: from sage.symbolic.integration.integral import indefinite_integral
96
sage: f = function('f'); a,b=var('a,b')
97
sage: h = indefinite_integral(f(x), x)
98
sage: h.diff(x) # indirect doctest
99
f(x)
100
sage: h.diff(a)
101
0
102
"""
103
if x.has(diff_param):
104
return f*x.derivative(diff_param)
105
else:
106
return f.derivative(diff_param).integral(x)
107
108
def _print_latex_(self, f, x):
109
"""
110
EXAMPLES::
111
112
sage: from sage.symbolic.integration.integral import indefinite_integral
113
sage: print_latex = indefinite_integral._print_latex_
114
sage: var('x,a,b')
115
(x, a, b)
116
sage: f = function('f')
117
sage: print_latex(f(x),x)
118
'\\int f\\left(x\\right)\\,{d x}'
119
"""
120
from sage.misc.latex import latex
121
if not is_SymbolicVariable(x):
122
dx_str = "{d \\left(%s\\right)}"%(latex(x))
123
else:
124
dx_str = "{d %s}"%(latex(x))
125
126
return "\\int %s\\,%s"%(latex(f), dx_str)
127
128
indefinite_integral = IndefiniteIntegral()
129
130
class DefiniteIntegral(BuiltinFunction):
131
def __init__(self):
132
"""
133
Symbolic function representing a definite integral.
134
135
EXAMPLES::
136
137
sage: from sage.symbolic.integration.integral import definite_integral
138
sage: definite_integral(sin(x),x,0,pi)
139
2
140
"""
141
# The automatic evaluation routine will try these integrators
142
# in the given order. This is an attribute of the class instead of
143
# a global variable in this module to enable customization by
144
# creating a subclasses which define a different set of integrators
145
self.integrators = [external.maxima_integrator]
146
147
BuiltinFunction.__init__(self, "integrate", nargs=4)
148
149
def _eval_(self, f, x, a, b):
150
"""
151
Returns the results of symbolic evaluation of the integral
152
153
EXAMPLES::
154
155
sage: from sage.symbolic.integration.integral import definite_integral
156
sage: definite_integral(exp(x),x,0,1) # indirect doctest
157
e - 1
158
"""
159
# Check for x
160
if not is_SymbolicVariable(x):
161
if len(x.variables()) == 1:
162
nx = x.variables()[0]
163
f = f*x.diff(nx)
164
x = nx
165
else:
166
return None
167
168
args = (f,x,a,b)
169
170
# we try all listed integration algorithms
171
for integrator in self.integrators:
172
try:
173
return integrator(*args)
174
except NotImplementedError:
175
pass
176
return None
177
178
def _evalf_(self, f, x, a, b, parent=None):
179
"""
180
Returns numerical approximation of the integral
181
182
EXAMPLES::
183
184
sage: from sage.symbolic.integration.integral import definite_integral
185
sage: h = definite_integral(sin(x)*log(x)/x^2, x, 1, 2); h
186
integrate(log(x)*sin(x)/x^2, x, 1, 2)
187
sage: h.n() # indirect doctest
188
0.14839875208053...
189
190
TESTS:
191
192
Check if #3863 is fixed::
193
194
sage: integrate(x^2.7 * e^(-2.4*x), x, 0, 3).n()
195
0.154572952320790
196
"""
197
from sage.gsl.integration import numerical_integral
198
# The gsl routine returns a tuple, which also contains the error.
199
# We only return the result.
200
return numerical_integral(f, a, b)[0]
201
202
def _tderivative_(self, f, x, a, b, diff_param=None):
203
"""
204
Returns derivative of symbolic integration
205
206
EXAMPLES::
207
208
sage: from sage.symbolic.integration.integral import definite_integral
209
sage: f = function('f'); a,b=var('a,b')
210
sage: h = definite_integral(f(x), x,a,b)
211
sage: h.diff(x) # indirect doctest
212
0
213
sage: h.diff(a)
214
-f(a)
215
sage: h.diff(b)
216
f(b)
217
"""
218
if not x.has(diff_param):
219
# integration variable != differentiation variable
220
ans = definite_integral(f.diff(diff_param), x, a, b)
221
else:
222
ans = SR(0)
223
return ans + f.subs(x==b)*b.diff(diff_param) \
224
- f.subs(x==a)*a.diff(diff_param)
225
226
def _print_latex_(self, f, x, a, b):
227
r"""
228
Returns LaTeX expression for integration of a symbolic function.
229
230
EXAMPLES::
231
232
sage: from sage.symbolic.integration.integral import definite_integral
233
sage: print_latex = definite_integral._print_latex_
234
sage: var('x,a,b')
235
(x, a, b)
236
sage: f = function('f')
237
sage: print_latex(f(x),x,0,1)
238
'\\int_{0}^{1} f\\left(x\\right)\\,{d x}'
239
sage: latex(integrate(1/(1+sqrt(x)),x,0,1))
240
\int_{0}^{1} \frac{1}{\sqrt{x} + 1}\,{d x}
241
"""
242
from sage.misc.latex import latex
243
if not is_SymbolicVariable(x):
244
dx_str = "{d \\left(%s\\right)}"%(latex(x))
245
else:
246
dx_str = "{d %s}"%(latex(x))
247
return "\\int_{%s}^{%s} %s\\,%s"%(latex(a), latex(b), latex(f), dx_str)
248
249
definite_integral = DefiniteIntegral()
250
251
252
def _normalize_integral_input(f, v=None, a=None, b=None):
253
r"""
254
Validate and return variable and endpoints for an integral.
255
256
INPUT:
257
258
- ``f`` -- an expression to integrate;
259
260
- ``v`` -- a variable of integration or a triple;
261
262
- ``a`` -- (optional) the left endpoint of integration;
263
264
- ``b`` -- (optional) the right endpoint of integration.
265
266
It is also possible to pass last three parameters in ``v`` as a triple.
267
268
OUPUT:
269
270
- a tuple of ``f``, ``v``, ``a``, and ``b``.
271
272
EXAMPLES::
273
274
sage: from sage.symbolic.integration.integral import \
275
... _normalize_integral_input
276
sage: _normalize_integral_input(x^2, x, 0, 3)
277
(x^2, x, 0, 3)
278
sage: _normalize_integral_input(x^2, [x, 0, 3], None, None)
279
(x^2, x, 0, 3)
280
sage: _normalize_integral_input(x^2, [0, 3], None, None)
281
doctest:...: DeprecationWarning:
282
Variable of integration should be specified explicitly.
283
See http://trac.sagemath.org/12438 for details.
284
(x^2, x, 0, 3)
285
sage: _normalize_integral_input(x^2, [x], None, None)
286
(x^2, x, None, None)
287
"""
288
if isinstance(v, (list, tuple)) and a is None and b is None:
289
if len(v) == 1: # bare variable in a tuple
290
v = v[0]
291
elif len(v) == 2: # endpoints only
292
a, b = v
293
v = None
294
elif len(v) == 3: # variable and endpoints
295
v, a, b = v
296
else:
297
raise ValueError("invalid input %s - please use variable, "
298
"with or without two endpoints" % repr(v))
299
elif b is None and a is not None:
300
# two arguments, must be endpoints
301
v, a, b = None, v, a
302
if v is None:
303
from sage.misc.superseded import deprecation
304
deprecation(12438, "Variable of integration should be specified explicitly.")
305
v = f.default_variable()
306
if isinstance(f, Function): # a bare function like sin
307
f = f(v)
308
if (a is None) ^ (b is None):
309
raise TypeError('only one endpoint was given!')
310
return f, v, a, b
311
312
def integrate(expression, v=None, a=None, b=None, algorithm=None):
313
r"""
314
Returns the indefinite integral with respect to the variable
315
`v`, ignoring the constant of integration. Or, if endpoints
316
`a` and `b` are specified, returns the definite
317
integral over the interval `[a, b]`.
318
319
If ``self`` has only one variable, then it returns the
320
integral with respect to that variable.
321
322
If definite integration fails, it could be still possible to
323
evaluate the definite integral using indefinite integration with
324
the Newton - Leibniz theorem (however, the user has to ensure that the
325
indefinite integral is continuous on the compact interval `[a,b]` and
326
this theorem can be applied).
327
328
INPUT:
329
330
- ``v`` - a variable or variable name. This can also be a tuple of
331
the variable (optional) and endpoints (i.e., ``(x,0,1)`` or ``(0,1)``).
332
333
- ``a`` - (optional) lower endpoint of definite integral
334
335
- ``b`` - (optional) upper endpoint of definite integral
336
337
- ``algorithm`` - (default: 'maxima') one of
338
339
- 'maxima' - use maxima (the default)
340
341
- 'sympy' - use sympy (also in Sage)
342
343
- 'mathematica_free' - use http://integrals.wolfram.com/
344
345
EXAMPLES::
346
347
sage: x = var('x')
348
sage: h = sin(x)/(cos(x))^2
349
sage: h.integral(x)
350
1/cos(x)
351
352
::
353
354
sage: f = x^2/(x+1)^3
355
sage: f.integral(x)
356
1/2*(4*x + 3)/(x^2 + 2*x + 1) + log(x + 1)
357
358
::
359
360
sage: f = x*cos(x^2)
361
sage: f.integral(x, 0, sqrt(pi))
362
0
363
sage: f.integral(x, a=-pi, b=pi)
364
0
365
366
::
367
368
sage: f(x) = sin(x)
369
sage: f.integral(x, 0, pi/2)
370
1
371
372
The variable is required, but the endpoints are optional::
373
374
sage: y=var('y')
375
sage: integral(sin(x), x)
376
-cos(x)
377
sage: integral(sin(x), y)
378
y*sin(x)
379
sage: integral(sin(x), x, pi, 2*pi)
380
-2
381
sage: integral(sin(x), y, pi, 2*pi)
382
pi*sin(x)
383
sage: integral(sin(x), (x, pi, 2*pi))
384
-2
385
sage: integral(sin(x), (y, pi, 2*pi))
386
pi*sin(x)
387
388
Constraints are sometimes needed::
389
390
sage: var('x, n')
391
(x, n)
392
sage: integral(x^n,x)
393
Traceback (most recent call last):
394
...
395
ValueError: Computation failed since Maxima requested additional
396
constraints; using the 'assume' command before integral evaluation
397
*may* help (example of legal syntax is 'assume(n+1>0)', see `assume?`
398
for more details)
399
Is n+1 zero or nonzero?
400
sage: assume(n > 0)
401
sage: integral(x^n,x)
402
x^(n + 1)/(n + 1)
403
sage: forget()
404
405
Usually the constraints are of sign, but others are possible::
406
407
sage: assume(n==-1)
408
sage: integral(x^n,x)
409
log(x)
410
411
Note that an exception is raised when a definite integral is
412
divergent::
413
414
sage: forget() # always remember to forget assumptions you no longer need
415
sage: integrate(1/x^3,(x,0,1))
416
Traceback (most recent call last):
417
...
418
ValueError: Integral is divergent.
419
sage: integrate(1/x^3,x,-1,3)
420
Traceback (most recent call last):
421
...
422
ValueError: Integral is divergent.
423
424
But Sage can calculate the convergent improper integral of
425
this function::
426
427
sage: integrate(1/x^3,x,1,infinity)
428
1/2
429
430
The examples in the Maxima documentation::
431
432
sage: var('x, y, z, b')
433
(x, y, z, b)
434
sage: integral(sin(x)^3, x)
435
1/3*cos(x)^3 - cos(x)
436
sage: integral(x/sqrt(b^2-x^2), b)
437
x*log(2*b + 2*sqrt(b^2 - x^2))
438
sage: integral(x/sqrt(b^2-x^2), x)
439
-sqrt(b^2 - x^2)
440
sage: integral(cos(x)^2 * exp(x), x, 0, pi)
441
3/5*e^pi - 3/5
442
sage: integral(x^2 * exp(-x^2), x, -oo, oo)
443
1/2*sqrt(pi)
444
445
We integrate the same function in both Mathematica and Sage (via
446
Maxima)::
447
448
sage: _ = var('x, y, z')
449
sage: f = sin(x^2) + y^z
450
sage: g = mathematica(f) # optional - mathematica
451
sage: print g # optional - mathematica
452
z 2
453
y + Sin[x ]
454
sage: print g.Integrate(x) # optional - mathematica
455
z Pi 2
456
x y + Sqrt[--] FresnelS[Sqrt[--] x]
457
2 Pi
458
sage: print f.integral(x)
459
x*y^z + 1/8*sqrt(pi)*((I + 1)*sqrt(2)*erf((1/2*I + 1/2)*sqrt(2)*x) + (I - 1)*sqrt(2)*erf((1/2*I - 1/2)*sqrt(2)*x))
460
461
Alternatively, just use algorithm='mathematica_free' to integrate via Mathematica
462
over the internet (does NOT require a Mathematica license!)::
463
464
sage: _ = var('x, y, z')
465
sage: f = sin(x^2) + y^z
466
sage: f.integrate(algorithm="mathematica_free") # optional - internet
467
sqrt(pi)*sqrt(1/2)*fresnels(sqrt(2)*x/sqrt(pi)) + y^z*x
468
469
We can also use Sympy::
470
471
sage: integrate(x*sin(log(x)), x)
472
-1/5*x^2*(cos(log(x)) - 2*sin(log(x)))
473
sage: integrate(x*sin(log(x)), x, algorithm='sympy')
474
-1/5*x^2*cos(log(x)) + 2/5*x^2*sin(log(x))
475
sage: _ = var('y, z')
476
sage: (x^y - z).integrate(y)
477
-y*z + x^y/log(x)
478
sage: (x^y - z).integrate(y, algorithm="sympy") # see Trac #14694
479
Traceback (most recent call last):
480
...
481
AttributeError: 'Piecewise' object has no attribute '_sage_'
482
483
We integrate the above function in Maple now::
484
485
sage: g = maple(f); g # optional - maple
486
sin(x^2)+y^z
487
sage: g.integrate(x) # optional - maple
488
1/2*2^(1/2)*Pi^(1/2)*FresnelS(2^(1/2)/Pi^(1/2)*x)+y^z*x
489
490
We next integrate a function with no closed form integral. Notice
491
that the answer comes back as an expression that contains an
492
integral itself.
493
494
::
495
496
sage: A = integral(1/ ((x-4) * (x^3+2*x+1)), x); A
497
-1/73*integrate((x^2 + 4*x + 18)/(x^3 + 2*x + 1), x) + 1/73*log(x - 4)
498
499
We now show that floats are not converted to rationals
500
automatically since we by default have keepfloat: true in maxima.
501
502
::
503
504
sage: integral(e^(-x^2),(x, 0, 0.1))
505
0.0562314580091*sqrt(pi)
506
507
ALIASES: integral() and integrate() are the same.
508
509
EXAMPLES:
510
511
Here is an example where we have to use assume::
512
513
sage: a,b = var('a,b')
514
sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)
515
Traceback (most recent call last):
516
...
517
ValueError: Computation failed since Maxima requested additional
518
constraints; using the 'assume' command before integral evaluation
519
*may* help (example of legal syntax is 'assume(a>0)', see `assume?`
520
for more details)
521
Is a positive or negative?
522
523
So we just assume that `a>0` and the integral works::
524
525
sage: assume(a>0)
526
sage: integrate(1/(x^3 *(a+b*x)^(1/3)), x)
527
2/9*sqrt(3)*b^2*arctan(1/3*sqrt(3)*(2*(b*x + a)^(1/3) + a^(1/3))/a^(1/3))/a^(7/3) - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3) + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4)
528
529
TESTS:
530
531
The following integral was broken prior to Maxima 5.15.0 -
532
see #3013::
533
534
sage: integrate(sin(x)*cos(10*x)*log(x), x)
535
-1/198*(9*cos(11*x) - 11*cos(9*x))*log(x) + 1/44*Ei(11*I*x) - 1/36*Ei(9*I*x) - 1/36*Ei(-9*I*x) + 1/44*Ei(-11*I*x)
536
537
It is no longer possible to use certain functions without an
538
explicit variable. Instead, evaluate the function at a variable,
539
and then take the integral::
540
541
sage: integrate(sin)
542
Traceback (most recent call last):
543
...
544
TypeError
545
546
sage: integrate(sin(x), x)
547
-cos(x)
548
sage: integrate(sin(x), x, 0, 1)
549
-cos(1) + 1
550
551
Check if #780 is fixed::
552
553
sage: _ = var('x,y')
554
sage: f = log(x^2+y^2)
555
sage: res = integral(f,x,0.0001414, 1.); res
556
Traceback (most recent call last):
557
...
558
ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(50015104*y^2-50015103>0)', see `assume?` for more details)
559
Is 50015104*y^2-50015103 positive, negative, or zero?
560
sage: assume(y>1)
561
sage: res = integral(f,x,0.0001414, 1.); res
562
-2*y*arctan(0.0001414/y) + 2*y*arctan(1/y) + log(y^2 + 1.0) - 0.0001414*log(y^2 + 1.999396e-08) - 1.9997172
563
sage: nres = numerical_integral(f.subs(y=2), 0.0001414, 1.); nres
564
(1.4638323264144..., 1.6251803529759...e-14)
565
sage: res.subs(y=2).n()
566
1.46383232641443
567
sage: nres = numerical_integral(f.subs(y=.5), 0.0001414, 1.); nres
568
(-0.669511708872807, 7.768678110854711e-15)
569
sage: res.subs(y=.5).n()
570
-0.669511708872807
571
572
Check if #6816 is fixed::
573
574
sage: var('t,theta')
575
(t, theta)
576
sage: integrate(t*cos(-theta*t),t,0,pi)
577
(pi*theta*sin(pi*theta) + cos(pi*theta))/theta^2 - 1/theta^2
578
sage: integrate(t*cos(-theta*t),(t,0,pi))
579
(pi*theta*sin(pi*theta) + cos(pi*theta))/theta^2 - 1/theta^2
580
sage: integrate(t*cos(-theta*t),t)
581
(t*theta*sin(t*theta) + cos(t*theta))/theta^2
582
sage: integrate(x^2,(x)) # this worked before
583
1/3*x^3
584
sage: integrate(x^2,(x,)) # this didn't
585
1/3*x^3
586
sage: integrate(x^2,(x,1,2))
587
7/3
588
sage: integrate(x^2,(x,1,2,3))
589
Traceback (most recent call last):
590
...
591
ValueError: invalid input (x, 1, 2, 3) - please use variable, with or without two endpoints
592
593
Note that this used to be the test, but it is
594
actually divergent (though Maxima as yet does
595
not say so)::
596
597
sage: integrate(t*cos(-theta*t),(t,-oo,oo))
598
integrate(t*cos(t*theta), t, -Infinity, +Infinity)
599
600
Check if #6189 is fixed::
601
602
sage: n = N; n
603
<function numerical_approx at ...>
604
sage: F(x) = 1/sqrt(2*pi*1^2)*exp(-1/(2*1^2)*(x-0)^2)
605
sage: G(x) = 1/sqrt(2*pi*n(1)^2)*exp(-1/(2*n(1)^2)*(x-n(0))^2)
606
sage: integrate( (F(x)-F(x))^2, x, -infinity, infinity).n()
607
0.000000000000000
608
sage: integrate( ((F(x)-G(x))^2).expand(), x, -infinity, infinity).n()
609
-6.26376265908397e-17
610
sage: integrate( (F(x)-G(x))^2, x, -infinity, infinity).n()# abstol 1e-6
611
0
612
613
This was broken before Maxima 5.20::
614
615
sage: exp(-x*i).integral(x,0,1)
616
I*e^(-I) - I
617
618
Test deprecation warning when variable is not specified::
619
620
sage: x.integral()
621
doctest:...: DeprecationWarning:
622
Variable of integration should be specified explicitly.
623
See http://trac.sagemath.org/12438 for details.
624
1/2*x^2
625
626
Test that #8729 is fixed::
627
628
sage: t = var('t')
629
sage: a = sqrt((sin(t))^2 + (cos(t))^2)
630
sage: integrate(a, t, 0, 2*pi)
631
2*pi
632
sage: a.simplify_full().simplify_trig()
633
1
634
635
Maxima uses Cauchy Principal Value calculations to
636
integrate certain convergent integrals. Here we test
637
that this does not raise an error message (see #11987)::
638
639
sage: integrate(sin(x)*sin(x/3)/x^2, x, 0, oo)
640
1/6*pi
641
642
Maxima returned a negative value for this integral prior to
643
maxima-5.24 (trac #10923). Ideally we would get an answer in terms
644
of the gamma function; however, we get something equivalent::
645
646
sage: actual_result = integral(e^(-1/x^2), x, 0, 1)
647
sage: actual_result.simplify_radical()
648
(sqrt(pi)*(erf(1)*e - e) + 1)*e^(-1)
649
sage: ideal_result = 1/2*gamma(-1/2, 1)
650
sage: error = actual_result - ideal_result
651
sage: error.numerical_approx() # abs tol 1e-10
652
0
653
654
We won't get an evaluated answer here, which is better than
655
the previous (wrong) answer of zero. See :trac:`10914`::
656
657
sage: f = abs(sin(x))
658
sage: integrate(f, x, 0, 2*pi) # long time (4s on sage.math, 2012)
659
integrate(abs(sin(x)), x, 0, 2*pi)
660
661
Another incorrect integral fixed upstream in Maxima, from
662
:trac:`11233`::
663
664
sage: a,t = var('a,t')
665
sage: assume(a>0)
666
sage: assume(x>0)
667
sage: f = log(1 + a/(x * t)^2)
668
sage: F = integrate(f, t, 1, Infinity)
669
sage: F(x=1, a=7).numerical_approx() # abs tol 1e-10
670
4.32025625668262
671
672
Verify that MinusInfinity works with sympy (:trac:`12345`)::
673
674
sage: integral(1/x^2, x, -infinity, -1, algorithm='sympy')
675
1
676
677
Check that :trac:`11737` is fixed::
678
679
sage: N(integrate(sin(x^2)/(x^2), x, 1, infinity))
680
0.285736646322858
681
682
"""
683
expression, v, a, b = _normalize_integral_input(expression, v, a, b)
684
if algorithm is not None:
685
integrator = available_integrators.get(algorithm)
686
if not integrator:
687
raise ValueError, "Unknown algorithm: %s" % algorithm
688
return integrator(expression, v, a, b)
689
if a is None:
690
return indefinite_integral(expression, v)
691
else:
692
return definite_integral(expression, v, a, b)
693
694
integral= integrate
695
696