30 #include <type_traits> 58 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
62 dx_.resize(xdx.
size());
63 size_t sz = rhs.
dx().size();
64 for (
size_t i = 0; i < sz; ++i) {
78 Fad() : val_( T(0.
f)), dx_(), defaultVal(T(0)) {;}
80 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
81 Fad(
const R & x) : val_(x), dx_(), defaultVal(T(0)) {;}
82 Fad(
const int sz,
const T & x) : val_(x), dx_(sz,T(0)), defaultVal(T(0)) {;}
83 Fad(
const int sz,
const int i,
const T & x) : val_(x), dx_(sz,T(0)), defaultVal(T(0))
85 Fad(
const int sz,
const T & x,
const T &
dx) : val_(x), dx_(sz, dx), defaultVal(T(0)) {;}
86 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
92 template <
class ExprT>
98 int sz = fadexpr.
size();
101 for(
int i=0; i<sz; ++i)
102 dx_[i] = fadexpr.
dx(i);
108 void diff(
const int ith,
const int n);
112 const T&
val()
const {
return val_;}
119 const T&
d(
int i)
const {
return dx_[i];}
124 const T&
dx(
int i)
const {
return dx_.
size() ? dx_[i] : defaultVal;}
134 Fad<T>& operator+= (
const T& x);
136 Fad<T>& operator*= (
const T& x);
137 Fad<T>& operator/= (
const T& x);
139 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
141 int xsz = x.
size(), sz = dx_.
size();
144 R* xdx = x.
dx().begin();
147 for (
int i=0; i<sz; ++i)
153 for (
int i=0; i<xsz; ++i)
163 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
165 int xsz = x.
size(), sz = dx_.
size();
171 for (
int i=0; i<sz; ++i)
177 for (
int i=0; i<xsz; ++i)
187 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
189 int xsz = x.
size(), sz = dx_.
size();
196 for (
int i=0; i<sz; ++i)
197 dxp[i] = val_ * xdx[i] + dxp[i] * xval;
202 for (
int i=0; i<xsz; ++i)
203 dxp[i] = val_ * xdx[i];
209 for (
int i=0; i<sz; ++i)
219 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
221 int xsz = x.
size(), sz = dx_.
size();
228 for (
int i=0; i<sz; ++i)
229 dxp[i] = (dxp[i]*xval - val_*xdx[i])/ (xval*xval);
234 for (
int i=0; i<xsz; ++i)
235 dxp[i] = -val_ * xdx[i] / (xval*xval);
241 for (
int i=0; i<sz; ++i)
251 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
256 if ( dx_.
size() ) dx_ = T(0.);
261 template<typename R, typename enable_if<is_convertible<R,T>::value,
int>::type * =
nullptr>
264 if (
this != (
Fad<T>*)(&rhs) ) copy(rhs);
276 friend ostream& operator<< (ostream& stream, const Fad<T>& x)
278 const int VAL_WIDTH = 8;
279 stream << std::setw(VAL_WIDTH)<<std::right<<x.val()<<
" dx: ";
280 for (
int i=0; i<x.dx_.size(); i++) {
281 stream << std::setw(VAL_WIDTH)<<std::right<< x.dx_[i] <<
" ";
288 return stream >> x.
val();
293 template<
class T>
inline 306 template <
class T>
inline void Fad<T>::diff(
const int ith,
const int n)
308 if ( dx_.empty() ) dx_.resize(n);
317 int sz = fadexpr.
size();
319 if ( sz != dx_.size() ) dx_.resize(sz);
324 for(
int i=0; i<sz; ++i)
327 for(
int i=0; i<sz; ++i)
328 dxp[i] = fadexpr.
dx(i);
331 val_ = fadexpr.
val();
370 for (
int i=0; i<sz;++i)
384 for (
int i=0; i<sz;++i)
395 int xsz = x.
size(), sz = dx_.size();
401 for (
int i=0; i<sz; ++i)
404 for (
int i=0; i<sz; ++i)
411 for (
int i=0; i<xsz; ++i)
414 for (
int i=0; i<xsz; ++i)
426 int xsz = x.
size(), sz = dx_.size();
432 for(
int i=0; i<sz; ++i)
435 for (
int i=0; i<sz; ++i)
442 for(
int i=0; i<xsz; ++i)
445 for (
int i=0; i<xsz; ++i)
458 int xsz = x.
size(), sz = dx_.size();
465 for(
int i=0; i<sz; ++i)
468 for (
int i=0; i<sz; ++i)
469 dxp[i] = val_ * x.
dx(i) + dxp[i] * xval;
475 for(
int i=0; i<xsz; ++i)
478 for (
int i=0; i<xsz; ++i)
479 dxp[i] = val_ * x.
dx(i);
485 for (
int i=0; i<sz; ++i)
497 int xsz = x.
size(), sz = dx_.size();
504 for(
int i=0; i<sz; ++i)
505 dxp[i] = ( dxp[i]*xval - val_*x.
fastAccessDx(i) )/ (xval*xval);
507 for (
int i=0; i<sz; ++i)
508 dxp[i] = ( dxp[i]*xval - val_*x.
dx(i) )/ (xval*xval);
514 for(
int i=0; i<xsz; ++i)
517 for (
int i=0; i<xsz; ++i)
518 dxp[i] = -val_ * x.
dx(i) / (xval*xval);
524 for (
int i=0; i<sz; ++i)
538 template <
class T>
inline ostream& operator << (ostream& os, const Fad<T>& a)
540 os.setf(ios::fixed,ios::floatfield);
542 os << a.val() <<
" [";
545 for (
int i=0; i< a.dx().size(); i++) {
565 explicit FadExpr(
const T& fadexpr) : fadexpr_(fadexpr) {;}
567 value_type
val()
const {
return fadexpr_.val();}
568 value_type
dx(
int i)
const {
return fadexpr_.dx(i);}
569 int size()
const {
return fadexpr_.size();}
572 value_type
fastAccessDx(
int i)
const {
return fadexpr_.fastAccessDx(i);}
587 explicit FadCst(
const T &value) : constant_(value), defaultVal(T(0)) {}
589 const value_type &
val()
const {
return constant_; }
590 const value_type
dx(
int i)
const {
return defaultVal; }
612 const value_type
val()
const {
return expr_.val();}
613 const value_type
dx(
int i)
const {
return expr_.dx(i);}
614 int size()
const {
return expr_.size();}
633 const value_type
val()
const {
return - expr_.val();}
634 const value_type
dx(
int i)
const {
return - expr_.dx(i);}
635 int size()
const {
return expr_.size();}
638 value_type
fastAccessDx(
int i)
const {
return - expr_.fastAccessDx(i);}
641 template <
class T>
inline 650 template <
class T>
inline 661 return Hash(
"Fad") ^ (ClassIdOrHash<T>()<<1);
Fad(const int sz, const T &x)
bool hasFastAccess() const
FadUnaryMin(const T &value)
const value_type val() const
const value_type & val() const
FadExpr< FadUnaryPlus< FadExpr< T > > > operator+(const FadExpr< T > &expr)
bool hasFastAccess() const
value_type fastAccessDx(int i) const
Fad(const FadExpr< ExprT > &fadexpr)
const T & fastAccessDx(int i) const
FadExpr< FadUnaryMin< Fad< T > > > operator-() const
const value_type dx(int i) const
TPZVec< T > & operator-=(TPZVec< T > &a, const TPZVec< T > &b)
substracts two vectors
FadUnaryPlus(const T &value)
value_type fastAccessDx(int i) const
bool hasFastAccess() const
REAL val(STATE &number)
Returns value of the variable.
Fad(const int sz, const int i, const T &x)
AutoPointerMutexArrayInit tmp
value_type fastAccessDx(int i) const
std::istream & operator>>(std::istream &out, TPZFlopCounter &val)
Implements to read (input) only the floating point value.
bool hasFastAccess() const
const value_type & fastAccessDx(int i) const
Fad< T > & operator=(const FadExpr< ExprT > &fadexpr)
expr_ dx(i) *cos(expr_.val())
Fad< T > & operator-=(const T &x)
FadSuper(const FadSuper &)
value_type dx(int i) const
const value_type dx(int i) const
FadExpr< FadUnaryPlus< Fad< T > > > operator+() const
void diff(const int ith, const int n)
Fad< T > & operator/=(const T &x)
Fad< T > & operator=(const Fad< R > &rhs)
Fad(const int sz, const T &x, const T &dx)
int32_t Hash(std::string str)
Fad< T > & operator=(const R &val)
void copy(const Fad< R > &rhs)
Fad< T > & operator+=(const T &x)
const T & dx(int i) const
const value_type val() const
Fad< T > & operator*=(const T &x)
FadExpr(const T &fadexpr)
FadExpr< FadUnaryMin< FadExpr< T > > > operator-(const FadExpr< T > &expr)
const value_type dx(int i) const
bool hasFastAccess() const
T fabs(const Fad< T > &val)
const Vector< T > & dx() const