33 #include "TestHelper.h"
34 bool shouldShowTests =
true;
35 bool shouldThrow =
false;
36 std::string lastSuccess =
"";
37 std::ostream* testout = &std::cout;
42 namespace algorithms {
50 bool isFixedCen,
bool isFixedGamma,
bool isFixedMu);
98 const BVec& b0,
int order,
99 bool fixcen,
bool fixgam,
bool fixmu) :
100 _pimpl(new
ESImpl3(b0,order,fixcen,fixgam,fixmu))
112 #ifdef ALWAYS_NUMERIC_J
121 const BVec& _b0,
int _order,
122 bool _fixcen,
bool _fixgam,
bool _fixmu) :
123 b0order(_b0.getOrder()), bxorder(_order), bxorderp2(bxorder+2),
124 b0size(_b0.size()), bxsize((bxorder+1)*(bxorder+2)/2),
125 bxsizep2((bxorderp2+1)*(bxorderp2+2)/2),
126 b0(_b0), bx(bxsize), bxsave(bxsize),
127 Daug(bxsize,bxsizep2), Saug(bxsize,bxsizep2),
128 Taug(bxsize,bxsizep2),
131 dbdE(6), Db0(bxsize), SDb0(bxsize), GDb0(bxsizep2), GthDb0(bxsizep2),
132 fixcen(_fixcen), fixgam(_fixgam), fixmu(_fixmu),
133 numeric_j(false), zerob11(true),
134 U((fixcen?0:2)+(fixgam?0:2)+(fixmu?0:1),5),
135 xinit(5), xx(5), ff(5), jj(5,5),
137 Gx(bxsizep2,bxsize), Gy(bxsizep2,bxsize),
138 Gg1(bxsizep2,bxsize), Gg2(bxsizep2,bxsize),
139 Gth(bxsizep2,bxsize), Gmu(bxsizep2,bxsize)
152 if (!
fixcen) {
U(k++,0) = 1.;
U(k++,1) = 1.; }
153 if (!
fixgam) {
U(k++,2) = 1.;
U(k++,3) = 1.; }
154 if (!
fixmu) {
U(k,4) = 1.; }
168 T.TMV_diag().TMV_setAllTo(1.);
171 S.TMV_diag().TMV_setAllTo(1.);
174 D.TMV_diag().TMV_setAllTo(1.);
187 if ((x-xinit).TMV_normInf() >
MAX_X_DEV) {
188 dbg<<
"bad x-xinit: "<<(x-xinit)<<std::endl;
189 xdbg<<
"x = "<<x<<std::endl;
190 f = 2.* bx.TMV_subVector(1,6) / bx(0);
195 std::complex<double> zc(x[0],x[1]);
196 std::complex<double> g(x[2],x[3]);
199 if (
norm(g) > 0.99) {
200 dbg<<
"bad gsq = "<<
norm(g)<<std::endl;
201 xdbg<<
"x = "<<x<<std::endl;
202 f = 2.* bx.TMV_subVector(1,6) / bx(0);
220 }
else if (fixm != 0.) {
234 }
else if (fixg1 != 0. || fixg2 != 0.) {
255 }
else if (fixuc != 0. || fixvc != 0.) {
265 dbg<<
"bad bx(0): "<<bx(0)<<std::endl;
266 xdbg<<
"x = "<<x<<std::endl;
267 xdbg<<
"bx = "<<bx<<std::endl;
268 f = 2.* bxsave.TMV_subVector(1,6) / bxsave(0);
273 f = bx.TMV_subVector(1,6) / bx(0);
275 if (!zerob11) f(4) = 0.;
287 if ((x-xinit).TMV_normInf() >
MAX_X_DEV) {
288 dbg<<
"bad x-xinit: "<<(x-xinit)<<std::endl;
289 xdbg<<
"x = "<<x<<std::endl;
290 f = 2.* bx.TMV_subVector(1,6) / bx(0);
295 std::complex<double> zc(x[0],x[1]);
309 }
else if (fixm != 0.) {
326 }
else if (fixuc != 0. || fixvc != 0.) {
336 dbg<<
"bad bx(0): "<<bx(0)<<std::endl;
337 xdbg<<
"x = "<<x<<std::endl;
338 xdbg<<
"bx = "<<x<<std::endl;
339 f = 2.* bxsave.TMV_subVector(1,6) / bxsave(0);
345 f = bx.TMV_subVector(1,6) / bx(0);
347 if (!zerob11) f(4) = 0.;
359 if ((x-xinit).TMV_normInf() >
MAX_X_DEV) {
360 dbg<<
"bad x-xinit: "<<(x-xinit)<<std::endl;
361 xdbg<<
"x = "<<x<<std::endl;
362 f = 2.* bx.TMV_subVector(1,6) / bx(0);
367 std::complex<double> zc(x[0],x[1]);
379 }
else if (fixuc != 0. || fixvc != 0.) {
384 dbg<<
"bad bx(0): "<<bx(0)<<std::endl;
385 xdbg<<
"x = "<<x<<std::endl;
386 xdbg<<
"bx = "<<x<<std::endl;
387 f = 2.* bxsave.TMV_subVector(1,6) / bxsave(0);
392 f = bx.TMV_subVector(1,6) / bx(0);
394 if (!zerob11) f(4) = 0.;
415 Assert(J.TMV_rowsize() == 5);
416 Assert(J.TMV_colsize() == 5);
418 std::complex<double> zc(x[0],x[1]);
419 std::complex<double> g(x[2],x[3]);
423 if (!fixcen || !fixgam) {
440 J.col(0) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
445 J.col(1) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
463 SDb0 = fact * Saug * GDb0;
466 J.col(2) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
470 SDb0 = fact * Saug * GDb0;
473 J.col(3) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
490 J.col(4) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
497 if (!zerob11) J.row(4).setZero();
502 dbg<<
"dE = "<<dE<<std::endl;
512 DMatrix Dbig(bxsizep2,bxsizep2,0.);
520 DMatrix Gmubig(bxsizep2,bxsize,0.);
523 DMatrix dDdmu_num = (D2-D1)/(2.*dE);
524 DMatrix d2D_num = (D2+D1-2.*D0)/(dE*dE);
526 dbg<<
"Gmu = "<<Gmubig.TMV_subMatrix(0,6,0,6)<<std::endl;
527 dbg<<
"D = "<<Dbig.TMV_subMatrix(0,6,0,6)<<std::endl;
528 dbg<<
"dDdmu = "<<dDdmu.TMV_subMatrix(0,6,0,6)<<std::endl;
529 dbg<<
"dDdmu_num = "<<dDdmu_num.TMV_subMatrix(0,6,0,6)<<std::endl;
530 dbg<<
"Norm(dD/dmu - numeric dD/dmu) = "<<Norm(dDdmu-dDdmu_num)<<std::endl;
531 dbg<<
"dE*Norm(d2D_num) = "<<dE*Norm(d2D_num)<<std::endl;
532 test(Norm(dDdmu-dDdmu_num) < 30.*dE*Norm(d2D_num),
"dDdmu");
537 DMatrix Sbig(bxsizep2,bxsizep2,0.);
545 DMatrix Gg1big(bxsizep2,bxsize,0.);
546 DMatrix Gthbig(bxsizep2,bxsize,0.);
550 DMatrix dSdg1_num = (S2-S1)/(2.*dE);
551 DMatrix d2S_num = (S2+S1-2.*S0)/(dE*dE);
553 dbg<<
"Gg1 = "<<Gg1big.TMV_subMatrix(0,6,0,6)<<std::endl;
554 dbg<<
"Gth = "<<Gthbig.TMV_subMatrix(0,6,0,6)<<std::endl;
555 dbg<<
"S = "<<Sbig.TMV_subMatrix(0,6,0,6)<<std::endl;
556 dbg<<
"dSdg1 = "<<dSdg1.TMV_subMatrix(0,6,0,6)<<std::endl;
557 dbg<<
"dSdg1_num = "<<dSdg1_num.TMV_subMatrix(0,6,0,6)<<std::endl;
558 dbg<<
"Norm(dS/dg1 - numeric dS/dg1) = "<<Norm(dSdg1-dSdg1_num)<<std::endl;
559 dbg<<
"dE*Norm(d2S_num) = "<<dE*Norm(d2S_num)<<std::endl;
560 test(Norm(dSdg1-dSdg1_num) < 30.*dE*Norm(d2S_num),
"dSdg1");
567 DMatrix Gg2big(bxsizep2,bxsize,0.);
570 DMatrix dSdg2_num = (S2-S1)/(2.*dE);
571 d2S_num = (S2+S1-2.*S0)/(dE*dE);
573 dbg<<
"Gg2 = "<<Gg2big.TMV_subMatrix(0,6,0,6)<<std::endl;
574 dbg<<
"Gth = "<<Gthbig.TMV_subMatrix(0,6,0,6)<<std::endl;
575 dbg<<
"S = "<<Sbig.TMV_subMatrix(0,6,0,6)<<std::endl;
576 dbg<<
"dSdg2 = "<<dSdg2.TMV_subMatrix(0,6,0,6)<<std::endl;
577 dbg<<
"dSdg2_num = "<<dSdg2_num.TMV_subMatrix(0,6,0,6)<<std::endl;
578 dbg<<
"Norm(dS/dg2 - numeric dS/dg2) = "<<Norm(dSdg2-dSdg2_num)<<std::endl;
579 dbg<<
"dE*Norm(d2S_num) = "<<dE*Norm(d2S_num)<<std::endl;
580 test(Norm(dSdg2-dSdg2_num) < 30.*dE*Norm(d2S_num),
"dSdg2");
585 DMatrix Tbig(bxsizep2,bxsizep2,0.);
593 DMatrix Gxbig(bxsizep2,bxsize,0.);
594 setupGx(Gxbig,bxorderp2,bxorder);
596 DMatrix dTdx_num = (T2-T1)/(2.*dE);
597 DMatrix d2T_num = (T2+T1-2.*T0)/(dE*dE);
599 dbg<<
"Gx = "<<Gxbig.TMV_subMatrix(0,6,0,6)<<std::endl;
600 dbg<<
"T = "<<Tbig.TMV_subMatrix(0,6,0,6)<<std::endl;
601 dbg<<
"dTdx = "<<dTdx.TMV_subMatrix(0,6,0,6)<<std::endl;
602 dbg<<
"dTdx_num = "<<dTdx_num.TMV_subMatrix(0,6,0,6)<<std::endl;
603 dbg<<
"Norm(dT/dx - numeric dT/dx) = "<<Norm(dTdx-dTdx_num)<<std::endl;
604 dbg<<
"dE*Norm(d2T_num) = "<<dE*Norm(d2T_num)<<std::endl;
605 test(Norm(dTdx-dTdx_num) < 30.*dE*Norm(d2T_num),
"dTdx");
612 DMatrix Gybig(bxsizep2,bxsize,0.);
613 setupGy(Gybig,bxorderp2,bxorder,0.);
615 DMatrix dTdy_num = (T2-T1)/(2.*dE);
616 d2T_num = (T2+T1-2.*T0)/(dE*dE);
618 dbg<<
"Gy = "<<Gybig.TMV_subMatrix(0,6,0,6)<<std::endl;
619 dbg<<
"T = "<<Tbig.TMV_subMatrix(0,6,0,6)<<std::endl;
620 dbg<<
"dTdy = "<<dTdy.TMV_subMatrix(0,6,0,6)<<std::endl;
621 dbg<<
"dTdy_num = "<<dTdy_num.TMV_subMatrix(0,6,0,6)<<std::endl;
622 dbg<<
"Norm(dT/dy - numeric dT/dy) = "<<Norm(dTdy-dTdy_num)<<std::endl;
623 dbg<<
"dE*Norm(d2T_num) = "<<dE*Norm(d2T_num)<<std::endl;
624 test(Norm(dTdy-dTdy_num) < 30.*dE*Norm(d2T_num),
"dTdy");
634 DVector bx0 = bx.TMV_subVector(0,6);
635 dbg<<
"x0 = "<<x0<<std::endl;
636 dbg<<
"b0 = "<<bx0<<std::endl;
637 dbg<<
"f0 = "<<f0<<std::endl;
639 for(
int k=0;k<5;k++) {
640 dbg<<
"k = "<<k<<std::endl;
645 DVector b1 = bx.TMV_subVector(0,6);
646 dbg<<
"x1 = "<<x1<<std::endl;
647 dbg<<
"b1 = "<<b1<<std::endl;
648 dbg<<
"f1 = "<<f1<<std::endl;
653 DVector b2 = bx.TMV_subVector(0,6);
654 dbg<<
"x2 = "<<x2<<std::endl;
655 dbg<<
"b2 = "<<b2<<std::endl;
656 dbg<<
"f2 = "<<f2<<std::endl;
658 DVector dbdE_num = (b2-b1)/(2.*dE);
659 dbg<<
"dbdE_num = "<<dbdE_num<<std::endl;
660 DVector d2bdE = (b2+b1-2.*bx0)/(dE*dE);
661 dbg<<
"d2bdE = "<<d2bdE<<std::endl;
667 dbg<<
"Db0 = "<<Db0<<std::endl;
669 case 0: SDb0 = S * Db0;
670 dbg<<
"SDb0 = "<<SDb0<<std::endl;
672 dbg<<
"GDb0 = "<<GDb0<<std::endl;
674 dbg<<
"dbdx = "<<dbdE<<std::endl;
675 dbg<<
"dbdx = dTdx S D b0 = "<<
678 case 1: SDb0 = S * Db0;
679 dbg<<
"SDb0 = "<<SDb0<<std::endl;
681 dbg<<
"GDb0 = "<<GDb0<<std::endl;
683 dbg<<
"dbdy = "<<dbdE<<std::endl;
684 dbg<<
"dbdy = dTdy S D b0 = "<<
687 case 2: GDb0 = Gg1 * Db0;
688 dbg<<
"GDb0 = "<<GDb0<<std::endl;
690 dbg<<
"GthDb0 = "<<GthDb0<<std::endl;
692 dbg<<
"GDb0 = "<<GDb0<<std::endl;
694 dbg<<
"SDb0 = "<<SDb0<<std::endl;
696 dbg<<
"dbdg1 = "<<dbdE<<std::endl;
697 dbg<<
"dbdg1 = T dSdg1 D b0 = "<<
700 case 3: GDb0 = Gg2 * Db0;
701 dbg<<
"GDb0 = "<<GDb0<<std::endl;
703 dbg<<
"GthDb0 = "<<GthDb0<<std::endl;
705 dbg<<
"GDb0 = "<<GDb0<<std::endl;
707 dbg<<
"SDb0 = "<<SDb0<<std::endl;
709 dbg<<
"dbdg2 = "<<dbdE<<std::endl;
710 dbg<<
"dbdg2 = T dSdg2 D b0 = "<<
714 dbg<<
"GDb0 = "<<GDb0<<std::endl;
716 dbg<<
"Db0 = "<<Db0<<std::endl;
718 dbg<<
"SDb0 = "<<SDb0<<std::endl;
720 dbg<<
"dbdmu = "<<dbdE<<std::endl;
721 dbg<<
"dbdmu = T S dDdmu b0 = "<<
726 dbg<<
"dbdE = "<<dbdE<<std::endl;
727 dbg<<
"Norm(dbdE-dbdE_num) = "<<Norm(dbdE-dbdE_num)<<std::endl;
728 dbg<<
"dE*Norm(d2bdE) = "<<dE*Norm(d2bdE)<<std::endl;
729 test(Norm(dbdE-dbdE_num) < 30.*dE*Norm(d2bdE),
"dbdE");
731 DVector dfdE = dbdE.TMV_subVector(1,6) - dbdE(0) * f0;
733 dbg<<
"dfdE from dbdE = "<<dfdE<<std::endl;
734 dbg<<
"dfdE from dbdE_num = "<<
735 (dbdE_num.TMV_subVector(1,6) - dbdE_num(0) * f0)/bx(0)<<std::endl;;
736 dbg<<
"dfdE in J = "<<J.col(k)<<std::endl;
737 J_num.col(k) = (f2-f1)/(2.*dE);
738 dbg<<
"dfdE_num = "<<J_num.col(k)<<std::endl;
739 DVector d2f_num = (f2+f1-2.*f0)/(dE*dE);
740 dbg<<
"d2f_num = "<<d2f_num<<std::endl;
741 dbg<<
"Norm(dfdE_num-dfdE) ="<<Norm(J_num.col(k)-dfdE)<<std::endl;
742 dbg<<
"dE*Norm(d2f_num) ="<<dE*Norm(d2f_num)<<std::endl;
743 test(Norm(J_num.col(k)-dfdE) < 30.*dE*Norm(d2f_num),
"dfdE");
746 dbg<<
"J_num = "<<J_num<<std::endl;
747 dbg<<
"analytic: "<<J<<std::endl;
750 if (fixmu) J_num.col(4).setZero();
751 dbg<<
"J_num => "<<J_num<<std::endl;
752 dbg<<
"Norm(diff) = "<<Norm(J-J_num)<<std::endl;
753 dbg<<
"dE*Norm(J) = "<<dE*Norm(J_num)<<std::endl;
754 test(Norm(J_num-J) < dE*Norm(J_num),
"dfdE");
767 Assert(J.TMV_rowsize() == 5);
768 Assert(J.TMV_colsize() == 5);
770 std::complex<double> zc(x[0],x[1]);
781 J.col(0) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
786 J.col(1) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
804 J.col(4) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
811 if (!zerob11) J.row(4).setZero();
823 Assert(J.TMV_rowsize() == 5);
824 Assert(J.TMV_colsize() == 5);
826 std::complex<double> zc(x[0],x[1]);
834 J.col(0) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
839 J.col(1) = dbdE.TMV_subVector(1,6) - dbdE(0) * f;
843 if (!zerob11) J.row(4).setZero();
849 Assert(x.size() == U.TMV_colsize());
850 Assert(f.size() == U.TMV_colsize());
852 Assert(xx.size() == U.TMV_rowsize());
854 if (fixcen) { xx[0] = fixuc; xx[1] = fixvc; }
855 if (fixgam) { xx[2] = fixg1; xx[3] = fixg2; }
856 if (fixmu) { xx[4] = fixm; }
858 if (!fixgam || fixg1 != 0. || fixg2 != 0.)
860 else if (!fixmu || fixm != 0.)
871 Assert(x.size() == U.TMV_colsize());
872 Assert(f.size() == U.TMV_colsize());
873 Assert(j.TMV_rowsize() == U.TMV_colsize());
874 Assert(j.TMV_colsize() == U.TMV_colsize());
877 if (fixcen) { xx[0] = fixuc; xx[1] = fixvc; }
878 if (fixgam) { xx[2] = fixg1; xx[3] = fixg2; }
879 if (fixmu) { xx[4] = fixm; }
881 if (!fixgam || fixg1 != 0. || fixg2 != 0.)
883 else if (!fixmu || fixm != 0.)
888 j = U*jj*U.transpose();
900 dbg<<
"getCovariance:\n";
901 dbg<<
"cov1 = "<<cov1<<std::endl;
902 dbg<<
"full cov = "<<cov<<std::endl;
910 dbg<<
"getInverseCovariance:\n";
911 dbg<<
"invcov1 = "<<invcov1<<std::endl;
912 dbg<<
"full invcov = "<<invcov<<std::endl;
958 xdbg<<
"Caught exception during NLSolver::solve"<<std::endl;
973 std::ostream* os,
double relerr)
const
virtual void getCovariance(DMatrix &cov) const
void calculateJ(const DVector &x, const DVector &f, DMatrix &J) const
void augmentMuTransformCols(double mu, int order, DMatrix &D)
void setupGg1(DMatrix &Gg1, int order1, int order2)
ESImpl3(const BVec &b0, int order, bool isFixedCen, bool isFixedGamma, bool isFixedMu)
void setupGth(DMatrix &Gth, int order1, int order2)
virtual bool solve(DVector &x, DVector &f) const
void getCovariance(DMatrix &cov) const
void getInverseCovariance(DMatrix &invcov) const
void calculateGTransform(std::complex< double > g, int order1, int order2, DMatrix &S)
bool solve(DVector &x, DVector &f) const
void augmentZTransformCols(std::complex< double > z, int order, DMatrix &T)
void calculateZTransform(std::complex< double > z, int order1, int order2, DMatrix &T)
virtual void calculateJ(const DVector &x, const DVector &f, DMatrix &j) const
#define TMV_colRange(m, j1, j2)
void doF1(const DVector &x, DVector &f) const
void calculateJ(const DVector &x, const DVector &f, DMatrix &df) const
void doF2(const DVector &x, DVector &f) const
void setupGx(DMatrix &Gx, int order1, int order2)
Eigen::Block< DMatrix > DMatrixView
void setupGy(DMatrix &Gy, int order1, int order2)
void augmentGTransformCols(std::complex< double > g, int order, DMatrix &S)
void calculateF(const DVector &x, DVector &f) const
void setupGg2(DMatrix &Gg2, int order1, int order2)
EllipseSolver3(const BVec &b0, int order, bool fixcen=false, bool fixgam=false, bool fixmu=false)
virtual bool testJ(const DVector &, DVector &, std::ostream *os=0, double relerr=0.) const
virtual void getInverseCovariance(DMatrix &invcov) const
void doJ1(const DVector &x, const DVector &f, DMatrix &J) const
#define TMV_rowRange(m, i1, i2)
bool testJ(const DVector &x, DVector &f, std::ostream *os=0, double relerr=0.) const
void calculateF(const DVector &x, DVector &f) const
void doJ2(const DVector &x, const DVector &f, DMatrix &J) const
std::ostream *const dbgout
#define EIGEN_Transpose(m)
void callF(const DVector &x, DVector &f) const
void doJ3(const DVector &x, const DVector &f, DMatrix &J) const
void doF3(const DVector &x, DVector &f) const
void calculateMuTransform(double mu, int order1, int order2, DMatrix &D)
void setupGmu(DMatrix &Gmu, int order1, int order2)