const gk_nodes = (
    (0.0,),
    (-1.732050807568877193e+00, 1.732050807568877193e+00),
    (-7.410953499945408529e-01, -2.861279576057058183e+00, -4.184956017672732287e+00, 7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00),
    (1.230423634027306035e+00, 2.596083115049202306e+00, 3.205333794499194422e+00, 5.187016039913656229e+00, 6.363394494336369611e+00, -1.230423634027306035e+00, -2.596083115049202306e+00, -3.205333794499194422e+00, -5.187016039913656229e+00, -6.363394494336369611e+00),
    (-3.035159417196289788e-01, -2.208370035755473193e+00, -3.673120807624038697e+00, -4.688944733396042786e+00, -5.738816857731674048e+00, -7.052048695931292244e+00, -7.809096669391978551e+00, -8.661390355138452790e+00, -9.691888484307325413e+00, 3.035159417196289788e-01, 2.208370035755473193e+00, 3.673120807624038697e+00, 4.688944733396042786e+00, 5.738816857731674048e+00, 7.052048695931292244e+00, 7.809096669391978551e+00, 8.661390355138452790e+00, 9.691888484307325413e+00),
    (-6.206215996851309757e-01, -9.041718401396522387e-01, -1.556920948898949275e+00, -1.874020592699235932e+00, -2.570161501011057670e+00, -3.588718867304420357e+00, -3.813668067407607332e+00, -4.519991606227620196e+00, -4.871869590277843720e+00, -5.474559779359854161e+00, -6.034481198100183796e+00, -6.707072145057108692e+00, -7.289858306075302607e+00, -7.460729234405580890e+00, -8.181394510092470540e+00, -8.612155515170002218e+00, -8.886347001304686799e+00, -9.295854667849322794e+00, -1.009182628500030354e+01, -1.049797509743559409e+01, -1.091154446366382835e+01, -1.133361676429119136e+01, -1.176537484480120277e+01, -1.220821525266299190e+01, -1.266385440024828846e+01, -1.313446784351190466e+01, -1.362290117407219192e+01, -1.413301728617947717e+01, -1.467031519464652867e+01, -1.524314486007864389e+01, -1.586543512793624977e+01, -1.656423516155702913e+01, -1.741041065963615253e+01, 6.206215996851309757e-01, 9.041718401396522387e-01, 1.556920948898949275e+00, 1.874020592699235932e+00, 2.570161501011057670e+00, 3.588718867304420357e+00, 3.813668067407607332e+00, 4.519991606227620196e+00, 4.871869590277843720e+00, 5.474559779359854161e+00, 6.034481198100183796e+00, 6.707072145057108692e+00, 7.289858306075302607e+00, 7.460729234405580890e+00, 8.181394510092470540e+00, 8.612155515170002218e+00, 8.886347001304686799e+00, 9.295854667849322794e+00, 1.009182628500030354e+01, 1.049797509743559409e+01, 1.091154446366382835e+01, 1.133361676429119136e+01, 1.176537484480120277e+01, 1.220821525266299190e+01, 1.266385440024828846e+01, 1.313446784351190466e+01, 1.362290117407219192e+01, 1.413301728617947717e+01, 1.467031519464652867e+01, 1.524314486007864389e+01, 1.586543512793624977e+01, 1.656423516155702913e+01, 1.741041065963615253e+01)
)
const gk_nodes_v = (
    [0.0],
    [-1.732050807568877193e+00, 1.732050807568877193e+00],
    [-7.410953499945408529e-01, -2.861279576057058183e+00, -4.184956017672732287e+00, 7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00],
    [1.230423634027306035e+00, 2.596083115049202306e+00, 3.205333794499194422e+00, 5.187016039913656229e+00, 6.363394494336369611e+00, -1.230423634027306035e+00, -2.596083115049202306e+00, -3.205333794499194422e+00, -5.187016039913656229e+00, -6.363394494336369611e+00],
    [-3.035159417196289788e-01, -2.208370035755473193e+00, -3.673120807624038697e+00, -4.688944733396042786e+00, -5.738816857731674048e+00, -7.052048695931292244e+00, -7.809096669391978551e+00, -8.661390355138452790e+00, -9.691888484307325413e+00, 3.035159417196289788e-01, 2.208370035755473193e+00, 3.673120807624038697e+00, 4.688944733396042786e+00, 5.738816857731674048e+00, 7.052048695931292244e+00, 7.809096669391978551e+00, 8.661390355138452790e+00, 9.691888484307325413e+00],
    [-6.206215996851309757e-01, -9.041718401396522387e-01, -1.556920948898949275e+00, -1.874020592699235932e+00, -2.570161501011057670e+00, -3.588718867304420357e+00, -3.813668067407607332e+00, -4.519991606227620196e+00, -4.871869590277843720e+00, -5.474559779359854161e+00, -6.034481198100183796e+00, -6.707072145057108692e+00, -7.289858306075302607e+00, -7.460729234405580890e+00, -8.181394510092470540e+00, -8.612155515170002218e+00, -8.886347001304686799e+00, -9.295854667849322794e+00, -1.009182628500030354e+01, -1.049797509743559409e+01, -1.091154446366382835e+01, -1.133361676429119136e+01, -1.176537484480120277e+01, -1.220821525266299190e+01, -1.266385440024828846e+01, -1.313446784351190466e+01, -1.362290117407219192e+01, -1.413301728617947717e+01, -1.467031519464652867e+01, -1.524314486007864389e+01, -1.586543512793624977e+01, -1.656423516155702913e+01, -1.741041065963615253e+01, 6.206215996851309757e-01, 9.041718401396522387e-01, 1.556920948898949275e+00, 1.874020592699235932e+00, 2.570161501011057670e+00, 3.588718867304420357e+00, 3.813668067407607332e+00, 4.519991606227620196e+00, 4.871869590277843720e+00, 5.474559779359854161e+00, 6.034481198100183796e+00, 6.707072145057108692e+00, 7.289858306075302607e+00, 7.460729234405580890e+00, 8.181394510092470540e+00, 8.612155515170002218e+00, 8.886347001304686799e+00, 9.295854667849322794e+00, 1.009182628500030354e+01, 1.049797509743559409e+01, 1.091154446366382835e+01, 1.133361676429119136e+01, 1.176537484480120277e+01, 1.220821525266299190e+01, 1.266385440024828846e+01, 1.313446784351190466e+01, 1.362290117407219192e+01, 1.413301728617947717e+01, 1.467031519464652867e+01, 1.524314486007864389e+01, 1.586543512793624977e+01, 1.656423516155702913e+01, 1.741041065963615253e+01]
)
const gk_nodes_0 = (0.0,)
const gk_nodes_1 = (-1.732050807568877193e+00, 1.732050807568877193e+00),
const gk_nodes_2 = (-7.410953499945408529e-01, -2.861279576057058183e+00, -4.184956017672732287e+00, 7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00)
const gk_nodes_3 = (1.230423634027306035e+00, 2.596083115049202306e+00, 3.205333794499194422e+00, 5.187016039913656229e+00, 6.363394494336369611e+00, -1.230423634027306035e+00, -2.596083115049202306e+00, -3.205333794499194422e+00, -5.187016039913656229e+00, -6.363394494336369611e+00)
const gk_nodes_4 =  (-3.035159417196289788e-01, -2.208370035755473193e+00, -3.673120807624038697e+00, -4.688944733396042786e+00, -5.738816857731674048e+00, -7.052048695931292244e+00, -7.809096669391978551e+00, -8.661390355138452790e+00, -9.691888484307325413e+00, 3.035159417196289788e-01, 2.208370035755473193e+00, 3.673120807624038697e+00, 4.688944733396042786e+00, 5.738816857731674048e+00, 7.052048695931292244e+00, 7.809096669391978551e+00, 8.661390355138452790e+00, 9.691888484307325413e+00)
const gk_nodes_5 = (-6.206215996851309757e-01, -9.041718401396522387e-01, -1.556920948898949275e+00, -1.874020592699235932e+00, -2.570161501011057670e+00, -3.588718867304420357e+00, -3.813668067407607332e+00, -4.519991606227620196e+00, -4.871869590277843720e+00, -5.474559779359854161e+00, -6.034481198100183796e+00, -6.707072145057108692e+00, -7.289858306075302607e+00, -7.460729234405580890e+00, -8.181394510092470540e+00, -8.612155515170002218e+00, -8.886347001304686799e+00, -9.295854667849322794e+00, -1.009182628500030354e+01, -1.049797509743559409e+01, -1.091154446366382835e+01, -1.133361676429119136e+01, -1.176537484480120277e+01, -1.220821525266299190e+01, -1.266385440024828846e+01, -1.313446784351190466e+01, -1.362290117407219192e+01, -1.413301728617947717e+01, -1.467031519464652867e+01, -1.524314486007864389e+01, -1.586543512793624977e+01, -1.656423516155702913e+01, -1.741041065963615253e+01, 6.206215996851309757e-01, 9.041718401396522387e-01, 1.556920948898949275e+00, 1.874020592699235932e+00, 2.570161501011057670e+00, 3.588718867304420357e+00, 3.813668067407607332e+00, 4.519991606227620196e+00, 4.871869590277843720e+00, 5.474559779359854161e+00, 6.034481198100183796e+00, 6.707072145057108692e+00, 7.289858306075302607e+00, 7.460729234405580890e+00, 8.181394510092470540e+00, 8.612155515170002218e+00, 8.886347001304686799e+00, 9.295854667849322794e+00, 1.009182628500030354e+01, 1.049797509743559409e+01, 1.091154446366382835e+01, 1.133361676429119136e+01, 1.176537484480120277e+01, 1.220821525266299190e+01, 1.266385440024828846e+01, 1.313446784351190466e+01, 1.362290117407219192e+01, 1.413301728617947717e+01, 1.467031519464652867e+01, 1.524314486007864389e+01, 1.586543512793624977e+01, 1.656423516155702913e+01, 1.741041065963615253e+01)



