# CombinationFunctions¶

## Overview¶

Functions that combine multiple items with the same shape, yielding a result with a single item that has the same shape as the individual items.

All CombinationFunctions must have two attributes - multiplicative_param and additive_param - each of which is assigned the name of one of the function’s parameters; this is for use by ModulatoryProjections (and, in particular, GatingProjections, when the CombinationFunction is used as the function of an InputPort or OutputPort).

class psyneulink.core.components.functions.combinationfunctions.Concatenate(default_variable=None, scale=None, offset=None, params=None, owner=None, prefs=None)

Concatenates items in outer dimension (axis 0) of of variable into a single array, optionally scaling and/or adding an offset to the result after concatenating.

function returns a 1d array with lenght equal to the sum of the lengths of the items in variable.

Parameters: default_variable (list or np.array : default class_defaults.variable) – specifies a template for the value to be transformed and its default value; all entries must be numeric. scale (float) – specifies a value by which to multiply each element of the output of function (see scale for details) offset (float) – specifies a value to add to each element of the output of function (see offset for details) params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. owner (Component) – component to which to assign the Function. name (str : default see name) – specifies the name of the Function. prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the PreferenceSet for the Function (see prefs for details).
default_variable

list or np.array – contains template of array(s) to be concatenated.

scale

float – value is applied multiplicatively to each element of the concatenated, before applying the offset (if it is specified).

offset

float – value is added to each element of the concatentated array, after scale has been applied (if it is specified).

owner

Componentcomponent to which the Function has been assigned.

name

str – the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).

prefs

PreferenceSet or specification dict : Function.classPreferences – the PreferenceSet for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned using classPreferences defined in __init__.py (see Preferences for details).

_function(variable=None, context=None, params=None)

Use numpy hstack to concatenate items in outer dimension (axis 0) of variable.

Parameters: variable (list or np.array : default class_defaults.variable) – a list or np.array of numeric values. params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. Concatenated array of items in variable – in an array that is one dimension less than variable. array
_validate_params(request_set, target_set=None, context=None)

Validate scale and offset parameters

Check that SCALE and OFFSET are scalars.

_validate_variable(variable, context=None)

Insure that list or array is 1d and that all elements are numeric

Parameters: variable – context –
class psyneulink.core.components.functions.combinationfunctions.Rearrange(default_variable=None, scale=None, offset=None, arrangement=None, params=None, owner=None, prefs=None)

Rearranges items in outer dimension (axis 0) of variable, as specified by arrangement, optionally scaling and/or adding an offset to the result after concatenating.

The arrangement argument specifies how to rearrange the items of variable, possibly concatenating subsets of them into single 1d arrays. The specification must be an integer, a tuple of integers, or a list containing either or both. Each integer must be an index of an item in the outer dimension (axis 0) of variable. Items referenced in a tuple are concatenated in the order specified into a single 1d array, and that 1d array is included in the resulting 2d array in the order it appears in arrangement. If arrangement is specified, then only the items of variable referenced in the specification are included in the result; if arrangement is not specified, all of the items of variable are concatenated into a single 1d array (i.e., it functions identically to Concatenate).

function returns a 2d array with the items of variable rearranged (and possibly concatenated) as specified by arrangement.

Examples

>>> r = Rearrange(arrangement=[(1,2),(0)])
>>> print(r(np.array([[0,0],[1,1],[2,2]])))
[array([1., 1., 2., 2.]) array([0., 0.])]

>>> r = Rearrange()
>>> print(r(np.array([[0,0],[1,1],[2,2]])))
[0. 0. 1. 1. 2. 2.]

Parameters: default_variable (list or np.array : default class_defaults.variable) – specifies a template for the value to be transformed and its default value; all entries must be numeric. arrangement (int, tuple, or list : default None) – specifies ordering of items in variable and/or ones to concatenate. (see above for details). scale (float) – specifies a value by which to multiply each element of the output of function (see scale for details). offset (float) – specifies a value to add to each element of the output of function (see offset for details). params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. owner (Component) – component to which to assign the Function. name (str : default see name) – specifies the name of the Function. prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the PreferenceSet for the Function (see prefs for details).
default_variable

list or np.array – contains template of array(s) to be concatenated.

arrangement

list of one or more tuples – determines ordering of items in variable and/or ones to concatenate (see above for additional details).

scale

float – value is applied multiplicatively to each element of the concatenated, before applying the offset (if it is specified).

offset

