24 using namespace shogun;
25 using namespace Eigen;
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 class CITransformFunction :
public CFunction
55 virtual ~CITransformFunction() {
SG_UNREF(m_f); }
70 return (*m_f)(gx)*dgx;
93 class CILTransformFunction :
public CFunction
108 virtual ~CILTransformFunction() {
SG_UNREF(m_f); }
123 return -(*m_f)(m_b-
CMath::sq(gx))*2*gx*dgx;
149 class CIUTransformFunction :
public CFunction
164 virtual ~CIUTransformFunction() {
SG_UNREF(m_f); }
179 return (*m_f)(m_a+
CMath::sq(gx))*2*gx*dgx;
200 class CTransformFunction :
public CFunction
217 virtual ~CTransformFunction() {
SG_UNREF(m_f); }
233 return (*m_f)(gx)*dgx;
254 REQUIRE(f,
"Integrable function should not be NULL\n")
255 REQUIRE(abs_tol>0.0,
"Absolute tolerance must be positive, but is %f\n",
257 REQUIRE(rel_tol>0.0,
"Relative tolerance must be positive, but is %f\n",
259 REQUIRE(max_iter>0,
"Maximum number of iterations must be greater than 0, "
260 "but is %d\n", max_iter)
261 REQUIRE(sn>0,
"Initial number of subintervals must be greater than 0, "
265 typedef void TQuadGKEvaluationFunction(
CFunction* f,
269 TQuadGKEvaluationFunction* evaluate_quadgk;
293 tf=
new CITransformFunction(f);
294 evaluate_quadgk=&evaluate_quadgk15;
300 tf=
new CILTransformFunction(f, b);
301 evaluate_quadgk=&evaluate_quadgk15;
307 tf=
new CIUTransformFunction(f, a);
308 evaluate_quadgk=&evaluate_quadgk15;
314 tf=
new CTransformFunction(f, a, b);
315 evaluate_quadgk=&evaluate_quadgk21;
337 evaluate_quadgk(tf, subs, q_subs, err_subs);
357 while (err>tol && iter<max_iter)
364 (*subs)[2*i])/(tb-ta))
367 float64_t mid=((*subs)[2*i]+(*subs)[2*i+1])/2.0;
391 evaluate_quadgk(tf, subs, q_subs, err_subs);
409 SG_SWARNING(
"Error tolerance not met. Estimated error is equal to %g "
410 "after %d iterations\n", err, iter)
439 REQUIRE(f,
"Integrable function should not be NULL\n")
440 REQUIRE(subs, "Array of subintervals should not be NULL\n")
441 REQUIRE(!(subs->get_array_size()%2), "Size of the array of subintervals "
443 REQUIRE(q, "Array of values of integrals should not be NULL\n")
444 REQUIRE(err, "Array of errors should not be NULL\n")
445 REQUIRE(n%2, "Order of Gauss-Kronrod should be odd\n")
446 REQUIRE(xgk, "Gauss-Kronrod nodes should not be NULL\n")
447 REQUIRE(wgk, "Gauss-Kronrod weights should not be NULL\n")
448 REQUIRE(wg, "Gauss weights should not be NULL\n")
451 Map<MatrixXd> eigen_subs(subs->get_array(), 2, subs->get_num_elements()/2);
452 Map<VectorXd> eigen_xgk(xgk, n);
453 Map<VectorXd> eigen_wg(wg, n/2);
454 Map<VectorXd> eigen_wgk(wgk, n);
457 VectorXd eigen_hw=(eigen_subs.row(1)-eigen_subs.row(0))/2.0;
458 VectorXd eigen_center=eigen_subs.colwise().sum()/2.0;
461 MatrixXd x=eigen_hw*eigen_xgk.adjoint()+eigen_center*
462 (VectorXd::Ones(n)).adjoint();
465 MatrixXd ygk(x.rows(), x.cols());
467 for (
index_t i=0; i<ygk.rows(); i++)
468 for (
index_t j=0; j<ygk.cols(); j++)
469 ygk(i,j)=(*f)(x(i,j));
472 VectorXd eigen_q=((ygk*eigen_wgk.asDiagonal()).rowwise().sum()).cwiseProduct(
474 q->set_array(eigen_q.data(), eigen_q.size());
477 MatrixXd yg(ygk.rows(), ygk.cols()/2);
479 for (
index_t i=1, j=0; i<ygk.cols(); i+=2, j++)
480 yg.col(j)=ygk.col(i);
483 VectorXd eigen_err=(((yg*eigen_wg.asDiagonal()).rowwise().sum()).cwiseProduct(
484 eigen_hw)-eigen_q).array().abs();
485 err->set_array(eigen_err.data(), eigen_err.size());
496 -0.991455371120812639206854697526329,
497 -0.949107912342758524526189684047851,
498 -0.864864423359769072789712788640926,
499 -0.741531185599394439863864773280788,
500 -0.586087235467691130294144838258730,
501 -0.405845151377397166906606412076961,
502 -0.207784955007898467600689403773245,
503 0.000000000000000000000000000000000,
504 0.207784955007898467600689403773245,
505 0.405845151377397166906606412076961,
506 0.586087235467691130294144838258730,
507 0.741531185599394439863864773280788,
508 0.864864423359769072789712788640926,
509 0.949107912342758524526189684047851,
510 0.991455371120812639206854697526329
516 0.129484966168869693270611432679082,
517 0.279705391489276667901467771423780,
518 0.381830050505118944950369775488975,
519 0.417959183673469387755102040816327,
520 0.381830050505118944950369775488975,
521 0.279705391489276667901467771423780,
522 0.129484966168869693270611432679082
528 0.022935322010529224963732008058970,
529 0.063092092629978553290700663189204,
530 0.104790010322250183839876322541518,
531 0.140653259715525918745189590510238,
532 0.169004726639267902826583426598550,
533 0.190350578064785409913256402421014,
534 0.204432940075298892414161999234649,
535 0.209482141084727828012999174891714,
536 0.204432940075298892414161999234649,
537 0.190350578064785409913256402421014,
538 0.169004726639267902826583426598550,
539 0.140653259715525918745189590510238,
540 0.104790010322250183839876322541518,
541 0.063092092629978553290700663189204,
542 0.022935322010529224963732008058970
546 evaluate_quadgk(f, subs, q, err, n, xgk, wg, wgk);
557 -0.995657163025808080735527280689003,
558 -0.973906528517171720077964012084452,
559 -0.930157491355708226001207180059508,
560 -0.865063366688984510732096688423493,
561 -0.780817726586416897063717578345042,
562 -0.679409568299024406234327365114874,
563 -0.562757134668604683339000099272694,
564 -0.433395394129247190799265943165784,
565 -0.294392862701460198131126603103866,
566 -0.148874338981631210884826001129720,
567 0.000000000000000000000000000000000,
568 0.148874338981631210884826001129720,
569 0.294392862701460198131126603103866,
570 0.433395394129247190799265943165784,
571 0.562757134668604683339000099272694,
572 0.679409568299024406234327365114874,
573 0.780817726586416897063717578345042,
574 0.865063366688984510732096688423493,
575 0.930157491355708226001207180059508,
576 0.973906528517171720077964012084452,
577 0.995657163025808080735527280689003
583 0.066671344308688137593568809893332,
584 0.149451349150580593145776339657697,
585 0.219086362515982043995534934228163,
586 0.269266719309996355091226921569469,
587 0.295524224714752870173892994651338,
588 0.295524224714752870173892994651338,
589 0.269266719309996355091226921569469,
590 0.219086362515982043995534934228163,
591 0.149451349150580593145776339657697,
592 0.066671344308688137593568809893332
598 0.011694638867371874278064396062192,
599 0.032558162307964727478818972459390,
600 0.054755896574351996031381300244580,
601 0.075039674810919952767043140916190,
602 0.093125454583697605535065465083366,
603 0.109387158802297641899210590325805,
604 0.123491976262065851077958109831074,
605 0.134709217311473325928054001771707,
606 0.142775938577060080797094273138717,
607 0.147739104901338491374841515972068,
608 0.149445554002916905664936468389821,
609 0.147739104901338491374841515972068,
610 0.142775938577060080797094273138717,
611 0.134709217311473325928054001771707,
612 0.123491976262065851077958109831074,
613 0.109387158802297641899210590325805,
614 0.093125454583697605535065465083366,
615 0.075039674810919952767043140916190,
616 0.054755896574351996031381300244580,
617 0.032558162307964727478818972459390,
618 0.011694638867371874278064396062192
621 evaluate_quadgk(f, subs, q, err, n, xgk, wg, wgk);
628 REQUIRE(f,
"Integrable function should not be NULL\n");
629 REQUIRE(xgh,
"Gauss-Hermite nodes should not be NULL\n");
630 REQUIRE(wgh,
"Gauss-Hermite weights should not be NULL\n");
635 q+=wgh[i]*(*f)(xgh[i]);
647 -10.52612316796054588332682628381528,
648 -9.895287586829539021204461477159608,
649 -9.373159549646721162545652439723862,
650 -8.907249099964769757295972885642943,
651 -8.477529083379863090564166344821916,
652 -8.073687285010225225858791140758144,
653 -7.68954016404049682844780422986949,
654 -7.321013032780949201189569363719477,
655 -6.965241120551107529242642193492688,
656 -6.620112262636027379036660108937914,
657 -6.284011228774828235418093195070243,
658 -5.955666326799486045344567180984366,
659 -5.634052164349972147249920483307154,
660 -5.318325224633270857323649515199378,
661 -5.007779602198768196443702627184136,
662 -4.701815647407499816097538015812822,
663 -4.399917168228137647767932535438923,
664 -4.101634474566656714970981238455522,
665 -3.806571513945360461165972000460225,
666 -3.514375935740906211539950586474333,
667 -3.224731291992035725848171110188419,
668 -2.93735082300462180968533902619139,
669 -2.651972435430635011005457785998431,
670 -2.368354588632401404111511265341516,
671 -2.086272879881762020832563302363221,
672 -1.805517171465544918903773574186889,
673 -1.525889140209863662948970133151528,
674 -1.24720015694311794069356453069359,
675 -0.9692694230711780167435414890191023,
676 -0.6919223058100445772682192875955947,
677 -0.4149888241210786845769291291996859,
678 -0.1383022449870097241150497679666744,
679 0.1383022449870097241150497679666744,
680 0.4149888241210786845769291291996859,
681 0.6919223058100445772682192875955947,
682 0.9692694230711780167435414890191023,
683 1.24720015694311794069356453069359,
684 1.525889140209863662948970133151528,
685 1.805517171465544918903773574186889,
686 2.086272879881762020832563302363221,
687 2.368354588632401404111511265341516,
688 2.651972435430635011005457785998431,
689 2.93735082300462180968533902619139,
690 3.224731291992035725848171110188419,
691 3.514375935740906211539950586474333,
692 3.806571513945360461165972000460225,
693 4.101634474566656714970981238455522,
694 4.399917168228137647767932535438923,
695 4.701815647407499816097538015812822,
696 5.007779602198768196443702627184136,
697 5.318325224633270857323649515199378,
698 5.634052164349972147249920483307154,
699 5.955666326799486045344567180984366,
700 6.284011228774828235418093195070243,
701 6.620112262636027379036660108937914,
702 6.965241120551107529242642193492688,
703 7.321013032780949201189569363719477,
704 7.68954016404049682844780422986949,
705 8.073687285010225225858791140758144,
706 8.477529083379863090564166344821916,
707 8.907249099964769757295972885642943,
708 9.373159549646721162545652439723862,
709 9.895287586829539021204461477159608,
710 10.52612316796054588332682628381528
716 5.535706535856942820575463300987E-49,
717 1.6797479901081592186662883306299E-43,
718 3.4211380112557405043272218281457E-39,
719 1.557390624629763802309335380265E-35,
720 2.549660899112999256604766580441E-32,
721 1.92910359546496685030196877906707E-29,
722 7.8617977889259103690999914962788E-27,
723 1.911706883300642829958456965534449E-24,
724 2.982862784279851154478700702016E-22,
725 3.15225456650378141612134668341E-20,
726 2.35188471067581911695767591555844E-18,
727 1.28009339132243804163956329526337E-16,
728 5.218623726590847522957808513052588E-15,
729 1.628340730709720362084307081240893E-13,
730 3.95917776694772392723644586425458E-12,
731 7.61521725014545135331529567531937E-11,
732 1.1736167423215493435425064670822E-9,
733 1.465125316476109354926622003804004E-8,
734 1.495532936727247061102461692934817E-7,
735 1.258340251031184576157842180019028E-6,
736 8.7884992308503591814440474067043E-6,
737 5.125929135786274660821911412739621E-5,
738 2.509836985130624860823620179819094E-4,
739 0.001036329099507577663456741746283101,
740 0.00362258697853445876066812537162265,
741 0.01075604050987913704946517278667313,
742 0.0272031289536889184538348212614932,
743 0.0587399819640994345496889462518317,
744 0.1084983493061868406330258455060973,
745 0.1716858423490837020007279701237768,
746 0.2329947860626780466505660293325675,
747 0.2713774249413039779456065084184279,
748 0.2713774249413039779456065084184279,
749 0.2329947860626780466505660293325675,
750 0.1716858423490837020007279701237768,
751 0.1084983493061868406330258455060973,
752 0.0587399819640994345496889462518317,
753 0.0272031289536889184538348212614932,
754 0.01075604050987913704946517278667313,
755 0.00362258697853445876066812537162265,
756 0.001036329099507577663456741746283101,
757 2.509836985130624860823620179819094E-4,
758 5.125929135786274660821911412739621E-5,
759 8.7884992308503591814440474067043E-6,
760 1.258340251031184576157842180019028E-6,
761 1.495532936727247061102461692934817E-7,
762 1.465125316476109354926622003804004E-8,
763 1.1736167423215493435425064670822E-9,
764 7.61521725014545135331529567531937E-11,
765 3.95917776694772392723644586425458E-12,
766 1.628340730709720362084307081240893E-13,
767 5.218623726590847522957808513052588E-15,
768 1.28009339132243804163956329526337E-16,
769 2.35188471067581911695767591555844E-18,
770 3.15225456650378141612134668341E-20,
771 2.982862784279851154478700702016E-22,
772 1.911706883300642829958456965534449E-24,
773 7.8617977889259103690999914962788E-27,
774 1.92910359546496685030196877906707E-29,
775 2.549660899112999256604766580441E-32,
776 1.557390624629763802309335380265E-35,
777 3.4211380112557405043272218281457E-39,
778 1.6797479901081592186662883306299E-43,
779 5.535706535856942820575463300987E-49
782 return evaluate_quadgh(f, n, xgh, wgh);