const gk_nodes_nonneg = (
    ((0.0,),),
    ( (0.0,), (1.732050807568877193e+00,) ),
    ( (0.0,), (1.732050807568877193e+00,), (7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00)),
    ( (0.0,), (1.732050807568877193e+00,), (7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00), (1.230423634027306035e+00,2.596083115049202306e+00, 3.205333794499194422e+00, 5.187016039913656229e+00, 6.363394494336369611e+00) ),
    ( (0.0,), (1.732050807568877193e+00,), (7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00), (1.230423634027306035e+00, 2.596083115049202306e+00, 3.205333794499194422e+00, 5.187016039913656229e+00, 6.363394494336369611e+00), (3.035159417196289788e-01, 2.208370035755473193e+00, 3.673120807624038697e+00, 4.688944733396042786e+00, 5.738816857731674048e+00, 7.052048695931292244e+00, 7.809096669391978551e+00, 8.661390355138452790e+00, 9.691888484307325413e+00) ),
    ( (0.0,), (1.732050807568877193e+00,), (7.410953499945408529e-01, 2.861279576057058183e+00, 4.184956017672732287e+00), (1.230423634027306035e+00, 2.596083115049202306e+00, 3.205333794499194422e+00, 5.187016039913656229e+00, 6.363394494336369611e+00), (3.035159417196289788e-01, 2.208370035755473193e+00, 3.673120807624038697e+00, 4.688944733396042786e+00, 5.738816857731674048e+00, 7.052048695931292244e+00, 7.809096669391978551e+00, 8.661390355138452790e+00, 9.691888484307325413e+00), (6.206215996851309757e-01, 9.041718401396522387e-01, 1.556920948898949275e+00, 1.874020592699235932e+00, 2.570161501011057670e+00, 3.588718867304420357e+00, 3.813668067407607332e+00, 4.519991606227620196e+00, 4.871869590277843720e+00, 5.474559779359854161e+00, 6.034481198100183796e+00, 6.707072145057108692e+00, 7.289858306075302607e+00, 7.460729234405580890e+00, 8.181394510092470540e+00, 8.612155515170002218e+00, 8.886347001304686799e+00, 9.295854667849322794e+00, 1.009182628500030354e+01, 1.049797509743559409e+01, 1.091154446366382835e+01, 1.133361676429119136e+01, 1.176537484480120277e+01, 1.220821525266299190e+01, 1.266385440024828846e+01, 1.313446784351190466e+01, 1.362290117407219192e+01, 1.413301728617947717e+01, 1.467031519464652867e+01, 1.524314486007864389e+01, 1.586543512793624977e+01, 1.656423516155702913e+01, 1.741041065963615253e+01) )
)

