Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_Exp_GeneralFad.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_FAD_EXP_GENERALFAD_HPP
31 #define SACADO_FAD_EXP_GENERALFAD_HPP
32 
37 
38 namespace Sacado {
39 
41  namespace Fad {
42  namespace Exp {
43 
45 
49  template <typename Storage>
50  class GeneralFad :
51  public Expr< GeneralFad<Storage> >, // Brings in expression interface
52  public Extender<Storage> // Brings in interface extensions & storage
53  {
54  public:
55 
57  typedef Storage StorageType;
58 
61 
64 
66  using typename StorageType::value_type;
67 
70 
72  static constexpr bool is_view = Storage::is_view;
73 
75  template <typename T>
76  struct apply {
77  typedef typename Storage::template apply<T>::type S;
79  };
80 
82  template <int N>
83  struct apply_N {
84  typedef typename Storage::template apply_N<N>::type S;
86  };
87 
92 
94  using ExtenderType::ExtenderType;
95 
98  GeneralFad() = default;
99 
102  GeneralFad(const GeneralFad& x) = default;
103 
106  GeneralFad(GeneralFad&& x) = default;
107 
109  template <typename S>
112  ExtenderType(x) {}
113 
115  template <typename S>
118  ExtenderType(x.derived().size(), value_type(0.), NoInitDerivArray)
119  {
121  }
122 
125  ~GeneralFad() = default;
126 
128 
135  void diff(const int ith, const int n) {
136  if (this->size() != n)
137  this->resize(n);
138 
139  this->zero();
140  this->fastAccessDx(ith) = value_type(1.);
141  }
142 
144 
147  void setUpdateValue(bool update_val) {}
148 
150 
153  bool updateValue() const { return true; }
154 
156 
159  void cache() const {}
160 
162  template <typename S>
164  SACADO_EXP_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& xx) const {
165  typedef typename Expr<S>::derived_type expr_type;
166  const expr_type& x = xx.derived();
167 
168  typedef IsEqual<value_type> IE;
169  if (x.size() != this->size()) return false;
170  bool eq = IE::eval(x.val(), this->val());
171  const int sz = this->size();
173  eq = eq && IE::eval(x.dx(i), this->dx(i));
174  return eq;
175  }
176 
178 
183 
189  int availableSize() const { return this->length(); }
190 
193  bool hasFastAccess() const { return this->size()!=0; }
194 
197  void setIsConstant(bool is_const) {
198  if (is_const && this->size()!=0)
199  this->resize(0);
200  }
201 
203 
208 
210  template <typename S>
212  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator=(const S& v) {
213  this->val() = v;
214  if (this->size()) this->resize(0);
215  return *this;
216  }
217 
220  GeneralFad&
221  operator=(const GeneralFad& x) = default;
222 
225  GeneralFad&
226  operator=(GeneralFad&& x) = default;
227 
229  template <typename S>
231  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator=(const Expr<S>& x) {
232  ExprAssign<GeneralFad>::assign_equal(*this, x.derived());
233  return *this;
234  }
235 
237 
242 
244  template <typename S>
246  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator += (const S& v) {
247  this->val() += v;
248  return *this;
249  }
250 
252  template <typename S>
254  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator -= (const S& v) {
255  this->val() -= v;
256  return *this;
257  }
258 
260  template <typename S>
262  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator *= (const S& v) {
263  const int sz = this->size();
264  this->val() *= v;
266  this->fastAccessDx(i) *= v;
267  return *this;
268  }
269 
271  template <typename S>
273  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator /= (const S& v) {
274  const int sz = this->size();
275  this->val() /= v;
277  this->fastAccessDx(i) /= v;
278  return *this;
279  }
280 
283  GeneralFad& operator += (const GeneralFad& x) {
285  return *this;
286  }
287 
290  GeneralFad& operator -= (const GeneralFad& x) {
292  return *this;
293  }
294 
297  GeneralFad& operator *= (const GeneralFad& x) {
299  return *this;
300  }
301 
304  GeneralFad& operator /= (const GeneralFad& x) {
306  return *this;
307  }
308 
310  template <typename S>
312  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator += (const Expr<S>& x) {
313  ExprAssign<GeneralFad>::assign_plus_equal(*this, x.derived());
314  return *this;
315  }
316 
318  template <typename S>
320  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator -= (const Expr<S>& x) {
321  ExprAssign<GeneralFad>::assign_minus_equal(*this, x.derived());
322  return *this;
323  }
324 
326  template <typename S>
328  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator *= (const Expr<S>& x) {
329  ExprAssign<GeneralFad>::assign_times_equal(*this, x.derived());
330  return *this;
331  }
332 
334  template <typename S>
336  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator /= (const Expr<S>& x) {
338  return *this;
339  }
340 
342 
343  }; // class GeneralFad
344 
345  template <typename S>
346  struct ExprLevel< GeneralFad<S> > {
347  static constexpr unsigned value =
349  };
350 
351  template <typename S>
352  struct IsFadExpr< GeneralFad<S> > {
353  static constexpr bool value = true;
354  };
355 
356  } // namespace Exp
357  } // namespace Fad
358 
359  template <typename S>
360  struct IsView< Fad::Exp::GeneralFad<S> > {
361  static constexpr bool value = Fad::Exp::GeneralFad<S>::is_view;
362  };
363 
364  template <typename S>
365  struct IsFad< Fad::Exp::GeneralFad<S> > {
366  static constexpr bool value = true;
367  };
368 
369  template <typename S>
370  struct IsExpr< Fad::Exp::GeneralFad<S> > {
371  static constexpr bool value = true;
372  };
373 
374  template <typename S>
375  struct BaseExprType< Fad::Exp::GeneralFad<S> > {
377  };
378 
379 } // namespace Sacado
380 
381 #include "Sacado_Fad_Exp_Ops.hpp"
382 
383 #endif // SACADO_FAD_EXP_GENERALFAD_HPP
Wrapper for a generic expression template.
static SACADO_INLINE_FUNCTION void assign_divide_equal(DstType &dst, const SrcType &x)
Implementation of dst /= x.
expr expr dx(i)
SACADO_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
static SACADO_INLINE_FUNCTION void assign_times_equal(DstType &dst, const SrcType &x)
Implementation of dst *= x.
SACADO_INLINE_FUNCTION GeneralFad(const Expr< S > &x, SACADO_EXP_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object (disabled for ViewFad)
Base template specification for whether a type is a Fad type.
#define SACADO_DEFAULTED_FUNCTION
SACADO_INLINE_FUNCTION SACADO_EXP_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &xx) const
Returns whether two Fad objects have the same values.
SACADO_INLINE_FUNCTION GeneralFad(const S &x, SACADO_EXP_ENABLE_VALUE_CTOR_DECL)
Constructor with value (disabled for ViewFad)
static SACADO_INLINE_FUNCTION void assign_equal(DstType &dst, const SrcType &x)
Implementation of dst = x.
Turn GeneralFad into a meta-function class usable with mpl::apply.
#define SACADO_EXP_ENABLE_EXPR_CTOR_DECL
Determine whether a given type is an expression.
Wrapper for a generic expression template.
Replace static derivative length.
SACADO_INLINE_FUNCTION void cache() const
Cache values.
Is a type an expression.
SACADO_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
expr val()
Storage::template apply_N< N >::type S
SACADO_INLINE_FUNCTION const derived_type & derived() const
Return derived object.
SACADO_INLINE_FUNCTION void diff(const int ith, const int n)
Set GeneralFad object as the ith independent variable.
Determine whether a given type is a view.
Base template specification for testing equivalence.
Forward-mode AD class templated on the storage for the derivative array.
#define SACADO_FAD_DERIV_LOOP(I, SZ)
Get the base Fad type from a view/expression.
Extender< Storage > ExtenderType
Extender type.
#define SACADO_EXP_ENABLE_VALUE_CTOR_DECL
T derived_type
Typename of derived object, returned by derived()
SACADO_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
Do not initialize the derivative array.
Storage::template apply< T >::type S
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from T)
static constexpr bool is_view
Whether we are a view.
Meta-function for determining nesting with an expression.
Expr< GeneralFad< Storage > > ExprType
Expression type.
int value
SACADO_INLINE_FUNCTION SACADO_ENABLE_VALUE_FUNC(GeneralFad &) operator
Assignment operator with constant right-hand-side.
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
SACADO_DEFAULTED_FUNCTION GeneralFad()=default
Default constructor.
static SACADO_INLINE_FUNCTION void assign_minus_equal(DstType &dst, const SrcType &x)
Implementation of dst -= x.
SACADO_DEFAULTED_FUNCTION ~GeneralFad()=default
Destructor.
SACADO_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
#define SACADO_INLINE_FUNCTION
SACADO_DEFAULTED_FUNCTION GeneralFad & operator=(GeneralFad &&x)=default
Move assignment with GeneralFad right-hand-side.
SACADO_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
static SACADO_INLINE_FUNCTION void assign_plus_equal(DstType &dst, const SrcType &x)
Implementation of dst += x.
Forward-mode AD class templated on the storage for the derivative array.
Extension class for extending interface of its argument.