relentless.model.DependentVariable#

class relentless.model.DependentVariable(*vardicts, **kwvars)#

Dependent quantity.

A dependent variable is composed from other variables. This is an abstract base class for any such variable. In addition to the value property of the Variable, a DependentVariable must also define \(compute\) and compute_derivative() methods that define how it (and its partial derivatives with respect to its dependencies) are computed.

The names of the dependencies of the variable are automatically deduced by the base constructor using dictionary keys and keyword arguments and assigned as attributes of the object. For example:

class NewVariable(relentless.variable.DependentVariable):
    def __init__(self, a, b):
        super().__init__({'a': a}, b=b)

will create a NewVariable with two dependent attributes a and b.

Dependencies are typically other Variable objects, but they can also be set to scalar values. In these case, the scalar is converted to an IndependentVariable first.

Parameters:
  • vardicts (dict) – Dependencies as entries in an arbitrary number of dictionaries.

  • kwvars (kwargs) – Dependencies as an arbitrary number of keyword arguments.

Methods

compute()

Implementation of the value.

compute_derivative(param)

Implementation of the derivative.

derivative(var)

Calculate derivative with respect to a Variable.

Attributes

count

names

params

Names of parameters

value

Value of the variable.

abstract compute()#

Implementation of the value.

This method should implement calculation of the variable from the parameter keywords. The parameters will be passed as keyword arguments.

abstract compute_derivative(param)#

Implementation of the derivative.

This method should implement the partial derivative with respect to the named dependency param given the current value of this variable. The parameters will be passed as keyword arguments.

Parameters:

param (str) – Name of the dependency.

derivative(var)#

Calculate derivative with respect to a Variable.

The derivative is evaluated using the VariableGraph. Any values of the graph that are needed will be updated.

Parameters:

var (Variable) – Variable with respect to which to take the derivative.

Returns:

The calculated derivative.

Return type:

float

property params#

Names of parameters

Type:

tuple

property value#

Value of the variable.

The variable will be evaluated using the VariableGraph if it needs to be recomputed.

Type:

float