const gk_weights = (
    ((1.0,),),
    ((6.666666666666666297e-01,), (1.666666666666666574e-01,)),
    ((2.539682539682539542e-01,), (9.485094850948509737e-02,), (2.700743295779378661e-01, 7.996325470893532808e-03, 9.426945755651748365e-05)),
    ((3.034671998542058935e-01,), (6.409605468680758256e-02,), (2.083249916496088816e-01, -6.337224793373735440e-03, 6.012336945984782032e-05), (6.115173012524767471e-02,1.808523425479845181e-02, 2.884880436506751140e-03, 6.094808731468982961e-07, 8.629684602229885931e-10)),
    ((9.688245529284254809e-02,), (4.421164421898454711e-02,), (1.430993028968333969e-01, 1.768022258182954352e-03, 3.226518598373974471e-05), (9.372082806552459178e-02, 4.333498812272349072e-03, 9.858275829964838720e-04, 2.959075202307440677e-07, 4.225258439631110457e-10), (1.476557104026862410e-01, 1.551310987485935398e-02, 2.349403664659752112e-04, 3.309758709792034345e-06, 1.659544880938981831e-08, 4.566176367618685878e-12, 1.822427515491293625e-14, 1.877818931437289588e-17, 1.903035094013049694e-21)),
    ((1.205674705975654931e-01,), (-2.241690549232919737e-03,), (-2.510946060860437534e-02, 2.317853741494368004e-03, 2.157340473532454820e-05), (6.054609539583663547e-02, -6.868088867756604043e-03, 8.108079306253045517e-04, 1.756589052393765114e-07, 2.175702428714775840e-10), (1.166672226897644410e-01, 1.165794765237651055e-02, -2.555098045918444941e-04, 2.736249936842626933e-07, 7.699205480918628382e-09, 2.123779208924948428e-12, 8.335951137105458603e-15, -1.603802571689216247e-17, 6.351777619826335869e-22), (1.161449102169717779e-01, 9.049907887097373427e-02, 3.958048330358010514e-02, 2.349638855141306104e-02, 1.187429460595669506e-02, 4.283096239652469652e-04, 1.399993373051156139e-04, 4.675973952563834421e-06, 8.812581632544770710e-07, 3.318711099677051844e-08, 1.560543759860818455e-09, 2.360855272927444972e-11, 1.169869203766870029e-13, 1.013359032410814206e-13, 4.429742347393624813e-16, 3.262744522103358460e-17, 1.322870363778446040e-18, 2.717875933335900016e-20, 1.232134560467146984e-23, 1.914714214218525338e-25, 2.332204174318528461e-27, 2.179136932463778929e-29, 1.524177029773773434e-31, 7.750724758884266004e-34, 2.764556078556950881e-36, 6.610498069228468602e-39, 9.992242712355998473e-42, 8.823151988713996391e-45, 4.072624717633741158e-48, 8.321030089138647840e-52, 5.728650998340608745e-56, 7.912918315856256558e-61, 5.881010699300556648e-67))
)

