33 #ifndef SACADO_ELRCACHEFAD_SFAD_HPP
34 #define SACADO_ELRCACHEFAD_SFAD_HPP
44 namespace ELRCacheFad {
47 template <
typename T,
int Num>
51 template <
typename T,
int Num>
class SFad;
60 template <
typename T,
int Num>
75 static const int num_args = 1;
78 static const bool is_linear =
true;
106 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
108 throw "ELRCacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
122 Expr(
const int sz,
const int i,
const T &
x) :
124 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
126 throw "ELRCacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
128 throw "ELRCacheFad::SFad() Error: Invalid derivative index.";
139 for (
int i=0;
i<Num;
i++)
144 template <
typename S>
147 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
149 throw "ELRCacheFad::SFad() Error: Attempt to assign with incompatible sizes";
154 this->
val() = x.val();
157 LocalAccumOp< Expr<S> > op(x);
160 for(
int i=0;
i<Num; ++
i) {
186 void diff(
const int ith,
const int n) {
187 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
189 throw "ELRCacheFad::diff() Error: Supplied derivative dimension does not match compile time length.";
203 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
205 throw "ELRCacheFad::resize() Error: Cannot resize fixed derivative array dimension";
234 template <
typename S>
238 if (x.size() != this->size())
return false;
239 bool eq = IE::eval(x.val(), this->
val());
240 for (
int i=0;
i<this->size();
i++)
241 eq = eq && IE::eval(x.dx(
i), this->
dx(
i));
269 int size()
const {
return Num;}
292 const T*
dx()
const {
return &(dx_[0]);}
296 const T&
dx(
int i)
const {
return dx_[
i]; }
315 dots[0] = this->dx_[
i];
340 template <
typename S>
350 Expr& operator=(
const Expr&
x) {
357 for (
int i=0;
i<Num;
i++)
364 template <
typename S>
367 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
369 throw "ELRCacheFad::operator=() Error: Attempt to assign with incompatible sizes";
375 LocalAccumOp< Expr<S> > op(x);
378 for(
int i=0;
i<Num; ++
i) {
390 this->
val() = x.val();
403 template <
typename S>
411 template <
typename S>
419 template <
typename S>
423 for (
int i=0;
i<Num; ++
i)
429 template <
typename S>
433 for (
int i=0;
i<Num; ++
i)
439 template <
typename S>
442 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
444 throw "ELRCacheFad::operator+=() Error: Attempt to assign with incompatible sizes";
450 LocalAccumOp< Expr<S> > op(x);
453 for(
int i=0;
i<Num; ++
i) {
465 this->
val() += x.val();
471 template <
typename S>
474 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
476 throw "ELRCacheFad::operator-=() Error: Attempt to assign with incompatible sizes";
482 LocalAccumOp< Expr<S> > op(x);
485 for(
int i=0;
i<Num; ++
i) {
497 this->
val() -= x.val();
503 template <
typename S>
510 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
512 throw "ELRCacheFad::operator*=() Error: Attempt to assign with incompatible sizes";
516 LocalAccumOp< Expr<S> > op(x);
519 for(
int i=0;
i<Num; ++
i) {
528 dx_[
i] =
val_ * op.t + dx_[
i] * xval;
538 template <
typename S>
545 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
547 throw "ELRCacheFad::operator/=() Error: Attempt to assign with incompatible sizes";
551 LocalAccumOp< Expr<S> > op(x);
556 for(
int i=0;
i<Num; ++
i) {
565 dx_[
i] = (dx_[
i] * xval -
val_ * op.t) / xval2;
591 template <
typename ExprT>
592 struct LocalAccumOp {
594 static const int N = ExprT::num_args;
603 template <
typename ArgT>
605 void operator () (ArgT arg)
const {
607 if (x.template isActive<Arg>())
608 t += partials[Arg] * x.template getTangent<Arg>(
i);
618 #define FAD_NS ELRCacheFad
625 #endif // SACADO_ELRCACHEFAD_SFAD_HPP
SACADO_INLINE_FUNCTION Expr(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
SACADO_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
SACADO_INLINE_FUNCTION const value_type * getDx(int j) const
Get dx array.
ScalarType< value_type >::type scalar_type
Typename of scalar's (which may be different from T)
SACADO_INLINE_FUNCTION void cache() const
Cache values.
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION ~Expr()
Destructor.
#define SACADO_ENABLE_VALUE_CTOR_DECL
SACADO_INLINE_FUNCTION SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &x) const
Returns whether two Fad objects have the same values.
SACADO_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.
SACADO_INLINE_FUNCTION Expr()
Default constructor.
SACADO_INLINE_FUNCTION void computePartials(const T &bar, value_type partials[]) const
Return partials w.r.t. arguments.
#define SACADO_ENABLE_EXPR_CTOR_DECL
SACADO_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
SACADO_INLINE_FUNCTION LocalAccumOp(const ExprT &x_)
SACADO_INLINE_FUNCTION Expr(const int sz, const int i, const T &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
SACADO_INLINE_FUNCTION void zero()
Zero out the derivative array.
SACADO_INLINE_FUNCTION void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
Base template specification for testing equivalence.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
Wrapper for a generic expression template.
ExprT::value_type value_type
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
RemoveConst< T >::type value_type
Typename of values.
SACADO_INLINE_FUNCTION Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
SACADO_INLINE_FUNCTION bool isActive() const
Return whether argument is active.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
SACADO_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
SACADO_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
Initialize the derivative array.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION void getTangents(int i, value_type dots[]) const
Return tangent component i of arguments.
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION T getTangent(int i) const
Return tangent component i of argument Arg.
A tag for specializing Expr for SFad expressions.