float – value is added to each element of the concatentated array, after scale has been applied (if it is specified).

owner

Componentcomponent to which the Function has been assigned.

name

str – the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).

prefs

PreferenceSet or specification dict : Function.classPreferences – the PreferenceSet for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned using classPreferences defined in __init__.py (see Preferences for details).

_function(variable=None, context=None, params=None)

Rearrange items in outer dimension (axis 0) of variable according to arrangement.

Parameters: variable (list or np.array : default class_defaults.variable) – a list or np.array of numeric values. params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. Rearranged items of outer dimension (axis 0) of **variable** – in a 2d array. array
_instantiate_attributes_before_function(function=None, context=None)

Insure all items of arrangement are tuples and compatibility with default_variable

If arrangement is specified, convert all items to tuples If default_variable is NOT specified, assign with length in outer dimension = max index in arragnement If default_variable IS _user_specified, compatiblility with arrangement is checked in _validate_params

_validate_params(request_set, target_set=None, context=None)

Validate arrangement, scale and offset parameters

_validate_variable(variable, context=None)

Insure that all elements are numeric and that list or array is at least 2d

class psyneulink.core.components.functions.combinationfunctions.Reduce(weights=None, exponents=None, default_variable=None, operation=None, scale=None, offset=None, params=None, owner=None, prefs=None)

Combines values in each of one or more arrays into a single value for each array, with optional weighting and/or exponentiation of each item within an array prior to combining, and scaling and/or offset of result after combining.

function returns an array of scalar values, one for each array in variable.

Parameters: default_variable (list or np.array : default class_defaults.variable) – specifies a template for the value to be transformed and its default value; all entries must be numeric. weights (1d or 2d np.array : default None) – specifies values used to multiply the elements of each array in variable. If it is 1d, its length must equal the number of items in variable; if it is 2d, the length of each item must be the same as those in variable, and there must be the same number of items as there are in variable (see weights for details) exponents (1d or 2d np.array : default None) – specifies values used to exponentiate the elements of each array in variable. If it is 1d, its length must equal the number of items in variable; if it is 2d, the length of each item must be the same as those in variable, and there must be the same number of items as there are in variable (see exponents for details) operation (SUM or PRODUCT : default SUM) – specifies whether to sum or multiply the elements in variable of function. scale (float) – specifies a value by which to multiply each element of the output of function (see scale for details) offset (float) – specifies a value to add to each element of the output of function (see offset for details) params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. owner (Component) – component to which to assign the Function. name (str : default see name) – specifies the name of the Function. prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the PreferenceSet for the Function (see prefs for details).
default_variable

list or np.array – contains array(s) to be reduced.

operation

SUM or PRODUCT – determines whether elements of each array in variable of function are summmed or multiplied.

scale

float – value is applied multiplicatively to each element of the array after applying the operation (see scale for details); this done before applying the offset (if it is specified).

offset

float – value is added to each element of the array after applying the operation and scale (if it is specified).

owner

Componentcomponent to which the Function has been assigned.

name

str – the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).

prefs

PreferenceSet or specification dict : Function.classPreferences – the PreferenceSet for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned using classPreferences defined in __init__.py (see Preferences for details).

_function(variable=None, context=None, params=None)
Parameters: variable (list or np.array : default class_defaults.variable) – a list or np.array of numeric values. params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. Sum or product of arrays in variable – in an array that is one dimension less than variable. array
_validate_params(request_set, target_set=None, context=None)

Validate weghts, exponents, scale and offset parameters

Check that WEIGHTS and EXPONENTS are lists or np.arrays of numbers with length equal to variable. Check that SCALE and OFFSET are scalars.