const weighted_gk_weights = (
    ((1.0,),),
    ((6.666666666666666297000000000000000000000000000000000000000000000000000000000002e-01,), (7.469481783896772988457527699794061672980745700022560408596162118340808426721962e-01,7.469481783896772988457527699794061672980745700022560408596162118340808426721962e-01)),
    ((2.539682539682539541999999999999999999999999999999999999999999999999999999999988e-01,), (4.250924592461578472759876805552536329599586523523682709259908670256577684243761e-01,4.250924592461578472759876805552536329599586523523682709259908670256577684243761e-01), (3.554229094726574982860442760983532555588167824313428698590967717008450205251302e-01, 4.793555157593584816398270172093156025646331160551993334837656250436617254414600e-01, 5.990412180534271700286076322376124144529767305328254509252998705048447247649803e-01,
    3.554229094726574982860442760983532555588167824313428698590967717008450205251302e-01, 4.793555157593584816398270172093156025646331160551993334837656250436617254414600e-01, 5.990412180534271700286076322376124144529767305328254509252998705048447247649803e-01)),
    ((3.034671998542058935000000000000000000000000000000000000000000000000000000000000e-01,), (2.872585877416563872923859174001655426169576823366256035985979317280372371506416e-01,
    2.872585877416563872923859174001655426169576823366256035985979317280372371506416e-01,), (2.741596165903048231254842627399873640947342306275468152337277728975795964203473e-01, -3.798974504437236345476663821676787033819236760209767324667376204925070214866375e-01, 3.82057746042619577466460231832021988947777427472900800439360113662814069216201e-01,
    2.741596165903048231254842627399873640947342306275468152337277728975795964203473e-01, -3.798974504437236345476663821676787033819236760209767324667376204925070214866375e-01, 3.82057746042619577466460231832021988947777427472900800439360113662814069216201e-01), (1.303638405254942398659921882662298597171952334211712030622447379181678924224902e-01, 5.257993039121150066942942760063309508915527286203890571040318547131226910007951e-01, 4.910597559959024396827368462951747108311140147810245954105871604633544199013856e-01, 4.239708523478058208142833543104371604410325009662179294092721824359687679500878e-01, 5.356639749505990342664174794050651607839649596753828921013245685339949128066048e-01,
    1.303638405254942398659921882662298597171952334211712030622447379181678924224902e-01, 5.257993039121150066942942760063309508915527286203890571040318547131226910007951e-01, 4.910597559959024396827368462951747108311140147810245954105871604633544199013856e-01, 4.239708523478058208142833543104371604410325009662179294092721824359687679500878e-01, 5.356639749505990342664174794050651607839649596753828921013245685339949128066048e-01)),
    ((9.688245529284254808999999999999999999999999999999999999999999999999999999999973e-02,), (1.981428426778980984412600814819064066197916004754116602341340067537383921399099e-01,
    1.981428426778980984412600814819064066197916004754116602341340067537383921399099e-01), (1.883213804828653468614944070827636503374772062253660876775346574368894296123359e-01, 1.059875844886804440092869110403572114614088909440086983015607835246120812498014e-01, 2.050311608171923251432647743399691597188646613280832007288539163124739211851223e-01,
    1.883213804828653468614944070827636503374772062253660876775346574368894296123359e-01, 1.059875844886804440092869110403572114614088909440086983015607835246120812498014e-01, 2.050311608171923251432647743399691597188646613280832007288539163124739211851223e-01), (1.997949536150074469472153344857304638452646615078951601015156424405452101031627e-01, 1.259895573866964685133959071955945228524379013064007907876920512461750179612389e-01, 1.67806002021515926781812697325227273981706539769413320575134754468269761718468e-01, 2.058410183088985338488574359666944058810790080238993669350031543082753239694951e-01, 2.622713152670181245847792066189062454897386919633485046840126633035438008876432e-01,
    1.997949536150074469472153344857304638452646615078951601015156424405452101031627e-01, 1.259895573866964685133959071955945228524379013064007907876920512461750179612389e-01, 1.67806002021515926781812697325227273981706539769413320575134754468269761718468e-01, 2.058410183088985338488574359666944058810790080238993669350031543082753239694951e-01, 2.622713152670181245847792066189062454897386919633485046840126633035438008876432e-01), (1.546159416265305613521062647988422327207440056433022066888637894519247591069617e-01, 1.777067237741947016011107256772430707943478489463044630983584233106965784410551e-01, 1.998335313964808739470910767885214844575959156383317195911069335120796374817492e-01, 1.968065697283842595393871134022617467268891580336636978901510751590663220829677e-01, 2.352449695913756290871153743077223673253485830732344148566236444253501744805265e-01, 2.874663303616266215185713034517514646718100831157478554314080989203956121474342e-01, 3.182154869178219322525709717261600085800217046015508192285268999262191679309989e-01, 3.66412995642927566567209315391227998614780120255270692272877691761267033639734e-01, 4.749785056705039548294688635784742503358477008080817452603676938307273765655888e-01,
    1.546159416265305613521062647988422327207440056433022066888637894519247591069617e-01, 1.777067237741947016011107256772430707943478489463044630983584233106965784410551e-01, 1.998335313964808739470910767885214844575959156383317195911069335120796374817492e-01, 1.968065697283842595393871134022617467268891580336636978901510751590663220829677e-01, 2.352449695913756290871153743077223673253485830732344148566236444253501744805265e-01, 2.874663303616266215185713034517514646718100831157478554314080989203956121474342e-01, 3.182154869178219322525709717261600085800217046015508192285268999262191679309989e-01, 3.66412995642927566567209315391227998614780120255270692272877691761267033639734e-01, 4.749785056705039548294688635784742503358477008080817452603676938307273765655888e-01)),
    ((1.205674705975654931000000000000000000000000000000000000000000000000000000000000e-01,), (-1.004656003357730823806188974619091124793125834446191621452480233775848578618848e-02,
    -1.004656003357730823806188974619091124793125834446191621452480233775848578618848e-02), (-3.304452355300148100907701108660353984545851091544121345792423435861862467492072e-02, 1.389483181685245561998489162800461481863219504281205217919283409423287612744794e-01, 1.370895620403929221796390803646287110031864827723960976487722418310067161769682e-01,
    -3.304452355300148100907701108660353984545851091544121345792423435861862467492072e-02, 1.389483181685245561998489162800461481863219504281205217919283409423287612744794e-01, 1.370895620403929221796390803646287110031864827723960976487722418310067161769682e-01), (1.290727426429006555492847049521242122808175102340887965026895848184566932833196e-01, -1.996787155198069833761048469679109478999974584765022381562309378357435651854228e-01, 1.380144353762277285999381191666272523336755548897420894976987031710802154905806e-01, 1.221929334587518376259432748909909467881910101233360640314212238681512645094766e-01, 1.350507538796819284118108665412593848348812381302706974707395964723043592556429e-01,
    1.290727426429006555492847049521242122808175102340887965026895848184566932833196e-01, -1.996787155198069833761048469679109478999974584765022381562309378357435651854228e-01, 1.380144353762277285999381191666272523336755548897420894976987031710802154905806e-01, 1.221929334587518376259432748909909467881910101233360640314212238681512645094766e-01, 1.350507538796819284118108665412593848348812381302706974707395964723043592556429e-01), (1.221667109516808190370518250428476817502289488961063083653087292766837791614186e-01, 1.335448340111544981514326484249212091804964172890004698602494327928524979978424e-01, -2.173293049894328541785639712838913063596493364318502740867021633896889137893188e-01, 1.627042969616786323017137575123847206390897111941153829472217340886271988531726e-02, 1.091383173808271059912672611652228508086363209626270939980457402609529582966774e-01, 1.337037745667200228518061287391775756922632633663635424192783589137993003687799e-01, 1.455546916115390192433400169433867669456458997723005332002505712744662073949346e-01, -3.129450315333034711164409656696647940753668072869003756323156170457042397932826e-01, 1.585340097882490853220738371167806400656320914148613019992326744838319491407595e-01,
    1.221667109516808190370518250428476817502289488961063083653087292766837791614186e-01, 1.335448340111544981514326484249212091804964172890004698602494327928524979978424e-01, -2.173293049894328541785639712838913063596493364318502740867021633896889137893188e-01, 1.627042969616786323017137575123847206390897111941153829472217340886271988531726e-02, 1.091383173808271059912672611652228508086363209626270939980457402609529582966774e-01, 1.337037745667200228518061287391775756922632633663635424192783589137993003687799e-01, 1.455546916115390192433400169433867669456458997723005332002505712744662073949346e-01, -3.129450315333034711164409656696647940753668072869003756323156170457042397932826e-01, 1.585340097882490853220738371167806400656320914148613019992326744838319491407595e-01), (1.408117965428582459162876149363881074540291961667053616321683414171503557489225e-01, 1.361970903861409165299347684909556849651325954647563203911887203853041023300648e-01, 1.329984629599679201317532559224692811885529412841919573627476858474076436367604e-01, 1.360229092078236952036773310956155750965076532404155400226276895185758575773446e-01, 3.228671754698710762567279580670329963945941193375398234348914314055081946042006e-01, 2.681489553918453044734559581543139773799622567549983705451696642331003349397749e-01, 2.015252077912545915669213003253490099523789818885819528195164688838272053088035e-01, 1.277204963843742976731996322288040800440204600020807221200522741183918951669275e-01, 1.256369893314551374852865986062839874863648830088856658669781405776860179840158e-01, 1.069170615807775867674670213187129633032953515717362686527691924792778806061541e-01, 1.260911971045680313452531871444771489334082184216232741175732749569181962079643e-01, 1.384835564679941519175122395707968073610522851416296138819888353652825409237885e-01, 4.053063396124052977373168417958918070128745886215735357114089793458949085693095e-02, 1.237990902870524369339555228492741317324406532190547262207679352134549377006608e-01, 1.517665185116327857270968738247869921185266823756355447386381896991320724004838e-01, 4.161262608892393519996858911156575973478844323821317971560465194785734821058607e-01, 1.857885153653686170918007032119532731433290633576380885676252281072935041150899e-01, 1.579630413832698922610809900762769849144924553399148831499524839581697316463859e-01, 1.606977230566520625746424052702404009903917533249360598570248398470460477209748e-01, 1.634385213505341330257697809858487597950195971180970507490215809799994145417857e-01, 1.666128532912926033083987912253689356198430144454419623138038929938517981765681e-01, 1.702300841125100847403377929828536379058458801150613409786168681599088845609372e-01, 1.743552051527007763833944392826094663092892128050440505451002005909832494783034e-01, 1.790935510945458777310090095483319171925624763449518313998782327281702016680291e-01, 1.845971971113272278601583938297960255043302296508511695418342101046108124115428e-01, 1.910850124205492485686214821819739358069442477378959613913593635449865408945723e-01, 1.988813937429899161582019334591191093761050728102145147551721737166719001341179e-01, 2.084906246429884317899166651270953944098724445617473229600911586848007892152892e-01, 2.207503567962314258656209780836950928147267874812765663538672701189731196089008e-01, 2.371870598593791815084402714470599016010287742002505126151560513044627873269844e-01, 2.60991031806506170012377255229863125256172232871574431654511691260342019258654e-01, 3.005170538128573338790480965022348051757581084764165815972783299215065489942171e-01, 3.905212369293002235972355867990859074521287143810981639577764587251872598475516e-01,
    1.408117965428582459162876149363881074540291961667053616321683414171503557489225e-01, 1.361970903861409165299347684909556849651325954647563203911887203853041023300648e-01, 1.329984629599679201317532559224692811885529412841919573627476858474076436367604e-01, 1.360229092078236952036773310956155750965076532404155400226276895185758575773446e-01, 3.228671754698710762567279580670329963945941193375398234348914314055081946042006e-01, 2.681489553918453044734559581543139773799622567549983705451696642331003349397749e-01, 2.015252077912545915669213003253490099523789818885819528195164688838272053088035e-01, 1.277204963843742976731996322288040800440204600020807221200522741183918951669275e-01, 1.256369893314551374852865986062839874863648830088856658669781405776860179840158e-01, 1.069170615807775867674670213187129633032953515717362686527691924792778806061541e-01, 1.260911971045680313452531871444771489334082184216232741175732749569181962079643e-01, 1.384835564679941519175122395707968073610522851416296138819888353652825409237885e-01, 4.053063396124052977373168417958918070128745886215735357114089793458949085693095e-02, 1.237990902870524369339555228492741317324406532190547262207679352134549377006608e-01, 1.517665185116327857270968738247869921185266823756355447386381896991320724004838e-01, 4.161262608892393519996858911156575973478844323821317971560465194785734821058607e-01, 1.857885153653686170918007032119532731433290633576380885676252281072935041150899e-01, 1.579630413832698922610809900762769849144924553399148831499524839581697316463859e-01, 1.606977230566520625746424052702404009903917533249360598570248398470460477209748e-01, 1.634385213505341330257697809858487597950195971180970507490215809799994145417857e-01, 1.666128532912926033083987912253689356198430144454419623138038929938517981765681e-01, 1.702300841125100847403377929828536379058458801150613409786168681599088845609372e-01, 1.743552051527007763833944392826094663092892128050440505451002005909832494783034e-01, 1.790935510945458777310090095483319171925624763449518313998782327281702016680291e-01, 1.845971971113272278601583938297960255043302296508511695418342101046108124115428e-01, 1.910850124205492485686214821819739358069442477378959613913593635449865408945723e-01, 1.988813937429899161582019334591191093761050728102145147551721737166719001341179e-01, 2.084906246429884317899166651270953944098724445617473229600911586848007892152892e-01, 2.207503567962314258656209780836950928147267874812765663538672701189731196089008e-01, 2.371870598593791815084402714470599016010287742002505126151560513044627873269844e-01, 2.60991031806506170012377255229863125256172232871574431654511691260342019258654e-01, 3.005170538128573338790480965022348051757581084764165815972783299215065489942171e-01, 3.905212369293002235972355867990859074521287143810981639577764587251872598475516e-01))
)

