15 #if defined(TINY_AD_USE_STD_VALARRAY)
16 #include "tiny_valarray.hpp"
17 #define TINY_VECTOR(type,size) tiny_vector<type, size>
18 #elif defined(TINY_AD_USE_EIGEN_VEC)
19 #include <Eigen/Dense>
20 #define TINY_VECTOR(type,size) Eigen::Array<type, size, 1>
23 #define TINY_VECTOR(type,size) tiny_vec<type, size>
27 template<
class Type,
class Vector>
60 #define COMPARISON_OPERATOR(OP) \
61 template<class other> \
62 bool operator OP (const other &x) const{ \
63 return (value OP x); \
71 #undef COMPARISON_OPERATOR
115 template<
class T,
class V>
119 template<
class T,
class V>
123 template<
class T,
class V>
127 template<
class T,
class V>
133 #define UNARY_MATH_ZERO_DERIV(F) \
134 template<class T, class V> \
135 double F (const ad<T, V> &x){ \
138 using ::floor; using ::ceil;
139 using ::trunc; using ::round;
145 double sign(const T &
x){
return (x > 0) - (x < 0);}
148 template<
class T,
class V>
150 #undef UNARY_MATH_ZERO_DERIV
152 #define UNARY_MATH_DERIVATIVE(F,DF) \
153 template<class T, class V> \
154 ad<T, V> F (const ad<T, V> &x){ \
155 return ad<T, V>(F (x.value), \
156 T(DF(x.value)) * x.deriv); \
163 T y =
cos(x);
return 1. / (y * y);
166 T y =
cosh(x);
return 1. / (y * y);
178 using ::expm1; using ::log1p;
180 template<class T> T
D_log1p(const T &
x) {
return 1. / (x + 1.);}
185 return 1. /
sqrt(1. - x * x);
188 return -1. /
sqrt(1. - x * x);
191 return 1. / (1. + x *
x);
196 #undef UNARY_MATH_DERIVATIVE
198 template<
class T,
class V>
203 template<
class T,
class V>
209 #define COMPARISON_OPERATOR_FLIP(OP1, OP2) \
210 template<class T, class V> \
211 bool operator OP1 (const double &x, const ad<T, V> &y) { \
212 return y.operator OP2(x); \
220 #undef COMPARISON_OPERATOR_FLIP
225 double Rf_lgammafn(
double);
226 double Rf_psigamma(
double,
double);
229 double lgamma(
const double &
x) {
230 return Rf_psigamma(x, deriv-1);
233 double lgamma<0>(
const double &
x)
CSKIP( {
return Rf_lgammafn(
x);} )
234 double lgamma(
const double &
x)
CSKIP( {
return lgamma<0>(
x);} )
235 template<int deriv, class T, class V>
236 ad<T, V>
lgamma (
const ad<T, V> &
x){
237 return ad<T, V> (lgamma< deriv >(x.value),
238 T(lgamma< deriv + 1 >(x.value)) * x.deriv);
240 template<
class T,
class V>
241 ad<T, V>
lgamma (
const ad<T, V> &
x){
246 template<
class T,
class V>
247 std::ostream &operator<<(std::ostream &os, const ad<T, V> &
x) {
249 os <<
" value=" <<
x.value;
250 os <<
" deriv=" <<
x.deriv;
263 #define VARIABLE(order, nvar, scalartype) variable<order, nvar, scalartype>
264 template<
int order,
int nvar,
class Double=
double>
266 TINY_VECTOR( VARIABLE(order-1, nvar, Double) , nvar) > {
277 template<
class Constant>
281 template<
class Constant>
287 this->
value.setid(i0, count);
288 this->
deriv[i0].setid(i0, count + 1);
293 for(
int i=0; i<nvar; i++)
294 ans.segment(i * stride, stride) = this->
deriv[i].getDeriv();
299 template<
int nvar,
class Double>
300 struct variable<1, nvar, Double> :
ad<Double, TINY_VECTOR(Double,nvar) >{
309 template<
class Constant>
313 template<
class Constant>
320 this->
deriv[i0] = 1.0;
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
variable(double x, int id)
ad< VARIABLE(order-1, nvar, Double), TINY_VECTOR(VARIABLE(order-1, nvar, Double), nvar) > Base
dvector tanh(const dvector &vec)
Returns dvector with hyperbolic tangent for each value of vec.
ad & operator+=(const ad &other)
void setid(int i0, int count=0)
#define COMPARISON_OPERATOR_FLIP(OP1, OP2)
#define COMPARISON_OPERATOR(OP)
dvariable lgamma(const prevariable &v)
variable(Constant x, int id)
df1_one_variable atan(const df1_one_variable &x)
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
dvector cosh(const dvector &vec)
Returns dvector with hyperbolic cosine for each value of vec.
ad< T, V > operator+(const double &x, const ad< T, V > &y)
df1_two_variable fabs(const df1_two_variable &x)
bool isfinite(const ad< T, V > &x)
ad< Double, TINY_VECTOR(Double, nvar) > Base
static const int result_size
ad< T, V > operator-(const double &x, const ad< T, V > &y)
ad & operator*=(const ad &other)
variable(double x, int id)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
#define UNARY_MATH_DERIVATIVE(F, DF)
ad< T, V > operator*(const double &x, const ad< T, V > &y)
ad & operator-=(const ad &other)
#define VARIABLE(order, nvar, scalartype)
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
ad< T, V > pow(const ad< T, V > &x, const ad< T, V > &y)
ad< T, V > operator/(const double &x, const ad< T, V > &y)
#define UNARY_MATH_ZERO_DERIV(F)
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
TINY_VECTOR(Double, nvar) getDeriv()
ad operator/(const ad &other) const
dvector sinh(const dvector &vec)
Returns dvector with hyperbolic sine for each value of vec.
void setid(int i0, int count=0)
variable(Constant x, int id)
ad operator*(const ad &other) const
dvector value(const df1_one_vector &v)
TINY_VECTOR(Double, result_size) getDeriv()
ad & operator/=(const ad &other)
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector acos(const dvector &vec)
Returns dvector with principal value of the arc cosine of vec, expressed in radians.
d3_array pow(const d3_array &m, int e)
Description not yet available.
variable< order-1, nvar, Double > Type
dvector asin(const dvector &vec)
Returns dvector with principal value of the arc sine of vec, expressed in radians.