33 #ifndef SACADO_ELRFAD_SFAD_HPP
34 #define SACADO_ELRFAD_SFAD_HPP
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 "SELRFad::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 "SELRFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
128 throw "SELRFad::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 "SELRFad::SFad() Error: Attempt to assign with incompatible sizes";
153 LocalAccumOp< Expr<S> > op(x);
156 for(
int i=0;
i<Num; ++
i) {
169 this->
val() = x.val();
184 void diff(
const int ith,
const int n) {
185 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
187 throw "SELRFad::diff() Error: Supplied derivative dimension does not match compile time length.";
201 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
203 throw "SELRFad::resize() Error: Cannot resize fixed derivative array dimension";
232 template <
typename S>
236 if (x.size() != this->size())
return false;
237 bool eq = IE::eval(x.val(), this->
val());
238 for (
int i=0;
i<this->size();
i++)
239 eq = eq && IE::eval(x.dx(
i), this->
dx(
i));
267 int size()
const {
return Num;}
290 const T*
dx()
const {
return &(dx_[0]);}
294 const T&
dx(
int i)
const {
return dx_[
i]; }
313 dots[0] = this->dx_[
i];
338 template <
typename S>
348 Expr& operator=(
const Expr&
x) {
355 for (
int i=0;
i<Num;
i++)
362 template <
typename S>
365 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
367 throw "SELRFad::operator=() Error: Attempt to assign with incompatible sizes";
371 LocalAccumOp< Expr<S> > op(x);
374 for(
int i=0;
i<Num; ++
i) {
400 template <
typename S>
408 template <
typename S>
416 template <
typename S>
420 for (
int i=0;
i<Num; ++
i)
426 template <
typename S>
430 for (
int i=0;
i<Num; ++
i)
436 template <
typename S>
439 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
441 throw "SELRFad::operator+=() Error: Attempt to assign with incompatible sizes";
445 LocalAccumOp< Expr<S> > op(x);
448 for(
int i=0;
i<Num; ++
i) {
467 template <
typename S>
470 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
472 throw "SELRFad::operator-=() Error: Attempt to assign with incompatible sizes";
476 LocalAccumOp< Expr<S> > op(x);
479 for(
int i=0;
i<Num; ++
i) {
498 template <
typename S>
503 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
505 throw "SELRFad::operator*=() Error: Attempt to assign with incompatible sizes";
509 LocalAccumOp< Expr<S> > op(x);
512 for(
int i=0;
i<Num; ++
i) {
521 dx_[
i] =
val_ * op.t + dx_[
i] * xval;
531 template <
typename S>
536 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
538 throw "SELRFad::operator/=() Error: Attempt to assign with incompatible sizes";
542 LocalAccumOp< Expr<S> > op(x);
547 for(
int i=0;
i<Num; ++
i) {
556 dx_[
i] = (dx_[
i] * xval -
val_ * op.t) / xval2;
582 template <
typename ExprT>
583 struct LocalAccumOp {
585 static const int N = ExprT::num_args;
596 template <
typename ArgT>
598 void operator () (ArgT arg)
const {
600 if (x.template isActive<Arg>())
601 t += partials[Arg] * x.template getTangent<Arg>(
i);
611 #define FAD_NS ELRFad
618 #endif // SACADO_ELRFAD_SFAD_HPP
SACADO_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.
SACADO_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
#define SACADO_ENABLE_VALUE_CTOR_DECL
RemoveConst< T >::type value_type
Typename of values.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
SACADO_INLINE_FUNCTION const T & getTangent(int i) const
Return tangent component i of argument Arg.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
#define SACADO_ENABLE_EXPR_CTOR_DECL
SACADO_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
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 S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
SACADO_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
SACADO_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
SACADO_INLINE_FUNCTION bool isActive() const
Return whether argument is active.
SACADO_INLINE_FUNCTION void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
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 const value_type * getDx(int j) const
Get dx array.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION LocalAccumOp(const ExprT &x_)
Base template specification for testing equivalence.
SACADO_INLINE_FUNCTION void getTangents(int i_)
SACADO_INLINE_FUNCTION void getTangents(int i, value_type dots[]) const
Return tangent component i of arguments.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION void cache() const
Cache values.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
Wrapper for a generic expression template.
ScalarType< value_type >::type scalar_type
Typename of scalar's (which may be different from T)
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION void computePartials(const T &bar, value_type partials[]) const
Return partials w.r.t. arguments.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
Initialize the derivative array.
ExprT::value_type value_type
SACADO_INLINE_FUNCTION Expr()
Default constructor.
SACADO_INLINE_FUNCTION Expr(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION ~Expr()
Destructor.
SACADO_INLINE_FUNCTION void zero()
Zero out the derivative array.
SACADO_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
A tag for specializing Expr for SFad expressions.