NEML2 1.4.0
Loading...
Searching...
No Matches
Variable< T > Class Template Reference

Concrete definition of a variable. More...

Detailed Description

template<typename T>
class neml2::Variable< T >

Concrete definition of a variable.

#include <Variable.h>

Inheritance diagram for Variable< T >:

Public Member Functions

template<typename T2 = T, typename = typename std::enable_if_t<!std::is_same_v<Tensor, T2>>>
 Variable (const VariableName &name_in, const Model *owner, TensorType type=TensorTypeEnum< T2 >::value)
 
template<typename T2 = T, typename = typename std::enable_if_t<std::is_same_v<Tensor, T2>>>
 Variable (const VariableName &name_in, const Model *owner, TensorShapeRef base_shape, TensorType type=TensorType::kTensor)
 
virtual void setup_views (const LabeledVector *value, const LabeledMatrix *deriv=nullptr, const LabeledTensor3D *secderiv=nullptr) override
 Setup the variable's views into blocks of the storage.
 
virtual void setup_views (const VariableBase *other) override
 Setup the variable's views following another variable.
 
virtual void requires_grad_ (bool req=true) override
 Set requires_grad for the underlying storage.
 
virtual TensorShapeRef base_sizes () const override
 Base shape.
 
virtual TensorShapeRef sizes () const override
 Total shape.
 
 Variable (const Variable< T > &)
 Suppressed constructor to prevent accidental dereferencing.
 
void operator= (const Variable< T > &)
 Suppressed assignment operator to prevent accidental dereferencing.
 
void operator= (const Tensor &val)
 Set the raw value to store val.
 
const T & value () const
 Variable value of the logical shape.
 
virtual const Tensor tensor () const override
 Variable value of the logical shape.
 
virtual TensorType type () const override
 Variable type.
 
operator- () const
 Negation.
 
 operator T () const
 
virtual void cache (TensorShapeRef batch_shape) override
 Set the batch shape and base shape according to val.
 
template<typename T2 = T, typename = typename std::enable_if_t<!std::is_same_v<T2, Tensor>>>
 operator Tensor () const
 
- Public Member Functions inherited from VariableBase
 VariableBase (const VariableName &name_in, const Model *owner)
 
virtual ~VariableBase ()=default
 
Derivative d (const VariableBase &x)
 Create a wrapper representing the derivative dy/dx.
 
Derivative d (const VariableBase &x1, const VariableBase &x2)
 Create a wrapper representing the second derivative d2y/dx2.
 
const Tensorraw_value () const
 Raw flattened variable value.
 
const VariableNamename () const
 Name of this variable.
 
const Modelowner () const
 The owner of this variable.
 
const VariableBasesrc () const
 The source variable.
 
TensorShapeRef batch_sizes () const
 Batch shape.
 
Size batch_dim () const
 Batch dimension.
 
Size base_dim () const
 Base dimension.
 
Size base_storage () const
 Base storage.
 
bool is_dependent () const
 Check if the derivative with respect to this variable should be evaluated.
 
bool is_state () const
 
bool is_old_state () const
 
bool is_force () const
 
bool is_old_force () const
 
bool is_residual () const
 
bool is_parameter () const
 
bool is_other () const
 
bool is_solve_dependent () const
 

Protected Attributes

const TensorType _type
 Variable tensor type.
 
const TensorShape _base_sizes
 Base shape of this variable.
 
TensorShape _sizes
 Shape of this variable.
 
_value
 Variable value of the logical shape.
 
- Protected Attributes inherited from VariableBase
const VariableName _name
 Name of the variable.
 
const Model_owner
 The model which declared this variable.
 
TensorShape _batch_sizes
 Batch shape of this variable.
 
Tensor _raw_value
 The raw (flattened) variable value.
 
std::map< VariableName, Tensor_dvalue_d
 The derivative of this variable w.r.t. arguments.
 
std::map< VariableName, std::map< VariableName, Tensor > > _d2value_d
 The second derivative of this variable w.r.t. arguments.
 
const VariableBase_src
 The source variable this variable follows.
 
const bool _is_state
 
const bool _is_old_state
 
const bool _is_force
 
const bool _is_old_force
 
const bool _is_residual
 
const bool _is_parameter
 
const bool _is_other
 
const bool _is_solve_dependent
 

Constructor & Destructor Documentation

◆ Variable() [1/3]

template<typename T >
template<typename T2 = T, typename = typename std::enable_if_t<!std::is_same_v<Tensor, T2>>>
Variable ( const VariableName & name_in,
const Model * owner,
TensorType type = TensorTypeEnum<T2>::value )
inline

◆ Variable() [2/3]

template<typename T >
template<typename T2 = T, typename = typename std::enable_if_t<std::is_same_v<Tensor, T2>>>
Variable ( const VariableName & name_in,
const Model * owner,
TensorShapeRef base_shape,
TensorType type = TensorType::kTensor )
inline

◆ Variable() [3/3]

template<typename T >
Variable ( const Variable< T > & )
inline

Suppressed constructor to prevent accidental dereferencing.

Member Function Documentation

◆ base_sizes()

template<typename T >
virtual TensorShapeRef base_sizes ( ) const
inlineoverridevirtual

Base shape.

Implements VariableBase.

◆ cache()

template<typename T >
virtual void cache ( TensorShapeRef batch_shape)
inlineoverridevirtual

Set the batch shape and base shape according to val.

Reimplemented from VariableBase.

◆ operator T()

template<typename T >
operator T ( ) const
inline

◆ operator Tensor()

template<typename T >
template<typename T2 = T, typename = typename std::enable_if_t<!std::is_same_v<T2, Tensor>>>
operator Tensor ( ) const
inline

◆ operator-()

template<typename T >
T operator- ( ) const
inline

Negation.

◆ operator=() [1/2]

template<typename T >
void operator= ( const Tensor & val)
inline

Set the raw value to store val.

Note that this is an in-place operation, and so we must reshape (flatten base dimensions of) val and modify raw_value.

◆ operator=() [2/2]

template<typename T >
void operator= ( const Variable< T > & )
inline

Suppressed assignment operator to prevent accidental dereferencing.

◆ requires_grad_()

template<typename T >
virtual void requires_grad_ ( bool req = true)
inlineoverridevirtual

Set requires_grad for the underlying storage.

Implements VariableBase.

◆ setup_views() [1/2]

template<typename T >
virtual void setup_views ( const LabeledVector * value,
const LabeledMatrix * deriv = nullptr,
const LabeledTensor3D * secderiv = nullptr )
inlineoverridevirtual

Setup the variable's views into blocks of the storage.

Reimplemented from VariableBase.

◆ setup_views() [2/2]

template<typename T >
virtual void setup_views ( const VariableBase * other)
inlineoverridevirtual

Setup the variable's views following another variable.

Reimplemented from VariableBase.

◆ sizes()

template<typename T >
virtual TensorShapeRef sizes ( ) const
inlineoverridevirtual

Total shape.

Implements VariableBase.

◆ tensor()

template<typename T >
virtual const Tensor tensor ( ) const
inlineoverridevirtual

Variable value of the logical shape.

Implements VariableBase.

◆ type()

template<typename T >
virtual TensorType type ( ) const
inlineoverridevirtual

Variable type.

Implements VariableBase.

◆ value()

template<typename T >
const T & value ( ) const
inline

Variable value of the logical shape.

Member Data Documentation

◆ _base_sizes

template<typename T >
const TensorShape _base_sizes
protected

Base shape of this variable.

◆ _sizes

template<typename T >
TensorShape _sizes
protected

Shape of this variable.

◆ _type

template<typename T >
const TensorType _type
protected

Variable tensor type.

◆ _value

template<typename T >
T _value
protected

Variable value of the logical shape.