30 struct interpol2Dtab {
32 double xmin, xmax, ymin, ymax;
40 T f(T x) {
return .5 * ( 1 + cos(x * M_PI) ) ; }
47 T kernel(T x) {
return f(1 - f(x)) ; }
50 T sq(T x) {
return x * x; }
57 int nrow = data.rows();
58 int ncol = data.cols();
59 T hx = (xmax - xmin) / (nrow - 1);
60 T hy = (ymax - ymin) / (ncol - 1);
62 T i_ = (x_ - xmin) / hx;
63 T j_ = (y_ - ymin) / hy;
65 bool ok = (0 <= i_) && (i_ <= nrow-1) && (0 <= j_) && (j_ <= ncol-1);
66 if (!ok)
return R_NaN;
69 int i_min =
std::max(asDouble(i_) - R, (
double) 0);
70 int j_min =
std::max(asDouble(j_) - R, (
double) 0);
71 int i_max =
std::min(asDouble(i_) + R, (
double) (nrow-1) );
72 int j_max =
std::min(asDouble(j_) + R, (
double) (ncol-1) );
74 T FWsum = 0, Wsum = 0;
75 for (
int i = i_min; i <= i_max; i++) {
76 for (
int j = j_min; j <= j_max; j++) {
77 T dist2 = sq( (T) i - i_ ) + sq( (T) j - j_ ) ;
79 T dist = sqrt( dist2 + tiny );
81 double F = data(i, j);
83 T W = kernel(dist / R);
94 double D_eval(
double x_,
double y_,
int ny) {
95 typedef atomic::tiny_ad::variable<order, 2> T;
96 int i = (1 << ny) - 1;
99 return eval(x, y).getDeriv()[i];
102 double operator()(
double x_,
double y_,
int nx=0,
int ny=0) {
106 }
else if (order == 1) {
107 return D_eval<1>(x_, y_, ny);
108 }
else if (order == 2) {
109 return D_eval<2>(x_, y_, ny);
110 }
else if (order == 3) {
111 return D_eval<3>(x_, y_, ny);
113 Rf_error(
"Order not implemented");
146 std::shared_ptr<interpol2Dtab<Type> > dtab;
150 for (
int i=0; i<x.rows(); i++) {
151 for (
int j=0; j<x.cols(); j++) {
152 if (do_check && CppAD::Variable(x(i,j)))
153 Rf_error(
"Matrix values must be constants");
154 y(i,j) = asDouble(x(i,j));
169 dtab(std::make_shared<interpol2Dtab<Type> >(interpol2Dtab<Type>({
170 asDoubleCheck(data, cfg.safe_check),
171 asDouble(x_range[0]),
172 asDouble(x_range[1]),
173 asDouble(y_range[0]),
174 asDouble(y_range[1]),
180 TMBad::Index input_size()
const {
183 TMBad::Index output_size()
const {
194 return (*dtab)(x, y, nx, ny);
203 std::vector<ad> xy(2); xy[0]=x; xy[1]=y;
205 cpy.xorder = nx; cpy.yorder=ny;
215 args.
y(0) = (*this)(args.
x(0), args.
x(1), xorder, yorder);
221 args.
dx(0) += (*this)(args.
x(0), args.
x(1), xorder + 1, yorder) * dy;
222 args.
dx(1) += (*this)(args.
x(0), args.
x(1), xorder, yorder + 1) * dy;
227 const char* op_name() {
return "IP2D"; }
#define VECTORIZE2_tt(FUN)
Vectorize 2-argument functions.
double R
Interpolation radius relative to integer lattice.
Access input/output values and derivatives during a reverse pass. Write access granted for the input ...
bool safe_check
Enable safety check that data really are constant?
Access input/output values during a forward pass. Write access granted for the output value only...
Configuration for Interpol2D
Type min(const vector< Type > &x)
Type x(Index j) const
j'th input variable of this operator
Get a smooth representation of a data matrix.
Type & y(Index j)
j'th output variable of this operator
Type x(Index j) const
j'th input variable of this operator
Type dy(Index j) const
Partial derivative of end result wrt. j'th output variable of this operator.
Taped sorting of a vector.
Operator auto-completion.
Operator that requires dynamic allocation. Compile time known input/output size.
double operator()(double x, double y, int nx=0, int ny=0)
Scalar evaluation with double types.
Vector class used by TMB.
interpol2D(matrix< Type > data, vector< Type > x_range, vector< Type > y_range, interpol2D_config< Type > cfg=interpol2D_config< Type >())
Construct interpolation object.
Type & dx(Index j)
Partial derivative of end result wrt. j'th input variable of this operator.
ad operator()(ad x, ad y, int nx=0, int ny=0)
Scalar evaluation with ad types.
Utility functions for TMB (automatically included)
Type max(const vector< Type > &x)