Numerical Integration

One of the most widely used methods of numerical integration is Gauss-Legendre quadrature. It posses very attractive property of to be exact on polynomials of degree up to 2n-1, while using only n integrand evaluations (n-point quadrature).
The algorithm consists in approximation of initial definite integral by the sum of weighted integrand values sampled at special points called abscissas:

    \[ \begin{matrix} \displaystyle \int_a^b f(x)\,\mathrm{d}x\approx\frac{b-a}{2}\sum_{i=1}^{n}{w_i\,f\left(\frac{b-a}{2}\xi_i+\frac{b+a}{2}\right)}&\\ &\\ \displaystyle w_i = \frac{2}{\left(1-\xi_i^2\right)\,\left[P'_n(\xi_i)\right]^2}&(n=1,2,\dots) \end{matrix} \]

where values \xi_i are n zeroes of the n^{th}-degree Legendre polynomial P_n(\xi).

Accuracy of the numerical integration depends significantly on precision of zeroes \xi_i. There is no easy-to-use analytical expression for \xi_i and usually they are computed numerically by root-finding algorithms.

Surprisingly popular mathematical references/software provide low-precision \xi_i,\,w_i (only 4-6 correct digits) for limited n\le 5. Obviously such situation doesn’t reflect contemporary computational capabilities and accuracy demands of many applications. For instance, commonly used floating-point type double of IEEE 754 standard is capable to store values with 16 digits precision (machine epsilon is about 1e-16 for 64 bits double).

This page aims to provide software libraries for calculation of high-precision abscissas \xi_i and weights w_i for any desired n.

Gauss-Legendre Quadrature for C/C++

This open-source library implements numerical integration based on Gauss-Legendre quadrature of any order.

Pre-calculated high-precision abscissas and weights with 25 correct decimal places are used for specific orders n=2,…, 20, 32, 64, 96, 100, 128, 256, 512, 1024. Although all values can be found in the source code human-friendly tables with abscissas and weights are available too. Nodes for all other n are generated on the fly with 1e-10 precision by fast root-finding algorithm.

Library also includes routine for numerical integration over 2D rectangle using product of two 1D Gaussian quadratures.

If you are looking for numerical integration over the unit disk (2D sphere) you might be interested in this page Cubature formulas for the unit disk. It contains derivation details and source code in C/C++.

This C/C++ library has been included without modifications in few prominent open source packages (e.g. GNU Scientific Library) as well as in several commercial software products (financial, medical, astronomical, etc.).

Multiprecision Computing Toolbox for MATLAB worth mentioning in particular. It provides Gauss-Legendre as well as a full set of Gaussian type quadrature (Jacobi, Chebyshev, Laguerre, Hermit and Gegenbauer) in arbitrary precision. Reference tables on this page can be easily re-computed using this extension.

Gauss-Legendre Quadrature for MATLAB

There are several routines for Gaussian quadrature implemented using Matlab programming language. All of them suffer from low performance and even some of them provide low accuracy results.

To alleviate the situation I am developing quadrature toolbox which has Matlab interface (m-files) and delegates all the heavy lifting to high-performance MEX library written in C/C++ (see previous section). So far it includes only Gauss-Legendre rule with many others to come:

Quadrature toolbox consists of three files:

GaussLegendre.m 
quadlab.mexw32 
quadlab.mexw64 

Toolbox was created and tested using Matlab R2010b, both on 32 and 64 bits versions. Usage example:

>>GaussLegendre(@cos,-pi/2,pi/2,1024)    
  ans =
       2.000000000000000

GaussLegendre() accepts Matlab function (inline or from m-file), integration boundaries [a,b], order of quadrature (1024 in example) and desired precision for nodes as optional parameter. Also it returns vectors of abscissas and weights if such outputs are specified.

Performance comparison with the fastest Matlab-language routine (x-quadrature order, y-execution time, green-quadrature toolbox, red-competitor, lower is better):

Besides being fast library usually provides results with better accuracy thanks to pre-calculated high-precision abscissas and weights.

I am extending quadrature toolbox for Matlab with other rules and adaptive methods. I would really appreciate any feature suggestions and advices on what methods to include first.

Abscissae and Weights of Gauss-Legendre Quadrature

Table below lists Gauss-Legendre quadrature nodes for n=2,…, 20, 32, 64, 100 with the accuracy of 25 decimal digits. Data for higher order formulae n = 128, 256, 512, 1024 can be found in C/C++ library source code.

High-precision Abscissae and Weights of Gaussian Quadrature.
Correctly rounded to 25 decimal digits to the nearest.
nAbscissae \xi_iWeights w_i
2±0.57735026918962576450914881.0000000000000000000000000
300.8888888888888888888888889
±0.77459666924148337703585310.5555555555555555555555556
4±0.33998104358485626480266580.6521451548625461426269361
±0.86113631159405257522394650.3478548451374538573730639
500.5688888888888888888888889
±0.53846931010568309103631440.4786286704993664680412915
±0.90617984593866399279762690.2369268850561890875142640
6±0.23861918608319690863050170.4679139345726910473898703
±0.66120938646626451366139960.3607615730481386075698335
±0.93246951420315202781230160.1713244923791703450402961
700.4179591836734693877551020
±0.40584515137739716690660640.3818300505051189449503698
±0.74153118559939443986386480.2797053914892766679014678
±0.94910791234275852452618970.1294849661688696932706114
8±0.18343464249564980493947610.3626837833783619829651504
±0.52553240991632898581773900.3137066458778872873379622
±0.79666647741362673959155390.2223810344533744705443560
±0.96028985649753623168356090.1012285362903762591525314
900.3302393550012597631645251
±0.32425342340380892903853800.3123470770400028400686304
±0.61337143270059039730870200.2606106964029354623187429
±0.83603110732663579429942980.1806481606948574040584720
±0.96816023950762608983557620.0812743883615744119718922
10±0.14887433898163121088482600.2955242247147528701738930
±0.43339539412924719079926590.2692667193099963550912269
±0.67940956829902440623432740.2190863625159820439955349
±0.86506336668898451073209670.1494513491505805931457763
±0.97390652851717172007796400.0666713443086881375935688
1100.2729250867779006307144835
±0.26954315595234497233153200.2628045445102466621806889
±0.51909612920681181592572570.2331937645919904799185237
±0.73015200557404932409341630.1862902109277342514260976
±0.88706259976809529907515780.1255803694649046246346943
±0.97822865814605699280393800.0556685671161736664827537
12±0.12523340851146891547244140.2491470458134027850005624
±0.36783149899818019375269150.2334925365383548087608499
±0.58731795428661744729670240.2031674267230659217490645
±0.76990267419430468703689380.1600783285433462263346525
±0.90411725637047485667846590.1069393259953184309602547
±0.98156063424671925069054910.0471753363865118271946160
1300.2325515532308739101945895
±0.23045831595513479406552810.2262831802628972384120902
±0.44849275103644685287791290.2078160475368885023125232
±0.64234933944034022064398460.1781459807619457382800467
±0.80157809073330991279420650.1388735102197872384636018
±0.91759839922297796520654780.0921214998377284479144218
±0.98418305471858814947282940.0404840047653158795200216
14±0.10805494870734366206624470.2152638534631577901958764
±0.31911236892788976043567180.2051984637212956039659241
±0.51524863635815409196529070.1855383974779378137417166
±0.68729290481168547014801980.1572031671581935345696019
±0.82720131506976499318979470.1215185706879031846894148
±0.92843488366357351733639110.0801580871597602098056333
±0.98628380869681233884159730.0351194603317518630318329
1500.2025782419255612728806202
±0.20119409399743452230062830.1984314853271115764561183
±0.39415134707756336989720740.1861610000155622110268006
±0.57097217260853884753722670.1662692058169939335532009
±0.72441773136017004741618610.1395706779261543144478048
±0.84820658341042721620064830.1071592204671719350118695
±0.93727339240070590430775890.0703660474881081247092674
±0.98799251802048542848956570.0307532419961172683546284
16±0.09501250983763744018531930.1894506104550684962853967
±0.28160355077925891323046050.1826034150449235888667637
±0.45801677765722738634241940.1691565193950025381893121
±0.61787624440264374844667180.1495959888165767320815017
±0.75540440835500303389510120.1246289712555338720524763
±0.86563120238783174388046790.0951585116824927848099251
±0.94457502307323257607798840.0622535239386478928628438
±0.98940093499164993259615420.0271524594117540948517806
1700.1794464703562065254582656
±0.17848418149584785585067750.1765627053669926463252710
±0.35123176345387631529718550.1680041021564500445099707
±0.51269053708647696788624660.1540457610768102880814316
±0.65767115921669076585030220.1351363684685254732863200
±0.78151400389680140692523010.1118838471934039710947884
±0.88023915372698590212295570.0850361483171791808835354
±0.95067552176876776122271700.0554595293739872011294402
±0.99057547531441733567543400.0241483028685479319601100
18±0.08477501304173530124226190.1691423829631435918406565
±0.25188622569150550958897290.1642764837458327229860538
±0.41175116146284264603593180.1546846751262652449254180
±0.55977083107394753460787150.1406429146706506512047313
±0.69168704306035320787489110.1225552067114784601845191
±0.80370495897252311568241750.1009420441062871655628140
±0.89260246649755573920606060.0764257302548890565291297
±0.95582394957139775518119590.0497145488949697964533349
±0.99156516842093094673001600.0216160135264833103133427
1900.1610544498487836959791636
±0.16035864564022537586809610.1589688433939543476499564
±0.31656409996362983199011730.1527660420658596667788554
±0.46457074137596094571726710.1426067021736066117757461
±0.60054530466168102346963820.1287539625393362276755158
±0.72096617733522937861709590.1115666455473339947160239
±0.82271465653714282497892250.0914900216224499994644621
±0.90315590361481790164266090.0690445427376412265807083
±0.96020815213483003085277880.0448142267656996003328382
±0.99240684384358440318901770.0194617882297264770363120
20±0.07652652113349733375464040.1527533871307258506980843
±0.22778585114164507808049620.1491729864726037467878287
±0.37370608871541956067254820.1420961093183820513292983
±0.51086700195082709800436410.1316886384491766268984945
±0.63605368072651502545283670.1181945319615184173123774
±0.74633190646015079261430510.1019301198172404350367501
±0.83911697182221882339452910.0832767415767047487247581
±0.91223442825132590586775240.0626720483341090635695065
±0.96397192727791379126766610.0406014298003869413310400
±0.99312859918509492478612240.0176140071391521183118620
32±0.04830766568773831623481260.0965400885147278005667648
±0.14447196158279649348518640.0956387200792748594190820
±0.23928736225213707454460320.0938443990808045656391802
±0.33186860228212764977991680.0911738786957638847128686
±0.42135127613063534536411940.0876520930044038111427715
±0.50689990893222939002374750.0833119242269467552221991
±0.58771575724076232904074550.0781938957870703064717409
±0.66304426693021520097511520.0723457941088485062253994
±0.73218211874028968038742670.0658222227763618468376501
±0.79448379596794240696309730.0586840934785355471452836
±0.84936761373256997013369300.0509980592623761761961632
±0.89632115576605212396530720.0428358980222266806568786
±0.93490607593773968917091910.0342738629130214331026877
±0.96476225558750643077381190.0253920653092620594557526
±0.98561151154526833540017500.0162743947309056706051706
±0.99726386184948156354498110.0070186100094700966004071
64±0.02435029266342443250895580.0486909570091397203833654
±0.07299312178779903944954290.0485754674415034269347991
±0.12146281929612055447037650.0483447622348029571697695
±0.16964442042399281803731360.0479993885964583077281262
±0.21742364374000708414964870.0475401657148303086622822
±0.26468716220876741637396420.0469681828162100173253263
±0.31132287199021095615751270.0462847965813144172959532
±0.35722015833766811595044260.0454916279274181444797710
±0.40227015796399160369576680.0445905581637565630601347
±0.44636601725346408798494770.0435837245293234533768279
±0.48940314570705295747852630.0424735151236535890073398
±0.53127946401989454565801390.0412625632426235286101563
±0.57189564620263403428387810.0399537411327203413866569
±0.61115535517239325024885300.0385501531786156291289625
±0.64896547125465733985776120.0370551285402400460404151
±0.68523631305423324256355840.0354722132568823838106931
±0.71988185017161082684894020.0338051618371416093915655
±0.75281990726053189661186380.0320579283548515535854675
±0.78397235894334140761022050.0302346570724024788679741
±0.81326531512279755974192330.0283396726142594832275113
±0.84062929625258036275169150.0263774697150546586716918
±0.86599939815409281976078340.0243527025687108733381776
±0.88931544599511410585340400.0222701738083832541592983
±0.91052213707850280575638070.0201348231535302093723403
±0.92956917213193957582149020.0179517157756973430850453
±0.94641137485840281606248150.0157260304760247193219660
±0.96100879965205371891861410.0134630478967186425980608
±0.97332682778991096374185350.0111681394601311288185905
±0.98333625388462595693129930.0088467598263639477230309
±0.99101337147674432073938240.0065044579689783628561174
±0.99634011677195527934692450.0041470332605624676352875
±0.99930504173577213945690560.0017832807216964329472961
100±0.01562898442154308287221670.0312554234538633569476425
±0.04687168242159163161492390.0312248842548493577323765
±0.07806858281343663669481740.0311638356962099067838183
±0.10918920358006111500342600.0310723374275665165878102
±0.14020313723611397320751460.0309504788504909882340635
±0.17108008053860327488753240.0307983790311525904277139
±0.20178986409573599723604890.0306161865839804484964594
±0.23230248184497396964951000.0304040795264548200165079
±0.26258812037150347916892930.0301622651051691449190687
±0.29261718803847196473755590.0298909795933328309168368
±0.32236034390052915172247660.0295904880599126425117545
±0.35178852637242172097234380.0292610841106382766201190
±0.38087298162462995676336250.0289030896011252031348762
±0.40958529167830154252886840.0285168543223950979909368
±0.43789740217203151310897800.0281027556591011733176483
±0.46578164977335804224921660.0276611982207923882942042
±0.49321078920819093356930880.0271926134465768801364916
±0.52015801988176305664681570.0266974591835709626603847
±0.54659701206509416746799430.0261762192395456763423087
±0.57250193262138119131687040.0256294029102081160756420
±0.59784747024717872126480650.0250575444815795897037642
±0.62260886020370777160419080.0244612027079570527199750
±0.64676190851412927983263030.0238409602659682059625604
±0.67028301560314101580258700.0231974231852541216224889
±0.69314919935580196594864790.0225312202563362727017970
±0.71533811757305644645996710.0218430024162473863139537
±0.73682808980202070551242770.0211334421125276415426723
±0.75759811851970717603566800.0204032326462094327668389
±0.77762790964949547562755140.0196530874944353058653815
±0.79689789239031447638957290.0188837396133749045529412
±0.81538923833917625439398880.0180959407221281166643908
±0.83308387988840082354291580.0172904605683235824393442
±0.84996452787959128429336260.0164680861761452126431050
±0.86601468849716462341074000.0156296210775460027239369
±0.88121867938501841557331680.0147758845274413017688800
±0.89556164497072698669852100.0139077107037187726879541
±0.90902957098252969046712630.0130259478929715422855586
±0.92160929814533395266695130.0121314576629794974077448
±0.93328853504307954592433370.0112251140231859771172216
±0.94405587013625597796277470.0103078025748689695857821
±0.95390078292549174284933690.0093804196536944579514182
±0.96281365425581552729365930.0084438714696689714026208
±0.97078577576370633193089790.0074990732554647115788287
±0.97780935848691828855378110.0065469484508453227641521
±0.98387754070605701549610020.0055884280038655151572119
±0.98898439524299174800441870.0046244500634221193510958
±0.99312493703744345965200990.0036559612013263751823425
±0.99629513473312514918613170.0026839253715534824194396
±0.99849195063959581840016340.0017093926535181052395294
±0.99971372677344123367822850.0007346344905056717304063

1 Star2 Stars3 Stars4 Stars5 Stars (21 votes, average: 5.00)
Loading...

45 Comments

  1. Ben
    Posted June 17, 2011 at 6:47 pm | #

    It’s worth looking at eigenvalue methods to calculate the nodes and weights. Basically the 3 term polynomial recurrence relation allows the roots to be expressed as eigenvalues of a tridiagonal matrix; with appropriate rescaling the matrix is transformed to symmetric tridiagonal and hence the roots can be determined to arbitrary precision e.g. with your MPFR library. Similarly the weights can be expressed in terms of the eigenvectors.

    • Posted June 18, 2011 at 12:59 pm | #

      Derivation of quadrature nodes using eigenvalues/vectors of Jacobi matrix is universal approach.

      Although it is very powerful I find it inefficient for particular well studied case – Gauss-Legendre quadrature.
      We have nice analytic formula for close approximation of Legendre polynomial roots. Starting from there Newton iterations converge to “real” root very quickly with any desired precision. I think even Golub-Welsch eigen solver optimized for symmetric 3-diagonal matrices cannot beat that.

      P.S.
      BTW I’ve used eigen method to derive quadrature for less-known weight function \omega(x)=|x| here: Cubature formulas for the unit disk.

      P.P.S.
      Thanks for reading pages on my site ;-).

  2. Gowri Priya
    Posted July 14, 2011 at 4:36 pm | #

    Thank u very much for giving useful information

  3. vladan
    Posted August 4, 2011 at 10:12 pm | #

    Do you know where I can find “Abscissae and Weights of Gauss-Legendre Quadrature” for “Associated Legendre”?

    • Posted August 5, 2011 at 9:27 am | #

      Do you mean quadrature based on “Associated Legendre” polynomials? I don’t know this kind of quadrature.
      Could you be more specific – what task do you solve ?

  4. ankit
    Posted October 12, 2011 at 10:33 pm | #

    excellent work….helped me alot..

  5. Posted October 24, 2011 at 5:38 pm | #

    show the difference between langrange polynomial and newton interpolating polynomial

  6. Sergey
    Posted November 15, 2011 at 5:26 pm | #

    Brilliant job!!! Thank you

  7. Ali
    Posted January 14, 2012 at 12:35 am | #

    nice job,but I really wants to know how to use it for double integration,if some how it’s doable please let me know?

  8. Stephen
    Posted January 17, 2012 at 3:32 pm | #

    I came across your site while searching for Gauss-Laguerre quadrature. Do you plan to include this in your toolbox? I am currently using the eigen method, but I expect Newton’s method with the initial approximations from Stroud and Secrest would be faster.

    • Posted January 17, 2012 at 4:47 pm | #

      I completely agree with you, direct root search with good starting point is much faster than eigen-solving Jacobi matrix.

      I have plans to cover all commonly used quadrature, plus to port QuadPack adaptive integration rules to Matlab. I will release them as a part of Multiprecision Computing Toolbox for MATLAB.

      It already has Gauss-Legendre and Gauss-Kronrod rules which is the most important for practical applications since it allows re-use of lower order quadrature nodes in higher order rules. Thus saving on function evaluations.

  9. Pablo
    Posted February 17, 2012 at 2:37 am | #

    Hi Pavel!

    First of all congrats for your excellent job.

    I’m currently using your code to integrate the elements of the stiffness matrix in a solver for Poisson Eq using weak form of Finite Elements Method.

    The point is that the code works well when I try to evaluate numerically the integral of a function in ‘x’, but I don’t manage to integrate a constant (i.e. imagine we want to do gausslegendre(@(x) 1,-1,1). The result of this operation is, as far as I’m concerned, random.

    I could neither do this with the original version of quad.m, but after performing some changes to the code now I’m able to do it with a modified ver called myquad.m

    Could it be possible to perform the integration of a constant using your code?

    Many thanks and congrats again for your work!

    Best regards from El Salvador

    Pablo

  10. Posted February 28, 2012 at 6:47 pm | #

    Hi Pavel,

    I am looking for a way to get forces from pressure distribution on a plate and this looks like exactly what i need.

    Many Thanks,

    Balaji Sankar.

  11. isen1988
    Posted October 26, 2012 at 3:18 am | #

    Thank god! I find you!My project takes much time doing integration. So, I want to write a integration mex file to minus time,
    But I just can’t find out a way to pass in the function pointer to the mex file. That has really puzzled me a lot. How do you solve this ?

  12. Grunde Waag
    Posted November 30, 2012 at 12:22 am | #

    Hi,
    Nice pages.

    I’m integrating some very spiky functions. They are almost zero most of the time and contains one or more spikes. What is the best scheme to use for such a situation? With the trapezoidal rule I have to sample very dense for the integration to converge. Higher order schemes doesn’t seem to improve the situation. Any advice?

    • Posted November 30, 2012 at 11:49 am | #

      You could try adaptive integration methods based on Gauss-Kronrod rules – it will isolate “bad” regions and sample it with finer grid.
      Or look up methods for integrating “oscillatory functions”.

  13. Amao
    Posted December 5, 2012 at 10:26 pm | #

    Hi Pavel,

    many thanks for the ultrafast code. However, i found the matlab code can not treat complex function. Do you have a solution to it? Thanks in advance.

    Amao

    • Posted December 6, 2012 at 1:34 pm | #

      Hi, you can re-formulate integration of complex function in real domain.

  14. Tejas
    Posted January 10, 2013 at 9:33 am | #

    Hey,

    thanks for the neat explanation of the Gauss-Legendre quadrature rule!
    I have 2 questions for you and would really appreciate if you could help in any way

    1) Say we have two functions f(x) and h(x) defined over the same interval (a,b) and we wish to integrate the product f(x)*h(x), does the coefficient term (Jacobi) appear only once in this case?

    2) now if we have f(x) over (a,b) and h(x) over (a,c) [ also lets assume ab > ac ], can we integrate the product f(x)*h(x) over (a,c) [ since ac is within ab] using Gauss-Legendre quadrature rule? if yes, would the Jacobi be (ac -> zeta mapping) or some combination of (ac -> zeta; ab-> zeta mapping)?

    thanks!

  15. Robert
    Posted March 7, 2013 at 5:12 pm | #

    Hi

    Thanks for making these procedures available.

    I need the mex files for Linux (mexa64), so I haven’t been able to them. Is it possible for you to share the source code so I can compile the files myself or add this type of mex file?

    Thanks!

    • Lionel
      Posted August 7, 2013 at 1:28 am | #

      Hi all,

      I’m also working on linux 64 and i facing exactly the same problem. Would it be possible for you to share them? (mexa64 or source code to compile it myself).

      Thank you for publishing these codes,

      Lionel

  16. David L
    Posted April 29, 2013 at 5:05 pm | #

    Thanks for the mex implementation.. its enormous fast!
    Nevertheless I suspect a memory leak (at least in) quadlab.mexw64.
    When you call the function several times in Matlab, it seems that the used memory increases.
    Try the following:
    for i =1:100000, quadgk(@sinc,0,1);end
    for i =1:100000, GaussLegendre(@sinc,0,1,1024);end
    for i =1:100000, quadgk(@sinc,0,1);end
    for i =1:100000, GaussLegendre(@sinc,0,1,1024);end
    % check memory in task manager
    for i =1:100000, quadgk(@sinc,0,1);end
    % check memory in task manager
    for i =1:100000, GaussLegendre(@sinc,0,1,1024);end
    % check memory in task manager

    After the first 2 calls of the for loops the memory is increasing for both integration methods. Later the memory stays constant for the quadgk command but keeps increasing with the GaussLegendre command.

    Greets,
    David

    • Posted June 22, 2014 at 9:31 pm | #

      Thank you for the report. Memory leak is now fixed – please re-download the library.

  17. annel
    Posted September 10, 2013 at 3:50 pm | #

    Great code, thanks !

    Anne

  18. Posted June 22, 2014 at 10:30 am | #

    Your Gauss-Legendere MATLAB code has speeded up a computational economics project I am working on: really nice work! But, I have two questions that are similar to other posters:

    i. would it be possible to get the source code to create .mexa64 files for LINUX/UNIX?
    ii. is it expected that the memory usage can get so high when quadlab is called? I have tried doing some parallel implementation on a 96GB windows workstation and all of the memory is being used up.

    Thanks

    Andrew

    • Posted June 22, 2014 at 9:49 pm | #

      Thank you for your feedback!

      Memory leak is now fixed. I have just released the new version – please re-download the lib (using the same link above).
      It still has only Windows binaries though.

      I am not ready to go open source, but Linux version is in my todo list.
      (It requires more time – I have to remove some Windows-specifics first).

      Let me know how is it working now,

      Will keep you updated on Linux version.

      • Andrew
        Posted June 23, 2014 at 10:43 pm | #

        I tried the new mex binaries. Memory usage is much more stable, but it runs much more slowly (by a factor of 4). Answers are identical up to 1e-15 on my example, so I may switch between the old and new ones depending on how I am using them.

        Thanks
        Andrew

        • Posted June 24, 2014 at 12:24 am | #

          Now fixed and should run even faster than the very first version (thanks to Intel C++). Please re-download & test the library.

  19. Posted July 9, 2014 at 5:02 am | #

    I think the scientific way to write a code, which is not filling it with numbers ,a much better way if you write a short piece of code to regenerate those weights and roots instead of putting them like this. Moreover, you will reduce the error that comes from human
    Cheers.

    • Posted July 9, 2014 at 6:10 pm | #

      Pre-computed coefficients are needed for high speed. Re-generation of high-precision coefficients every time would take forever.

      • Posted July 10, 2014 at 2:03 pm | #

        Thanks for this useful comment, you are absolutely right. I would say if you put a commented code, since you already have it, for those people who don’t worry about the speed but the accuracy. Such a code make your post wider and much helpful popular .

        • Posted July 10, 2014 at 2:20 pm | #

          Library includes the code to generate nodes & weights for any required order (in double precision).
          Only few particular cases are pre-computed, all others are generated on the fly.

          If you are interested in generating coefficients in arbitrary precision, then check out this MATLAB code:
          Arbitrary Precision Gauss-Legendre Quadrature

          It requires multiprecision toolbox for MATLAB, which includes computation of nodes & weights of all common Gauss-type quadrature in arbitrary precision. See here:
          Abscissas and Weights of Classical Gaussian Quadrature Rules

          Btw, I am author of the toolbox :).

          • Posted July 11, 2014 at 2:16 pm | #

            Thanks Pavel . It is good to know that I am dealing with professional people 🙂

            I already coded Gauss quadrature rule using C++, and every time I regenerate the weights and the roots using a little piece of code, but when I need to find the weights and the roots where N=64 then it takes so long time. does that what happen with you when you generate such roots and weights.
            Thanks.

          • Posted July 11, 2014 at 3:48 pm | #

            It as always better to do a little research before coding. Just count number of operation (algorithmic complexity). Or learn how it is done in other libraries.

  20. Simone
    Posted April 4, 2015 at 11:54 pm | #

    Thanks for the code, it works perfectly! However, is it possible to use the Gauss-Legendre integration also for double integrals?

    • labd1
      Posted October 2, 2015 at 11:31 pm | #

      How is it possible to implement this code for 2D integration please?

  21. Ali
    Posted October 14, 2015 at 3:35 am | #

    First, I thank you for your useful mex function. It helped me a lot with my thesis.
    One problem though: I need to integrate complex functions. Since your function does not cover complex variables, I integrate once imag(f(x)) and once real(f(x)). This doubles the number of integrals I have to compute and integration is a serious bottleneck in my programs.
    I would appreciate any suggestions.

  22. Jay
    Posted April 12, 2016 at 10:28 pm | #

    Hi. I was really impressed by your Gauss-Legendre Quadrature for C/C++.
    I’ve intersted, is the ability to estimate of the absolute error in the result. like it implements here http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html (abserr) ? Thanks!

    • Posted April 13, 2016 at 1:44 pm | #

      You can run two quadrature of different orders and difference in their results will give you the absolute error.

      • Jay
        Posted April 18, 2016 at 9:38 pm | #

        I guess that for such approach there is need https://en.wikipedia.org/wiki/Gaussian_quadrature#Gauss.E2.80.93Kronrod_rules.
        I think that different orders is not enough, different grid is also needed.

        • Posted April 18, 2016 at 9:46 pm | #

          Yes, of course, Gauss-Kronrod or Curtis-Clenshaw are the best methods for such purposes.
          I thought you are restricted to use Gauss-Legendre.

          • Jay
            Posted April 19, 2016 at 7:50 pm | #

            I mean that that could be good addon to your lib;)

          • Posted April 20, 2016 at 11:00 am | #

            Absolutely – been thinking about it for some time. Also all Gauss-type quadrature, multi-dimensional cubature for various types of domains (disk, hypercube, simplex, etc.), Monte-Carlo integration, etc. The only question is where to get the time & $ :).

  23. Justin
    Posted August 20, 2016 at 11:09 pm | #

    Hi Pavel, thanks for hosting such an informative thread. In situations where we integrate over the unit sphere, the basis functions become the Associated Legendre polynomials P_{n}^{m}(\theta), which certainly have unique \xi_i and w_i compared to P_n^0. In this case, it is unclear to me how the algorithm should be changed. And advice is appreciated!

Post a Reply to Ali

Your email is never published nor shared.

Use native LaTeX syntax to include formulas: $ ... $, \[ ... \], etc. Do not forget to preview comment before posting.

Also you may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Subscribe without commenting