26#ifndef EIGEN_DONT_PARALLELIZE
27#define EIGEN_DONT_PARALLELIZE
84#define DECLARE_DIFFSCALAR_BASE() \
85 thread_local size_t DiffScalarBase::m_variableCount = 0
110template <
typename _Scalar,
typename _Gradient = Eigen::Matrix<_Scalar, Eigen::Dynamic, 1>>
127 grad.resize(variableCount);
136 grad.resize(variableCount);
231 throw std::runtime_error(
"DScalar1: Division by zero!");
249 invValueSqr = (
Scalar)1 / valueSqr;
312 temp = a * std::pow(s.
value, a - 1);
347 if (std::abs(s.
value) >= 1)
348 throw std::runtime_error(
"acos: Expected a value in (-1, 1)");
359 if (std::abs(s.
value) >= 1)
360 throw std::runtime_error(
"asin: Expected a value in (-1, 1)");
375 y.grad * (
x.value / denom) -
x.grad * (
y.value / denom));
425#if defined(__MITSUBA_MITSUBA_H_)
458template <
typename Scalar,
typename VecType>
462 <<
", grad=" << s.
getGradient().format(Eigen::IOFormat(4, 1,
", ",
"; ",
"",
"",
"[",
"]"))
490template <
typename _Scalar,
typename _Gradient = Eigen::Matrix<_Scalar, Eigen::Dynamic, 1>,
491 typename _Hessian = Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic>>
510 grad.resize(variableCount);
512 hess.resize(variableCount, variableCount);
522 grad.resize(variableCount);
525 hess.resize(variableCount, variableCount);
623 throw std::runtime_error(
"DScalar2: Division by zero!");
641 valueCub = valueSqr * s.
value,
642 invValueSqr = (
Scalar)1 / valueSqr;
648 result.
grad = s.
grad * -invValueSqr;
651 result.
hess = s.
hess * -invValueSqr;
734 temp = a * std::pow(s.
value, a - 1);
744 * (a * (a - 1) * std::pow(s.
value, a - 2));
786 cosVal = std::cos(s.
value);
804 cosVal = std::cos(s.
value);
820 if (std::abs(s.
value) >= 1)
821 throw std::runtime_error(
"acos: Expected a value in (-1, 1)");
834 * s.
value / (temp * temp * temp);
841 if (std::abs(s.
value) >= 1)
842 throw std::runtime_error(
"asin: Expected a value in (-1, 1)");
855 * s.
value / (temp * temp * temp);
863 DScalar2 result(std::atan2(
y.value,
x.value));
866 Scalar denom =
x.value *
x.value +
y.value *
y.value,
867 denomSqr = denom * denom;
868 result.
grad =
y.grad * (
x.value / denom)
869 -
x.grad * (
y.value / denom);
873 result.
hess = (
y.hess *
x.value
874 +
y.grad *
x.grad.transpose()
876 -
x.grad *
y.grad.transpose())
880 (
y.grad * (
x.value / denomSqr) -
x.grad * (
y.value / denomSqr)) * (
x.grad * ((
Scalar)2 *
x.value) +
y.grad * ((
Scalar)2 *
y.value)).transpose();
922#if defined(__MITSUBA_MITSUBA_H_)
968template <
typename Scalar,
typename VecType,
typename MatType>
972 <<
", grad=" << s.
getGradient().format(Eigen::IOFormat(4, 1,
", ",
"; ",
"",
"",
"[",
"]"))
973 <<
", hess=" << s.
getHessian().format(Eigen::IOFormat(4, 0,
", ",
"; ",
"",
"",
"[",
"]"))
std::ostream & operator<<(std::ostream &out, const DScalar1< Scalar, VecType > &s)
Automatic differentiation scalar with first-order derivatives.
friend DScalar1 sin(const DScalar1 &s)
DScalar1(const DScalar1 &s)
Copy constructor.
DScalar1 & operator/=(const Scalar &v)
friend DScalar1 atan2(const DScalar1 &y, const DScalar1 &x)
friend DScalar1 operator-(const DScalar1 &s)
friend DScalar1 cos(const DScalar1 &s)
bool operator<=(const Scalar &s) const
DScalar1 & operator+=(const Scalar &v)
friend DScalar1 operator*(const DScalar1 &lhs, const Scalar &rhs)
DScalar1 & operator-=(const DScalar1 &s)
friend DScalar1 operator/(const DScalar1 &lhs, const DScalar1 &rhs)
bool operator==(const Scalar &s) const
friend DScalar1 operator*(const DScalar1 &lhs, const DScalar1 &rhs)
friend DScalar1 operator-(const DScalar1 &lhs, const DScalar1 &rhs)
const Gradient & getGradient() const
bool operator<(const DScalar1 &s) const
DScalar1 & operator+=(const DScalar1 &s)
bool operator>=(const Scalar &s) const
friend DScalar1 operator-(const DScalar1 &lhs, const Scalar &rhs)
bool operator>(const Scalar &s) const
bool operator!=(const Scalar &s) const
friend DScalar1 inverse(const DScalar1 &s)
friend DScalar1 operator/(const DScalar1 &lhs, const Scalar &rhs)
friend DScalar1 operator/(const Scalar &lhs, const DScalar1 &rhs)
DScalar1(Scalar value_, const Gradient &grad_)
Construct a scalar associated with the given gradient.
friend DScalar1 operator*(const Scalar &lhs, const DScalar1 &rhs)
static DVector2 vector(const Eigen::Matrix< Scalar, 2, 1 > &v)
Initialize a constant two-dimensional vector.
DScalar1 & operator*=(const Scalar &v)
Eigen::Matrix< DScalar1, 2, 1 > DVector2
static DVector3 vector(const Eigen::Matrix< Scalar, 3, 1 > &v)
Create a constant three-dimensional vector.
bool operator>=(const DScalar1 &s) const
friend DScalar1 asin(const DScalar1 &s)
friend DScalar1 operator+(const DScalar1 &lhs, const Scalar &rhs)
DScalar1(size_t index, const Scalar &value_)
Construct a new scalar with the specified value and one first derivative set to 1.
friend DScalar1 pow(const DScalar1 &s, const Scalar &a)
bool operator<=(const DScalar1 &s) const
DScalar1(Scalar value_=(Scalar) 0)
Create a new constant automatic differentiation scalar.
friend DScalar1 operator-(const Scalar &lhs, const DScalar1 &rhs)
DScalar1 & operator-=(const Scalar &v)
friend DScalar1 exp(const DScalar1 &s)
Eigen::Matrix< DScalar1, 3, 1 > DVector3
friend DScalar1 operator+(const Scalar &lhs, const DScalar1 &rhs)
friend DScalar1 operator+(const DScalar1 &lhs, const DScalar1 &rhs)
void operator=(const Scalar &v)
friend DScalar1 acos(const DScalar1 &s)
bool operator>(const DScalar1 &s) const
friend DScalar1 sqrt(const DScalar1 &s)
friend DScalar1 log(const DScalar1 &s)
const Scalar & getValue() const
bool operator<(const Scalar &s) const
void operator=(const DScalar1 &s)
Automatic differentiation scalar with first- and second-order derivatives.
Eigen::Matrix< DScalar2, 2, 1 > DVector2
bool operator<=(const Scalar &s) const
DScalar2 & operator+=(const DScalar2 &s)
bool operator<(const Scalar &s) const
friend DScalar2 atan2(const DScalar2 &y, const DScalar2 &x)
friend DScalar2 sin(const DScalar2 &s)
bool operator>(const Scalar &s) const
bool operator>(const DScalar2 &s) const
const Scalar & getValue() const
friend DScalar2 operator+(const DScalar2 &lhs, const DScalar2 &rhs)
const Gradient & getGradient() const
bool operator<(const DScalar2 &s) const
friend DScalar2 inverse(const DScalar2 &s)
DScalar2 & operator/=(const Scalar &v)
friend DScalar2 operator*(const DScalar2 &lhs, const Scalar &rhs)
void operator=(const Scalar &v)
bool operator==(const Scalar &s) const
DScalar2 & operator+=(const Scalar &v)
friend DScalar2 cos(const DScalar2 &s)
friend DScalar2 acos(const DScalar2 &s)
DScalar2(Scalar value_, const Gradient &grad_, const Hessian &hess_)
Construct a scalar associated with the given gradient and Hessian.
DScalar2(size_t index, const Scalar &value_)
Construct a new scalar with the specified value and one first derivative set to 1.
bool operator>=(const Scalar &s) const
DScalar2(const DScalar2 &s)
Copy constructor.
bool operator>=(const DScalar2 &s) const
friend DScalar2 operator-(const DScalar2 &lhs, const DScalar2 &rhs)
static DVector2 vector(const Eigen::Matrix< Scalar, 2, 1 > &v)
Initialize a constant two-dimensional vector.
const Hessian & getHessian() const
Eigen::Matrix< DScalar2, 3, 1 > DVector3
friend DScalar2 operator+(const DScalar2 &lhs, const Scalar &rhs)
friend DScalar2 exp(const DScalar2 &s)
friend DScalar2 log(const DScalar2 &s)
friend DScalar2 operator/(const DScalar2 &lhs, const Scalar &rhs)
DScalar2(Scalar value_=(Scalar) 0)
Create a new constant automatic differentiation scalar.
friend DScalar2 operator*(const DScalar2 &lhs, const DScalar2 &rhs)
void operator=(const DScalar2 &s)
friend DScalar2 operator+(const Scalar &lhs, const DScalar2 &rhs)
bool operator<=(const DScalar2 &s) const
friend DScalar2 operator/(const Scalar &lhs, const DScalar2 &rhs)
friend DScalar2 sqrt(const DScalar2 &s)
friend DScalar2 operator-(const DScalar2 &s)
friend DScalar2 asin(const DScalar2 &s)
DScalar2 & operator-=(const Scalar &v)
DScalar2 & operator-=(const DScalar2 &s)
friend DScalar2 pow(const DScalar2 &s, const Scalar &a)
friend DScalar2 operator*(const Scalar &lhs, const DScalar2 &rhs)
static DVector3 vector(const Eigen::Matrix< Scalar, 3, 1 > &v)
Create a constant three-dimensional vector.
DScalar2 & operator*=(const Scalar &v)
friend DScalar2 operator-(const Scalar &lhs, const DScalar2 &rhs)
bool operator!=(const Scalar &s) const
friend DScalar2 operator/(const DScalar2 &lhs, const DScalar2 &rhs)
friend DScalar2 operator-(const DScalar2 &lhs, const Scalar &rhs)
Base class of all automatic differentiation types.
static thread_local size_t m_variableCount
static size_t getVariableCount()
Get the variable count used by the automatic differentiation layer.
static void setVariableCount(size_t value)
Set the independent variable count used by the automatic differentiation layer.