33 #include <cugar/linalg/matrix.h> 34 #include <cugar/linalg/tensor.h> 59 template <u
int32 O1, u
int32 O2>
62 static const uint32 value = O1 < O2 ? O1 : O2;
78 template <
typename ValType, u
int32 N, u
int32 O>
81 typedef ValType value_type;
90 CUGAR_HOST_DEVICE
diff_var() : du(diff_component_type(value_type(0.0f))) {}
101 CUGAR_HOST_DEVICE
diff_var(
const value_type _u,
const diff_type _du) : u(_u), du(_du) {}
107 CUGAR_HOST_DEVICE
explicit diff_var(value_type _u) : u(value_type(_u)), du(diff_component_type(0.0f)) {}
111 CUGAR_HOST_DEVICE
operator value_type()
const {
return u; }
119 CUGAR_HOST_DEVICE diff_type
diff()
const {
return du; }
131 template <
typename ValType, u
int32 N>
134 typedef ValType value_type;
135 typedef ValType diff_component_type;
152 CUGAR_HOST_DEVICE
explicit diff_var(value_type _u) : u(_u) {}
156 CUGAR_HOST_DEVICE
operator value_type()
const {
return u; }
164 CUGAR_HOST_DEVICE diff_type
diff()
const {
return diff_type(ValType(0.0f)); }
175 template <
typename ValType, u
int32 O>
178 typedef ValType value_type;
187 CUGAR_HOST_DEVICE
diff_var() : du(diff_component_type(value_type(0.0f))) {}
198 CUGAR_HOST_DEVICE
diff_var(
const value_type _u,
const diff_type _du) : u(_u), du(_du) {}
205 CUGAR_HOST_DEVICE
diff_var(
const value_type _u,
const value_type _du) : u(_u), du(diff_type(_du)) {}
211 CUGAR_HOST_DEVICE
explicit diff_var(value_type _u) : u(value_type(_u)), du(diff_component_type(value_type(0.0f))) {}
215 CUGAR_HOST_DEVICE
operator value_type()
const {
return u; }
223 CUGAR_HOST_DEVICE diff_type
diff()
const {
return du; }
238 template <
typename ValType>
241 typedef ValType value_type;
242 typedef ValType diff_component_type;
243 typedef ValType diff_type;
259 CUGAR_HOST_DEVICE
explicit diff_var(value_type _u) : u(_u) {}
263 CUGAR_HOST_DEVICE
operator value_type()
const {
return u; }
271 CUGAR_HOST_DEVICE diff_type
diff()
const {
return diff_type(ValType(0.0f)); }
281 template <
typename VT, u
int32 O>
291 template <
typename VT, u
int32 N, u
int32 O>
300 template <
typename VT, u
int32 N>
311 template <
typename VT, u
int32 N, u
int32 O>
317 for (uint32 i = 0; i < N; ++i)
324 template <
typename VT, u
int32 N>
335 template <
typename VT, u
int32 N, u
int32 O>
341 for (uint32 i = 0; i < N; ++i)
348 template <
typename VT, u
int32 N, u
int32 O>
353 for (uint32 i = 0; i < N; ++i)
360 template <
typename VT, u
int32 N, u
int32 O>
365 for (uint32 i = 0; i < N; ++i)
372 template <
typename VT, u
int32 N, u
int32 O, u
int32 TO>
377 for (uint32 i = 0; i < N; ++i)
385 template <
typename VT, u
int32 N, u
int32 O>
394 template <
typename VT, u
int32 N, u
int32 O>
399 for (uint32 i = 0; i < N; ++i)
400 for (uint32 j = 0; j < N; ++j)
408 template <
typename VT, u
int32 N, u
int32 O>
413 for (uint32 i = 0; i < N; ++i)
421 template <
typename VT, u
int32 N, u
int32 O>
426 for (uint32 i = 0; i < N; ++i)
434 template <
typename VT, u
int32 N, u
int32 O>
439 for (uint32 i = 0; i < N; ++i)
440 for (uint32 j = 0; j < N; ++j)
453 template <
typename VT, u
int32 N, u
int32 O>
458 for (uint32 i = 0; i < N; ++i)
459 for (uint32 j = 0; j < N; ++j)
469 template <
typename VT, u
int32 N, u
int32 O>
474 for (uint32 i = 0; i < N; ++i)
475 for (uint32 j = 0; j < N; ++j)
485 template <
typename VT, u
int32 N, u
int32 O>
490 for (uint32 i = 0; i < N; ++i)
491 for (uint32 j = 0; j < N; ++j)
492 for (uint32 k = 0; k < N; ++k)
505 template <u
int32 ORDER,
typename VT, u
int32 N, u
int32 O>
513 template <
typename VT, u
int32 N>
521 template <
typename VT, u
int32 N>
529 template <
typename VT, u
int32 N>
537 template <
typename VT, u
int32 N>
546 template <
typename VT, u
int32 N>
554 template <
typename VT, u
int32 N>
562 template <
typename VT, u
int32 N>
570 template <
typename VT, u
int32 N>
578 template <
typename VT, u
int32 N>
586 template <
typename VT, u
int32 N>
594 template <
typename VT, u
int32 N>
602 template <
typename VT, u
int32 N>
612 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
626 for (uint32 i = 0; i < N; ++i)
627 r.du[i] = a.du[i] + b.du[i];
634 template <
typename VT, u
int32 N>
637 static const uint32 RO = 0;
647 template <
typename VT, u
int32 N, u
int32 O>
650 static const uint32 RO = 0;
660 template <
typename VT, u
int32 N, u
int32 O>
663 static const uint32 RO = 0;
673 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
687 for (uint32 i = 0; i < N; ++i)
688 r.du[i] = a.du[i] * b + a * b.du[i];
695 template <
typename VT, u
int32 N>
698 static const uint32 RO = 0;
708 template <
typename VT, u
int32 N, u
int32 O>
711 static const uint32 RO = 0;
721 template <
typename VT, u
int32 N, u
int32 O>
724 static const uint32 RO = 0;
735 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
749 for (uint32 i = 0; i < N; ++i)
750 r.du[i] = (a.du[i] * b - a * b.du[i]) / (b * b);
757 template <
typename VT, u
int32 N>
760 static const uint32 RO = 0;
770 template <
typename VT, u
int32 N, u
int32 O>
773 static const uint32 RO = 0;
783 template <
typename VT, u
int32 N, u
int32 O>
786 static const uint32 RO = 0;
799 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
801 inline diff_var<VT,N,detail::dispatch_sum<VT,N,O1,O2>::RO> operator+(
const diff_var<VT,N,O1> a,
const diff_var<VT,N,O2> b)
808 template <
typename VT, u
int32 N, u
int32 O>
817 template <
typename VT, u
int32 N, u
int32 O>
826 template <
typename VT, u
int32 N, u
int32 O>
835 template <
typename VT, u
int32 N, u
int32 O>
844 template <
typename VT, u
int32 N, u
int32 O>
853 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
855 inline diff_var<VT,N,detail::dispatch_sum<VT,N,O1,O2>::RO> operator-(
const diff_var<VT,N,O1> a,
const diff_var<VT,N,O2> b)
862 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
864 inline diff_var<VT,N,detail::dispatch_mul<VT,N,O1,O2>::RO> operator*(
const diff_var<VT,N,O1> a,
const diff_var<VT,N,O2> b)
871 template <
typename VT, u
int32 N, u
int32 O>
879 for (uint32 i = 0; i < N; ++i)
880 r.du[i] = a.du[i] * b;
886 template <
typename VT, u
int32 N, u
int32 O>
894 for (uint32 i = 0; i < N; ++i)
895 r.du[i] = a * b.du[i];
902 template <
typename VT, u
int32 N, u
int32 O1, u
int32 O2>
904 inline diff_var<VT,N,detail::dispatch_div<VT,N,O1,O2>::RO> operator/(
const diff_var<VT,N,O1> a,
const diff_var<VT,N,O2> b)
911 template <
typename VT, u
int32 N, u
int32 O>
920 template <
typename VT, u
int32 N, u
int32 O>
929 template <
typename VT, u
int32 N, u
int32 O>
938 template <
typename VT, u
int32 N, u
int32 O>
947 template <
typename VT, u
int32 N, u
int32 O>
956 template <
typename VT, u
int32 N, u
int32 O>
1002 template <u
int32 N, u
int32 O>
1010 for (uint32 i = 0; i < N; ++i)
1016 template <u
int32 N, u
int32 O>
1024 for (uint32 i = 0; i < N; ++i)
1031 template <u
int32 N, u
int32 O>
1039 for (uint32 i = 0; i < N; ++i)
1040 r.du[i] = a.du[i] / a;
1046 template <u
int32 N, u
int32 O>
1054 for (uint32 i = 0; i < N; ++i)
1061 template <u
int32 N, u
int32 O>
1069 for (uint32 i = 0; i < N; ++i)
1075 template <u
int32 N, u
int32 O>
1079 template <u
int32 N, u
int32 O>
1083 template <u
int32 N, u
int32 O>
1087 template <u
int32 N, u
int32 O>
1091 template <u
int32 N, u
int32 O>
1096 template <
typename VT, u
int32 N>
1103 template <
typename VT, u
int32 N>
1110 template <
typename VT, u
int32 N>
1117 template <
typename VT, u
int32 N>
1124 template <
typename VT, u
int32 N>
1132 template <
typename VT, u
int32 N, u
int32 O>
1140 for (uint32 i = 0; i < N; ++i)
1146 template <
typename VT, u
int32 N, u
int32 O>
1154 for (uint32 i = 0; i < N; ++i)
1161 template <
typename VT, u
int32 N, u
int32 O>
1169 for (uint32 i = 0; i < N; ++i)
1170 r.du[i] = a.du[i] / a;
1176 template <
typename VT, u
int32 N, u
int32 O>
1184 for (uint32 i = 0; i < N; ++i)
1191 template <
typename VT, u
int32 N, u
int32 O>
1199 for (uint32 i = 0; i < N; ++i)
1205 template <
typename VT, u
int32 N, u
int32 O>
1209 template <
typename VT, u
int32 N, u
int32 O>
1213 template <
typename VT, u
int32 N, u
int32 O>
1215 inline bool operator< (const diff_var<VT,N,O> a,
const diff_var<VT,N,O> b) {
return a.u < b.u; }
1217 template <
typename VT, u
int32 N, u
int32 O>
1219 inline bool operator<= (const diff_var<VT,N,O> a,
const diff_var<VT,N,O> b) {
return a.u <= b.u; }
1221 template <
typename VT, u
int32 N, u
int32 O>
1225 template <
typename VT, u
int32 N, u
int32 O>
CUGAR_HOST_DEVICE Tensor< VT, 2, N > hessian(const diff_var< VT, N, O > op)
Definition: diff.h:436
CUGAR_HOST_DEVICE Tensor< VT, 1, N > jacobian(const diff_var< VT, N, O > op)
Definition: diff.h:423
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator+=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:931
CUGAR_HOST_DEVICE diff_type diff() const
Definition: diff.h:271
CUGAR_HOST_DEVICE diff_var(value_type _u)
Definition: diff.h:211
CUGAR_HOST_DEVICE diff_var(value_type _u)
Definition: diff.h:259
CUGAR_HOST_DEVICE diff_type diff() const
Definition: diff.h:119
CUGAR_HOST_DEVICE diff_var< VT, N, 1 > raise_order(const diff_var< VT, N, 0 > &op)
Definition: diff.h:326
CUGAR_HOST_DEVICE diff_type diff() const
Definition: diff.h:164
CUGAR_HOST_DEVICE diff_var(const value_type _u, const diff_type _du)
Definition: diff.h:101
CUGAR_HOST_DEVICE diff_var & operator=(const value_type &_u)
Definition: diff.h:160
CUGAR_HOST_DEVICE diff_var(value_type _u)
Definition: diff.h:152
CUGAR_HOST_DEVICE diff_var(const value_type _u, const value_type _du)
Definition: diff.h:205
CUGAR_HOST_DEVICE diff_var(const diff_var< ValType, 1, 0 > &_other)
Definition: diff.h:253
CUGAR_HOST_DEVICE diff_var(const diff_var< ValType, N, O > &_other)
Definition: diff.h:94
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator*=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:949
CUGAR_HOST_DEVICE diff_var< VT, N, 0 > decrease_order(const diff_var< VT, N, 1 > &op)
Definition: diff.h:302
CUGAR_HOST_DEVICE diff_var(const diff_var< ValType, 1, O > &_other)
Definition: diff.h:191
CUGAR_HOST_DEVICE diff_var(const value_type _u, const diff_type _du)
Definition: diff.h:198
CUGAR_HOST_DEVICE diff_var & operator=(const value_type &_u)
Definition: diff.h:115
CUGAR_HOST_DEVICE diff_var()
Definition: diff.h:249
CUGAR_HOST_DEVICE diff_var()
Definition: diff.h:90
CUGAR_HOST_DEVICE Tensor< diff_var< VT, N, O-2 >, 2, N > diff_hessian(const diff_var< VT, N, O > op)
Definition: diff.h:396
CUGAR_HOST_DEVICE diff_type diff() const
Definition: diff.h:223
CUGAR_HOST_DEVICE diff_var & operator=(const value_type &_u)
Definition: diff.h:219
CUGAR_HOST_DEVICE diff_var & operator=(const value_type &_u)
Definition: diff.h:267
CUGAR_HOST_DEVICE Tensor< VT, 1, N > gradient(const diff_var< VT, N, O > op)
Definition: diff.h:410
CUGAR_HOST_DEVICE diff_var()
Definition: diff.h:187
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_HOST_DEVICE diff_var()
Definition: diff.h:142
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator/=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:958
CUGAR_HOST_DEVICE diff_var(value_type _u)
Definition: diff.h:107
CUGAR_HOST_DEVICE diff_var(const diff_var< ValType, N, 0 > &_other)
Definition: diff.h:146
CUGAR_HOST_DEVICE Tensor< VT, ORDER, N > diff_tensor(const diff_var< VT, N, O > op)
Definition: diff.h:507
CUGAR_HOST_DEVICE void set_primary(diff_var< VT, 1, O > &var, const VT deriv=VT(1.0))
Definition: diff.h:283
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator-=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:940
CUGAR_HOST_DEVICE diff_var< VT, N, O >::diff_type diff(const diff_var< VT, N, O > op)
Definition: diff.h:387