Note: the checks of compatibility with variable are only performed for validation calls during execution
(i.e., from check_args(), since during initialization or COMMAND_LINE assignment, a parameter may be re-assigned before variable assigned during is known
_validate_variable(variable, context=None)

Insure that list or array is 1d and that all elements are numeric

Parameters: variable – context –
class psyneulink.core.components.functions.combinationfunctions.LinearCombination(default_variable=None, weights=None, exponents=None, operation=None, scale=None, offset=None, params=None, owner=None, prefs=None)

Linearly combine arrays of values, optionally weighting and/or exponentiating each array prior to combining, and scaling and/or offsetting after combining.

function combines the arrays in the outermost dimension (axis 0) of variable either additively or multiplicatively (as specified by operation), applying weights and/or exponents (if specified) to each array prior to combining them, and applying scale and/or offeset (if specified) to the result after combining, and returns an array of the same length as the operand arrays.

Parameters: variable (1d or 2d np.array : default class_defaults.variable) – specifies a template for the arrays to be combined. If it is 2d, all items must have the same length. weights (scalar or 1d or 2d np.array : default None) – specifies values used to multiply the elements of each array in variable. If it is 1d, its length must equal the number of items in variable; if it is 2d, the length of each item must be the same as those in variable, and there must be the same number of items as there are in variable (see weights for details of how weights are applied). exponents (scalar or 1d or 2d np.array : default None) – specifies values used to exponentiate the elements of each array in variable. If it is 1d, its length must equal the number of items in variable; if it is 2d, the length of each item must be the same as those in variable, and there must be the same number of items as there are in variable (see exponents for details of how exponents are applied). operation (SUM or PRODUCT : default SUM) – specifies whether the function takes the elementwise (Hadamarad) sum or product of the arrays in variable. scale (float or np.ndarray : default None) – specifies a value by which to multiply each element of the result of function (see scale for details) offset (float or np.ndarray : default None) – specifies a value to add to each element of the result of function (see offset for details) params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. owner (Component) – component to which to assign the Function. name (str : default see name) – specifies the name of the Function. prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the PreferenceSet for the Function (see prefs for details).
variable

1d or 2d np.array – contains the arrays to be combined by function. If it is 1d, the array is simply linearly transformed by and scale and offset. If it is 2d, the arrays (all of which must be of equal length) are weighted and/or exponentiated as specified by weights and/or exponents and then combined as specified by operation.

weights

scalar or 1d or 2d np.array – if it is a scalar, the value is used to multiply all elements of all arrays in variable; if it is a 1d array, each element is used to multiply all elements in the corresponding array of variable; if it is a 2d array, then each array is multiplied elementwise (i.e., the Hadamard Product is taken) with the corresponding array of variable. All weights are applied before any exponentiation (if it is specified).

exponents

scalar or 1d or 2d np.array – if it is a scalar, the value is used to exponentiate all elements of all arrays in variable; if it is a 1d array, each element is used to exponentiate the elements of the corresponding array of variable; if it is a 2d array, the element of each array is used to exponentiate the corresponding element of the corresponding array of variable. In either case, all exponents are applied after application of the weights (if any are specified).

operation

SUM or PRODUCT – determines whether the function takes the elementwise (Hadamard) sum or product of the arrays in variable.

scale

float or np.ndarray – value is applied multiplicatively to each element of the array after applying the operation (see scale for details); this done before applying the offset (if it is specified).

offset

float or np.ndarray – value is added to each element of the array after applying the operation and scale (if it is specified).

owner

Componentcomponent to which the Function has been assigned.

name

str – the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).

prefs

PreferenceSet or specification dict : Function.classPreferences – the PreferenceSet for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned using classPreferences defined in __init__.py (see Preferences for details).

_function(variable=None, context=None, params=None)
Parameters: variable (1d or 2d np.array : default class_defaults.variable) – a single numeric array, or multiple arrays to be combined; if it is 2d, all arrays must have the same length. params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. combined array – the result of linearly combining the arrays in variable. 1d array
_validate_params(request_set, target_set=None, context=None)

Validate weghts, exponents, scale and offset parameters

Check that WEIGHTS and EXPONENTS are lists or np.arrays of numbers with length equal to variable Check that SCALE and OFFSET are either scalars or np.arrays of numbers with length and shape equal to variable

