37#ifndef VIGRA_AUTODIFF_HXX
38#define VIGRA_AUTODIFF_HXX
40#include "tinyvector.hxx"
41#include "mathutil.hxx"
86template <
class T,
int N>
194 d = o.v * d + v * o.d;
208 d = (o.v * d - v * o.d) / sq(o.v);
224template <
class T,
int N>
225TinyVector<DualVector<T, N>, N>
226dualMatrix(TinyVector<T, N>
const & v)
228 TinyVector<DualVector<T, N>, N> res;
229 for(
int k=0; k<N; ++k)
232 res[k].d[k] = T(1.0);
237template <
class T,
int N>
238inline DualVector<T, N> operator+(DualVector<T, N> v1, DualVector<T, N>
const & v2)
243template <
class T,
int N>
244inline DualVector<T, N> operator+(DualVector<T, N> v1, T v2)
249template <
class T,
int N>
250inline DualVector<T, N> operator+(T v1, DualVector<T, N> v2)
255template <
class T,
int N>
256inline DualVector<T, N> operator-(DualVector<T, N> v1, DualVector<T, N>
const & v2)
261template <
class T,
int N>
262inline DualVector<T, N> operator-(DualVector<T, N> v1, T v2)
267template <
class T,
int N>
268inline DualVector<T, N> operator-(T v1, DualVector<T, N>
const & v2)
270 return DualVector<T, N>(v1 - v2.v, -v2.d);
273template <
class T,
int N>
274inline DualVector<T, N> operator*(DualVector<T, N> v1, DualVector<T, N>
const & v2)
279template <
class T,
int N>
280inline DualVector<T, N> operator*(DualVector<T, N> v1, T v2)
285template <
class T,
int N>
286inline DualVector<T, N> operator*(T v1, DualVector<T, N> v2)
291template <
class T,
int N>
292inline DualVector<T, N> operator/(DualVector<T, N> v1, DualVector<T, N>
const & v2)
297template <
class T,
int N>
298inline DualVector<T, N> operator/(DualVector<T, N> v1, T v2)
303template <
class T,
int N>
304inline DualVector<T, N> operator/(T v1, DualVector<T, N>
const & v2)
306 return DualVector<T, N>(v1 / v2.v, -v1*v2.d / sq(v2.v));
311template <
typename T,
int N>
312inline DualVector<T, N> abs(DualVector<T, N>
const & v)
314 return v.v < T(0.0) ? -v : v;
319template <
typename T,
int N>
320inline DualVector<T, N> fabs(DualVector<T, N>
const & v)
322 return v.v < T(0.0) ? -v : v;
327template <
typename T,
int N>
328inline DualVector<T, N> log(DualVector<T, N> v)
337template <
class T,
int N>
338inline DualVector<T, N> exp(DualVector<T, N> v)
347template <
typename T,
int N>
348inline DualVector<T, N> sqrt(DualVector<T, N> v)
358template <
typename T,
int N>
359inline DualVector<T, N> sin(DualVector<T, N> v)
367template <
typename T,
int N>
368inline DualVector<T, N> cos(DualVector<T, N> v)
378template <
typename T,
int N>
379inline DualVector<T, N> sin_pi(DualVector<T, N> v)
381 v.d *= M_PI*cos_pi(v.v);
387template <
typename T,
int N>
388inline DualVector<T, N> cos_pi(DualVector<T, N> v)
390 v.d *= -M_PI*sin_pi(v.v);
397template <
typename T,
int N>
398inline DualVector<T, N> asin(DualVector<T, N> v)
400 v.d /= sqrt(T(1.0) - sq(v.v));
407template <
typename T,
int N>
408inline DualVector<T, N> acos(DualVector<T, N> v)
410 v.d /= -sqrt(T(1.0) - sq(v.v));
417template <
typename T,
int N>
418inline DualVector<T, N> tan(DualVector<T, N> v)
421 v.d *= T(1.0) + sq(v.v);
427template <
typename T,
int N>
428inline DualVector<T, N> atan(DualVector<T, N> v)
430 v.d /= T(1.0) + sq(v.v);
438template <
typename T,
int N>
439inline DualVector<T, N> sinh(DualVector<T, N> v)
447template <
typename T,
int N>
448inline DualVector<T, N> cosh(DualVector<T, N> v)
457template <
typename T,
int N>
458inline DualVector<T, N> tanh(DualVector<T, N> v)
461 v.d *= T(1.0) - sq(v.v);
467template <
class T,
int N>
468inline DualVector<T, N> sq(DualVector<T, N> v)
477template <
typename T,
int N>
478inline DualVector<T, N> atan2(DualVector<T, N> v1, DualVector<T, N>
const & v2)
480 v1.d = (v2.v * v1.d - v1.v * v2.d) / (sq(v1.v) + sq(v2.v));
481 v1.v = atan2(v1.v, v2.v);
488template <
typename T,
int N>
489inline DualVector<T, N> pow(DualVector<T, N> v, T p)
491 T pow_p_1 = pow(v.v, p-T(1.0));
498template <
typename T,
int N>
499inline DualVector<T, N> pow(T v, DualVector<T, N> p)
508template <
typename T,
int N>
509inline DualVector<T, N> pow(DualVector<T, N> v, DualVector<T, N>
const & p)
511 T pow_p_1 = pow(v.v, p.v-T(1.0)),
512 pow_p = v.v * pow_p_1;
513 v.d = p.v * pow_p_1 * v.d + pow_p * log(v.v) * p.d;
519template <
class T,
int N>
520inline DualVector<T, N> min(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
527template <
class T,
int N>
528inline DualVector<T, N> min(T v1, DualVector<T, N>
const & v2)
531 ? DualVector<T, N>(v1)
535template <
class T,
int N>
536inline DualVector<T, N> min(DualVector<T, N>
const & v1, T v2)
540 : DualVector<T, N>(v2);
544template <
class T,
int N>
545inline DualVector<T, N> max(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
552template <
class T,
int N>
553inline DualVector<T, N> max(T v1, DualVector<T, N>
const & v2)
556 ? DualVector<T, N>(v1)
560template <
class T,
int N>
561inline DualVector<T, N> max(DualVector<T, N>
const & v1, T v2)
565 : DualVector<T, N>(v2);
568template <
class T,
int N>
570operator==(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
572 return v1.v == v2.v && v1.d == v2.d;
575template <
class T,
int N>
577operator!=(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
579 return v1.v != v2.v || v1.d != v2.d;
582#define VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(op) \
583template <class T, int N> \
585operator op(DualVector<T, N> const & v1, DualVector<T, N> const & v2) \
587 return v1.v op v2.v; \
590template <class T, int N> \
592operator op(T v1, DualVector<T, N> const & v2) \
597template <class T, int N> \
599operator op(DualVector<T, N> const & v1, T v2) \
604VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(<)
605VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(<=)
606VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(>)
607VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(>=)
609#undef VIGRA_DUALVECTOR_RELATIONAL_OPERATORS
611template <
class T,
int N>
613closeAtTolerance(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2,
614 T epsilon = NumericTraits<T>::epsilon())
626template <
class T,
int N>
630 out << l.v <<
" " << l.d;
Class for a single RGB value.
Definition rgbvalue.hxx:128
Definition autodiff.hxx:88
DualVector(T const &val, T const &g0, T const &g1)
Definition autodiff.hxx:126
T value_type
type of function values and gradient elements
Definition autodiff.hxx:90
DualVector(T const &val, int targetElement)
Definition autodiff.hxx:136
Gradient const & gradient() const
Definition autodiff.hxx:151
DualVector(T const &val)
Definition autodiff.hxx:104
DualVector(T const &val, T const &g0)
Definition autodiff.hxx:118
TinyVector< T, N > Gradient
type of the gradient vector
Definition autodiff.hxx:91
DualVector(T const &val, Gradient const &grad)
Definition autodiff.hxx:110
DualVector()
Definition autodiff.hxx:98
T value() const
Definition autodiff.hxx:144
REAL cos_pi(REAL x)
cos(pi*x).
Definition mathutil.hxx:1242
REAL sin_pi(REAL x)
sin(pi*x).
Definition mathutil.hxx:1204
SquareRootTraits< FixedPoint< IntBits, FracBits > >::SquareRootResult sqrt(FixedPoint< IntBits, FracBits > v)
square root.
Definition fixedpoint.hxx:616
NumericTraits< T >::Promote sq(T t)
The square function.
Definition mathutil.hxx:382
FFTWComplex< R >::NormType abs(const FFTWComplex< R > &a)
absolute value (= magnitude)
Definition fftw3.hxx:1002
bool closeAtTolerance(T1 l, T2 r, typename PromoteTraits< T1, T2 >::Promote epsilon)
Tolerance based floating-point equality.
Definition mathutil.hxx:1638