14 if (x.size() != fac.size()) Rf_error(
"x and fac must have equal length.");
16 for (
int i = 0; i < fac.size(); i++)
17 if (fac[i] >= nlevels) nlevels = fac[i] + 1;
21 for (
int i = 0; i < fac.size(); i++) lngt[fac[i]]++;
22 for (
int i = 0; i < nlevels; i++) ans[i].resize(lngt[i]);
24 for (
int i = 0; i < fac.size(); i++) {
25 ans[fac[i]][lngt[fac[i]]] = x[i];
32 template <
template <
class>
class Vector,
class Type>
33 Type
sum(Vector<Type> x) {
43 return A * x.matrix();
49 return (A * x.matrix()).
array();
61 x = x / sqrt(Type(2));
62 return Type(.5) * tanh((a + b * x * x) * x) + Type(.5);
75 Type y = .5 * (log(x) - log(1 - x));
80 Type C = pow(.5 * Delta1 + .5 * sqrt(pow(Delta1, 2) - 4 * pow(Delta0, 3)),
82 return -(C + Delta0 / C) * sqrt(Type(2)) / Type(3);
95 for (
int i = 0; i < n - 1; i++) ans[i] = x[i + 1] - x[i];
105 return log(x/(Type(1.0)-x));
115 return Type(1.0)/(Type(1.0)+exp(-x));
129 Type
matern(Type u, Type phi, Type kappa){
130 Type x = CppAD::CondExpEq(u, Type(0), Type(1), u / phi);
131 Type ans = 1.0 / ( exp(
lgamma(kappa)) * pow(2, kappa - 1.0) ) * pow(x, kappa) *
besselK(x, kappa);
132 return CppAD::CondExpEq(u, Type(0), Type(1), ans);
138 Type eps = std::numeric_limits<double>::epsilon();
139 u = (1.0 - eps) * (u - .5) + .5;
144 template <
class Type>
148 for(
int i=0; i < x.size(); i++){
149 res = CppAD::CondExpGt(res, x[i], res, x[i]);
155 template <
class Type>
159 for(
int i = 0; i < x.size(); i++){
160 res = CppAD::CondExpLt(res, x[i], res, x[i]);
182 if ( !CppAD::Variable(logx) && logx == Type(-INFINITY) )
184 if ( !CppAD::Variable(logy) && logy == Type(-INFINITY) )
186 CppAD::vector<Type> tx(3);
206 CppAD::vector<Type> tx(3);
Vector class used by TMB.
#define VECTORIZE1_t(FUN)
Vectorize 1-argument functions.
Type pnorm_approx(Type x)
Approximate normal cumulative distribution function, similar to R's pnorm (one-argument case only)...
vector< Type > diff(vector< Type > x)
vector< Type > operator*(matrix< Type > A, vector< Type > x)
Type lgamma(Type x)
Logarithm of gamma function (following R argument convention).
Type min(const vector< Type > &x)
Matrix class used by TMB.
Type qnorm_approx(Type x)
Approximate inverse normal cumulative distribution function, similar to R's qnorm (one-argument case ...
Type besselK(Type x, Type nu)
besselK function (same as besselK from R).
vector< vector< Type > > split(vector< Type > x, vector< int > fac)
Similar to R's split function: split(x,fac) devides x into groups defined by fac .
Type sum(Vector< Type > x)
Type matern(Type u, Type phi, Type kappa)
Type logspace_add(Type logx, Type logy)
Addition in log-space.
Type logspace_sub(Type logx, Type logy)
Subtraction in log-space.
Type max(const vector< Type > &x)