Note: the checks of compatibility with variable are only performed for validation calls during execution
(i.e., from check_args(), since during initialization or COMMAND_LINE assignment, a parameter may be re-assigned before variable assigned during is known
_validate_variable(variable, context=None)

Insure that all items of list or np.ndarray in variable are of the same length

Parameters: variable – context –
class psyneulink.core.components.functions.combinationfunctions.CombineMeans(default_variable=None, weights=None, exponents=None, operation=None, scale=None, offset=None, params=None, owner=None, prefs=None)

Calculate and combine mean(s) for arrays of values, optionally weighting and/or exponentiating each mean prior to combining, and scaling and/or offsetting after combining.

function takes the mean of each array in the outermost dimension (axis 0) of variable, and combines them either additively or multiplicatively (as specified by operation), applying weights and/or exponents (if specified) to each mean prior to combining them, and applying scale and/or offeset (if specified) to the result after combining, and returns a scalar value.

Parameters: variable (1d or 2d np.array : default class_defaults.variable) – specifies a template for the arrays to be combined. If it is 2d, all items must have the same length. weights (1d or 2d np.array : default None) – specifies values used to multiply the elements of each array in variable. If it is 1d, its length must equal the number of items in variable; if it is 2d, the length of each item must be the same as those in variable, and there must be the same number of items as there are in variable (see weights for details) exponents (1d or 2d np.array : default None) – specifies values used to exponentiate the elements of each array in variable. If it is 1d, its length must equal the number of items in variable; if it is 2d, the length of each item must be the same as those in variable, and there must be the same number of items as there are in variable (see exponents for details) operation (SUM or PRODUCT : default SUM) – specifies whether the function takes the sum or product of the means of the arrays in variable. scale (float or np.ndarray : default None) – specifies a value by which to multiply the result of function (see scale for details) offset (float or np.ndarray : default None) – specifies a value to add to the result of function (see offset for details) params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. owner (Component) – component to which to assign the Function. name (str : default see name) – specifies the name of the Function. prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the PreferenceSet for the Function (see prefs for details).
variable

1d or 2d np.array – contains the arrays to be combined by function. If it is 1d, the array is simply linearly transformed by and scale and offset. If it is 2d, the arrays (all of which must be of equal length) are weighted and/or exponentiated as specified by weights and/or exponents and then combined as specified by operation.

weights

1d or 2d np.array : default NOne – if it is 1d, each element is used to multiply all elements in the corresponding array of variable; if it is 2d, then each array is multiplied elementwise (i.e., the Hadamard Product is taken) with the corresponding array of variable. All weights are applied before any exponentiation (if it is specified).

exponents

1d or 2d np.array : default None – if it is 1d, each element is used to exponentiate the elements of the corresponding array of variable; if it is 2d, the element of each array is used to exponentiate the corresponding element of the corresponding array of variable. In either case, exponentiating is applied after application of the weights (if any are specified).

operation

SUM or PRODUCT : default SUM – determines whether the function takes the elementwise (Hadamard) sum or product of the arrays in variable.

scale

float or np.ndarray : default None – value is applied multiplicatively to each element of the array after applying the operation (see scale for details); this done before applying the offset (if it is specified).

offset

float or np.ndarray : default None – value is added to each element of the array after applying the operation and scale (if it is specified).

owner

Componentcomponent to which the Function has been assigned.

name

str – the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).

prefs

PreferenceSet or specification dict : Function.classPreferences – the PreferenceSet for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned using classPreferences defined in __init__.py (see Preferences for details).

_function(variable=None, context=None, params=None)
Parameters: variable (1d or 2d np.array : default class_defaults.variable) – a single numeric array, or multiple arrays to be combined; if it is 2d, all arrays must have the same length. params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. combined means – the result of taking the means of each array in variable and combining them. number
_validate_params(request_set, target_set=None, context=None)

Validate weights, exponents, scale and offset parameters

Check that WEIGHTS and EXPONENTS are lists or np.arrays of numbers with length equal to variable Check that SCALE and OFFSET are either scalars or np.arrays of numbers with length and shape equal to variable

Note: the checks of compatibility with variable are only performed for validation calls during execution
(i.e., from check_args(), since during initialization or COMMAND_LINE assignment, a parameter may be re-assigned before variable assigned during is known
_validate_variable(variable, context=None)

Insure that all items of variable are numeric

class psyneulink.core.components.functions.combinationfunctions.PredictionErrorDeltaFunction(default_variable=None, gamma=None, params=None, owner=None, prefs=None)

Calculate temporal difference prediction error.

function returns the prediction error using arrays in variable:

$\delta(t) = r(t) + \gamma sample(t) - sample(t - 1)$
_function(variable=None, context=None, params=None)
Parameters: variable (2d np.array : default class_defaults.variable) – a 2d array representing the sample and target values to be used to calculate the temporal difference delta values. Both arrays must have the same length params (Dict[param keyword, param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. delta values 1d np.array
_validate_params(request_set, target_set=None, context=None)

Checks that WEIGHTS is a list or np.array of numbers with length equal to variable.

Note: the checks of compatibility with variable are only performed for validation calls during execution (i.e. from check_args()), since during initialization or COMMAND_LINE assignment, a parameter may be re-assigned before variable assigned during is known

Parameters: request_set – target_set – context – None
_validate_variable(variable, context=None)

Insure that all items of variable are numeric

Parameters: variable – context – variable if all items are numeric