23 #if !defined(__AUTODIFF_H)
25 #define EIGEN_DONT_PARALLELIZE
26 #define EIGEN_NO_DEBUG
29 #include <Eigen/Geometry>
33 #if defined(__WINDOWS__)
34 #define __tls_decl __declspec(thread)
36 #define __tls_decl __thread
73 #define DECLARE_DIFFSCALAR_BASE() \
74 __tls_decl size_t DiffScalarBase::m_variableCount = 0
99 template <
typename _Scalar,
typename _Gradient = Eigen::Matrix<_Scalar, Eigen::Dynamic, 1> >
114 grad.resize(Eigen::NoChange_t(), variableCount);
122 grad.resize(Eigen::NoChange_t(), variableCount);
129 : value(value), grad(grad) { }
205 throw std::runtime_error(
"DScalar1: Division by zero!");
220 invValueSqr = (
Scalar) 1 / valueSqr;
276 temp = a * std::pow(s.
value, a-1);
306 if (std::abs(s.
value) >= 1)
307 throw std::runtime_error(
"acos: Expected a value in (-1, 1)");
317 if (std::abs(s.
value) >= 1)
318 throw std::runtime_error(
"asin: Expected a value in (-1, 1)");
362 #if defined(__MITSUBA_MITSUBA_H_)
391 template <
typename Scalar,
typename VecType>
392 std::ostream &operator<<(std::ostream &out, const DScalar1<Scalar, VecType> &s) {
393 out <<
"[" << s.getValue()
394 <<
", grad=" << s.getGradient().format(Eigen::IOFormat(4, 1,
", ",
"; ",
"",
"",
"[",
"]"))
422 template <
typename _Scalar,
typename _Gradient = Eigen::Matrix<_Scalar, Eigen::Dynamic, 1>,
423 typename _Hessian = Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic> >
440 grad.resize(Eigen::NoChange_t(), variableCount);
442 hess.resize(variableCount, variableCount);
451 grad.resize(Eigen::NoChange_t(), variableCount);
454 hess.resize(variableCount, variableCount);
460 : value(value), grad(grad), hess(hess) { }
540 throw std::runtime_error(
"DScalar2: Division by zero!");
555 valueCub = valueSqr * s.
value,
556 invValueSqr = (
Scalar) 1 / valueSqr;
562 result.
grad = s.
grad * -invValueSqr;
565 result.
hess = s.
hess * -invValueSqr;
567 * ((
Scalar) 2 / valueCub);
641 temp = a * std::pow(s.
value, a-1);
651 * (a * (a-1) * std::pow(s.
value, a-2));
690 cosVal = std::cos(s.
value);
707 cosVal = std::cos(s.
value);
722 if (std::abs(s.
value) >= 1)
723 throw std::runtime_error(
"acos: Expected a value in (-1, 1)");
736 * s.
value / (temp*temp*temp);
742 if (std::abs(s.
value) >= 1)
743 throw std::runtime_error(
"asin: Expected a value in (-1, 1)");
756 * s.
value / (temp*temp*temp);
767 denomSqr = denom*denom;
813 #if defined(__MITSUBA_MITSUBA_H_)
843 template <
typename Scalar,
typename VecType,
typename MatType>
844 std::ostream &operator<<(std::ostream &out, const DScalar2<Scalar, VecType, MatType> &s) {
845 out <<
"[" << s.getValue()
846 <<
", grad=" << s.getGradient().format(Eigen::IOFormat(4, 1,
", ",
"; ",
"",
"",
"[",
"]"))
847 <<
", hess=" << s.getHessian().format(Eigen::IOFormat(4, 0,
", ",
"; ",
"",
"",
"[",
"]"))
friend DScalar1 operator/(const DScalar1 &lhs, const DScalar1 &rhs)
Definition: autodiff.h:214
friend DScalar2 asin(const DScalar2 &s)
Definition: autodiff.h:741
DScalar2 & operator+=(const Scalar &v)
Definition: autodiff.h:493
bool operator>=(const Scalar &s) const
Definition: autodiff.h:351
friend DScalar1 operator-(const DScalar1 &lhs, const DScalar1 &rhs)
Definition: autodiff.h:171
Base class of all automatic differentiation types.
Definition: autodiff.h:45
bool operator<=(const Scalar &s) const
Definition: autodiff.h:349
friend DScalar1 operator-(const Scalar &lhs, const DScalar1 &rhs)
Definition: autodiff.h:179
bool operator>=(const Scalar &s) const
Definition: autodiff.h:802
friend DScalar2 log(const DScalar2 &s)
Definition: autodiff.h:672
Eigen::Matrix< DScalar1, 2, 1 > DVector2
Definition: autodiff.h:104
T y
Definition: point.h:419
DScalar2 & operator-=(const DScalar2 &s)
Definition: autodiff.h:521
friend DScalar2 atan2(const DScalar2 &y, const DScalar2 &x)
Definition: autodiff.h:761
DScalar2 & operator*=(const Scalar &v)
Definition: autodiff.h:607
T x
Definition: point.h:419
Eigen::Matrix< DScalar2, 3, 1 > DVector3
Definition: autodiff.h:430
Automatic differentiation scalar with first- and second-order derivatives.
Definition: autodiff.h:424
friend DScalar2 operator+(const DScalar2 &lhs, const Scalar &rhs)
Definition: autodiff.h:478
static size_t getVariableCount()
Get the variable count used by the automatic differentiation layer.
Definition: autodiff.h:63
friend DScalar2 sin(const DScalar2 &s)
Definition: autodiff.h:688
void operator=(const DScalar2 &s)
Definition: autodiff.h:793
T y
Definition: vector.h:455
DScalar1 & operator*=(const Scalar &v)
Definition: autodiff.h:252
friend DScalar2 operator-(const DScalar2 &s)
Definition: autodiff.h:517
friend DScalar1 operator*(const DScalar1 &lhs, const Scalar &rhs)
Definition: autodiff.h:238
bool operator<=(const DScalar1 &s) const
Definition: autodiff.h:345
friend DScalar2 operator-(const DScalar2 &lhs, const Scalar &rhs)
Definition: autodiff.h:509
friend DScalar1 log(const DScalar1 &s)
Definition: autodiff.h:288
friend DScalar2 operator-(const Scalar &lhs, const DScalar2 &rhs)
Definition: autodiff.h:513
friend DScalar2 operator*(const DScalar2 &lhs, const Scalar &rhs)
Definition: autodiff.h:584
Eigen::Matrix< DScalar2, 2, 1 > DVector2
Definition: autodiff.h:429
Eigen::Matrix< DScalar1, 3, 1 > DVector3
Definition: autodiff.h:105
friend DScalar2 operator/(const DScalar2 &lhs, const DScalar2 &rhs)
Definition: autodiff.h:549
friend DScalar1 operator*(const DScalar1 &lhs, const DScalar1 &rhs)
Definition: autodiff.h:246
_Gradient Gradient
Definition: autodiff.h:427
friend DScalar1 atan2(const DScalar1 &y, const DScalar1 &x)
Definition: autodiff.h:327
friend DScalar1 sin(const DScalar1 &s)
Definition: autodiff.h:295
friend DScalar1 sqrt(const DScalar1 &s)
Definition: autodiff.h:265
DScalar2 & operator+=(const DScalar2 &s)
Definition: autodiff.h:486
_Hessian Hessian
Definition: autodiff.h:428
friend DScalar2 operator/(const DScalar2 &lhs, const Scalar &rhs)
Definition: autodiff.h:538
const Scalar & getValue() const
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:135
friend DScalar2 operator+(const DScalar2 &lhs, const DScalar2 &rhs)
Definition: autodiff.h:473
bool operator>(const Scalar &s) const
Definition: autodiff.h:801
bool operator<=(const DScalar2 &s) const
Definition: autodiff.h:796
bool operator==(const Scalar &s) const
Definition: autodiff.h:803
Automatic differentiation scalar with first-order derivatives.
Definition: autodiff.h:100
static __thread size_t m_variableCount
Definition: autodiff.h:70
friend DScalar2 operator-(const DScalar2 &lhs, const DScalar2 &rhs)
Definition: autodiff.h:505
DScalar1 & operator-=(const DScalar1 &s)
Definition: autodiff.h:187
friend DScalar2 operator+(const Scalar &lhs, const DScalar2 &rhs)
Definition: autodiff.h:482
friend DScalar2 operator*(const DScalar2 &lhs, const DScalar2 &rhs)
Definition: autodiff.h:592
friend DScalar1 operator+(const DScalar1 &lhs, const DScalar1 &rhs)
Definition: autodiff.h:141
friend DScalar1 operator+(const Scalar &lhs, const DScalar1 &rhs)
Definition: autodiff.h:149
friend DScalar1 exp(const DScalar1 &s)
Definition: autodiff.h:281
DScalar1(Scalar value, const Gradient &grad)
Construct a scalar associated with the given gradient.
Definition: autodiff.h:128
T x
Definition: vector.h:251
T x
Definition: point.h:226
const Hessian & getHessian() const
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:468
DScalar1(size_t index, const Scalar &value)
Construct a new scalar with the specified value and one first derivative set to 1.
Definition: autodiff.h:119
friend DScalar2 cos(const DScalar2 &s)
Definition: autodiff.h:705
friend DScalar1 operator/(const Scalar &lhs, const DScalar1 &rhs)
Definition: autodiff.h:210
friend DScalar1 inverse(const DScalar1 &s)
Definition: autodiff.h:218
Parameterizable two-dimensional point data structure.
Definition: point.h:222
DScalar1 & operator+=(const Scalar &v)
Definition: autodiff.h:159
bool operator<(const Scalar &s) const
Definition: autodiff.h:348
bool operator>(const DScalar2 &s) const
Definition: autodiff.h:797
bool operator<(const DScalar1 &s) const
Definition: autodiff.h:344
_Gradient Gradient
Definition: autodiff.h:103
friend DScalar2 pow(const DScalar2 &s, const Scalar &a)
Definition: autodiff.h:639
void operator=(const Scalar &v)
Definition: autodiff.h:343
friend DScalar2 operator*(const Scalar &lhs, const DScalar2 &rhs)
Definition: autodiff.h:588
DScalar2(size_t index, const Scalar &value)
Construct a new scalar with the specified value and one first derivative set to 1.
Definition: autodiff.h:447
T z
Definition: vector.h:455
static void setVariableCount(size_t value)
Set the independent variable count used by the automatic differentiation layer.
Definition: autodiff.h:58
DScalar1(Scalar value=(Scalar) 0)
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:112
Parameterizable three-dimensional vector data structure.
Definition: vector.h:450
friend DScalar1 asin(const DScalar1 &s)
Definition: autodiff.h:316
friend DScalar2 acos(const DScalar2 &s)
Definition: autodiff.h:721
DScalar2 & operator/=(const Scalar &v)
Definition: autodiff.h:572
void operator=(const Scalar &v)
Definition: autodiff.h:794
friend DScalar1 cos(const DScalar1 &s)
Definition: autodiff.h:300
const Gradient & getGradient() const
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:136
DScalar2(Scalar value, const Gradient &grad, const Hessian &hess)
Construct a scalar associated with the given gradient and Hessian.
Definition: autodiff.h:459
T x
Definition: vector.h:455
Gradient grad
Definition: autodiff.h:388
DScalar1 & operator-=(const Scalar &v)
Definition: autodiff.h:193
Hessian hess
Definition: autodiff.h:840
friend DScalar2 exp(const DScalar2 &s)
Definition: autodiff.h:656
friend DScalar1 pow(const DScalar1 &s, const Scalar &a)
Definition: autodiff.h:274
bool operator<(const Scalar &s) const
Definition: autodiff.h:799
friend DScalar1 operator+(const DScalar1 &lhs, const Scalar &rhs)
Definition: autodiff.h:145
bool operator<=(const Scalar &s) const
Definition: autodiff.h:800
bool operator!=(const Scalar &s) const
Definition: autodiff.h:804
Parameterizable three-dimensional point data structure.
Definition: point.h:415
T y
Definition: vector.h:251
friend DScalar1 operator*(const Scalar &lhs, const DScalar1 &rhs)
Definition: autodiff.h:242
friend DScalar1 operator-(const DScalar1 &s)
Definition: autodiff.h:183
DScalar1 & operator+=(const DScalar1 &s)
Definition: autodiff.h:153
DScalar1 & operator/=(const Scalar &v)
Definition: autodiff.h:226
friend DScalar1 operator-(const DScalar1 &lhs, const Scalar &rhs)
Definition: autodiff.h:175
Scalar value
Definition: autodiff.h:387
Parameterizable two-dimensional vector data structure.
Definition: vector.h:246
_Scalar Scalar
Definition: autodiff.h:102
DScalar2 & operator-=(const Scalar &v)
Definition: autodiff.h:528
const Gradient & getGradient() const
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:467
bool operator>=(const DScalar1 &s) const
Definition: autodiff.h:347
Scalar value
Definition: autodiff.h:838
bool operator==(const Scalar &s) const
Definition: autodiff.h:352
_Scalar Scalar
Definition: autodiff.h:426
friend DScalar2 operator/(const Scalar &lhs, const DScalar2 &rhs)
Definition: autodiff.h:545
void operator=(const DScalar1 &s)
Definition: autodiff.h:342
DScalar1(const DScalar1 &s)
Copy constructor.
Definition: autodiff.h:132
bool operator!=(const Scalar &s) const
Definition: autodiff.h:353
bool operator>(const DScalar1 &s) const
Definition: autodiff.h:346
bool operator>(const Scalar &s) const
Definition: autodiff.h:350
DScalar2(Scalar value=(Scalar) 0)
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:437
bool operator>=(const DScalar2 &s) const
Definition: autodiff.h:798
friend DScalar1 acos(const DScalar1 &s)
Definition: autodiff.h:305
#define __tls_decl
Definition: autodiff.h:36
friend DScalar1 operator/(const DScalar1 &lhs, const Scalar &rhs)
Definition: autodiff.h:203
T z
Definition: point.h:419
DScalar2(const DScalar2 &s)
Copy constructor.
Definition: autodiff.h:463
const Scalar & getValue() const
Create a new constant automatic differentiation scalar.
Definition: autodiff.h:466
friend DScalar2 inverse(const DScalar2 &s)
Definition: autodiff.h:553
bool operator<(const DScalar2 &s) const
Definition: autodiff.h:795
Gradient grad
Definition: autodiff.h:839
T y
Definition: point.h:226
friend DScalar2 sqrt(const DScalar2 &s)
Definition: autodiff.h:621