Welcome, guest | Sign In | My Account | Store | Cart

If you have the freedom to choose your abscissas and your integrand is smooth or has a log singularity, then this script is for you. It computes the definite integral of a user defined function over the interval [a, b]. The user can specify the number of Gauss points (1 <= ng <= 12), the default being ng=10.

Python, 453 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
 #!/usr/bin/env python
# A. Pletzer Tue Mar 20 11:42:05 EST 2001

"""
Gauss Integration
"""


_ngmax = 12
_ngmin = 1


_nodes  =(
(0.,),
(-0.5773502691896257,
 0.5773502691896257,),
(-0.7745966692414834,
 0.,
 0.7745966692414834,),
(-0.861136311594053,
 -0.3399810435848562,
 0.3399810435848562,
 0.861136311594053,),
(-0.906179845938664,
 -0.5384693101056829,
 0.,
 0.5384693101056829,
 0.906179845938664,),
(-0.932469514203152,
 -0.6612093864662646,
 -0.2386191860831968,
 0.2386191860831968,
 0.6612093864662646,
 0.932469514203152,),
(-0.949107912342759,
 -0.7415311855993937,
 -0.4058451513773972,
 0.,
 0.4058451513773971,
 0.7415311855993945,
 0.949107912342759,),
(-0.960289856497537,
 -0.7966664774136262,
 -0.5255324099163289,
 -0.1834346424956498,
 0.1834346424956498,
 0.5255324099163289,
 0.7966664774136262,
 0.960289856497537,),
(-0.968160239507626,
 -0.836031107326637,
 -0.6133714327005903,
 -0.3242534234038088,
 0.,
 0.3242534234038088,
 0.6133714327005908,
 0.836031107326635,
 0.968160239507627,),
(-0.973906528517172,
 -0.865063366688984,
 -0.6794095682990246,
 -0.433395394129247,
 -0.1488743389816312,
 0.1488743389816312,
 0.433395394129247,
 0.6794095682990246,
 0.865063366688984,
 0.973906528517172,),
(-0.97822865814604,
 -0.88706259976812,
 -0.7301520055740422,
 -0.5190961292068116,
 -0.2695431559523449,
 0.,
 0.2695431559523449,
 0.5190961292068117,
 0.73015200557405,
 0.887062599768093,
 0.978228658146058,),
(-0.981560634246732,
 -0.904117256370452,
 -0.7699026741943177,
 -0.5873179542866143,
 -0.3678314989981804,
 -0.1252334085114688,
 0.1252334085114688,
 0.3678314989981804,
 0.5873179542866143,
 0.7699026741943177,
 0.904117256370452,
 0.981560634246732,),
)

_weights=(
(2.,),
(1.,
 1.,),
(0.5555555555555553,
 0.888888888888889,
 0.5555555555555553,),
(0.3478548451374539,
 0.6521451548625462,
 0.6521451548625462,
 0.3478548451374539,),
(0.2369268850561887,
 0.4786286704993665,
 0.5688888888888889,
 0.4786286704993665,
 0.2369268850561887,),
(0.1713244923791709,
 0.3607615730481379,
 0.4679139345726913,
 0.4679139345726913,
 0.3607615730481379,
 0.1713244923791709,),
(0.129484966168868,
 0.2797053914892783,
 0.3818300505051186,
 0.4179591836734694,
 0.3818300505051188,
 0.279705391489276,
 0.1294849661688697,),
(0.1012285362903738,
 0.2223810344533786,
 0.3137066458778874,
 0.3626837833783619,
 0.3626837833783619,
 0.3137066458778874,
 0.2223810344533786,
 0.1012285362903738,),
(0.0812743883615759,
 0.1806481606948543,
 0.2606106964029356,
 0.3123470770400029,
 0.3302393550012597,
 0.3123470770400025,
 0.2606106964029353,
 0.1806481606948577,
 0.0812743883615721,),
(0.06667134430868681,
 0.149451349150573,
 0.2190863625159832,
 0.2692667193099968,
 0.2955242247147529,
 0.2955242247147529,
 0.2692667193099968,
 0.2190863625159832,
 0.149451349150573,
 0.06667134430868681,),
(0.05566856711621584,
 0.1255803694648743,
 0.1862902109277404,
 0.2331937645919927,
 0.2628045445102466,
 0.2729250867779006,
 0.2628045445102466,
 0.2331937645919933,
 0.1862902109277339,
 0.1255803694649132,
 0.05566856711616958,),
(0.04717533638647547,
 0.1069393259953637,
 0.1600783285433586,
 0.2031674267230672,
 0.2334925365383534,
 0.2491470458134027,
 0.2491470458134027,
 0.2334925365383534,
 0.2031674267230672,
 0.1600783285433586,
 0.1069393259953637,
 0.04717533638647547,),
)