const weighted_gk_weights_nonneg = (
    ((1.0,),),
    ((6.666666666666666297000000000000000000000000000000000000000000000000000000000002e-01,), (7.469481783896772988457527699794061672980745700022560408596162118340808426721962e-01,)),
    ((2.539682539682539541999999999999999999999999999999999999999999999999999999999988e-01,), (4.250924592461578472759876805552536329599586523523682709259908670256577684243761e-01,), (3.554229094726574982860442760983532555588167824313428698590967717008450205251302e-01, 4.793555157593584816398270172093156025646331160551993334837656250436617254414600e-01, 5.990412180534271700286076322376124144529767305328254509252998705048447247649803e-01)),
    ((3.034671998542058935000000000000000000000000000000000000000000000000000000000000e-01,), (2.872585877416563872923859174001655426169576823366256035985979317280372371506416e-01,), (2.741596165903048231254842627399873640947342306275468152337277728975795964203473e-01, -3.798974504437236345476663821676787033819236760209767324667376204925070214866375e-01, 3.82057746042619577466460231832021988947777427472900800439360113662814069216201e-01), (1.303638405254942398659921882662298597171952334211712030622447379181678924224902e-01, 5.257993039121150066942942760063309508915527286203890571040318547131226910007951e-01, 4.910597559959024396827368462951747108311140147810245954105871604633544199013856e-01, 4.239708523478058208142833543104371604410325009662179294092721824359687679500878e-01, 5.356639749505990342664174794050651607839649596753828921013245685339949128066048e-01)),
    ((9.688245529284254808999999999999999999999999999999999999999999999999999999999973e-02,), (1.981428426778980984412600814819064066197916004754116602341340067537383921399099e-01,), (1.883213804828653468614944070827636503374772062253660876775346574368894296123359e-01, 1.059875844886804440092869110403572114614088909440086983015607835246120812498014e-01, 2.050311608171923251432647743399691597188646613280832007288539163124739211851223e-01), (1.997949536150074469472153344857304638452646615078951601015156424405452101031627e-01, 1.259895573866964685133959071955945228524379013064007907876920512461750179612389e-01, 1.67806002021515926781812697325227273981706539769413320575134754468269761718468e-01, 2.058410183088985338488574359666944058810790080238993669350031543082753239694951e-01, 2.622713152670181245847792066189062454897386919633485046840126633035438008876432e-01), (1.546159416265305613521062647988422327207440056433022066888637894519247591069617e-01, 1.777067237741947016011107256772430707943478489463044630983584233106965784410551e-01, 1.998335313964808739470910767885214844575959156383317195911069335120796374817492e-01, 1.968065697283842595393871134022617467268891580336636978901510751590663220829677e-01, 2.352449695913756290871153743077223673253485830732344148566236444253501744805265e-01, 2.874663303616266215185713034517514646718100831157478554314080989203956121474342e-01, 3.182154869178219322525709717261600085800217046015508192285268999262191679309989e-01, 3.66412995642927566567209315391227998614780120255270692272877691761267033639734e-01, 4.749785056705039548294688635784742503358477008080817452603676938307273765655888e-01)),
    ((1.205674705975654931000000000000000000000000000000000000000000000000000000000000e-01,), (-1.004656003357730823806188974619091124793125834446191621452480233775848578618848e-02,), (-3.304452355300148100907701108660353984545851091544121345792423435861862467492072e-02, 1.389483181685245561998489162800461481863219504281205217919283409423287612744794e-01, 1.370895620403929221796390803646287110031864827723960976487722418310067161769682e-01), (1.290727426429006555492847049521242122808175102340887965026895848184566932833196e-01, -1.996787155198069833761048469679109478999974584765022381562309378357435651854228e-01, 1.380144353762277285999381191666272523336755548897420894976987031710802154905806e-01, 1.221929334587518376259432748909909467881910101233360640314212238681512645094766e-01, 1.350507538796819284118108665412593848348812381302706974707395964723043592556429e-01), (1.221667109516808190370518250428476817502289488961063083653087292766837791614186e-01, 1.335448340111544981514326484249212091804964172890004698602494327928524979978424e-01, -2.173293049894328541785639712838913063596493364318502740867021633896889137893188e-01, 1.627042969616786323017137575123847206390897111941153829472217340886271988531726e-02, 1.091383173808271059912672611652228508086363209626270939980457402609529582966774e-01, 1.337037745667200228518061287391775756922632633663635424192783589137993003687799e-01, 1.455546916115390192433400169433867669456458997723005332002505712744662073949346e-01, -3.129450315333034711164409656696647940753668072869003756323156170457042397932826e-01, 1.585340097882490853220738371167806400656320914148613019992326744838319491407595e-01), (1.408117965428582459162876149363881074540291961667053616321683414171503557489225e-01, 1.361970903861409165299347684909556849651325954647563203911887203853041023300648e-01, 1.329984629599679201317532559224692811885529412841919573627476858474076436367604e-01, 1.360229092078236952036773310956155750965076532404155400226276895185758575773446e-01, 3.228671754698710762567279580670329963945941193375398234348914314055081946042006e-01, 2.681489553918453044734559581543139773799622567549983705451696642331003349397749e-01, 2.015252077912545915669213003253490099523789818885819528195164688838272053088035e-01, 1.277204963843742976731996322288040800440204600020807221200522741183918951669275e-01, 1.256369893314551374852865986062839874863648830088856658669781405776860179840158e-01, 1.069170615807775867674670213187129633032953515717362686527691924792778806061541e-01, 1.260911971045680313452531871444771489334082184216232741175732749569181962079643e-01, 1.384835564679941519175122395707968073610522851416296138819888353652825409237885e-01, 4.053063396124052977373168417958918070128745886215735357114089793458949085693095e-02, 1.237990902870524369339555228492741317324406532190547262207679352134549377006608e-01, 1.517665185116327857270968738247869921185266823756355447386381896991320724004838e-01, 4.161262608892393519996858911156575973478844323821317971560465194785734821058607e-01, 1.857885153653686170918007032119532731433290633576380885676252281072935041150899e-01, 1.579630413832698922610809900762769849144924553399148831499524839581697316463859e-01, 1.606977230566520625746424052702404009903917533249360598570248398470460477209748e-01, 1.634385213505341330257697809858487597950195971180970507490215809799994145417857e-01, 1.666128532912926033083987912253689356198430144454419623138038929938517981765681e-01, 1.702300841125100847403377929828536379058458801150613409786168681599088845609372e-01, 1.743552051527007763833944392826094663092892128050440505451002005909832494783034e-01, 1.790935510945458777310090095483319171925624763449518313998782327281702016680291e-01, 1.845971971113272278601583938297960255043302296508511695418342101046108124115428e-01, 1.910850124205492485686214821819739358069442477378959613913593635449865408945723e-01, 1.988813937429899161582019334591191093761050728102145147551721737166719001341179e-01, 2.084906246429884317899166651270953944098724445617473229600911586848007892152892e-01, 2.207503567962314258656209780836950928147267874812765663538672701189731196089008e-01, 2.371870598593791815084402714470599016010287742002505126151560513044627873269844e-01, 2.60991031806506170012377255229863125256172232871574431654511691260342019258654e-01, 3.005170538128573338790480965022348051757581084764165815972783299215065489942171e-01, 3.905212369293002235972355867990859074521287143810981639577764587251872598475516e-01))
)
const rule_lengths = (1,2,6,10,18,66)
const rules_total = (1,3,9,19,37,103)
const path = "/home/chris/Documents/progwork/julia/hermite/rules_nw"


