37 #ifndef VIGRA_AUTODIFF_HXX
38 #define VIGRA_AUTODIFF_HXX
40 #include "tinyvector.hxx"
41 #include "mathutil.hxx"
86 template <
class T,
int N>
139 d[targetElement] = T(1.0);
194 d = o.v * d + v * o.d;
208 d = (o.v * d - v * o.d) /
sq(o.v);
224 template <
class T,
int N>
225 TinyVector<DualVector<T, N>, N>
226 dualMatrix(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);
237 template <
class T,
int N>
238 inline DualVector<T, N>
operator+(DualVector<T, N> v1, DualVector<T, N>
const & v2)
243 template <
class T,
int N>
244 inline DualVector<T, N>
operator+(DualVector<T, N> v1, T v2)
249 template <
class T,
int N>
250 inline DualVector<T, N>
operator+(T v1, DualVector<T, N> v2)
255 template <
class T,
int N>
256 inline DualVector<T, N>
operator-(DualVector<T, N> v1, DualVector<T, N>
const & v2)
261 template <
class T,
int N>
262 inline DualVector<T, N>
operator-(DualVector<T, N> v1, T v2)
267 template <
class T,
int N>
268 inline DualVector<T, N>
operator-(T v1, DualVector<T, N>
const & v2)
270 return DualVector<T, N>(v1 - v2.v, -v2.d);
273 template <
class T,
int N>
274 inline DualVector<T, N> operator*(DualVector<T, N> v1, DualVector<T, N>
const & v2)
279 template <
class T,
int N>
280 inline DualVector<T, N> operator*(DualVector<T, N> v1, T v2)
285 template <
class T,
int N>
286 inline DualVector<T, N> operator*(T v1, DualVector<T, N> v2)
291 template <
class T,
int N>
292 inline DualVector<T, N> operator/(DualVector<T, N> v1, DualVector<T, N>
const & v2)
297 template <
class T,
int N>
298 inline DualVector<T, N> operator/(DualVector<T, N> v1, T v2)
303 template <
class T,
int N>
304 inline 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));
311 template <
typename T,
int N>
312 inline DualVector<T, N>
abs(DualVector<T, N>
const & v)
314 return v.v < T(0.0) ? -v : v;
319 template <
typename T,
int N>
320 inline DualVector<T, N> fabs(DualVector<T, N>
const & v)
322 return v.v < T(0.0) ? -v : v;
327 template <
typename T,
int N>
328 inline DualVector<T, N>
log(DualVector<T, N> v)
337 template <
class T,
int N>
338 inline DualVector<T, N>
exp(DualVector<T, N> v)
347 template <
typename T,
int N>
348 inline DualVector<T, N>
sqrt(DualVector<T, N> v)
358 template <
typename T,
int N>
359 inline DualVector<T, N>
sin(DualVector<T, N> v)
367 template <
typename T,
int N>
368 inline DualVector<T, N>
cos(DualVector<T, N> v)
378 template <
typename T,
int N>
379 inline DualVector<T, N>
sin_pi(DualVector<T, N> v)
387 template <
typename T,
int N>
388 inline DualVector<T, N>
cos_pi(DualVector<T, N> v)
397 template <
typename T,
int N>
398 inline DualVector<T, N>
asin(DualVector<T, N> v)
400 v.d /=
sqrt(T(1.0) -
sq(v.v));
407 template <
typename T,
int N>
408 inline DualVector<T, N>
acos(DualVector<T, N> v)
410 v.d /= -
sqrt(T(1.0) -
sq(v.v));
417 template <
typename T,
int N>
418 inline DualVector<T, N>
tan(DualVector<T, N> v)
421 v.d *= T(1.0) +
sq(v.v);
427 template <
typename T,
int N>
428 inline DualVector<T, N>
atan(DualVector<T, N> v)
430 v.d /= T(1.0) +
sq(v.v);
438 template <
typename T,
int N>
439 inline DualVector<T, N> sinh(DualVector<T, N> v)
447 template <
typename T,
int N>
448 inline DualVector<T, N> cosh(DualVector<T, N> v)
457 template <
typename T,
int N>
458 inline DualVector<T, N> tanh(DualVector<T, N> v)
461 v.d *= T(1.0) -
sq(v.v);
467 template <
class T,
int N>
468 inline DualVector<T, N>
sq(DualVector<T, N> v)
477 template <
typename T,
int N>
478 inline 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);
488 template <
typename T,
int N>
489 inline DualVector<T, N> pow(DualVector<T, N> v, T p)
491 T pow_p_1 = pow(v.v, p-T(1.0));
498 template <
typename T,
int N>
499 inline DualVector<T, N> pow(T v, DualVector<T, N> p)
508 template <
typename T,
int N>
509 inline 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;
519 template <
class T,
int N>
520 inline DualVector<T, N> min(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
527 template <
class T,
int N>
528 inline DualVector<T, N> min(T v1, DualVector<T, N>
const & v2)
531 ? DualVector<T, N>(v1)
535 template <
class T,
int N>
536 inline DualVector<T, N> min(DualVector<T, N>
const & v1, T v2)
540 : DualVector<T, N>(v2);
544 template <
class T,
int N>
545 inline DualVector<T, N> max(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
552 template <
class T,
int N>
553 inline DualVector<T, N> max(T v1, DualVector<T, N>
const & v2)
556 ? DualVector<T, N>(v1)
560 template <
class T,
int N>
561 inline DualVector<T, N> max(DualVector<T, N>
const & v1, T v2)
565 : DualVector<T, N>(v2);
568 template <
class T,
int N>
570 operator==(DualVector<T, N>
const & v1, DualVector<T, N>
const & v2)
572 return v1.v == v2.v && v1.d == v2.d;
575 template <
class T,
int N>
577 operator!=(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) \
583 template <class T, int N> \
585 operator op(DualVector<T, N> const & v1, DualVector<T, N> const & v2) \
587 return v1.v op v2.v; \
590 template <class T, int N> \
592 operator op(T v1, DualVector<T, N> const & v2) \
597 template <class T, int N> \
599 operator op(DualVector<T, N> const & v1, T v2) \
604 VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(<)
605 VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(<=)
606 VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(>)
607 VIGRA_DUALVECTOR_RELATIONAL_OPERATORS(>=)
609 #undef VIGRA_DUALVECTOR_RELATIONAL_OPERATORS
611 template <
class T,
int N>
614 T epsilon = NumericTraits<T>::epsilon())
626 template <
class T,
int N>
628 operator<<(ostream & out, vigra::autodiff::DualVector<T, N>
const & l)
630 out << l.v <<
" " << l.d;
636 #endif // VIGRA_AUTODIFF_HXX
REAL sin_pi(REAL x)
sin(pi*x).
Definition: mathutil.hxx:1204
TinyVector< T, N > Gradient
type of the gradient vector
Definition: autodiff.hxx:91
linalg::TemporaryMatrix< T > acos(MultiArrayView< 2, T, C > const &v)
FixedPoint16< 2, OverflowHandling > atan2(FixedPoint16< IntBits, OverflowHandling > y, FixedPoint16< IntBits, OverflowHandling > x)
Arctangent. Accuracy better than 1/3 degree (9 significant bits).
Definition: fixedpoint.hxx:1654
Diff2D operator-(Diff2D const &a, Diff2D const &b)
Definition: diff2d.hxx:711
linalg::TemporaryMatrix< T > sin(MultiArrayView< 2, T, C > const &v)
Definition: autodiff.hxx:87
linalg::TemporaryMatrix< T > exp(MultiArrayView< 2, T, C > const &v)
Gradient const & gradient() const
Definition: autodiff.hxx:151
T value() const
Definition: autodiff.hxx:144
Diff2D operator+(Diff2D const &a, Diff2D const &b)
Definition: diff2d.hxx:739
linalg::TemporaryMatrix< T > asin(MultiArrayView< 2, T, C > const &v)
DualVector(T const &val)
Definition: autodiff.hxx:104
REAL cos_pi(REAL x)
cos(pi*x).
Definition: mathutil.hxx:1242
DualVector(T const &val, T const &g0)
Definition: autodiff.hxx:118
NumericTraits< T >::Promote sq(T t)
The square function.
Definition: mathutil.hxx:382
DualVector(T const &val, T const &g0, T const &g1)
Definition: autodiff.hxx:126
bool operator!=(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
not equal
Definition: fftw3.hxx:841
T value_type
type of function values and gradient elements
Definition: autodiff.hxx:90
bool operator==(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
equal
Definition: fftw3.hxx:825
DualVector(T const &val, Gradient const &grad)
Definition: autodiff.hxx:110
TinyVector< V, SIZE > pow(TinyVectorBase< V, SIZE, D1, D2 > const &v, E exponent)
Definition: tinyvector.hxx:2036
V const & max(TinyVectorBase< V, SIZE, D1, D2 > const &l)
maximum element
Definition: tinyvector.hxx:2210
DualVector()
Definition: autodiff.hxx:98
bool closeAtTolerance(T1 l, T2 r, typename PromoteTraits< T1, T2 >::Promote epsilon)
Tolerance based floating-point equality.
Definition: mathutil.hxx:1638
linalg::TemporaryMatrix< T > log(MultiArrayView< 2, T, C > const &v)
FFTWComplex< R >::NormType abs(const FFTWComplex< R > &a)
absolute value (= magnitude)
Definition: fftw3.hxx:1002
linalg::TemporaryMatrix< T > atan(MultiArrayView< 2, T, C > const &v)
linalg::TemporaryMatrix< T > tan(MultiArrayView< 2, T, C > const &v)
linalg::TemporaryMatrix< T > cos(MultiArrayView< 2, T, C > const &v)
DualVector(T const &val, int targetElement)
Definition: autodiff.hxx:136
SquareRootTraits< FixedPoint< IntBits, FracBits > >::SquareRootResult sqrt(FixedPoint< IntBits, FracBits > v)
square root.
Definition: fixedpoint.hxx:616
V const & min(TinyVectorBase< V, SIZE, D1, D2 > const &l)
minimum element
Definition: tinyvector.hxx:2161