Automatic differentiation is a great tool for various scientific and financial computing needs. The best approach, I think, is to use operator overloading so that the resulting automatic differentiation class can be a drop-in replacement for common variable types such as double. This approach does make it harder to create the class though, as one has to be careful to avoid temporaries that can drastically slow down calculations. The traditional way to fight temporaries is to use expression templates, but there are other ways around it. I remember at one place I used to work, the approach was to not allow binary operators and only use addition-assignment (and others) operators. Obviously, that approach makes the code a pain to read, but the resulting code was very fast.
Now that I have access to a c++11 development environment, there is a new alternative: to use move constructors and move assignment operators. So, below is my attempt to embrace it in this context. The class ardv is designed to be a drop-in replacement for the double type. A typical usage pattern might be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
template <class T> T addNumbers(T const& a, T const& b) { T res = a + b; return res; } adrv a1(1.0, 2); adrv b1(2.0, 2); a1.d(0) = 1.0; b1.d(1) = 1.0; double a2(1.0); double b2(2.0); adrv c1; double c2; c1 = addNumbers(a1, b1); c2 = addNumbers(a2, b2); //c1.d(0) is the derivative of c1 with respect to a1 //c1.d(1) is the derivative of c1 with respect to b1 std::cout << c1.v() << " " << c1.d(0) << " " << c1.d(1) << std::endl; |
And below is the actual class. Note that I make absolutely no guarantee as to its accuracy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
static const double AutoDiff_pi = 4.0 * atan(1.0); static const double AutoDiff_2_over_sqrt_pi = 2.0 / std::sqrt(AutoDiff_pi); template <class T> class adrv { T v_; std::vector<T> deriv_; public: typedef T value_type; /** Constructors **/ adrv() : v_(0.0) {} explicit adrv(T const& x) : v_(x) {} explicit adrv(int const& x) : v_(static_cast<T>(x)) {} adrv(T const& x, std::size_t const& nDerivs) : v_(x), deriv_(std::vector<T>(nDerivs, 0)) {} /** Copy constructor **/ adrv(adrv<T> const& orig); /** Move constructor **/ adrv(adrv<T>&& orig); /** Accessors **/ inline T& v() { return v_; } inline const T v() const { return v_; } inline const T d(std::size_t const& indx) const { return deriv_[indx]; } inline T& d(std::size_t const& indx) { return deriv_[indx]; } inline std::vector<T>& d() { return deriv_; } inline std::size_t numDerivs() const { return deriv_.size(); } inline std::size_t size() const { return deriv_.size(); } adrv<T>& operator=(T const& rhs); adrv<T>& operator=(adrv<T> const& rhs); //Copy assignment operator adrv<T>& operator=(adrv<T>&& rhs); //Move assignment operator adrv<T>& operator+=(T const& x); adrv<T>& operator-=(T const& x); adrv<T>& operator*=(T const& x); adrv<T>& operator/=(T const& x); adrv<T>& operator+=(adrv<T> const& x); adrv<T>& operator-=(adrv<T> const& x); adrv<T>& operator*=(adrv<T> const& x); adrv<T>& operator/=(adrv<T> const& x); adrv<T>& operator-(); adrv<T>& operator+(); inline bool operator>(T const& x) const; inline bool operator>=(T const& x) const; inline bool operator<(T const& x) const; inline bool operator<=(T const& x) const; inline bool operator==(T const& x) const; inline bool operator!=(T const& x) const; inline bool operator>(adrv<T> const& x) const; inline bool operator>=(adrv<T> const& x) const; inline bool operator<(adrv<T> const& x) const; inline bool operator<=(adrv<T> const& x) const; inline bool operator==(adrv<T> const& x) const; inline bool operator!=(adrv<T> const& x) const; /** basic math functions **/ template <class U> friend adrv<U> sqrt(adrv<U> x); template <class U> friend adrv<U> exp(adrv<U> x); template <class U> friend adrv<U> log(adrv<U> x); template <class U> friend adrv<U> erfc(adrv<U> x); template <class U> friend adrv<U> sin(adrv<U> x); template <class U> friend adrv<U> cos(adrv<U> x); template <class U> friend adrv<U> tan(adrv<U> x);s template <class U> friend adrv<U> sinh(adrv<U> x); template <class U> friend adrv<U> cosh(adrv<U> x); template <class U> friend adrv<U> tanh(adrv<U> x); template <class U> friend adrv<U> abs(adrv<U> x); }; //----------------------------------------------------------------------------------------------- // Definitions: //----------------------------------------------------------------------------------------------- template <class T> adrv<T>::adrv(adrv<T> const& orig) { if (this != &orig) { v_ = orig.v_; deriv_ = orig.deriv_; } } template <class T> adrv<T>::adrv(adrv<T>&& orig) : v_( std::move(orig.v_) ), deriv_(std::move(orig.deriv_)) {} template <class T> inline adrv<T>& adrv<T>::operator=(T const& rhs) { v_ = rhs; if (deriv_.size() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = 0.0; } } return *this; } template <class T> inline adrv<T>& adrv<T>::operator=(adrv<T> const& rhs) { if (this != &rhs) { v_ = rhs.v_; if (deriv_.size() != rhs.size()) deriv_.resize(rhs.size()); for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = rhs.d(indx); } } return *this; } template <class T> inline adrv<T>& adrv<T>::operator=(adrv<T>&& rhs) { v_ = std::move(rhs.v_); deriv_ = std::move(rhs.deriv_); return *this; } template <class T> inline adrv<T>& adrv<T>::operator+=(T const& x) { v_ += x; return *this; } template <class T> inline adrv<T>& adrv<T>::operator-=(T const& x) { v_ -= x; return *this; } template <class T> inline adrv<T>& adrv<T>::operator*=(T const& x) { v_ *= x; for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] *= x; } return *this; } template <class T> inline adrv<T>& adrv<T>::operator/=(T const& x) { v_ /= x; for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] /= x; } return *this; } template <class T> inline adrv<T>& adrv<T>::operator+=(adrv<T> const& x) { if (x.numDerivs() != deriv_.size() && (deriv_.size() != 0 && x.numDerivs() != 0)) { throw "sizes of derivative vectors must match"; } //allow 0 size... in this case, resize the current d_ vector to the size of x.d() if (deriv_.size() == 0 && x.numDerivs() > 0) deriv_.resize(x.numDerivs(), 0.0); if (x.numDerivs() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] += x.d(indx); } } v_ += x.v(); return *this; } template <class T> inline adrv<T>& adrv<T>::operator-=(adrv<T> const& x) { if (x.numDerivs() != deriv_.size() && (deriv_.size() != 0 && x.numDerivs() != 0)) { throw "sizes of derivative vectors must match"; } //allow 0 size... in this case, resize the current d_ vector to the size of x.d() if (deriv_.size() == 0 && x.size() > 0) deriv_.resize(x.size(), 0.0); if (x.numDerivs() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] -= x.d(indx); } } v_ -= x.v(); return *this; } template <class T> inline adrv<T>& adrv<T>::operator*=(adrv<T> const& x) { if (x.numDerivs() != deriv_.size() && (deriv_.size() != 0 && x.numDerivs() != 0)) { throw "sizes of derivative vectors must match"; } //allow 0 size... in this case, resize the current d_ vector to the size of x.d() if (deriv_.size() == 0 && x.numDerivs() > 0) deriv_.resize(x.numDerivs(), 0.0); const T xv = x.v(); if (x.numDerivs() > 0 && deriv_.size() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = v_ * x.d(indx) + deriv_[indx] * xv; } } else if (x.numDerivs() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = v_ * x.d(indx); } } else { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = deriv_[indx] * xv; } } v_ *= x.v(); return *this; } template <class T> inline adrv<T>& adrv<T>::operator/=(adrv<T> const& x) { if (x.numDerivs() != deriv_.size() && (deriv_.size() != 0 && x.numDerivs() != 0)) { throw "sizes of derivative vectors must match"; } //allow 0 size... in this case, resize the current d_ vector to the size of x.d() if (deriv_.size() == 0 && x.size() > 0) deriv_.resize(x.size(), 0.0); T xv = x.v(); if (x.numDerivs() > 0 && deriv_.size() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = (deriv_[indx] * xv - v_ * x.d(indx)) / (xv * xv); } } else if (x.numDerivs() > 0) { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = -v_ * x.d(indx) / (xv * xv); } } else { for(std::size_t indx = 0, endIndx = deriv_.size(); indx < endIndx; ++indx) { deriv_[indx] = deriv_[indx] / xv; } } v_ /= x.v(); return *this; } //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- // Unary operators //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- template <class T> inline adrv<T>& adrv<T>::operator-() { for(std::size_t indx = 0; indx < deriv_.size(); ++indx) { deriv_[indx] = -deriv_[indx]; } v_ = -v_; return *this; } template <class T> inline adrv<T>& adrv<T>::operator+() { return *this; } //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- // Basic binary arithmetics //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- template <class T> adrv<T> operator+(adrv<T> lhs, adrv<T> const& rhs) { lhs += rhs; return lhs; } template <class T> adrv<T> operator+(adrv<T> lhs, T const& rhs) { lhs += rhs; return lhs; } template <class T> adrv<T> operator+(T const& lhs, adrv<T> rhs) { rhs += lhs; return rhs; } template <class T> adrv<T> operator-(adrv<T> lhs, adrv<T> const& rhs) { lhs -= rhs; return lhs; } template <class T> adrv<T> operator-(adrv<T> lhs, T const& rhs) { lhs -= rhs; return lhs; } template <class T> adrv<T> operator-(T const& lhs, adrv<T> rhs) { rhs = -rhs; rhs += lhs; return rhs; } template <class T> adrv<T> operator*(adrv<T> lhs, adrv<T> const& rhs) { lhs *= rhs; return lhs; } template <class T> adrv<T> operator*(adrv<T> lhs, T const& rhs) { lhs *= rhs; return lhs; } template <class T> adrv<T> operator*(T const& lhs, adrv<T> rhs) { rhs *= lhs; return rhs; } template <class T> adrv<T> operator/(adrv<T> lhs, adrv<T> const& rhs) { lhs /= rhs; return lhs; } template <class T> adrv<T> operator/(adrv<T> lhs, T const& rhs) { lhs /= rhs; return lhs; } template <class T> adrv<T> operator/(T const& lhs, adrv<T> rhs) { for(std::size_t indx = 0; indx < rhs.numDerivs(); ++indx) { rhs.d()[indx] *= -lhs / (rhs.v() * rhs.v()); } rhs.v() = lhs / rhs.v(); return rhs; } template <class T> adrv<T> pow(adrv<T> lhs, adrv<T> const& rhs) { if (lhs.numDerivs() != rhs.numDerivs() && (lhs.numDerivs() != 0 && rhs.numDerivs() != 0)) { throw "sizes of derivative vectors must match"; } if (lhs.numDerivs() > 0 && rhs.numDerivs() > 0) { const T log_lhs = std::log(lhs.v()); const T pow_term = std::pow(lhs.v(), rhs.v() - 1.0); for(std::size_t indx = 0; indx < rhs.numDerivs(); ++indx) { lhs.d()[indx] = rhs.v() * pow_term * lhs.d(indx) + log_lhs * std::exp(rhs.v() * log_lhs) * rhs.d(indx); } } else if (lhs.numDerivs() > 0) { const T pow_term = std::pow(lhs.v(), rhs.v() - 1.0); for(std::size_t indx = 0; indx < rhs.numDerivs(); ++indx) { lhs.d()[indx] = rhs.v() * pow_term * lhs.d(indx); } } else { const T log_lhs = std::log(lhs.v()); for(std::size_t indx = 0; indx < rhs.numDerivs(); ++indx) { lhs.d()[indx] = log_lhs * std::exp(rhs.v() * log_lhs) * rhs.d(indx); } } lhs.v() = std::pow(lhs.v(), rhs.v()); return lhs; } template <class T> adrv<T> pow(adrv<T> lhs, T const& rhs) { const T pow_term = std::pow(lhs.v(), rhs - 1.0); for(std::size_t indx = 0; indx < rhs.numDerivs(); ++indx) { lhs.d()[indx] = rhs * pow_term * lhs.d(indx); } lhs.v() = std::pow(lhs.v(), rhs.v()); return lhs; } template <class T> adrv<T> pow(T const& lhs, adrv<T> rhs) { const T log_lhs = std::log(lhs); for(std::size_t indx = 0; indx < rhs.numDerivs(); ++indx) { rhs.d()[indx] = log_lhs * std::exp(rhs.v() * log_lhs) * rhs.d(indx); } rhs.v() = std::pow(lhs, rhs.v()); return rhs; } //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- // Functions //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- template <class U> adrv<U> sqrt(adrv<U> x) { U tmp = 0.5 / std::sqrt(x.v()); for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= tmp; } x.v() = std::sqrt(x.v()); return x; } template <class U> adrv<U> exp(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= std::exp(x.v()); } x.v() = std::exp(x.v()); return x; } template <class U> adrv<U> log(adrv<U> x) { U tmp = 1.0 / x.v(); for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= tmp; } x.v() = std::log(x.v()); return x; } template <class U> adrv<U> erfc(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= -AutoDiff_2_over_sqrt_pi*std::exp(-x.v() * x.v()); } x.v() = std::erfc(x.v()); return x; } template <class U> adrv<U> sin(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= std::cos(x.v()); } x.v() = std::sin(x.v()); return x; } template <class U> adrv<U> cos(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= -std::sin(x.v()); } x.v() = std::cos(x.v()); return x; } template <class U> adrv<U> tan(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] /= std::cos(x.v()) * std::cos(x.v()); } x.v() = std::tan(x.v()); return x; } template <class U> adrv<U> sinh(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= std::cosh(x.v()); } x.v() = std::sinh(x.v()); return x; } template <class U> adrv<U> cosh(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] *= std::sinh(x.v()); } x.v() = std::cosh(x.v()); return x; } template <class U> adrv<U> tanh(adrv<U> x) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] /= std::cosh(x.v()) * std::cosh(x.v()); } x.v() = std::tanh(x.v()); return x; } template <class U> adrv<U> abs(adrv<U> x) { if (x.v() < 0) { for(std::size_t indx = 0; indx < x.numDerivs(); ++indx) { x.deriv_[indx] = -x.deriv_[indx]; } } x.v() = std::abs(x.v()); return x; } //------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------ // Various comparison operators //------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------ template <class T> inline bool adrv<T>::operator> (T const& x) const { return v_ > x; } template <class T> inline bool adrv<T>::operator>=(T const& x) const { return v_ >= x; } template <class T> inline bool adrv<T>::operator< (T const& x) const { return v_ < x; } template <class T> inline bool adrv<T>::operator<=(T const& x) const { return v_ <= x; } template <class T> inline bool adrv<T>::operator==(T const& x) const { return v_ == x; } template <class T> inline bool adrv<T>::operator!=(T const& x) const { return v_ != x; } template <class T> inline bool adrv<T>::operator> (adrv<T> const& x) const { return v_ > x.v_; } template <class T> inline bool adrv<T>::operator>=(adrv<T> const& x) const { return v_ >= x.v_; } template <class T> inline bool adrv<T>::operator< (adrv<T> const& x) const { return v_ < x.v_; } template <class T> inline bool adrv<T>::operator<=(adrv<T> const& x) const { return v_ <= x.v_; } template <class T> inline bool adrv<T>::operator==(adrv<T> const& x) const { return v_ == x.v_; } template <class T> inline bool adrv<T>::operator!=(adrv<T> const& x) const { return v_ != x.v_; } template <class T, class U> bool operator> (adrv<T> const& expr, U const& val) { return expr.v() > static_cast<T>(val); } template <class T, class U> bool operator>=(adrv<T> const& expr, U const& val) { return expr.v() >= static_cast<T>(val); } template <class T, class U> bool operator< (adrv<T> const& expr, U const& val) { return expr.v() < static_cast<T>(val); } template <class T, class U> bool operator<=(adrv<T> const& expr, U const& val) { return expr.v() <= static_cast<T>(val); } template <class T, class U> bool operator==(adrv<T> const& expr, U const& val) { return expr.v() == static_cast<T>(val); } template <class T, class U> bool operator!=(adrv<T> const& expr, U const& val) { return expr.v() != static_cast<T>(val); } template <class T, class U> bool operator> (U const& val, adrv<T> const& expr) { return static_cast<T>(val) > expr.v(); } template <class T, class U> bool operator>=(U const& val, adrv<T> const& expr) { return static_cast<T>(val) >= expr.v(); } template <class T, class U> bool operator< (U const& val, adrv<T> const& expr) { return static_cast<T>(val) < expr.v(); } template <class T, class U> bool operator<=(U const& val, adrv<T> const& expr) { return static_cast<T>(val) <= expr.v(); } template <class T, class U> bool operator==(U const& val, adrv<T> const& expr) { return static_cast<T>(val) == expr.v(); } template <class T, class U> bool operator!=(U const& val, adrv<T> const& expr) { return static_cast<T>(val) != expr.v(); } /** IO operators **/ template <class T> std::ostream& operator<<(std::ostream& os, adrv<T> const& val) { os << val.v(); return os; } |
In a future post, I’ll write about the efficiency of this approach compared to a version using expression templates. In brief though, it is pretty competitive under most circumstances.