LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
lsst::meas::algorithms::shapelet::Function2D Class Referenceabstract

#include <Function2D.h>

Inheritance diagram for lsst::meas::algorithms::shapelet::Function2D:
lsst::meas::algorithms::shapelet::Constant2D lsst::meas::algorithms::shapelet::Legendre2D lsst::meas::algorithms::shapelet::Polynomial2D

Public Member Functions

 Function2D ()
 
 Function2D (int xo, int yo)
 
 Function2D (int xo, int yo, const DMatrix &c)
 
 Function2D (const Function2D &rhs)
 
virtual ~Function2D ()
 
virtual void write (std::ostream &fout) const =0
 
virtual std::auto_ptr< Function2Dcopy () const =0
 
virtual void addLinear (double a, double b, double c)=0
 
virtual void linearPreTransform (double a, double b, double c, double d, double e, double f)=0
 
virtual std::auto_ptr< Function2DdFdX () const =0
 
virtual std::auto_ptr< Function2DdFdY () const =0
 
virtual std::auto_ptr< Function2Dconj () const
 
virtual void operator*= (double scale)
 
virtual double operator() (double x, double y) const
 
double operator() (const Position &p) const
 
virtual void setTo (double value)
 
bool isNonZero () const
 
int getXOrder () const
 
int getYOrder () const
 
const DMatrixgetCoeffs () const
 
virtual void simpleFit (int order, const std::vector< Position > &pos, const std::vector< double > &v, const std::vector< bool > &shouldUse, const std::vector< double > *sigList=0, double *chisqOut=0, int *dofOut=0, DMatrix *cov=0)
 
virtual void outlierFit (int order, double nsig, const std::vector< Position > &pos, const std::vector< double > &v, std::vector< bool > *shouldUse, const std::vector< double > *sigList=0, double *chisqout=0, int *dofout=0, DMatrix *cov=0)
 
virtual void orderFit (int maxOrder, double equivProb, const std::vector< Position > &pos, const std::vector< double > &v, const std::vector< bool > &shouldUse, const std::vector< double > *sigList=0, double *chisqout=0, int *dofout=0, DMatrix *cov=0)
 
virtual void linearTransform (double a, double b, double c, const Function2D &f, const Function2D &g)
 
virtual void linearTransform (double a, double b, const Function2D &f)
 
virtual void operator+= (const Function2D &rhs)=0
 
virtual void setFunction (int _xOrder, int _yOrder, const DVector &fVect)=0
 

Static Public Member Functions

static std::auto_ptr< Function2Dread (std::istream &fin)
 

Protected Member Functions

virtual DVector definePX (int order, double x) const =0
 
virtual DVector definePY (int order, double y) const =0
 

Protected Attributes

int _xOrder
 
int _yOrder
 
std::auto_ptr< DMatrix_coeffs
 

Private Member Functions

void doSimpleFit (int xOrder, int yOrder, const std::vector< Position > &pos, const std::vector< double > &v, const std::vector< bool > &shouldUse, DVector *f, const std::vector< double > *sigList=0, int *dof=0, DVector *diff=0, DMatrix *cov=0)
 

Detailed Description

Definition at line 31 of file Function2D.h.

Constructor & Destructor Documentation

lsst::meas::algorithms::shapelet::Function2D::Function2D ( )
inline

Definition at line 35 of file Function2D.h.

lsst::meas::algorithms::shapelet::Function2D::Function2D ( int  xo,
int  yo 
)
inline

Definition at line 38 of file Function2D.h.

lsst::meas::algorithms::shapelet::Function2D::Function2D ( int  xo,
int  yo,
const DMatrix c 
)
inline
lsst::meas::algorithms::shapelet::Function2D::Function2D ( const Function2D rhs)
inline

Definition at line 45 of file Function2D.h.

45  :
46  _xOrder(rhs._xOrder), _yOrder(rhs._yOrder),
47  _coeffs(new DMatrix(*rhs._coeffs)) {}
virtual lsst::meas::algorithms::shapelet::Function2D::~Function2D ( )
inlinevirtual

Definition at line 49 of file Function2D.h.

49 {}

Member Function Documentation

virtual void lsst::meas::algorithms::shapelet::Function2D::addLinear ( double  a,
double  b,
double  c 
)
pure virtual
std::auto_ptr< Function2D > lsst::meas::algorithms::shapelet::Function2D::conj ( ) const
virtual

Definition at line 315 of file Function2D.cc.

316  {
317  std::auto_ptr<Function2D> temp = copy();
318  TMV_conjugateSelf(*(temp->_coeffs));
319  return temp;
320  }
#define TMV_conjugateSelf(m)
Definition: MyMatrix.h:210
virtual std::auto_ptr< Function2D > copy() const =0
virtual std::auto_ptr<Function2D> lsst::meas::algorithms::shapelet::Function2D::copy ( ) const
pure virtual
virtual DVector lsst::meas::algorithms::shapelet::Function2D::definePX ( int  order,
double  x 
) const
protectedpure virtual
virtual DVector lsst::meas::algorithms::shapelet::Function2D::definePY ( int  order,
double  y 
) const
protectedpure virtual
virtual std::auto_ptr<Function2D> lsst::meas::algorithms::shapelet::Function2D::dFdX ( ) const
pure virtual
virtual std::auto_ptr<Function2D> lsst::meas::algorithms::shapelet::Function2D::dFdY ( ) const
pure virtual
void lsst::meas::algorithms::shapelet::Function2D::doSimpleFit ( int  xOrder,
int  yOrder,
const std::vector< Position > &  pos,
const std::vector< double > &  v,
const std::vector< bool > &  shouldUse,
DVector f,
const std::vector< double > *  sigList = 0,
int *  dof = 0,
DVector diff = 0,
DMatrix cov = 0 
)
private

Definition at line 404 of file Function2D.cc.

410  {
411  // f(x,y) = Sum_pq k_pq px_p py_q
412  // = P . K (where each is a vector in pq)
413  // chisq = Sum_n ((v_n - f(x,y))/s_n)^2
414  // minchisq => 0 = Sum_n (v_n - f(x,y)) P_pq/s_n^2
415  // => [Sum_n P_pq P/s_n^2].K = [Sum_n v_n P_pq/s_n^2]
416  // Using ^ to represent an outer product, this can be written:
417  //
418  // [Sum_n (P/s_n)^(P/s_n)] . K = [Sum_n (v_n/s_n) (P/s_n)]
419  //
420  // Or if P' is a matrix with n index for rows, and pq index for columns,
421  // with each element P'(n,pq) = P_n,pq/s_n
422  // and v' is a vector with v'(n) = v_n/s_n
423  //
424  // Then, this can be written:
425  //
426  // P' K = v'
427  //
428  // The solution to this equation, then, gives our answer for K.
429  //
430  Assert(pos.size() == vals.size());
431  Assert(shouldUse.size() == vals.size());
432  Assert((sigList==0) || (sigList->size() == vals.size()));
433  xdbg<<"Start SimpleFit: size = "<<pos.size()<<std::endl;
434  xdbg<<"order = "<<xOrder<<','<<yOrder<<std::endl;
435 
436  int highOrder = std::max(xOrder,yOrder);
437  int size = fitSize(xOrder,yOrder);
438  xdbg<<"size = "<<size<<std::endl;
439 
440  const int nVals = vals.size();
441 
442  Assert(int(f->size()) == size);
443  Assert(!diff || int(diff->size()) == nVals);
444  Assert(!cov ||
445  (int(cov->TMV_colsize()) == size && int(cov->TMV_rowsize()) == size));
446 
447  int nUse = std::count(shouldUse.begin(),shouldUse.end(),true);
448  xdbg<<"nuse = "<<nUse<<std::endl;
449 
450  DMatrix P(nUse,size);
451  P.setZero();
452  DVector V(nUse);
453 
454  int ii=0;
455  for(int i=0;i<nVals;++i) if (shouldUse[i]) {
456  if (sigList) {
457  Assert((*sigList)[i] > 0.);
458  V(ii) = vals[i]/(*sigList)[i];
459  } else {
460  V(ii) = vals[i];
461  }
462 
463  DVector px = definePX(xOrder,pos[i].getX());
464  DVector py = definePY(yOrder,pos[i].getY());
465  int pq=0;
466  for(int pplusq=0;pplusq<=highOrder;++pplusq) {
467  for(int p=std::min(pplusq,xOrder),q=pplusq-p;
468  q<=std::min(pplusq,yOrder);--p,++q,++pq) {
469  Assert(p<int(px.size()));
470  Assert(q<int(py.size()));
471  Assert(pq<int(P.TMV_rowsize()));
472  P(ii,pq) = px[p]*py[q];
473  }
474  }
475  Assert(pq == int(P.TMV_rowsize()));
476  if (sigList) P.row(ii) /= (*sigList)[i];
477  ++ii;
478  }
479  Assert(ii==nUse);
480 
481  xdbg<<"Done make V,P\n";
482  xdbg<<"V = "<<EIGEN_Transpose(V)<<std::endl;
483  //P.divideUsing(tmv::QR);
484  //P.saveDiv();
485  //*f = V/P;
486  TMV_QR(P);
487  TMV_QR_Solve(P,*f,V);
488  xdbg<<"*f = "<<EIGEN_Transpose(*f)<<std::endl;
489 
490  if (diff) {
491  int k=0;
492  for(int i=0;i<nVals;++i) {
493  if (shouldUse[i]) {
494  (*diff)(i) =
495  V(k) - EIGEN_ToScalar(P.row(k) * (*f));
496  ++k;
497  } else {
498  (*diff)(i) = 0.;
499  }
500  }
501  }
502  if (dof) {
503  *dof = P.TMV_colsize() - P.TMV_rowsize();
504  if (*dof < 0) *dof = 0;
505  }
506  if (cov) {
507  //P.makeInverseATA(*cov);
508  TMV_QR_InverseATA(P,*cov);
509  }
510  xdbg<<"Done simple fit\n";
511  }
virtual DVector definePY(int order, double y) const =0
virtual DVector definePX(int order, double x) const =0
#define TMV_QR_Solve(m, x, b)
Definition: MyMatrix.h:227
#define EIGEN_ToScalar(m)
Definition: MyMatrix.h:212
int fitSize(const int xOrder, const int yOrder)
Definition: Function2D.cc:397
#define TMV_QR(m)
Definition: MyMatrix.h:222
#define TMV_QR_InverseATA(m, cov)
Definition: MyMatrix.h:229
#define xdbg
Definition: dbg.h:70
#define EIGEN_Transpose(m)
Definition: MyMatrix.h:211
#define Assert(x)
Definition: dbg.h:73
const DMatrix& lsst::meas::algorithms::shapelet::Function2D::getCoeffs ( ) const
inline

Definition at line 96 of file Function2D.h.

96 { return *_coeffs; }
int lsst::meas::algorithms::shapelet::Function2D::getXOrder ( ) const
inline

Definition at line 92 of file Function2D.h.

int lsst::meas::algorithms::shapelet::Function2D::getYOrder ( ) const
inline

Definition at line 94 of file Function2D.h.

bool lsst::meas::algorithms::shapelet::Function2D::isNonZero ( ) const
inline

Definition at line 89 of file Function2D.h.

virtual void lsst::meas::algorithms::shapelet::Function2D::linearPreTransform ( double  a,
double  b,
double  c,
double  d,
double  e,
double  f 
)
pure virtual
void lsst::meas::algorithms::shapelet::Function2D::linearTransform ( double  a,
double  b,
double  c,
const Function2D f,
const Function2D g 
)
virtual

Definition at line 355 of file Function2D.cc.

358  {
359  if(dynamic_cast<Constant2D*>(this)) {
360  Assert(dynamic_cast<const Constant2D*>(&f));
361  Assert(dynamic_cast<const Constant2D*>(&g));
362  }
363  if(dynamic_cast<Polynomial2D*>(this)) {
364  Assert(dynamic_cast<const Polynomial2D*>(&f));
365  Assert(dynamic_cast<const Polynomial2D*>(&g));
366  }
367 #ifdef LEGENDRE2D_H
368  if(dynamic_cast<Legendre2D*>(this)) {
369  Assert(dynamic_cast<const Legendre2D*>(&f));
370  Assert(dynamic_cast<const Legendre2D*>(&g));
371  Assert(dynamic_cast<const Legendre2D*>(&f)->getBounds() ==
372  dynamic_cast<const Legendre2D*>(&g)->getBounds());
373  }
374 #endif
375 #ifdef CHEBY2D_H
376  if(dynamic_cast<Cheby2D*>(this)) {
377  Assert(dynamic_cast<const Cheby2D*>(&f));
378  Assert(dynamic_cast<const Cheby2D*>(&g));
379  Assert(dynamic_cast<const Cheby2D*>(&f)->getBounds() ==
380  dynamic_cast<const Cheby2D*>(&g)->getBounds());
381  }
382 #endif
383  Assert(f.getXOrder() == g.getXOrder());
384  Assert(f.getYOrder() == g.getYOrder());
385 
386  if (_xOrder != f.getXOrder() || _yOrder != f.getYOrder()) {
387  _xOrder = f.getXOrder();
388  _yOrder = f.getYOrder();
389  _coeffs.reset(new DMatrix(_xOrder+1,_yOrder+1));
390  _coeffs->setZero();
391  } else _coeffs->setZero();
392  for(int i=0;i<=_xOrder;++i) for(int j=0;j<=_yOrder;++j) {
393  (*_coeffs)(i,j) = a + b*f.getCoeffs()(i,j) + c*g.getCoeffs()(i,j);
394  }
395  }
afw::table::Key< double > b
#define Assert(x)
Definition: dbg.h:73
virtual void lsst::meas::algorithms::shapelet::Function2D::linearTransform ( double  a,
double  b,
const Function2D f 
)
inlinevirtual

Definition at line 132 of file Function2D.h.

133  { linearTransform(a,b,0.,f,f); }
virtual void linearTransform(double a, double b, double c, const Function2D &f, const Function2D &g)
Definition: Function2D.cc:355
afw::table::Key< double > b
double lsst::meas::algorithms::shapelet::Function2D::operator() ( double  x,
double  y 
) const
virtual

Reimplemented in lsst::meas::algorithms::shapelet::Constant2D.

Definition at line 322 of file Function2D.cc.

323  {
324  DVector px = definePX(_xOrder,x);
325  DVector py = definePY(_yOrder,y);
326  double result = EIGEN_ToScalar(EIGEN_Transpose(px) * (*_coeffs) * py);
327  return result;
328  }
int y
virtual DVector definePY(int order, double y) const =0
virtual DVector definePX(int order, double x) const =0
#define EIGEN_ToScalar(m)
Definition: MyMatrix.h:212
double x
#define EIGEN_Transpose(m)
Definition: MyMatrix.h:211
double lsst::meas::algorithms::shapelet::Function2D::operator() ( const Position p) const
inline

Definition at line 77 of file Function2D.h.

78  { return operator()(p.getX(),p.getY()); }
virtual double operator()(double x, double y) const
Definition: Function2D.cc:322
virtual void lsst::meas::algorithms::shapelet::Function2D::operator*= ( double  scale)
inlinevirtual

Definition at line 72 of file Function2D.h.

73  { *_coeffs *= scale; }
virtual void lsst::meas::algorithms::shapelet::Function2D::operator+= ( const Function2D rhs)
pure virtual
void lsst::meas::algorithms::shapelet::Function2D::orderFit ( int  maxOrder,
double  equivProb,
const std::vector< Position > &  pos,
const std::vector< double > &  v,
const std::vector< bool > &  shouldUse,
const std::vector< double > *  sigList = 0,
double *  chisqout = 0,
int *  dofout = 0,
DMatrix cov = 0 
)
virtual

Definition at line 602 of file Function2D.cc.

607  {
608  xdbg<<"Start OrderFit\n";
609  DVector fVectMax(fitSize(maxOrder,maxOrder));
610  DVector diff(vals.size());
611  int dofmax;
612  doSimpleFit(maxOrder,maxOrder,pos,vals,shouldUse,
613  &fVectMax,sig,&dofmax,&diff,cov);
614  double chisqmax = diff.TMV_normSq();
615  xdbg<<"chisq,dof(n="<<maxOrder<<") = "<<chisqmax<<','<<dofmax<<std::endl;
616  int tryOrder;
617  double chisq=-1.;
618  int dof=-1;
619  std::auto_ptr<DVector > fVect(0);
620  for(tryOrder=0;tryOrder<maxOrder;++tryOrder) {
621  fVect.reset(new DVector(fitSize(tryOrder,tryOrder)));
622  doSimpleFit(tryOrder,tryOrder,pos,vals,shouldUse,
623  fVect.get(),sig,&dof,&diff,cov);
624  chisq = diff.TMV_normSq();
625  xdbg<<"chisq,dof(n="<<tryOrder<<") = "<<chisq<<','<<dof<<".... ";
626  if (Equivalent(chisqmax,chisq,dofmax,dof,equivProb)) {
627  xdbg<<"equiv\n";
628  break;
629  }
630  xdbg<<"not equiv\n";
631  }
632  if (tryOrder == maxOrder) {
633  setFunction(tryOrder,tryOrder,fVectMax);
634  if(chisqOut) *chisqOut = chisqmax;
635  if(dofOut) *dofOut = dofmax;
636  } else {
637  Assert(fVect.get());
638  setFunction(tryOrder,tryOrder,*fVect);
639  if(chisqOut) *chisqOut = chisq;
640  if(dofOut) *dofOut = dof;
641  }
642  }
virtual void setFunction(int _xOrder, int _yOrder, const DVector &fVect)=0
int fitSize(const int xOrder, const int yOrder)
Definition: Function2D.cc:397
double chisq
void doSimpleFit(int xOrder, int yOrder, const std::vector< Position > &pos, const std::vector< double > &v, const std::vector< bool > &shouldUse, DVector *f, const std::vector< double > *sigList=0, int *dof=0, DVector *diff=0, DMatrix *cov=0)
Definition: Function2D.cc:404
#define xdbg
Definition: dbg.h:70
#define Assert(x)
Definition: dbg.h:73
bool Equivalent(double chisq1, double chisq2, int n1, int n2, double equivProb)
Definition: Function2D.cc:584
void lsst::meas::algorithms::shapelet::Function2D::outlierFit ( int  order,
double  nsig,
const std::vector< Position > &  pos,
const std::vector< double > &  v,
std::vector< bool > *  shouldUse,
const std::vector< double > *  sigList = 0,
double *  chisqout = 0,
int *  dofout = 0,
DMatrix cov = 0 
)
virtual

Definition at line 534 of file Function2D.cc.

540  {
541  xdbg<<"start outlier fit\n";
542  const int nVals = vals.size();
543  bool isDone=false;
544  DVector fVect(fitSize(order,order));
545  int dof;
546  double chisq=0.;
547  double nSigSq = nSig*nSig;
548  while (!isDone) {
549  DVector diff(nVals);
550  xdbg<<"before dosimple\n";
551  doSimpleFit(order,order,pos,vals,*shouldUse,&fVect,sig,&dof,&diff,cov);
552  xdbg<<"after dosimple\n";
553  // Caclulate chisq, keeping the vector diffsq for later when
554  // looking for outliers
555  chisq = diff.TMV_normSq();
556  // If sigmas are given, then chisq should be 1, since diff is
557  // already normalized by sig_i. But if not, then this effectively
558  // assumes that all the given errors are off by some uniform factor.
559 
560  // Look for outliers, setting isDone = false if any are found
561  isDone = true;
562  if (dof <= 0) break;
563  double thresh=nSigSq*chisq/dof;
564  xdbg<<"chisq = "<<chisq<<", thresh = "<<thresh<<std::endl;
565  for(int i=0;i<nVals;++i) if( (*shouldUse)[i]) {
566  xdbg<<"pos ="<<pos[i]<<", v = "<<vals[i]<<
567  " diff = "<<diff[i]<<std::endl;
568  if (absSq(diff[i]) > thresh) {
569  isDone = false;
570  (*shouldUse)[i] = false;
571  xdbg<<i<<" ";
572  }
573  }
574  if (!isDone) xdbg<<" are outliers\n";
575  }
576  setFunction(order,order,fVect);
577  if (chisqOut) *chisqOut = chisq;
578  if (dofOut) *dofOut = dof;
579  xdbg<<"done outlier fit\n";
580  }
virtual void setFunction(int _xOrder, int _yOrder, const DVector &fVect)=0
int fitSize(const int xOrder, const int yOrder)
Definition: Function2D.cc:397
double chisq
void doSimpleFit(int xOrder, int yOrder, const std::vector< Position > &pos, const std::vector< double > &v, const std::vector< bool > &shouldUse, DVector *f, const std::vector< double > *sigList=0, int *dof=0, DVector *diff=0, DMatrix *cov=0)
Definition: Function2D.cc:404
#define xdbg
Definition: dbg.h:70
double absSq(const double &x)
Definition: Function2D.cc:530
std::auto_ptr< Function2D > lsst::meas::algorithms::shapelet::Function2D::read ( std::istream &  fin)
static

Definition at line 330 of file Function2D.cc.

331  {
332  char fc,tc;
333 
334  fin >> fc >> tc;
335  if (tc != 'D' && tc != 'C') fin.putback(tc);
336  std::auto_ptr<Function2D> ret;
337  switch(fc) {
338  case 'C' : ret.reset(new Constant2D(fin));
339  break;
340  case 'P' : ret.reset(new Polynomial2D(fin));
341  break;
342 #ifdef LEGENDRE2D_H
343  case 'L' : ret.reset(new Legendre2D(fin));
344  break;
345 #endif
346 #ifdef CHEBY2D_H
347  case 'X' : ret.reset(new Cheby2D(fin));
348  break;
349 #endif
350  default: throw std::runtime_error("invalid type");
351  }
352  return ret;
353  }
virtual void lsst::meas::algorithms::shapelet::Function2D::setFunction ( int  _xOrder,
int  _yOrder,
const DVector fVect 
)
pure virtual
virtual void lsst::meas::algorithms::shapelet::Function2D::setTo ( double  value)
inlinevirtual

Definition at line 80 of file Function2D.h.

81  {
82  if (_xOrder || _yOrder) {
83  _xOrder = 0; _yOrder = 0;
84  _coeffs.reset(new DMatrix(1,1));
85  }
86  (*_coeffs)(0,0) = value;
87  }
void lsst::meas::algorithms::shapelet::Function2D::simpleFit ( int  order,
const std::vector< Position > &  pos,
const std::vector< double > &  v,
const std::vector< bool > &  shouldUse,
const std::vector< double > *  sigList = 0,
double *  chisqOut = 0,
int *  dofOut = 0,
DMatrix cov = 0 
)
virtual

Definition at line 513 of file Function2D.cc.

518  {
519  DVector fVect(fitSize(order,order));
520  if (chisqOut) {
521  DVector diff(vals.size());
522  doSimpleFit(order,order,pos,vals,shouldUse,&fVect,sig,dofOut,&diff,cov);
523  *chisqOut = diff.TMV_normSq();
524  } else {
525  doSimpleFit(order,order,pos,vals,shouldUse,&fVect,sig);
526  }
527  setFunction(order,order,fVect);
528  }
virtual void setFunction(int _xOrder, int _yOrder, const DVector &fVect)=0
int fitSize(const int xOrder, const int yOrder)
Definition: Function2D.cc:397
void doSimpleFit(int xOrder, int yOrder, const std::vector< Position > &pos, const std::vector< double > &v, const std::vector< bool > &shouldUse, DVector *f, const std::vector< double > *sigList=0, int *dof=0, DVector *diff=0, DMatrix *cov=0)
Definition: Function2D.cc:404
virtual void lsst::meas::algorithms::shapelet::Function2D::write ( std::ostream &  fout) const
pure virtual

Member Data Documentation

std::auto_ptr<DMatrix> lsst::meas::algorithms::shapelet::Function2D::_coeffs
protected

Definition at line 146 of file Function2D.h.

int lsst::meas::algorithms::shapelet::Function2D::_xOrder
protected

Definition at line 145 of file Function2D.h.

int lsst::meas::algorithms::shapelet::Function2D::_yOrder
protected

Definition at line 145 of file Function2D.h.


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