function create_filename(rule)
    file = path * "/rule_"
    for i ∈ 1:rule
        file *= string(rules_list[i]) * "_"
    end
    file *= "qr_0.csv"
    file
end


"""
Creates and outputs sorted symmetric nested rules
so that we 
"""
function print_rules(rule)
    out = Vector{Matrix{String}}(uninitialized, rule)
    floats = Vector{Vector{Float64}}(uninitialized, rule)
    for i ∈ rule:-1:1
        file = create_filename(i)
        temp_nodes = round.(@view(readdlm( file, ',')[1,:]),10)
        non_negative_temp_nodes = temp_nodes .>= 0
        out[i] = readdlm( file, ',', String)[[1,3],non_negative_temp_nodes]
        floats[i] = temp_nodes[non_negative_temp_nodes]
        if i < rule
            fi = floats[i]
            fip1 = floats[i+1]
            fadditions = fip1[[ fip1[k] ∉ fi for k ∈ eachindex(fip1) ]]
            n_ip1 = length(fip1)
        end
        for j ∈ i+1:rule
            fj = floats[j]
            inds = 1:n_ip1
            new_inds = [ fj[k] ∈ fadditions for k ∈ 1:n_ip1 ]
            sortperm_new_inds = sortperm(fj[inds][new_inds])
    #        if i > 1
                sortperm_unsorted = new_inds .== false
    #            @show sum(sortperm_unsorted)
                ind_sort = vcat( inds[sortperm_unsorted], (inds[new_inds])[sortperm_new_inds], n_ip1+1:length(fj) )
     #       else
     #           ind_sort = vcat( inds[new_inds][sortperm_new_inds], n_ip1+1:length(fj) )
     #       end
            fjsorted = fj[ind_sort]
            # @show (i,j)
            # @show fj
            # @show ind_sort
            # @show fjsorted
            # @show (length(fj), length(ind_sort))
            # println("\n")
            floats[j] = fjsorted
            out[j] = out[j][:,ind_sort]
        end
    end
    out
