Adjoint Mode Type AReal

Overview

template <typename T>
class
 AReal : public Expression<T, AReal<T>>

Defines and active data type version of the underlying type T that tracks derivatives on a tape for adjoint computation.

Derivatives will only be tracked on tape if the variable has been registered or is dependent on other registered variables. Hence creating and using variables without an active tape is not problematic.

Members

Types

type tape_type

The type of the tape that is used to store operations on this class.

type slot_type

The type used for storing this instance’s slot in the tape. This type is useful for checkpointing, where the slot of the inputs and outputs needs to be stored in the checkpoint in order to retrieve or increment their derivatives during adjoint computation.

typedef T value_type

The value-type of this class, i.e., T.

Construct, Destruct, and Assign

AReal(const T &val = T())

Constructs a new instance of this class, given its initial passive value.

Ensure that there is an active tape instance for the current thread, otherwise creating instances of this class results in undefined behaviour. This error condition is only checked in debug builds, where it throws an NoTapeException. Release builds do not perform this check.

Parameters:
const T &val = T()

Value to initialize the the instance with.

Throws:

NoTapeException – If there is no active tape for the current thread (debug mode only)

AReal(const AReal &val)

Copy-constructor

Parameters:
const AReal &val

Value to initialize the instance with.

Throws:

NoTapeException – If there is no active tape for the current thread (debug mode only)

AReal(AReal &&o)

Move-constructor

Parameters:
AReal &&o

Other object to move from

AReal(const Expression<T, Expr> &expr)

Construct from an expression. This constructor gets called from statements like this, where the right-hand side involves and active data type:

AReal<double> y = x + x*sin(x);
Parameters:
const Expression<T, Expr> &expr

The expression to construct from

Throws:

NoTapeException – If there is no active tape for the current thread (debug mode only)

AReal &operator=(const T &val)

Assign from a passive value.

Parameters:
const T &val

Value to be assigned to this object.

Returns:

A reference to this

Throws:

NoTapeException – If there is no active tape for the current thread (debug mode only)

AReal &operator=(const AReal &val)

Assign from another AReal object.

Parameters:
const AReal &val

Value to be assigned to this object.

Returns:

A reference to this

Throws:

NoTapeException – If there is no active tape for the current thread (debug mode only)

AReal &operator=(AReal &&val)

Move-assignment

Parameters:
AReal &&val

Value to be moved into this object

Returns:

A reference to this

AReal &operator=(const Expression<T, Expr> &expr)

Assign an expression

Parameters:
const Expression<T, Expr> &expr

Expression to be assigned to this object.

Returns:

A reference to this

Throws:

NoTapeException – If there is no active tape for the current thread (debug mode only)

~AReal()

Destructor.

Values and Derivatives

T getValue() const

Get the value of this object, as the underlying type.

Returns:

The value of this object

const T &value() const

Get a const reference to the value of this object.

Returns:

The value of this object

T &value()

Get a reference to the value of this object, i.e. it is assignable

Returns:

Reference to the value of this object

T getDerivative() const

Get the stored derivative of this object.

Returns:

The derivative (adjoint) of this object

const T &derivative() const

Get a const reference to the stored derivative of this object.

Returns:

The derivative (adjoint) of this object

Throws:

OutOfRange – If the derivatives have not been initialized yet

T &derivative()

Get a reference to the stored derivative of this object, i.e., it is assignable.

Returns:

A reference to the derivative (adjoint) of this object

void setDerivative(const T &a)

Sets the derivative of this object. This is the same as calling derivative() = a.

Parameters:
const T &a

The value to assign to the derivative.

void setAdjoint(const T &a)

Synonym for setDerivative(a).

bool shouldRecord() const

Checks if the variable has been registered with a tape and should therefore be recorded.

Other Operations

In addition, AReal supports all other mathematical arithmetic operations, such as operator+= and friends. Also, as AReal is an Expression, all free math functions defined for expressions also work on instances of this class.


Last update: September 2022