14 template <
class T=
double,
unsigned int N=0>
27 AD(
double x):
x_(x) {zero();}
33 unsigned int size()
const {
return N;}
35 void diff(
unsigned int i,
unsigned int n=0)
44 const double&
val()
const {
return x_;}
46 T&
dx(
unsigned int i) {
return dx_[i];}
48 const T&
dx(
unsigned int i)
const {
return dx_[i];}
50 inline void resize(
unsigned int i) {(void)i;}
52 operator double()
const {
return double(
x_);}
84 for (
unsigned int i=0; i < N; ++i)
99 for (
unsigned int i=0; i < N; ++i)
107 for (
unsigned int i=0; i < N; ++i)
116 for (
unsigned int i=0; i < N; ++i)
123 for (
unsigned int i=0; i < N; ++i)
132 for (
unsigned int i=0; i < N; ++i)
133 dx_[i] = (
dx_[i]*other - other*other.
dx_[i]) / (other*other);
138 for (
unsigned int i=0; i < N; ++i)
143 void copy(AD<B, N>
const& other)
146 for (
unsigned int i=0; i < N; ++i)
147 this->dx_[i] = (T)other.dx_[i];
160 const static double _zero_;
168 unsigned size() {
return dx_.size();}
169 unsigned size()
const {
return dx_.size();}
170 void diff(
unsigned int i,
unsigned int n)
179 const double&
val()
const {
return x_;}
181 T&
dx(
unsigned int i)
185 unsigned int temp_size =
size();
186 dx_.resize_copy(i + 1);
187 for(
unsigned int x = temp_size; x <= i; x++)
193 const T
dx(
unsigned int i)
const {
199 operator double()
const {
return double(
x_);}
226 for (
unsigned int i=0; i <
size(); ++i)
239 if (other.size() >
size())
242 for (
unsigned int i=0; i <
size(); ++i)
250 for (
unsigned int i=0; i <
size(); ++i)
259 for (
unsigned int i=0; i <
size(); ++i)
267 for (
unsigned int i=0; i <
size(); ++i)
276 for (
unsigned int i=0; i <
size(); ++i)
280 void resize(
unsigned int n)
287 for (
unsigned int i=0; i <
size(); ++i)
291 void copy(AD<B, 0>
const& other)
293 if(
size() != other.size())
294 dx_.resize(other.size());
296 for (
unsigned int i=0; i <
size(); ++i)
297 dx_[i] = (T)other.dx_[i];
309 template <
class T,
unsigned int N>
323 template <
class T,
unsigned int N>
328 for (
unsigned int i=0; i < tmp.
size(); ++i)
329 tmp.
dx(i) = T(R.
dx(i));
335 template <
class T,
unsigned int N>
340 for (
unsigned int i=0; i < tmp.
size(); ++i)
341 tmp.
dx(i) = T(L.
dx(i));
346 template <
class T,
class B,
unsigned int N>
352 for (
unsigned int i=0; i < tmp.
size(); ++i)
353 tmp.
dx(i) = T(L.
dx(i) + R.
dx(i));
359 template <
class T,
unsigned int N>
364 for (
unsigned int i=0; i < tmp.
size(); ++i)
365 tmp.
dx(i) = T(-R.
dx(i));
371 template <
class T,
unsigned int N>
376 for (
unsigned int i=0; i < tmp.
size(); ++i)
377 tmp.
dx(i) = T(L.
dx(i));
383 template <
class T,
class B,
unsigned int N>
389 for (
unsigned int i=0; i < tmp.
size(); ++i)
390 tmp.
dx(i) = T(L.
dx(i) - R.
dx(i));
396 template <
class T,
unsigned int N>
401 for (
unsigned int i=0; i < tmp.
size(); ++i)
402 tmp.
dx(i) = T(L * R.
dx(i));
408 template <
class T,
unsigned int N>
413 for (
unsigned int i=0; i < tmp.
size(); ++i)
414 tmp.
dx(i) = T(L.
dx(i) * R);
420 template <
class T,
class B,
unsigned int N>
426 for (
unsigned int i=0; i < tmp.
size(); ++i)
427 tmp.
dx(i) = T(L.
dx(i) * R + L * R.
dx(i));
433 template <
class T,
unsigned int N>
439 for (
unsigned int i = 0; i < R.
size(); i++)
440 tmp.
dx(i) = T(-L * R.
dx(i) * (1. / R_tmp) * (1. / R_tmp));
446 template <
class T,
unsigned int N>
451 for (
unsigned int i=0; i < L.
size(); ++i)
452 tmp.
dx(i) = T(L.
dx(i) / R);
458 template <
class T,
class B,
unsigned int N>
464 for (
unsigned int i=0; i < tmp.
size(); ++i)
465 tmp.
dx(i) = T(((L.
dx(i) * R) - (L * R.
dx(i))) * (1. / R) * (1. / R));
481 template <
class T,
unsigned int N>
488 for (
unsigned int i=0; i < A.
size(); ++i)
489 tmp.
dx(i) = T(A.
dx(i) *
exp(A_tmp));
500 template <
class T,
unsigned int N>
507 for (
unsigned int i=0; i < A.
size(); ++i)
508 tmp.
dx(i) = T(A.
dx(i) / A_tmp);
513 double pow(
double A,
double e)
519 template <
class T,
unsigned int N>
526 for (
unsigned int i=0; i < tmp.
size(); ++i)
532 template <
class T,
unsigned int N>
539 for (
unsigned int i=0; i < tmp.
size(); ++i)
540 tmp.
dx(i) = T(e*A.
dx(i)*
pow(A_tmp, e - 1.));
545 template <
class T,
unsigned int N>
552 for (
unsigned int i=0; i < tmp.
size(); ++i)
553 tmp.
dx(i) = T(
std::log((
double)base) *
pow((
double)base, A_tmp) * A.
dx(i));
558 template <
class T,
unsigned int N>
565 for (
unsigned int i=0; i < tmp.
size(); ++i)
572 template <
class T,
unsigned int N>
581 for (
unsigned int i=0; i < tmp.
size(); ++i)
582 tmp.
dx(i) = T(e.
dx(i) *
log(A_tmp) *
pow(A_tmp, e_tmp) +
583 e_tmp * A.
dx(i) *
pow(A_tmp, e_tmp - 1.));
594 template <
class T,
unsigned int N>
601 for (
unsigned int i=0; i < tmp.
size(); ++i)
602 tmp.
dx(i) = T(.5 * A.
dx(i) /
sqrt(A_tmp));
619 template <
class T,
unsigned int N>
626 for(
unsigned int i = 0; i < tmp.
size(); i++)
627 tmp.
dx(i) = T(
cos(A_tmp) * A.
dx(i));
632 template <
class T,
unsigned int N>
639 for(
unsigned int i = 0; i < tmp.
size(); i++)
640 tmp.
dx(i) = T(-
sin(A_tmp) * A.
dx(i));
645 template <
class T,
unsigned int N>
652 for(
unsigned int i = 0; i < tmp.
size(); i++)
653 tmp.
dx(i) = T(A.
dx(i) * (1. / (
cos(A_tmp) *
cos(A_tmp))));
658 template <
class T,
unsigned int N>
661 int sign = A.
val() > 0 ? 1 : 0;
671 template <
class T,
unsigned int N>
678 template <
class T,
unsigned int N>
685 template <
class T,
unsigned int N>
692 template <
class T,
unsigned int N>
699 template <
class T,
unsigned int N>
706 template <
class T,
unsigned int N>
709 return R.
val() <= L.
val();
713 template <
class T,
unsigned int N>
720 template <
class T,
unsigned int N>
727 template <
class T,
unsigned int N>
734 template <
class T,
unsigned int N>
741 template <
class T,
unsigned int N>
748 template <
class T,
unsigned int N>
751 return R.
val() >= L.
val();
compile-time (static) array of size N
forward automatic differentiation variable with dynamic variable array
AD< T, 0 > & operator=(double other)
assignment to a double
AD(AD< B, 0 > const &other)
constructs from other AD
AD(double val)
constructs from a double
AD< T, 0 > & operator+=(double other)
addition assignment with a double
T & dx(unsigned int i)
get the ith derivative value (mutable)
AD< T, 0 > & operator=(AD< B, 0 > const &other)
assignment to another AD variable
AD< T, 0 > & operator-=(double other)
subtraction assignment with a double
AD< T, 0 > & operator+=(AD< T, 0 > const &other)
addition assignment with another AD variable
AD< T, 0 > & operator*=(double other)
multiplication assignment with a double
AD< T, 0 > & operator/=(double other)
division assignment with a double
AD< T, 0 > & operator/=(AD< T, 0 > const &other)
division assignment with another AD variable
AD< T, 0 > & operator-=(AD< T, 0 > const &other)
subtraction assignment with another AD variable
double x_
the variable value
const double & val() const
get the value of the variable (immutable)
const T dx(unsigned int i) const
get the ith deriative value (immutable)
can::Array< T > dx_
the dynamic derivative array
double & val()
get the value of the variable (mutable)
AD< T, 0 > & operator*=(AD< T, 0 > const &other)
multiplication assignment with another AD variable
forward automatic differentiation variable
unsigned int size() const
get the size of the derivative array
void resize(unsigned int i)
resize for static AD (no-op)
AD< T, N > & operator=(AD< T, N > const &other)
assignment to another AD variable
double & val()
get the value of the variable (mutable)
AD(double x)
default constructor from a double
AD< T, N > & operator*=(AD< B, N > const &other)
multiplication assignment with another AD variable
AD< T, N > & operator/=(double other)
division assignment with a double
AD< T, N > & operator/=(AD< B, N > const &other)
division assignment with another AD variable
AD< T, N > & operator-=(double other)
subtraction assignment with a double
T dx_[N]
the derivative array
AD< T, N > & operator*=(double other)
multiplication assignment with a double
double x_
the variable value
AD(AD< T, N > const &other)
copy constructor
void diff(unsigned int i, unsigned int n=0)
set as the ith variable of N
AD< T, N > & operator+=(double other)
addition assignment with a double
AD< T, N > & operator+=(AD< B, N > const &other)
addition assignment with another AD variable
AD< T, N > & operator-=(AD< B, N > const &other)
subtraction assignment with another AD variable
T & dx(unsigned int i)
get the ith derivative value (mutable)
const T & dx(unsigned int i) const
get the ith derivative value (immutable)
const double & val() const
get the value of the variable (immutable)
AD< T, N > & operator=(double other)
assignment to a double
All MTH functions are contained in this namespace.
AD< T, N > operator-(AD< T, N > const &A)
unary subtraction
double sqrt(double A)
wrapper for standard sqrt function
AD< T, N > pow(AD< T, N > const &A, AD< T, N > const &e)
AD variable raised to an AD variable power.
AD< T, N > sin(AD< T, N > const &A)
sin of an AD variable
AD< T, N > cos(AD< T, N > const &A)
cos of an AD variable
AD< T, N > operator*(double L, AD< T, N > const &R)
binary multiplication between a double and an AD variable
double exp(double x)
wrapper to standard exp function
AD< T, N > log(AD< T, N > const &A)
logarithm of an AD variable
bool operator<=(double L, AD< T, N > const &R)
double less than or equal to an AD variable
AD< T, N > abs(AD< T, N > const &A)
absolute value of an AD variable
bool operator>(double L, AD< T, N > const &R)
double greater than an AD variable
AD< T, N > exp(AD< T, N > const &A)
exponent of an AD variable
AD< T, N > sqrt(AD< T, N > const &A)
square root of an AD variable
AD< T, N > tan(AD< T, N > const &A)
tan of an AD variable
AD< T, N > operator+(double L, AD< T, N > const &R)
binary addition between a double and an AD variable
double pow(double A, double e)
wrapper to standard pow function
AD< T, N > operator/(double L, AD< T, N > const &R)
binary division between a double and an AD variable
bool operator<(double L, AD< T, N > const &R)
double less than an AD variable
double log(double A)
wrapper for stander log function
double sin(double A)
wrapper for standard sin function
double cos(double A)
wrapper for standard cos function
bool operator>=(double L, AD< T, N > const &R)
double greater than or equal to an AD variable