end

function print_string_mat(x)
    reduce((a,b) -> a * "," * b, x)
end
print_weighted_weights(A) = print_weighted_weights(A[1,:], A[2,:])
function print_weighted_weights(nodes, weights)
    out = ""
    for i in eachindex(nodes)
        n = BigFloat(strip(nodes[i]))
        w = BigFloat(strip(weights[i]))
        out *= string(exp(n^2/2)*w) * ", "
    end
    out
end


#using Reduce, SpecialFunctions
#Reduce.Rational(false);

gen_diff(v, i) = v > 0 ? :( $(Symbol('`' + i, :_, v)) - $(Symbol('`' + i, :_, v-1)) ) : Symbol('`' + i, :_, v)
function tupGridd(x::NTuple{N,T}) where {N,T<:Integer}
    Expr(:call, :*, ntuple(i -> gen_diff(x[i], i), Val{N}())... )
end
@generated function max_degree_poly(::Val{N}, l::Int, T = Int) where N
    quote
        j_0 = l
        s_0 = 0
        ind = 0
        out = Vector{NTuple{N,T}}(undef, binomial($N+l,$N))
        @nloops $N i p -> begin
            0:j_{$N-p}
        end p -> begin
            s_{$N-p+1} = s_{$N-p} + i_p
            j_{$N-p+1} = l - s_{$N-p+1}
        end begin
            ind += 1
            out[ind] = @ntuple $N j -> T(i_{j})
        end
        out
    end