_nodesLog  =(
(0.3333333333333333,),
(0.1120088061669761,
 0.6022769081187381,),
(0.06389079308732544,
 0.3689970637156184,
 0.766880303938942,),
(0.04144848019938324,
 0.2452749143206022,
 0.5561654535602751,
 0.848982394532986,),
(0.02913447215197205,
 0.1739772133208974,
 0.4117025202849029,
 0.6773141745828183,
 0.89477136103101,),
(0.02163400584411693,
 0.1295833911549506,
 0.3140204499147661,
 0.5386572173517997,
 0.7569153373774084,
 0.922668851372116,),
(0.0167193554082585,
 0.100185677915675,
 0.2462942462079286,
 0.4334634932570557,
 0.6323509880476823,
 0.81111862674023,
 0.940848166743287,),
(0.01332024416089244,
 0.07975042901389491,
 0.1978710293261864,
 0.354153994351925,
 0.5294585752348643,
 0.7018145299391673,
 0.849379320441094,
 0.953326450056343,),
(0.01086933608417545,
 0.06498366633800794,
 0.1622293980238825,
 0.2937499039716641,
 0.4466318819056009,
 0.6054816627755208,
 0.7541101371585467,
 0.877265828834263,
 0.96225055941096,),
(0.00904263096219963,
 0.05397126622250072,
 0.1353118246392511,
 0.2470524162871565,
 0.3802125396092744,
 0.5237923179723384,
 0.6657752055148032,
 0.7941904160147613,
 0.898161091216429,
 0.9688479887196,),
(0.007643941174637681,
 0.04554182825657903,
 0.1145222974551244,
 0.2103785812270227,
 0.3266955532217897,
 0.4554532469286375,
 0.5876483563573721,
 0.7139638500230458,
 0.825453217777127,
 0.914193921640008,
 0.973860256264123,),
(0.006548722279080035,
 0.03894680956045022,
 0.0981502631060046,
 0.1811385815906331,
 0.2832200676673157,
 0.398434435164983,
 0.5199526267791299,
 0.6405109167754819,
 0.7528650118926111,
 0.850240024421055,
 0.926749682988251,
 0.977756129778486,),
)

_weightsLog=(
(-1.,),
(-0.7185393190303845,
 -0.2814606809696154,),
(-0.5134045522323634,
 -0.3919800412014877,
 -0.0946154065661483,),
(-0.3834640681451353,
 -0.3868753177747627,
 -0.1904351269501432,
 -0.03922548712995894,),
(-0.2978934717828955,
 -0.3497762265132236,
 -0.234488290044052,
 -0.0989304595166356,
 -0.01891155214319462,),
(-0.2387636625785478,
 -0.3082865732739458,
 -0.2453174265632108,
 -0.1420087565664786,
 -0.05545462232488041,
 -0.01016895869293513,),
(-0.1961693894252476,
 -0.2703026442472726,
 -0.239681873007687,
 -0.1657757748104267,
 -0.0889432271377365,
 -0.03319430435645653,
 -0.005932787015162054,),
(-0.164416604728002,
 -0.2375256100233057,
 -0.2268419844319134,
 -0.1757540790060772,
 -0.1129240302467932,
 -0.05787221071771947,
 -0.02097907374214317,
 -0.003686407104036044,),
(-0.1400684387481339,
 -0.2097722052010308,
 -0.211427149896601,
 -0.1771562339380667,
 -0.1277992280331758,
 -0.07847890261203835,
 -0.0390225049841783,
 -0.01386729555074604,
 -0.002408041036090773,),
(-0.12095513195457,
 -0.1863635425640733,
 -0.1956608732777627,
 -0.1735771421828997,
 -0.135695672995467,
 -0.0936467585378491,
 -0.05578772735275126,
 -0.02715981089692378,
 -0.00951518260454442,
 -0.001638157633217673,),
(-0.1056522560990997,
 -0.1665716806006314,
 -0.1805632182877528,
 -0.1672787367737502,
 -0.1386970574017174,
 -0.1038334333650771,
 -0.06953669788988512,
 -0.04054160079499477,
 -0.01943540249522013,
 -0.006737429326043388,
 -0.001152486965101561,),
(-0.09319269144393,
 -0.1497518275763289,
 -0.166557454364573,
 -0.1596335594369941,
 -0.1384248318647479,
 -0.1100165706360573,
 -0.07996182177673273,
 -0.0524069547809709,
 -0.03007108900074863,
 -0.01424924540252916,
 -0.004899924710875609,
 -0.000834029009809656,),
)


def sum(a):
	return reduce(lambda x, y: x+y, a)

def mult(a, b):
	return map(lambda x, y: x*y, a, b)

# make global 
xmin, xmax, =0., 1.
dx = xmax - xmin

def gauss(xmin, xmax, funct, ng=10):
	"""
	Gauss quadature (weight function = 1.0):
	xmin, xmax: boundaries of integration domain
	funct: integrand function
	ng: Gauss integration order
	"""
	ng = max((min((ng, _ngmax)), _ngmin))
	ns = _nodes[ng-1]
	ws = _weights[ng-1];
	dx = xmax - xmin
	x = map(lambda y: (dx*y + xmin + xmax)/2., ns)
	return 0.5*dx*sum(mult(funct(x), ws))

