Manipulating Arrays in Expressions Using Mathematical Operators

The manner in which the standard operators in GoldSim can be used with arrays is summarized below:

Addition and Subtraction of Arrays. The addition (+) and subtraction (-) operators can be used between arrays if and only if the two arrays have the same dimensions and order, and are defined using the same set of array labels. GoldSim carries out the operation term-by-term and produces an array of the same order. For example, if A was a vector based on the set “Days”, and B was a vector based on the set “Days”,  C=A + B would also produce a vector based on the set “Days”. The first item of C would be the sum of the first item of A and the first item of B; the second item of C would be the sum of the second item of A and the second item of B, and so on. You cannot add or subtract an array to/from a scalar.

Multiplication or Division of an Array by a Scalar. The multiplication (*) and division (/) operators can be used between arrays and a scalar. Each item of the array is multiplied or divided by the scalar. For example, you could create the expression  2 * A, where A was a vector. The output of the Expression would be a vector, identical to the A vector (and its output attributes would need to be defined accordingly), except each item of the vector would be two times as great. Note that not only can you divide an array by a scalar, but you can also divide a scalar by an array. In both cases, the result is an array.

Multiplication and Division of Arrays of Same Order. When the multiplication (*) and division (/) operators are used between arrays that have the same order (and are defined using the same set of array labels), GoldSim carries out the operation term-by-term and produces an array of the same order as the original arrays. For example, if A was a vector based on the set “Days”, and B was a vector based on the set “Days”,  C=A * B would also produce a vector based on the set “Days”. The first item of C would be the product of the first item of A and the first item of B, etc.

Multiplying and/or Dividing the Rows or Columns of a 2-D Array (Matrix) by a 1-D Array (Vector). The following operations are supported in GoldSim for manipulating matrices: Matrix*Vector, Vector*Matrix, and Matrix/Vector. In these cases, the array label set of the Vector must match the array label set of either the rows or columns for the Matrix. If it matches that of the rows, then each row of the matrix is multiplied (or divided) by the corresponding term in the Vector. If it matches that of the columns, then each column of the matrix is multiplied (or divided) by the corresponding term in the Vector. If it matches both, the operation is carried out on the rows.  In either case, the result is a Matrix of the same order as the original matrix.

   Note: The operation described here is quite different from the linear algebra operation of multiplying a Vector by a Matrix (or vice versa), with the result being a Vector.  That particular operation can be carried out using the specialized array function “Mult”.

Raising an Array to a Power. You can use the exponentiation operator (** or ^) to raise the items of an array to a power. GoldSim carries out the operation term-by-term and produces an array of the same order. For example, you could create an expression  named C defined as A^2, where A was a vector. C must have the same order and set of array labels as A. The first item of C would be the first item of A squared, the second item of C would be the first item of A squared, etc.

Using Relational Operators with Arrays.  Relational operators (e.g., >, <, =, ==, >=) can be used between arrays if and only if the two arrays have the same dimensions and order, and are defined using the same set of array labels. GoldSim carries out the operation term-by-term and produces an array of conditions. For example, if A was a vector based on the set “Days”, and B was a vector based on the set “Days”, the Expression C defined as A > B would produce a vector based on the set “Days”. The first item of C would be the outcome (true or false) of the expression “first item of A” > “first item of B”;  the second item of C would be the outcome (true or false) of the expression “second item of A” > “second item of B”, and so on.

Relational operators can also be used between arrays and scalars.  For example, if A was a vector based on the set “Days”, and W was a scalar, the Expression D defined as A > W would produce a vector based on the set “Days”. The first item of D would be the outcome (true or false) of the expression “first item of A” > ”scalar value W”;  the second item of D would be the outcome (true or false) of the expression “second item of A” >”scalar value W”, and so on.

Using Arrays in If Statements. If statements can use mixtures of arrays and scalars.  The rules for how such statements are interpreted are as follows:

1.  The first argument (the condition) can be an array or scalar. If it is an array, then the second and third argument can either be arrays with the same set of array labels as the condition or scalars. Scalars are treated as arrays of identical values with the same set of array labels as the condition.  If the first argument is an array, GoldSim does an item by item evaluation to construct the output array.

2.  If the condition is scalar, and the second and third arguments are arrays, they must be of the same order, and the output has the same order as these arguments. Either one array or the other is output in its entirety as the result.

3.  If the condition is a scalar, one of the latter two arguments can be a scalar, and one can be an array.  The output of the If statement is then an array, and the scalar is treated as an array of identical values. Either one array or the other is output in its entirety as the result.

Related Topics…

Learn more about: