LSSTApplications  16.0-10-g4f78f78+16,16.0-10-gc1446dd+42,16.0-11-g09ed895+1,16.0-13-g7649090,16.0-14-g0a28612+1,16.0-14-g6c7ed55+16,16.0-15-ga29f190+1,16.0-16-g89065d4+14,16.0-16-gd8e3590+16,16.0-16-ge6a35c8+6,16.0-17-g7e0e4ff+10,16.0-17-ga3d2e9f,16.0-19-gb830ed4e+16,16.0-2-g0febb12+21,16.0-2-g9d5294e+61,16.0-2-ga8830df+5,16.0-24-gc1c7f52+9,16.0-25-g07af9f2+1,16.0-3-ge00e371+21,16.0-36-g07840cb1,16.0-4-g18f3627+5,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+16,16.0-4-gade8416+9,16.0-4-gb13d127+5,16.0-5-g6a53317+21,16.0-5-gb3f8a4b+74,16.0-5-gef99c9f+12,16.0-6-g9321be7+4,16.0-6-gcbc7b31+22,16.0-6-gf49912c+16,16.0-63-gae20905ba,16.0-7-gd2eeba5+31,16.0-8-g21fd5fe+16,16.0-8-g3a9f023+12,16.0-8-g4734f7a,16.0-9-g85d1a16+16,16.0-9-gf5c1f43,master-g07ce7b41a7,w.2018.48
LSSTDataManagementBasePackage
Public Member Functions | List of all members
lsst::jointcal::GtransfoPoly Class Reference

Polynomial transformation class. More...

#include <Gtransfo.h>

Inheritance diagram for lsst::jointcal::GtransfoPoly:
lsst::jointcal::Gtransfo lsst::jointcal::GtransfoLin lsst::jointcal::GtransfoLinRot lsst::jointcal::GtransfoLinScale lsst::jointcal::GtransfoLinShift

Public Member Functions

 GtransfoPoly (const unsigned order=1)
 Default transfo : identity for all orders (>=1 ). More...
 
 GtransfoPoly (const Gtransfo *gtransfo, const Frame &frame, unsigned order, unsigned nPoint=1000)
 Constructs a "polynomial image" from an existing transfo, over a specified domain. More...
 
 GtransfoPoly (std::shared_ptr< afw::geom::TransformPoint2ToPoint2 > transform, jointcal::Frame const &domain, unsigned const order, unsigned const nSteps=50)
 Constructs a polynomial approximation to an afw::geom::TransformPoint2ToPoint2. More...
 
void setOrder (const unsigned order)
 Sets the polynomial order (the highest sum of exponents of the largest monomial). More...
 
unsigned getOrder () const
 Returns the polynomial order. More...
 
void apply (const double xIn, const double yIn, double &xOut, double &yOut) const override
 
void computeDerivative (Point const &where, GtransfoLin &derivative, const double step=0.01) const override
 specialised analytic routine More...
 
virtual void transformPosAndErrors (const FatPoint &in, FatPoint &out) const override
 a mix of apply and Derivative More...
 
int getNpar () const override
 total number of parameters More...
 
void dump (std::ostream &stream=std::cout) const override
 print out of coefficients in a readable form. More...
 
double fit (StarMatchList const &starMatchList) override
 guess what More...
 
GtransfoPoly operator* (GtransfoPoly const &right) const
 Composition (internal stuff in quadruple precision) More...
 
GtransfoPoly operator+ (GtransfoPoly const &right) const
 Addition. More...
 
GtransfoPoly operator- (GtransfoPoly const &right) const
 Subtraction. More...
 
std::unique_ptr< GtransfocomposeAndReduce (GtransfoPoly const &right) const
 Return a reduced composition of newTransfo = this(right()), or nullptr if it cannot be reduced. More...
 
std::unique_ptr< Gtransfoclone () const override
 returns a copy (allocated by new) of the transformation. More...
 
double coeff (const unsigned powX, const unsigned powY, const unsigned whichCoord) const
 access to coefficients (read only) More...
 
double & coeff (const unsigned powX, const unsigned powY, const unsigned whichCoord)
 write access More...
 
double coeffOrZero (const unsigned powX, const unsigned powY, const unsigned whichCoord) const
 read access, zero if beyond order More...
 
double determinant () const
 
double paramRef (const int i) const override
 
double & paramRef (const int i) override
 
void paramDerivatives (Point const &where, double *dx, double *dy) const override
 Derivative w.r.t parameters. More...
 
std::shared_ptr< ast::MappingtoAstMap (jointcal::Frame const &domain) const override
 Create an equivalent AST mapping for this transformation, including an analytic inverse if possible. More...
 
void write (std::ostream &s) const override
 
void read (std::istream &s)
 
void apply (Point const &in, Point &out) const
 applies the tranfo to in and writes into out. Is indeed virtual. More...
 
Point apply (Point const &in) const
 All these apply(..) shadow the virtual one in derived classes, unless one writes "using Gtransfo::apply". More...
 
Frame apply (Frame const &inputframe, bool inscribed) const
 Transform a bounding box, taking either the inscribed or circumscribed box. More...
 
std::string __str__ ()
 
void transformStar (FatPoint &in) const
 allows to write MyTransfo(MyStar) More...
 
virtual double getJacobian (Point const &point) const
 returns the local jacobian. More...
 
virtual double getJacobian (const double x, const double y) const
 returns the local jacobian. More...
 
virtual std::unique_ptr< GtransfocomposeAndReduce (Gtransfo const &right) const
 Return a reduced composition of newTransfo = this(right()), or nullptr if it cannot be reduced. More...
 
virtual GtransfoLin linearApproximation (Point const &where, const double step=0.01) const
 linear (local) approximation. More...
 
virtual void transformErrors (Point const &where, const double *vIn, double *vOut) const
 transform errors (represented as double[3] in order V(xx),V(yy),Cov(xy)) More...
 
virtual std::unique_ptr< GtransfoinverseTransfo (const double precision, const Frame &region) const
 returns an inverse transfo. Numerical if not overloaded. More...
 
void getParams (double *params) const
 params should be at least Npar() long More...
 
void offsetParams (Eigen::VectorXd const &delta)
 
virtual std::unique_ptr< GtransforoughInverse (const Frame &region) const
 Rough inverse. More...
 
void write (const std::string &fileName) const
 

Detailed Description

Polynomial transformation class.

Definition at line 276 of file Gtransfo.h.

Constructor & Destructor Documentation

◆ GtransfoPoly() [1/3]

lsst::jointcal::GtransfoPoly::GtransfoPoly ( const unsigned  order = 1)

Default transfo : identity for all orders (>=1 ).

Default transfo : identity for all orders (>=1 )

Parameters
orderThe highest total power (x+y) of monomials of this polynomial.

Definition at line 447 of file Gtransfo.cc.

447  : _order(order) {
448  _nterms = (order + 1) * (order + 2) / 2;
449 
450  // allocate and fill coefficients
451  _coeffs.resize(2 * _nterms, 0.);
452  // the default is supposed to be the identity, (for order>=1).
453  if (_order >= 1) {
454  coeff(1, 0, 0) = 1;
455  coeff(0, 1, 1) = 1;
456  }
457 }
T resize(T... args)
double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const
access to coefficients (read only)
Definition: Gtransfo.cc:711

◆ GtransfoPoly() [2/3]

lsst::jointcal::GtransfoPoly::GtransfoPoly ( const Gtransfo gtransfo,
const Frame frame,
unsigned  order,
unsigned  nPoint = 1000 
)

Constructs a "polynomial image" from an existing transfo, over a specified domain.

Definition at line 460 of file Gtransfo.cc.

460  {
461  StarMatchList sm;
462 
463  double step = std::sqrt(fabs(frame.getArea()) / double(nPoint));
464  for (double x = frame.xMin + step / 2; x <= frame.xMax; x += step)
465  for (double y = frame.yMin + step / 2; y <= frame.yMax; y += step) {
466  auto pix = std::make_shared<BaseStar>(x, y, 0, 0);
467  double xtr, ytr;
468  gtransfo->apply(x, y, xtr, ytr);
469  auto tp = std::make_shared<BaseStar>(xtr, ytr, 0, 0);
470  /* These are fake stars so no need to transform fake errors.
471  all errors (and weights) will be equal : */
472  sm.push_back(StarMatch(*pix, *tp, pix, tp));
473  }
474  GtransfoPoly ret(order);
475  ret.fit(sm);
476  *this = ret;
477 }
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
int y
Definition: SpanSet.cc:49
int const step
T fabs(T... args)
double x
T sqrt(T... args)

◆ GtransfoPoly() [3/3]

lsst::jointcal::GtransfoPoly::GtransfoPoly ( std::shared_ptr< afw::geom::TransformPoint2ToPoint2 transform,
jointcal::Frame const &  domain,
unsigned const  order,
unsigned const  nSteps = 50 
)

Constructs a polynomial approximation to an afw::geom::TransformPoint2ToPoint2.

Parameters
[in]transformThe transform to be approximated.
[in]domainThe valid domain of the transform.
[in]orderThe polynomial order to use when approximating.
[in]nStepsThe number of sample points per axis (nSteps^2 total points).

Definition at line 480 of file Gtransfo.cc.

481  {
482  jointcal::StarMatchList starMatchList;
483  double xStart = domain.xMin;
484  double yStart = domain.yMin;
485  double xStep = domain.getWidth() / (nSteps + 1);
486  double yStep = domain.getHeight() / (nSteps + 1);
487  for (unsigned i = 0; i < nSteps; ++i) {
488  for (unsigned j = 0; j < nSteps; ++j) {
489  // TODO: once DM-4044 is done, we can remove the redundancy in `Point`/`Point2D` here
490  jointcal::Point in(xStart + i * xStep, yStart + j * yStep);
491  afw::geom::Point2D inAfw(in.x, in.y);
492  afw::geom::Point2D outAfw = transform->applyForward(inAfw);
493  jointcal::Point out(outAfw.getX(), outAfw.getY());
494  starMatchList.emplace_back(in, out, nullptr, nullptr);
495  }
496  }
497  GtransfoPoly poly(order);
498  poly.fit(starMatchList);
499  *this = poly;
500 }
A point in a plane.
Definition: Point.h:36
Low-level polynomials (including special polynomials) in C++.
Definition: Basis1d.h:26
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
Point< double, 2 > Point2D
Definition: Point.h:324
T emplace_back(T... args)

Member Function Documentation

◆ __str__()

std::string lsst::jointcal::Gtransfo::__str__ ( )
inlineinherited

Definition at line 94 of file Gtransfo.h.

94  {
96  dump(s);
97  return s.str();
98  }
T str(T... args)
solver_t * s
virtual void dump(std::ostream &stream=std::cout) const =0
dumps the transfo coefficients to stream.

◆ apply() [1/4]

void lsst::jointcal::Gtransfo::apply ( Point const &  in,
Point out 
) const
inlineinherited

applies the tranfo to in and writes into out. Is indeed virtual.

Definition at line 71 of file Gtransfo.h.

71 { apply(in.x, in.y, out.x, out.y); }
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0

◆ apply() [2/4]

Point lsst::jointcal::Gtransfo::apply ( Point const &  in) const
inlineinherited

All these apply(..) shadow the virtual one in derived classes, unless one writes "using Gtransfo::apply".

Definition at line 75 of file Gtransfo.h.

75  {
76  double xout, yout;
77  apply(in.x, in.y, xout, yout);
78  return Point(xout, yout);
79  }
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0

◆ apply() [3/4]

Frame lsst::jointcal::Gtransfo::apply ( Frame const &  inputframe,
bool  inscribed 
) const
inherited

Transform a bounding box, taking either the inscribed or circumscribed box.

Parameters
[in]inputframeThe frame to be transformed.
[in]inscribedReturn the inscribed (true) or circumscribed (false) box.
Returns
The transformed frame.

Definition at line 74 of file Gtransfo.cc.

74  {
75  // 2 opposite corners
76  double xtmin1, xtmax1, ytmin1, ytmax1;
77  apply(inputframe.xMin, inputframe.yMin, xtmin1, ytmin1);
78  apply(inputframe.xMax, inputframe.yMax, xtmax1, ytmax1);
79  Frame fr1(std::min(xtmin1, xtmax1), std::min(ytmin1, ytmax1), std::max(xtmin1, xtmax1),
80  std::max(ytmin1, ytmax1));
81  // 2 other corners
82  double xtmin2, xtmax2, ytmin2, ytmax2;
83  apply(inputframe.xMin, inputframe.yMax, xtmin2, ytmax2);
84  apply(inputframe.xMax, inputframe.yMin, xtmax2, ytmin2);
85  Frame fr2(std::min(xtmin2, xtmax2), std::min(ytmin2, ytmax2), std::max(xtmin2, xtmax2),
86  std::max(ytmin2, ytmax2));
87 
88  if (inscribed) return fr1 * fr2;
89  return fr1 + fr2;
90 }
T min(T... args)
T max(T... args)
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0

◆ apply() [4/4]

void lsst::jointcal::GtransfoPoly::apply ( const double  xIn,
const double  yIn,
double &  xOut,
double &  yOut 
) const
overridevirtual

Implements lsst::jointcal::Gtransfo.

Definition at line 546 of file Gtransfo.cc.

546  {
547  /*
548  This routine computes the monomials only once for both
549  polynomials. This is why GtransfoPoly does not use an auxilary
550  class (such as PolyXY) to handle each polynomial.
551 
552  The code works even if &xIn == &xOut (or &yIn == &yOut)
553  It uses Variable Length Allocation (VLA) rather than a vector<double>
554  because allocating the later costs about 50 ns. All VLA uses are tagged.
555  */
556  double monomials[_nterms]; // this is VLA, which is (perhaps) not casher C++
557  computeMonomials(xIn, yIn, monomials);
558 
559  xOut = 0;
560  yOut = 0;
561  const double *c = &_coeffs[0];
562  const double *pm = &monomials[0];
563  // the ordering of the coefficients and the monomials are identical.
564  for (int k = _nterms; k--;) xOut += (*(pm++)) * (*(c++));
565  pm = &monomials[0];
566  for (int k = _nterms; k--;) yOut += (*(pm++)) * (*(c++));
567 }

◆ clone()

std::unique_ptr<Gtransfo> lsst::jointcal::GtransfoPoly::clone ( ) const
inlineoverridevirtual

returns a copy (allocated by new) of the transformation.

Implements lsst::jointcal::Gtransfo.

Reimplemented in lsst::jointcal::GtransfoLin.

Definition at line 337 of file Gtransfo.h.

337  {
338  return std::unique_ptr<Gtransfo>(new GtransfoPoly(*this));
339  }
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
STL class.

◆ coeff() [1/2]

double lsst::jointcal::GtransfoPoly::coeff ( const unsigned  powX,
const unsigned  powY,
const unsigned  whichCoord 
) const

access to coefficients (read only)

Definition at line 711 of file Gtransfo.cc.

711  {
712  assert((degX + degY <= _order) && whichCoord < 2);
713  /* this assertion above is enough to ensure that the index used just
714  below is within bounds since the reserved length is
715  2*_nterms=(order+1)*(order+2) */
716  return _coeffs[(degX + degY) * (degX + degY + 1) / 2 + degY + whichCoord * _nterms];
717 }

◆ coeff() [2/2]

double & lsst::jointcal::GtransfoPoly::coeff ( const unsigned  powX,
const unsigned  powY,
const unsigned  whichCoord 
)

write access

Definition at line 719 of file Gtransfo.cc.

719  {
720  assert((degX + degY <= _order) && whichCoord < 2);
721  return _coeffs[(degX + degY) * (degX + degY + 1) / 2 + degY + whichCoord * _nterms];
722 }

◆ coeffOrZero()

double lsst::jointcal::GtransfoPoly::coeffOrZero ( const unsigned  powX,
const unsigned  powY,
const unsigned  whichCoord 
) const

read access, zero if beyond order

Definition at line 724 of file Gtransfo.cc.

724  {
725  // assert((degX+degY<=order) && whichCoord<2);
726  assert(whichCoord < 2);
727  if (degX + degY <= _order)
728  return _coeffs[(degX + degY) * (degX + degY + 1) / 2 + degY + whichCoord * _nterms];
729  return 0;
730 }

◆ composeAndReduce() [1/2]

std::unique_ptr< Gtransfo > lsst::jointcal::Gtransfo::composeAndReduce ( Gtransfo const &  right) const
virtualinherited

Return a reduced composition of newTransfo = this(right()), or nullptr if it cannot be reduced.

"Reduced" in this context means that they are capable of being merged into a single transform, for example, for two polynomials:

\[ f(x) = 1 + x^2, g(x) = -1 + 3x \]

we would have h = f.composeAndReduce(g) == 2 - 6x + 9x^2.

To be overloaded by derived classes if they can properly reduce the composition.

Parameters
rightThe transform to apply first.
Returns
The new reduced and composed gtransfo, or nullptr if no such reduction is possible.

Reimplemented in lsst::jointcal::GtransfoIdentity.

Definition at line 92 of file Gtransfo.cc.

93  { // by default no way to compose
94  return std::unique_ptr<Gtransfo>(nullptr);
95 }
STL class.

◆ composeAndReduce() [2/2]

std::unique_ptr< Gtransfo > lsst::jointcal::GtransfoPoly::composeAndReduce ( GtransfoPoly const &  right) const

Return a reduced composition of newTransfo = this(right()), or nullptr if it cannot be reduced.

"Reduced" in this context means that they are capable of being merged into a single transform, for example, for two polynomials:

\[ f(x) = 1 + x^2, g(x) = -1 + 3x \]

we would have h = f.composeAndReduce(g) == 2 - 6x + 9x^2.

To be overloaded by derived classes if they can properly reduce the composition.

Parameters
rightThe transform to apply first.
Returns
The new reduced and composed gtransfo, or nullptr if no such reduction is possible.

Definition at line 898 of file Gtransfo.cc.

898  {
899  if (getOrder() == 1 && right.getOrder() == 1)
900  return std::make_unique<GtransfoLin>((*this) * (right)); // does the composition
901  else
902  return std::make_unique<GtransfoPoly>((*this) * (right)); // does the composition
903 }
T right(T... args)
unsigned getOrder() const
Returns the polynomial order.
Definition: Gtransfo.h:302

◆ computeDerivative()

void lsst::jointcal::GtransfoPoly::computeDerivative ( Point const &  where,
GtransfoLin derivative,
const double  step = 0.01 
) const
overridevirtual

specialised analytic routine

Reimplemented from lsst::jointcal::Gtransfo.

Reimplemented in lsst::jointcal::GtransfoLin.

Definition at line 569 of file Gtransfo.cc.

570  { /* routine checked against numerical derivatives from Gtransfo::Derivative */
571  if (_order == 1) {
572  derivative = GtransfoLin(*this);
573  derivative.dx() = derivative.dy() = 0;
574  return;
575  }
576 
577  double dermx[2 * _nterms]; // VLA
578  double *dermy = dermx + _nterms;
579  double xin = where.x;
580  double yin = where.y;
581 
582  double xx = 1;
583  double xxm1 = 1; // xx^(ix-1)
584  for (unsigned ix = 0; ix <= _order; ++ix) {
585  unsigned k = (ix) * (ix + 1) / 2;
586  // iy = 0
587  dermx[k] = ix * xxm1;
588  dermy[k] = 0;
589  k += ix + 2;
590  double yym1 = 1; // yy^(iy-1)
591  for (unsigned iy = 1; iy <= _order - ix; ++iy) {
592  dermx[k] = ix * xxm1 * yym1 * yin;
593  dermy[k] = iy * xx * yym1;
594  yym1 *= yin;
595  k += ix + iy + 2;
596  }
597  xx *= xin;
598  if (ix >= 1) xxm1 *= xin;
599  }
600 
601  derivative.dx() = 0;
602  derivative.dy() = 0;
603 
604  const double *mx = &dermx[0];
605  const double *my = &dermy[0];
606  const double *c = &_coeffs[0];
607  // dx'
608  double a11 = 0, a12 = 0;
609  for (int k = _nterms; k--;) {
610  a11 += (*(mx++)) * (*c);
611  a12 += (*(my++)) * (*(c++));
612  }
613  derivative.a11() = a11;
614  derivative.a12() = a12;
615  // dy'
616  double a21 = 0, a22 = 0;
617  mx = &dermx[0];
618  my = &dermy[0];
619  for (int k = _nterms; k--;) {
620  a21 += (*(mx++)) * (*c);
621  a22 += (*(my++)) * (*(c++));
622  }
623  derivative.a21() = a21;
624  derivative.a22() = a22;
625 }

◆ determinant()

double lsst::jointcal::GtransfoPoly::determinant ( ) const

Definition at line 782 of file Gtransfo.cc.

782  {
783  if (_order >= 1) return coeff(1, 0, 0) * coeff(0, 1, 1) - coeff(0, 1, 0) * coeff(1, 0, 1);
784  return 0;
785 }
double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const
access to coefficients (read only)
Definition: Gtransfo.cc:711

◆ dump()

void lsst::jointcal::GtransfoPoly::dump ( std::ostream stream = std::cout) const
overridevirtual

print out of coefficients in a readable form.

Implements lsst::jointcal::Gtransfo.

Definition at line 763 of file Gtransfo.cc.

763  {
764  auto oldPrecision = stream.precision();
765  stream.precision(12);
766  for (unsigned ic = 0; ic < 2; ++ic) {
767  if (ic == 0)
768  stream << "newx = ";
769  else
770  stream << "newy = ";
771  for (unsigned p = 0; p <= _order; ++p)
772  for (unsigned py = 0; py <= p; ++py) {
773  if (p + py != 0) stream << " + ";
774  stream << coeff(p - py, py, ic) << monomialString(p - py, py);
775  }
776  stream << endl;
777  }
778  if (_order > 0) stream << " Linear determinant = " << determinant() << endl;
779  stream.precision(oldPrecision);
780 }
T endl(T... args)
T precision(T... args)
double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const
access to coefficients (read only)
Definition: Gtransfo.cc:711
double determinant() const
Definition: Gtransfo.cc:782

◆ fit()

double lsst::jointcal::GtransfoPoly::fit ( StarMatchList const &  starMatchList)
overridevirtual

guess what

Implements lsst::jointcal::Gtransfo.

Reimplemented in lsst::jointcal::GtransfoLinRot, and lsst::jointcal::GtransfoLinShift.

Definition at line 880 of file Gtransfo.cc.

880  {
881  if (starMatchList.size() < _nterms) {
882  LOGLS_FATAL(_log, "GtransfoPoly::fit trying to fit a polynomial transfo of order "
883  << _order << " with only " << starMatchList.size() << " matches.");
884  return -1;
885  }
886 
887  GtransfoPoly conditionner = shiftAndNormalize(starMatchList);
888 
889  computeFit(starMatchList, conditionner, false); // get a rough solution
890  computeFit(starMatchList, conditionner, true); // weight with it
891  double chi2 = computeFit(starMatchList, conditionner, true); // once more
892 
893  (*this) = (*this) * conditionner;
894  if (starMatchList.size() == _nterms) return 0;
895  return chi2;
896 }
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
#define LOGLS_FATAL(logger, message)
Log a fatal-level message using an iostream-based interface.
Definition: Log.h:697

◆ getJacobian() [1/2]

virtual double lsst::jointcal::Gtransfo::getJacobian ( Point const &  point) const
inlinevirtualinherited

returns the local jacobian.

Definition at line 111 of file Gtransfo.h.

111 { return getJacobian(point.x, point.y); }
virtual double getJacobian(Point const &point) const
returns the local jacobian.
Definition: Gtransfo.h:111

◆ getJacobian() [2/2]

double lsst::jointcal::Gtransfo::getJacobian ( const double  x,
const double  y 
) const
virtualinherited

returns the local jacobian.

Definition at line 97 of file Gtransfo.cc.

97  {
98  double x2, y2;
99  double eps = x * 0.01;
100  if (eps == 0) eps = 0.01;
101  apply(x, y, x2, y2);
102  double dxdx, dydx;
103  apply(x + eps, y, dxdx, dydx);
104  dxdx -= x2;
105  dydx -= y2;
106  double dxdy, dydy;
107  apply(x, y + eps, dxdy, dydy);
108  dxdy -= x2;
109  dydy -= y2;
110  return ((dxdx * dydy - dxdy * dydx) / (eps * eps));
111 }
int y
Definition: SpanSet.cc:49
double x
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0

◆ getNpar()

int lsst::jointcal::GtransfoPoly::getNpar ( ) const
inlineoverridevirtual

total number of parameters

Reimplemented from lsst::jointcal::Gtransfo.

Reimplemented in lsst::jointcal::GtransfoLinScale, lsst::jointcal::GtransfoLinRot, and lsst::jointcal::GtransfoLinShift.

Definition at line 316 of file Gtransfo.h.

316 { return 2 * _nterms; }

◆ getOrder()

unsigned lsst::jointcal::GtransfoPoly::getOrder ( ) const
inline

Returns the polynomial order.

Definition at line 302 of file Gtransfo.h.

302 { return _order; }

◆ getParams()

void lsst::jointcal::Gtransfo::getParams ( double *  params) const
inherited

params should be at least Npar() long

Definition at line 210 of file Gtransfo.cc.

210  {
211  int npar = getNpar();
212  for (int i = 0; i < npar; ++i) params[i] = paramRef(i);
213 }
virtual int getNpar() const
returns the number of parameters (to compute chi2&#39;s)
Definition: Gtransfo.h:180
virtual double paramRef(const int i) const
Definition: Gtransfo.cc:220

◆ inverseTransfo()

std::unique_ptr< Gtransfo > lsst::jointcal::Gtransfo::inverseTransfo ( const double  precision,
const Frame region 
) const
virtualinherited

returns an inverse transfo. Numerical if not overloaded.

precision and region refer to the "input" side of this, and hence to the output side of the returned Gtransfo.

Reimplemented in lsst::jointcal::TanRaDecToPixel, lsst::jointcal::TanSipPixelToRaDec, lsst::jointcal::TanPixelToRaDec, lsst::jointcal::GtransfoLin, and lsst::jointcal::GtransfoInverse.

Definition at line 292 of file Gtransfo.cc.

292  {
293  return std::unique_ptr<Gtransfo>(new GtransfoInverse(this, precision, region));
294 }
STL class.

◆ linearApproximation()

GtransfoLin lsst::jointcal::Gtransfo::linearApproximation ( Point const &  where,
const double  step = 0.01 
) const
virtualinherited

linear (local) approximation.

Reimplemented in lsst::jointcal::GtransfoLin, and lsst::jointcal::GtransfoIdentity.

Definition at line 133 of file Gtransfo.cc.

133  {
134  Point outwhere = apply(where);
135  GtransfoLin der;
136  computeDerivative(where, der, step);
137  return GtransfoLinShift(outwhere.x, outwhere.y) * der * GtransfoLinShift(-where.x, -where.y);
138 }
virtual void computeDerivative(Point const &where, GtransfoLin &derivative, const double step=0.01) const
Computes the local Derivative of a transfo, w.r.t.
Definition: Gtransfo.cc:116
int const step
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0

◆ offsetParams()

void lsst::jointcal::Gtransfo::offsetParams ( Eigen::VectorXd const &  delta)
inherited

Definition at line 215 of file Gtransfo.cc.

215  {
216  int npar = getNpar();
217  for (int i = 0; i < npar; ++i) paramRef(i) += delta[i];
218 }
virtual int getNpar() const
returns the number of parameters (to compute chi2&#39;s)
Definition: Gtransfo.h:180
virtual double paramRef(const int i) const
Definition: Gtransfo.cc:220

◆ operator*()

GtransfoPoly lsst::jointcal::GtransfoPoly::operator* ( GtransfoPoly const &  right) const

Composition (internal stuff in quadruple precision)

Definition at line 1003 of file Gtransfo.cc.

1003  {
1004  // split each transfo into 2d polynomials
1005  PolyXY plx(*this, 0);
1006  PolyXY ply(*this, 1);
1007  PolyXY prx(right, 0);
1008  PolyXY pry(right, 1);
1009 
1010  // compute the compositions
1011  PolyXY rx(composition(plx, prx, pry));
1012  PolyXY ry(composition(ply, prx, pry));
1013 
1014  // copy the results the hard way.
1015  GtransfoPoly result(_order * right._order);
1016  for (unsigned px = 0; px <= result._order; ++px)
1017  for (unsigned py = 0; py <= result._order - px; ++py) {
1018  result.coeff(px, py, 0) = rx.coeff(px, py);
1019  result.coeff(px, py, 1) = ry.coeff(px, py);
1020  }
1021  return result;
1022 }
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
py::object result
Definition: schema.cc:284
T right(T... args)

◆ operator+()

GtransfoPoly lsst::jointcal::GtransfoPoly::operator+ ( GtransfoPoly const &  right) const

Addition.

Definition at line 1024 of file Gtransfo.cc.

1024  {
1025  if (_order >= right._order) {
1026  GtransfoPoly res(*this);
1027  for (unsigned i = 0; i <= right._order; ++i)
1028  for (unsigned j = 0; j <= right._order - i; ++j) {
1029  res.coeff(i, j, 0) += right.coeff(i, j, 0);
1030  res.coeff(i, j, 1) += right.coeff(i, j, 1);
1031  }
1032  return res;
1033  } else
1034  return (right + (*this));
1035 }
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
T right(T... args)

◆ operator-()

GtransfoPoly lsst::jointcal::GtransfoPoly::operator- ( GtransfoPoly const &  right) const

Subtraction.

Definition at line 1037 of file Gtransfo.cc.

1037  {
1038  GtransfoPoly res(std::max(_order, right._order));
1039  for (unsigned i = 0; i <= res._order; ++i)
1040  for (unsigned j = 0; j <= res._order - i; ++j) {
1041  res.coeff(i, j, 0) = coeffOrZero(i, j, 0) - right.coeffOrZero(i, j, 0);
1042  res.coeff(i, j, 1) = coeffOrZero(i, j, 1) - right.coeffOrZero(i, j, 1);
1043  }
1044  return res;
1045 }
GtransfoPoly(const unsigned order=1)
Default transfo : identity for all orders (>=1 ).
Definition: Gtransfo.cc:447
T right(T... args)
double coeffOrZero(const unsigned powX, const unsigned powY, const unsigned whichCoord) const
read access, zero if beyond order
Definition: Gtransfo.cc:724
T max(T... args)

◆ paramDerivatives()

void lsst::jointcal::GtransfoPoly::paramDerivatives ( Point const &  where,
double *  dx,
double *  dy 
) const
overridevirtual

Derivative w.r.t parameters.

Derivatives should be al least 2*NPar long. first Npar, for x, last Npar for y.

Reimplemented from lsst::jointcal::Gtransfo.

Definition at line 743 of file Gtransfo.cc.

744  { /* first half : dxout/dpar, second half : dyout/dpar */
745  computeMonomials(where.x, where.y, dx);
746  for (unsigned k = 0; k < _nterms; ++k) {
747  dy[_nterms + k] = dx[k];
748  dx[_nterms + k] = dy[k] = 0;
749  }
750 }

◆ paramRef() [1/2]

double lsst::jointcal::GtransfoPoly::paramRef ( const int  i) const
overridevirtual

Reimplemented from lsst::jointcal::Gtransfo.

Definition at line 733 of file Gtransfo.cc.

733  {
734  assert(unsigned(i) < 2 * _nterms);
735  return _coeffs[i];
736 }

◆ paramRef() [2/2]

double & lsst::jointcal::GtransfoPoly::paramRef ( const int  i)
overridevirtual

Reimplemented from lsst::jointcal::Gtransfo.

Definition at line 738 of file Gtransfo.cc.

738  {
739  assert(unsigned(i) < 2 * _nterms);
740  return _coeffs[i];
741 }

◆ read()

void lsst::jointcal::GtransfoPoly::read ( std::istream s)

Definition at line 1062 of file Gtransfo.cc.

1062  {
1063  int format;
1064  s >> format;
1065  if (format != 1)
1066  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, " GtransfoPoly::read : format is not 1 ");
1067 
1068  string order;
1069  s >> order >> _order;
1070  if (order != "order")
1071  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
1072  " GtransfoPoly::read : expecting \"order\" and found " + order);
1073  setOrder(_order);
1074  for (unsigned k = 0; k < 2 * _nterms; ++k) s >> _coeffs[k];
1075 }
void setOrder(const unsigned order)
Sets the polynomial order (the highest sum of exponents of the largest monomial). ...
Definition: Gtransfo.cc:525
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:129
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48

◆ roughInverse()

std::unique_ptr< Gtransfo > lsst::jointcal::Gtransfo::roughInverse ( const Frame region) const
virtualinherited

Rough inverse.

Stored by the numerical inverter to guess starting point for the trials. Just here to enable overloading.

Reimplemented in lsst::jointcal::TanRaDecToPixel, lsst::jointcal::TanPixelToRaDec, and lsst::jointcal::GtransfoInverse.

Definition at line 190 of file Gtransfo.cc.

190  {
191  // "in" and "out" refer to the inverse direction.
192  Point centerOut = region.getCenter();
193  Point centerIn = apply(centerOut);
194  GtransfoLin der;
195  computeDerivative(centerOut, der, std::sqrt(region.getArea()) / 5.);
196  der = der.inverted();
197  der = GtransfoLinShift(centerOut.x, centerOut.y) * der * GtransfoLinShift(-centerIn.x, -centerIn.y);
198  return std::unique_ptr<Gtransfo>(new GtransfoLin(der));
199 }
virtual void computeDerivative(Point const &where, GtransfoLin &derivative, const double step=0.01) const
Computes the local Derivative of a transfo, w.r.t.
Definition: Gtransfo.cc:116
STL class.
T sqrt(T... args)
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0

◆ setOrder()

void lsst::jointcal::GtransfoPoly::setOrder ( const unsigned  order)

Sets the polynomial order (the highest sum of exponents of the largest monomial).

Definition at line 525 of file Gtransfo.cc.

525  {
526  _order = order;
527  unsigned old_nterms = _nterms;
528  _nterms = (_order + 1) * (_order + 2) / 2;
529 
530  // temporarily save coefficients
531  vector<double> old_coeffs = _coeffs;
532  // reallocate enough size
533  _coeffs.resize(2 * _nterms);
534  // reassign to zero (this is necessary because ycoeffs
535  // are after xcoeffs and so their meaning changes
536  for (unsigned k = 0; k < _nterms; ++k) _coeffs[k] = 0;
537  // put back what we had before
538  unsigned kmax = min(old_nterms, _nterms);
539  for (unsigned k = 0; k < kmax; ++k) {
540  _coeffs[k] = old_coeffs[k]; // x terms
541  _coeffs[k + _nterms] = old_coeffs[k + old_nterms]; // y terms
542  }
543 }
int min
T resize(T... args)

◆ toAstMap()

std::shared_ptr< ast::Mapping > lsst::jointcal::GtransfoPoly::toAstMap ( jointcal::Frame const &  domain) const
overridevirtual

Create an equivalent AST mapping for this transformation, including an analytic inverse if possible.

Parameters
domainThe domain of the transfo, to help find an inverse.
Returns
An AST Mapping that represents this transformation.

Reimplemented from lsst::jointcal::Gtransfo.

Definition at line 1047 of file Gtransfo.cc.

1047  {
1048  auto inverse = inversePolyTransfo(*this, domain, 1e-7, _order + 2, 100);
1049  return std::make_shared<ast::PolyMap>(toAstPolyMapCoefficients(), inverse->toAstPolyMapCoefficients());
1050 }
std::shared_ptr< GtransfoPoly > inversePolyTransfo(Gtransfo const &forward, Frame const &domain, double const precision, int const maxOrder=9, unsigned const nSteps=50)
Approximate the inverse by a polynomial, to some precision.
Definition: Gtransfo.cc:1096

◆ transformErrors()

void lsst::jointcal::Gtransfo::transformErrors ( Point const &  where,
const double *  vIn,
double *  vOut 
) const
virtualinherited

transform errors (represented as double[3] in order V(xx),V(yy),Cov(xy))

Definition at line 157 of file Gtransfo.cc.

157  {
158  GtransfoLin der;
159  computeDerivative(where, der, 0.01);
160  double a11 = der.A11();
161  double a22 = der.A22();
162  double a21 = der.A21();
163  double a12 = der.A12();
164 
165  /* (a11 a12) (vxx vxy)
166  M = ( ) and V = ( )
167  (a21 a22) (xvy vyy)
168 
169  Vxx = Vin[0], vyy = Vin[1], Vxy = Vin[2];
170  we want to compute M*V*tp(M)
171  A lin alg light package would be perfect...
172  */
173  int xx = 0;
174  int yy = 1;
175  int xy = 2;
176  // M*V :
177 
178  double b11 = a11 * vIn[xx] + a12 * vIn[xy];
179  double b22 = a21 * vIn[xy] + a22 * vIn[yy];
180  double b12 = a11 * vIn[xy] + a12 * vIn[yy];
181  double b21 = a21 * vIn[xx] + a22 * vIn[xy];
182 
183  // (M*V) * tp(M)
184 
185  vOut[xx] = b11 * a11 + b12 * a12;
186  vOut[xy] = b11 * a21 + b12 * a22;
187  vOut[yy] = b21 * a21 + b22 * a22;
188 }
virtual void computeDerivative(Point const &where, GtransfoLin &derivative, const double step=0.01) const
Computes the local Derivative of a transfo, w.r.t.
Definition: Gtransfo.cc:116

◆ transformPosAndErrors()

void lsst::jointcal::GtransfoPoly::transformPosAndErrors ( const FatPoint in,
FatPoint out 
) const
overridevirtual

a mix of apply and Derivative

Reimplemented from lsst::jointcal::Gtransfo.

Definition at line 627 of file Gtransfo.cc.

627  {
628  /*
629  The results from this routine were compared to what comes out
630  from apply and transformErrors. The Derivative routine was
631  checked against numerical derivatives from
632  Gtransfo::Derivative. (P.A dec 2009).
633 
634  This routine could be made much simpler by calling apply and
635  Derivative (i.e. you just suppress it, and the fallback is the
636  generic version in Gtransfo). BTW, I checked that both routines
637  provide the same result. This version is however faster
638  (monomials get recycled).
639  */
640  double monomials[_nterms]; // VLA
641 
642  FatPoint res; // to store the result, because nothing forbids &in == &out.
643 
644  double dermx[2 * _nterms]; // monomials for derivative w.r.t. x (VLA)
645  double *dermy = dermx + _nterms; // same for y
646  double xin = in.x;
647  double yin = in.y;
648 
649  double xx = 1;
650  double xxm1 = 1; // xx^(ix-1)
651  for (unsigned ix = 0; ix <= _order; ++ix) {
652  unsigned k = (ix) * (ix + 1) / 2;
653  // iy = 0
654  dermx[k] = ix * xxm1;
655  dermy[k] = 0;
656  monomials[k] = xx;
657  k += ix + 2;
658  double yy = yin;
659  double yym1 = 1; // yy^(iy-1)
660  for (unsigned iy = 1; iy <= _order - ix; ++iy) {
661  monomials[k] = xx * yy;
662  dermx[k] = ix * xxm1 * yy;
663  dermy[k] = iy * xx * yym1;
664  yym1 *= yin;
665  yy *= yin;
666  k += ix + iy + 2;
667  }
668  xx *= xin;
669  if (ix >= 1) xxm1 *= xin;
670  }
671 
672  // output position
673  double xout = 0, yout = 0;
674  const double *c = &_coeffs[0];
675  const double *pm = &monomials[0];
676  for (int k = _nterms; k--;) xout += (*(pm++)) * (*(c++));
677  pm = &monomials[0];
678  for (int k = _nterms; k--;) yout += (*(pm++)) * (*(c++));
679  res.x = xout;
680  res.y = yout;
681 
682  // derivatives
683  c = &_coeffs[0];
684  const double *mx = &dermx[0];
685  const double *my = &dermy[0];
686  double a11 = 0, a12 = 0;
687  for (int k = _nterms; k--;) {
688  a11 += (*(mx++)) * (*c);
689  a12 += (*(my++)) * (*(c++));
690  }
691 
692  double a21 = 0, a22 = 0;
693  mx = &dermx[0];
694  my = &dermy[0];
695  for (int k = _nterms; k--;) {
696  a21 += (*(mx++)) * (*c);
697  a22 += (*(my++)) * (*(c++));
698  }
699 
700  // output co-variance
701  res.vx = a11 * (a11 * in.vx + 2 * a12 * in.vxy) + a12 * a12 * in.vy;
702  res.vy = a21 * a21 * in.vx + a22 * a22 * in.vy + 2. * a21 * a22 * in.vxy;
703  res.vxy = a21 * a11 * in.vx + a22 * a12 * in.vy + (a21 * a12 + a11 * a22) * in.vxy;
704  out = res;
705 }

◆ transformStar()

void lsst::jointcal::Gtransfo::transformStar ( FatPoint in) const
inlineinherited

allows to write MyTransfo(MyStar)

Definition at line 108 of file Gtransfo.h.

108 { transformPosAndErrors(in, in); }
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:140

◆ write() [1/2]

void lsst::jointcal::Gtransfo::write ( const std::string fileName) const
inherited

Definition at line 239 of file Gtransfo.cc.

239  {
240  ofstream s(fileName.c_str());
241  write(s);
242  bool ok = !s.fail();
243  s.close();
244  if (!ok)
245  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
246  "Gtransfo::write, something went wrong for file " + fileName);
247 }
void write(const std::string &fileName) const
Definition: Gtransfo.cc:239
STL class.
solver_t * s
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
T c_str(T... args)

◆ write() [2/2]

void lsst::jointcal::GtransfoPoly::write ( std::ostream s) const
overridevirtual

Reimplemented from lsst::jointcal::Gtransfo.

Definition at line 1052 of file Gtransfo.cc.

1052  {
1053  s << " GtransfoPoly 1" << endl;
1054  s << "order " << _order << endl;
1055  int oldprec = s.precision();
1056  s << setprecision(12);
1057  for (unsigned k = 0; k < 2 * _nterms; ++k) s << _coeffs[k] << ' ';
1058  s << endl;
1059  s << setprecision(oldprec);
1060 }
T endl(T... args)
T precision(T... args)
T setprecision(T... args)

The documentation for this class was generated from the following files: