36 namespace algorithms {
39 static std::string makeRangeExceptionMessage(
40 const Position& p,
const Bounds&
b)
43 s <<
"Range error: Position "<<p<<
" is not in Bounds "<<
b;
48 std::runtime_error(makeRangeExceptionMessage(p,b)), _p(p),
_b(b)
52 int xOrder,
int yOrder,
const DVector& fVect)
62 (*_coeffs)(i,
m-i) = fVect(k++);
65 Assert(k==(
int)fVect.size());
73 if (!fin)
throw std::runtime_error(
"reading order, bounds");
77 for(
int m=0;
m<=maxOrder; ++
m) {
79 fin >> (*_coeffs)(i,
m-i);
82 if (!fin)
throw std::runtime_error(
"reading (legendre) function");
87 int oldprec = fout.precision(6);
88 std::ios::fmtflags oldf =
89 fout.setf(std::ios::scientific,std::ios::floatfield);
92 fout <<
"C " << (*_coeffs)(0,0) << std::endl;
95 for(
int m=0;
m<=maxOrder; ++
m) {
97 fout << (*_coeffs)(i,
m-i) <<
' ';
102 if (!fout)
throw std::runtime_error(
"writing (legendre) function");
103 fout.precision(oldprec);
114 (*_coeffs)(0,0) += a + b*xAve + c*yAve;
115 (*_coeffs)(1,0) += b*xRange/2.;
116 (*_coeffs)(0,1) += c*yRange/2.;
120 double ,
double ,
double ,
double ,
double ,
double )
136 std::auto_ptr<DMatrix > newc(
137 new DMatrix(newXOrder+1,newYOrder+1));
152 return std::auto_ptr<Function2D >(
new Constant2D());
155 return std::auto_ptr<Function2D >(
162 std::auto_ptr<Legendre2D > temp(
168 for(
int j=std::min(maxOrder-i,
_yOrder);j>=0;--j) {
170 for(
int k=0;k<=i/2-1;++k)
171 (*temp->_coeffs)(2*k+1,j) += (4.*k+3.)*(*_coeffs)(i,j);
173 for(
int k=0;k<=(i-1)/2;++k)
174 (*temp->_coeffs)(2*k,j) += (4.*k+1.)*(*_coeffs)(i,j);
178 maxOrder = std::max(newXOrder,newYOrder);
179 for(
int i=newXOrder;i>=0;--i) {
180 for(
int j=std::min(maxOrder-i,newYOrder);j>=0;--j) {
184 return std::auto_ptr<Function2D >(temp);
192 return std::auto_ptr<Function2D >(
new Constant2D());
195 return std::auto_ptr<Function2D >(
new Constant2D(
202 std::auto_ptr<Legendre2D > temp(
208 for(
int i=std::min(maxOrder-j,
_xOrder);i>=0;--i) {
210 for(
int k=0;k<=(j-2)/2;++k)
211 (*temp->_coeffs)(i,2*k+1) += (4.*k+3.)*(*_coeffs)(i,j);
213 for(
int k=0;k<=(j-1)/2;++k)
214 (*temp->_coeffs)(i,2*k) += (4.*k+1.)*(*_coeffs)(i,j);
218 maxOrder = std::max(newXOrder,newYOrder);
219 for(
int j=newYOrder;j>=0;--j) {
220 for(
int i=std::min(maxOrder-j,newXOrder);i>=0;--i) {
224 return std::auto_ptr<Function2D >(temp);
228 int order,
double xy,
double min,
double max)
const
231 double xyNew = (2.*xy-min-max)/(max-min);
232 temp[0] = 1.;
if(order>0) temp[1] = xyNew;
233 for(
int i=2;i<=order;++i)
234 temp[i] = ((2.*i-1.)*xyNew*temp[i-1] - (i-1.)*temp[i-2])/i;
241 dbg<<
"Error: x = "<<x<<
251 dbg<<
"Error: y = "<<y<<
virtual void write(std::ostream &fout) const
std::auto_ptr< DMatrix > _coeffs
virtual std::auto_ptr< Function2D > dFdY() const
virtual DVector definePY(int order, double y) const
DVector definePXY(int order, double xy, double min, double max) const
virtual void addLinear(double a, double b, double c)
virtual void linearPreTransform(double a, double b, double c, double d, double e, double f)
virtual DVector definePX(int order, double x) const
afw::table::Key< double > b
RangeException(const Position &p, const Bounds &b)
const Bounds & getBounds() const
virtual std::auto_ptr< Function2D > dFdX() const
virtual void operator+=(const Function2D &rhs)
virtual void setFunction(int xorder, int yorder, const DVector &fvect)