30 using namespace Eigen;
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 class CITransformFunction :
public CFunction
60 virtual ~CITransformFunction() {
SG_UNREF(m_f); }
75 return (*m_f)(gx)*dgx;
98 class CILTransformFunction :
public CFunction
113 virtual ~CILTransformFunction() {
SG_UNREF(m_f); }
128 return -(*m_f)(m_b-
CMath::sq(gx))*2*gx*dgx;
154 class CIUTransformFunction :
public CFunction
169 virtual ~CIUTransformFunction() {
SG_UNREF(m_f); }
184 return (*m_f)(m_a+
CMath::sq(gx))*2*gx*dgx;
205 class CTransformFunction :
public CFunction
222 virtual ~CTransformFunction() {
SG_UNREF(m_f); }
238 return (*m_f)(gx)*dgx;
259 REQUIRE(f,
"Integrable function should not be NULL\n")
260 REQUIRE(abs_tol>0.0,
"Absolute tolerance must be positive, but is %f\n",
262 REQUIRE(rel_tol>0.0,
"Relative tolerance must be positive, but is %f\n",
264 REQUIRE(max_iter>0,
"Maximum number of iterations must be greater than 0, "
265 "but is %d\n", max_iter)
266 REQUIRE(sn>0,
"Initial number of subintervals must be greater than 0, "
270 typedef void TQuadGKEvaluationFunction(
CFunction* f,
274 TQuadGKEvaluationFunction* evaluate_quadgk;
298 tf=
new CITransformFunction(f);
299 evaluate_quadgk=&evaluate_quadgk15;
305 tf=
new CILTransformFunction(f, b);
306 evaluate_quadgk=&evaluate_quadgk15;
312 tf=
new CIUTransformFunction(f, a);
313 evaluate_quadgk=&evaluate_quadgk15;
319 tf=
new CTransformFunction(f, a, b);
320 evaluate_quadgk=&evaluate_quadgk21;
342 evaluate_quadgk(tf, subs, q_subs, err_subs);
362 while (err>tol && iter<max_iter)
369 (*subs)[2*i])/(tb-ta))
372 float64_t mid=((*subs)[2*i]+(*subs)[2*i+1])/2.0;
396 evaluate_quadgk(tf, subs, q_subs, err_subs);
414 SG_SWARNING(
"Error tolerance not met. Estimated error is equal to %g "
415 "after %d iterations\n", err, iter)
443 "The length of node array (%d) and weight array (%d) should be the same\n",
460 REQUIRE(f,
"Integrable function should not be NULL\n")
461 REQUIRE(subs, "Array of subintervals should not be NULL\n")
462 REQUIRE(!(subs->get_array_size()%2), "Size of the array of subintervals "
464 REQUIRE(q, "Array of values of integrals should not be NULL\n")
465 REQUIRE(err, "Array of errors should not be NULL\n")
466 REQUIRE(n%2, "Order of Gauss-Kronrod should be odd\n")
467 REQUIRE(xgk, "Gauss-Kronrod nodes should not be NULL\n")
468 REQUIRE(wgk, "Gauss-Kronrod weights should not be NULL\n")
469 REQUIRE(wg, "Gauss weights should not be NULL\n")
472 Map<
MatrixXd> eigen_subs(subs->get_array(), 2, subs->get_num_elements()/2);
473 Map<VectorXd> eigen_xgk(xgk, n);
474 Map<VectorXd> eigen_wg(wg, n/2);
475 Map<VectorXd> eigen_wgk(wgk, n);
478 VectorXd eigen_hw=(eigen_subs.row(1)-eigen_subs.row(0))/2.0;
479 VectorXd eigen_center=eigen_subs.colwise().sum()/2.0;
482 MatrixXd x=eigen_hw*eigen_xgk.adjoint()+eigen_center*
483 (VectorXd::Ones(n)).adjoint();
488 for (
index_t i=0; i<ygk.rows(); i++)
489 for (
index_t j=0; j<ygk.cols(); j++)
490 ygk(i,j)=(*f)(x(i,j));
493 VectorXd eigen_q=((ygk*eigen_wgk.asDiagonal()).rowwise().sum()).cwiseProduct(
495 q->set_array(eigen_q.data(), eigen_q.size());
498 MatrixXd yg(ygk.rows(), ygk.cols()/2);
500 for (
index_t i=1, j=0; i<ygk.cols(); i+=2, j++)
501 yg.col(j)=ygk.col(i);
504 VectorXd eigen_err=(((yg*eigen_wg.asDiagonal()).rowwise().sum()).cwiseProduct(
505 eigen_hw)-eigen_q).array().abs();
506 err->set_array(eigen_err.data(), eigen_err.size());
512 "The length of node array (%d) and weight array (%d) should be the same\n",
519 generate_gauher20(xgh, wgh);
526 eigen_xgh = MatrixXd::Zero(n,1);
527 eigen_wgh = MatrixXd::Ones(n,1);
535 v.block(0, 1, n-1, n-1).diagonal() = (0.5*ArrayXd::LinSpaced(n-1,1,n-1)).sqrt();
536 v.block(1, 0, n-1, n-1).diagonal() = v.block(0, 1, n-1, n-1).diagonal();
537 EigenSolver<MatrixXd> eig(v);
540 eigen_wgh = eig.eigenvectors().row(0).transpose().real().array().pow(2);
543 eigen_xgh = eig.eigenvalues().real()*sqrt(2.0);
551 "The length of node array (%d) and weight array (%d) should be the same\n",
553 REQUIRE(xgh.
vlen == 20,
"The length of xgh and wgh should be 20\n");
558 0.0000000000001257800672437920121938444754,
559 0.0000000002482062362315158465220083577413,
560 0.0000000612749025998290679114578012251502,
561 0.0000044021210902308611768963750310312832,
562 0.0001288262799619289543807260089991473251,
563 0.0018301031310804880686271545187082665507,
564 0.0139978374471010288959682554832397727296,
565 0.0615063720639768204967445797137770568952,
566 0.1617393339840000332507941038784338161349,
567 0.2607930634495548849471902030927594751120,
568 0.2607930634495547739248877405771054327488,
569 0.1617393339840003108065502601675689220428,
570 0.0615063720639767788633811562704067910090,
571 0.0139978374471010080792865437615546397865,
572 0.0018301031310804856833823750505985117343,
573 0.0001288262799619298488475183095403053812,
574 0.0000044021210902308865878847926600414553,
575 0.0000000612749025998294252534824241331057,
576 0.0000000002482062362315177593771748866178,
577 0.0000000000001257800672437921636551382778
582 -7.6190485416797573137159815814811736345291,
583 -6.5105901570136559541879250900819897651672,
584 -5.5787388058932032564030123467091470956802,
585 -4.7345813340460569662582201999612152576447,
586 -3.9439673506573176275935566081898286938667,
587 -3.1890148165533904744961546384729444980621,
588 -2.4586636111723669806394809711491689085960,
589 -1.7452473208141270344384565760265104472637,
590 -1.0429453488027506935509336472023278474808,
591 -0.3469641570813560282893206476728664711118,
592 0.3469641570813561393116231101885205134749,
593 1.0429453488027513596847484222962521016598,
594 1.7452473208141265903492467259638942778111,
595 2.4586636111723669806394809711491689085960,
596 3.1890148165533904744961546384729444980621,
597 3.9439673506573162953259270580019801855087,
598 4.7345813340460569662582201999612152576447,
599 5.5787388058932014800461729464586824178696,
600 6.5105901570136532896526659897062927484512,
601 7.6190485416797573137159815814811736345291
605 for (
index_t idx = 0; idx < n; idx++)
607 wgh[idx] = wgh_pre[idx];
608 xgh[idx] = xgh_pre[idx];
621 -0.991455371120812639206854697526329,
622 -0.949107912342758524526189684047851,
623 -0.864864423359769072789712788640926,
624 -0.741531185599394439863864773280788,
625 -0.586087235467691130294144838258730,
626 -0.405845151377397166906606412076961,
627 -0.207784955007898467600689403773245,
628 0.000000000000000000000000000000000,
629 0.207784955007898467600689403773245,
630 0.405845151377397166906606412076961,
631 0.586087235467691130294144838258730,
632 0.741531185599394439863864773280788,
633 0.864864423359769072789712788640926,
634 0.949107912342758524526189684047851,
635 0.991455371120812639206854697526329
641 0.129484966168869693270611432679082,
642 0.279705391489276667901467771423780,
643 0.381830050505118944950369775488975,
644 0.417959183673469387755102040816327,
645 0.381830050505118944950369775488975,
646 0.279705391489276667901467771423780,
647 0.129484966168869693270611432679082
653 0.022935322010529224963732008058970,
654 0.063092092629978553290700663189204,
655 0.104790010322250183839876322541518,
656 0.140653259715525918745189590510238,
657 0.169004726639267902826583426598550,
658 0.190350578064785409913256402421014,
659 0.204432940075298892414161999234649,
660 0.209482141084727828012999174891714,
661 0.204432940075298892414161999234649,
662 0.190350578064785409913256402421014,
663 0.169004726639267902826583426598550,
664 0.140653259715525918745189590510238,
665 0.104790010322250183839876322541518,
666 0.063092092629978553290700663189204,
667 0.022935322010529224963732008058970
671 evaluate_quadgk(f, subs, q, err, n, xgk, wg, wgk);
682 -0.995657163025808080735527280689003,
683 -0.973906528517171720077964012084452,
684 -0.930157491355708226001207180059508,
685 -0.865063366688984510732096688423493,
686 -0.780817726586416897063717578345042,
687 -0.679409568299024406234327365114874,
688 -0.562757134668604683339000099272694,
689 -0.433395394129247190799265943165784,
690 -0.294392862701460198131126603103866,
691 -0.148874338981631210884826001129720,
692 0.000000000000000000000000000000000,
693 0.148874338981631210884826001129720,
694 0.294392862701460198131126603103866,
695 0.433395394129247190799265943165784,
696 0.562757134668604683339000099272694,
697 0.679409568299024406234327365114874,
698 0.780817726586416897063717578345042,
699 0.865063366688984510732096688423493,
700 0.930157491355708226001207180059508,
701 0.973906528517171720077964012084452,
702 0.995657163025808080735527280689003
708 0.066671344308688137593568809893332,
709 0.149451349150580593145776339657697,
710 0.219086362515982043995534934228163,
711 0.269266719309996355091226921569469,
712 0.295524224714752870173892994651338,
713 0.295524224714752870173892994651338,
714 0.269266719309996355091226921569469,
715 0.219086362515982043995534934228163,
716 0.149451349150580593145776339657697,
717 0.066671344308688137593568809893332
723 0.011694638867371874278064396062192,
724 0.032558162307964727478818972459390,
725 0.054755896574351996031381300244580,
726 0.075039674810919952767043140916190,
727 0.093125454583697605535065465083366,
728 0.109387158802297641899210590325805,
729 0.123491976262065851077958109831074,
730 0.134709217311473325928054001771707,
731 0.142775938577060080797094273138717,
732 0.147739104901338491374841515972068,
733 0.149445554002916905664936468389821,
734 0.147739104901338491374841515972068,
735 0.142775938577060080797094273138717,
736 0.134709217311473325928054001771707,
737 0.123491976262065851077958109831074,
738 0.109387158802297641899210590325805,
739 0.093125454583697605535065465083366,
740 0.075039674810919952767043140916190,
741 0.054755896574351996031381300244580,
742 0.032558162307964727478818972459390,
743 0.011694638867371874278064396062192
746 evaluate_quadgk(f, subs, q, err, n, xgk, wg, wgk);
753 REQUIRE(f,
"Integrable function should not be NULL\n");
754 REQUIRE(xgh,
"Gauss-Hermite nodes should not be NULL\n");
755 REQUIRE(wgh,
"Gauss-Hermite weights should not be NULL\n");
760 q+=wgh[i]*(*f)(xgh[i]);
772 -10.52612316796054588332682628381528,
773 -9.895287586829539021204461477159608,
774 -9.373159549646721162545652439723862,
775 -8.907249099964769757295972885642943,
776 -8.477529083379863090564166344821916,
777 -8.073687285010225225858791140758144,
778 -7.68954016404049682844780422986949,
779 -7.321013032780949201189569363719477,
780 -6.965241120551107529242642193492688,
781 -6.620112262636027379036660108937914,
782 -6.284011228774828235418093195070243,
783 -5.955666326799486045344567180984366,
784 -5.634052164349972147249920483307154,
785 -5.318325224633270857323649515199378,
786 -5.007779602198768196443702627184136,
787 -4.701815647407499816097538015812822,
788 -4.399917168228137647767932535438923,
789 -4.101634474566656714970981238455522,
790 -3.806571513945360461165972000460225,
791 -3.514375935740906211539950586474333,
792 -3.224731291992035725848171110188419,
793 -2.93735082300462180968533902619139,
794 -2.651972435430635011005457785998431,
795 -2.368354588632401404111511265341516,
796 -2.086272879881762020832563302363221,
797 -1.805517171465544918903773574186889,
798 -1.525889140209863662948970133151528,
799 -1.24720015694311794069356453069359,
800 -0.9692694230711780167435414890191023,
801 -0.6919223058100445772682192875955947,
802 -0.4149888241210786845769291291996859,
803 -0.1383022449870097241150497679666744,
804 0.1383022449870097241150497679666744,
805 0.4149888241210786845769291291996859,
806 0.6919223058100445772682192875955947,
807 0.9692694230711780167435414890191023,
808 1.24720015694311794069356453069359,
809 1.525889140209863662948970133151528,
810 1.805517171465544918903773574186889,
811 2.086272879881762020832563302363221,
812 2.368354588632401404111511265341516,
813 2.651972435430635011005457785998431,
814 2.93735082300462180968533902619139,
815 3.224731291992035725848171110188419,
816 3.514375935740906211539950586474333,
817 3.806571513945360461165972000460225,
818 4.101634474566656714970981238455522,
819 4.399917168228137647767932535438923,
820 4.701815647407499816097538015812822,
821 5.007779602198768196443702627184136,
822 5.318325224633270857323649515199378,
823 5.634052164349972147249920483307154,
824 5.955666326799486045344567180984366,
825 6.284011228774828235418093195070243,
826 6.620112262636027379036660108937914,
827 6.965241120551107529242642193492688,
828 7.321013032780949201189569363719477,
829 7.68954016404049682844780422986949,
830 8.073687285010225225858791140758144,
831 8.477529083379863090564166344821916,
832 8.907249099964769757295972885642943,
833 9.373159549646721162545652439723862,
834 9.895287586829539021204461477159608,
835 10.52612316796054588332682628381528
841 5.535706535856942820575463300987E-49,
842 1.6797479901081592186662883306299E-43,
843 3.4211380112557405043272218281457E-39,
844 1.557390624629763802309335380265E-35,
845 2.549660899112999256604766580441E-32,
846 1.92910359546496685030196877906707E-29,
847 7.8617977889259103690999914962788E-27,
848 1.911706883300642829958456965534449E-24,
849 2.982862784279851154478700702016E-22,
850 3.15225456650378141612134668341E-20,
851 2.35188471067581911695767591555844E-18,
852 1.28009339132243804163956329526337E-16,
853 5.218623726590847522957808513052588E-15,
854 1.628340730709720362084307081240893E-13,
855 3.95917776694772392723644586425458E-12,
856 7.61521725014545135331529567531937E-11,
857 1.1736167423215493435425064670822E-9,
858 1.465125316476109354926622003804004E-8,
859 1.495532936727247061102461692934817E-7,
860 1.258340251031184576157842180019028E-6,
861 8.7884992308503591814440474067043E-6,
862 5.125929135786274660821911412739621E-5,
863 2.509836985130624860823620179819094E-4,
864 0.001036329099507577663456741746283101,
865 0.00362258697853445876066812537162265,
866 0.01075604050987913704946517278667313,
867 0.0272031289536889184538348212614932,
868 0.0587399819640994345496889462518317,
869 0.1084983493061868406330258455060973,
870 0.1716858423490837020007279701237768,
871 0.2329947860626780466505660293325675,
872 0.2713774249413039779456065084184279,
873 0.2713774249413039779456065084184279,
874 0.2329947860626780466505660293325675,
875 0.1716858423490837020007279701237768,
876 0.1084983493061868406330258455060973,
877 0.0587399819640994345496889462518317,
878 0.0272031289536889184538348212614932,
879 0.01075604050987913704946517278667313,
880 0.00362258697853445876066812537162265,
881 0.001036329099507577663456741746283101,
882 2.509836985130624860823620179819094E-4,
883 5.125929135786274660821911412739621E-5,
884 8.7884992308503591814440474067043E-6,
885 1.258340251031184576157842180019028E-6,
886 1.495532936727247061102461692934817E-7,
887 1.465125316476109354926622003804004E-8,
888 1.1736167423215493435425064670822E-9,
889 7.61521725014545135331529567531937E-11,
890 3.95917776694772392723644586425458E-12,
891 1.628340730709720362084307081240893E-13,
892 5.218623726590847522957808513052588E-15,
893 1.28009339132243804163956329526337E-16,
894 2.35188471067581911695767591555844E-18,
895 3.15225456650378141612134668341E-20,
896 2.982862784279851154478700702016E-22,
897 1.911706883300642829958456965534449E-24,
898 7.8617977889259103690999914962788E-27,
899 1.92910359546496685030196877906707E-29,
900 2.549660899112999256604766580441E-32,
901 1.557390624629763802309335380265E-35,
902 3.4211380112557405043272218281457E-39,
903 1.6797479901081592186662883306299E-43,
904 5.535706535856942820575463300987E-49
907 return evaluate_quadgh(f, n, xgh, wgh);
static float64_t integrate_quadgk(CFunction *f, float64_t a, float64_t b, float64_t abs_tol=1e-10, float64_t rel_tol=1e-5, uint32_t max_iter=1000, index_t sn=10)
static const float64_t INFTY
infinity
Class that contains certain methods related to numerical integration.
Class of a function of one variable.
static float64_t integrate_quadgh(CFunction *f)
static void generate_gauher20(SGVector< float64_t > xgh, SGVector< float64_t > wgh)
void set_array(T *p_array, int32_t p_num_elements, int32_t array_size)
all of classes and functions are contained in the shogun namespace
int32_t get_num_elements() const
static float64_t integrate_quadgh_customized(CFunction *f, SGVector< float64_t > xgh, SGVector< float64_t > wgh)