def gaussLog(xmin, xmax, funct, ng=10):
	"""
	Gauss quadature with Log singularity at x=xmin:
	xmin, xmax: boundaries of integration domain
	funct: integrand function
	ng: Gauss integration order
	"""
	ng = max((min((ng, _ngmax)), _ngmin))
	ns = _nodesLog[ng-1]
	ws = _weightsLog[ng-1];
	dx = xmax - xmin
	x = map(lambda y: (dx*y + xmin), ns)
	return dx*sum(mult(funct(x), ws))


####

if __name__ == '__main__':

	from math import *


	def f2(x):
		return map(lambda y: y**2, x)
	def f3(x):
		return map(lambda y: y**4, x)
	def f4(x):
		return map(lambda y: cos(2.*pi*(y-0.128726465)), x)
	def f5(x):
		return map(lambda y: 2.*cos(2.*pi*(y-0.128726465))**2, x)

	print '-'*80
	print 'Gauss (weight function = 1)'
	print '-'*80

	# simple tests
	print 'gauss(0., 1., f3, 1)=', gauss(0., 1., f3, 1)
	print 'gauss(0., 1., f3, 2)=', gauss(0., 1., f3, 2)
	print 'gauss(0., 1., f4, 3)=', gauss(0., 1., f3, 3)
	print 'gauss(0., 1., f3   )=', gauss(0., 1., f3   )

	# convergence test 
	ng = range(_ngmin, _ngmax+1)

	print """\n
	Integrate[Cos[2.*Pi*(x-0.128726465)], {x, 0, 1}]
	\n"""

	error = []
	for n in ng:
		error.append(gauss(0., 10.0, f4, n))
	print '    n = ', '%8d'*len(ng)    % tuple(ng)
	print 'error = ','%8.0e'*len(error) % tuple(error) 
		
	print """\n
	Integrate[2.*Cos[2.*Pi*(x-0.128726465)]^2, {x, 0, 1}]
	\n"""

	error = []
	for n in ng:
		error.append(gauss(0., 1.0, f5, n)-1.0)
	print '    n = ', '%8d'*len(ng)    % tuple(ng)
	print 'error = ','%8.0e'*len(error) % tuple(error) 


	print '-'*80
	print 'Gauss with Log singularity at left boundary'
	print '-'*80

	a, b = 0., 1.

	print """\n
	Integrate[Log[x]*x^2, {x, 0, 1}]
	\n"""

	exact = -1./9.
	error = []
	for n in ng:
		error.append(gaussLog(a, b, f2, n) - exact)
	print '    n = ', '%8d'*len(ng)    % tuple(ng)
	print 'error = ','%8.0e'*len(error) % tuple(error) 

	print """\n
	Integrate[Log[x]*2.*Cos[2.*Pi*(x-0.128726465)]^2, {x, 0, 1}]
	\n"""

	exact = -1.242002481967963
	error = []
	for n in ng:
		error.append(gaussLog(a, b, f5, n) - exact)
	print '    n = ', '%8d'*len(ng)    % tuple(ng)
	print 'error = ','%8.0e'*len(error) % tuple(error) 

The Gaussian quadrature is among the most accurate integration scheme for smooth integrands. It replaces a integral by a sum of sampled values of the integrand function times some weight factors. The values where the sampling occurs (Gauss's nodes) are the roots of orthogonal polynomials. There are many variants of Gauss's formula applicable to integrands with various types of weight functions (Gauss-Legendre, Gauss-Chebyshev, etc.). See "Numerical Recipes in Fortran" (Cambridge University Press), p. 140 for a good introduction to Gaussian quadrature.

Expect the quadrature error to go as (b-a)*(2n+1) f^(2n), where (b-a) is the interval width, 1<=n<=12 the number of nodes used to evaluate the integral, and f^(2n) the 2*n-th derivative of the integrand taken somewhere inside the interval. So convergence is extremely fast with decreasing (b-a). In practice, machine accuracy will limit the optimal n to ~10. If your f(x) is only smooth up to order m, then I suggest you take n ~ (m-1)/2.

A typical call is:

gauss(a, b, f, ng=10) # integral of f(x) from a to b

or, if your integrand has a log singularity

gaussLog(a, b, f, ng=10) # integral of [f(x) log(x)] from a to b

Don't be put off by the length of the script, most of it is Mathematica generated code for the node and weight data, which are identical to those in "Handbook of Mathematical Functions" by Abramowitz and Stegun (Dover Publications Inc, New York), pp. 916-912. This code saves you the effort of typing them in by hand and also contains additional weights and nodes for the case w=Log(x).

1 comment

Gabriel Genellina 12 years, 9 months ago  # | flag

Recipe 576900 is just a rewrite of this one in a more modern way, compatible with Python 2.4 and above.

Created by Alexander Pletzer on Wed, 21 Mar 2001 (PSF)
Python recipes (4591)
Alexander Pletzer's recipes (6)

Required Modules

Other Information and Tasks