Assignments and types

<< Click to Display Table of Contents >>

Navigation:  Appendix >

Assignments and types

Previous pageReturn to chapter overviewNext page

Assignments are of the form ... = ... ; with a = symbol, for instance = 2 * x;, %= 2 * %x;, #= a, b;, and so on. So the right-hand side (RHS) expression is assigned to the left-hand side (LHS) variable. There may also be a type indicator, like SERIES y = 2 * x;, stating that y should become a series type. There are the following type indicators:

 

series: SERIES y = ...;

val: VAL %= ...;

date: DATE %= ...;

string: STRING %= ...;

list: LIST #= ...;

map: MAP #= ...;

matrix: MATRIX #= ...;

var: = ...; or %= ...; or #= ...; or VAR y = ...; or VAR %= ...; or VAR #= ...;

 

Note that var type in the descriptions below include the common case where the type is not explicitly stated.

In the following, LHS means left-hand side

In the following, RHS means right-hand side

 

There are the following rules regarding assignments.

 

LHS name starts with % symbol (scalar).

 

Fails if there is LHS type indicator and this is not val, date, string or var type.

%= ...timeless...;. The RHS is a timeless series. If the LHS type is val or var, the LHS becomes a val with the value of the timeless series.

%= ...val...;. The RHS is a val. If the LHS type is val or var, the LHS becomes the RHS val. If the LHS type is a date, Gekko tries to convert the val into a date (for instance, 2020 into 2020a).

%= ...string...;. The RHS is a string. If the LHS type is string or var, the LHS becomes the RHS string.

%= ...date...;. The RHS is a date. If the LHS type is date or var, the LHS becomes the RHS date. If the LHS type is a date, Gekko tries to convert the val into a date (for instance, 2020 into 2020a).

%= ...1x1 matrix...;. The RHS is a 1x1 matrix. If the LHS type is val or var, the LHS becomes the RHS matrix element.

 

Left-hand side name starts with # symbol (collection).

 

Fails if there is LHS type indicator and this is not list, map or matrix.

matrix #= ...series...;. The RHS is a normal series. If the LHS type is matrix, the LHS becomes a matrix corresponding to the values inside the series. If the RHS is a timeless series, and the LHS type is matrix, the LHS becomes a matrix corresponding to the value inside the series (duplicated for each period in the current time period). If the type is not stated, for instance #= a;, Gekko will not auto-convert the series a into a list of values #y (this could create confusion in relation to naked list definitions).

#y = ...list...;. The RHS is a list. If the LHS type is list or var, the LHS becomes a list corresponding to the RHS list.

#y = ...map...;. The RHS is a map. If the LHS type is map or var, the LHS becomes a map corresponding to the RHS map.

#y = ...matrix...;. The RHS is a matrix. If the LHS type is matrix or var, the LHS becomes a matrix corresponding to the RHS matrix.

 

Left-hand side does not start with % or # symbol (series).

 

Fails if there is LHS type indicator and this is not series

= ...series...;. The RHS is a series. If the LHS type is series or var, the LHS becomes a series corresponding to the RHS series (frequencies must conform).

= ...val...;. The RHS is a val. The val is duplicated into all of the series observations.

= ...list...;. The RHS is a list. The list elements are put into the series observations (the list elements must be of val type, and if the last list element is stated with rep *, the list is expanded to fit with the time period).

= ...1x1 matrix...;. The RHS is a 1x1 matrix. The matrix element is duplicated into all of the series observations.

= ...nx1 matrix...;. The RHS is a nx1 matrix. If the size of the matrix fits the time period, the matrix elements are put into the series observations.

 

 

Note in general that when assigning variables, the left-hand side is always assigned to a copy of the right-hand side (pass by value, not pass by reference).