end
function weights(::Val{N}, l::Int) where N
    p1 = max_degree_poly(Val{N}(), l)
    s1 = process_weights_no_deriv(p1)
    rcall(s1, :expand)::Expr
end
function process_weights_no_deriv(x::Vector{NTuple{N,Int}}) where N
    if length(x) == 1
        return tupGridd(x[1])
    end
    expr = Expr(:call, :+, tupGridd(x[1]), tupGridd(x[2]) )
    for i in 3:length(x)
        push!(expr.args, tupGridd(x[i]) )
    end
    expr
end

function inds(i::I, j::I, m::NTuple{N,I}, mcourse::NTuple{N,I}) where {N,I<:Integer}
    distbase = mcourse[i]
    distcurrent =  j-1 + distbase
    distcurrent, cld(hermite_weights[distcurrent],2) + m[i] - cld(length(hermite_weights[distbase]),2)
end

function weight(offstep::NTuple{N}, mcp1::NTuple{N}, mcourse::NTuple{N}, mfine::NTuple{N}, ::Type{T} = Float64) where {N,T}
    out = one(T)
    for k in 1:N
        i_k = offstep[k]
        mcp1_k = mcp1[k]
        mf_k = mfine[k]
        if i_k == 0
            out *= weighted_gk_weights[mcp1_k][mcp1_k][mf_k]
        else
            out *= weighted_gk_weights[i_k+mcp1_k][mcp1_k][mf_k] -  weighted_gk_weights[i_k+mcourse[k]][mcp1_k][mf_k]
        end
    end
    out
end
@generated function calc_weights(l_remaining::I, mcourse::NTuple{N,I}, mfine::NTuple{N,I}, ::Type{T} = Float64) where {N,I<:Integer,T}
    quote
        j_0 = l_remaining
        s_0 = 0
        out = zero($T)
        mcp1 = @ntuple $N i -> 1 + mcourse[i]
        @nloops $N i p -> begin
            0:j_{$N-p}
        end p -> begin
            s_{$N-p+1} = s_{$N-p} + i_p
            j_{$N-p+1} = l_remaining - s_{$N-p+1}
        end begin
            out += weight( (@ntuple $N k -> i_k), mcp1, mcourse, mfine, T )
        end
        out
    end
end




@generated function calc_weights2(l_remaining::I, mcourse::NTuple{N,I}, mfine::NTuple{N,I}, ::Type{T} = Float64) where {N,I<:Integer,T}
    quote
        j_0 = l_remaining
        s_0 = 0
        out = Vector{$T}(undef, 0)
        mcp1 = @ntuple $N i -> 1 + mcourse[i]
        @nloops $N i p -> begin
            0:j_{$N-p}
        end p -> begin
            s_{$N-p+1} = s_{$N-p} + i_p
            j_{$N-p+1} = l_remaining - s_{$N-p+1}
        end begin
            push!(out, weight( (@ntuple $N k -> i_k), mcp1, mcourse, mfine, T ))
        end
        out
    end
end