Courses: The GoldSim Contaminant Transport Module:

Unit 2 - Using Arrays in GoldSim

Lesson 5 – Referencing Vectors and Manipulating Vectors Using Mathematical Operators

Now that we have seen how to create vectors, let’s see what we can do with them.

If it is not already open, open the model we built in the previous Lesson containing two vectors (you should have saved that model and named it Vectors.gsm).

Let’s start by learning how to reference a single item of one of the vectors we created. Create an Expression element, name it X, and give it units of kg.  Then define it using the Equation “2*Crop[Oranges]”, as shown below:

What we can see here is that we can reference a single item of an array using the syntax OutputName[ItemName]. In this example, the output of the element X is a scalar (with units of kg) that is equal to 14g (twice the value of the “Orange” item of the Crop element).  That is, Crop[Orange] is a scalar value (it is a single item of the vector).

Now let’s change the Equation to “2*Crop”:

Note that the input field turns red, indicating an error.  The problem is that the element is defined as a scalar, but the Equation produces a vector output.  If you press the Type… button and define the element as a vector of Fruit, you will see that the element now becomes valid:

In this example, the result was a new vector that in which each item of the original vector was multiplied by two.

This illustrates that standard operators can be used to manipulate vectors. The rules for the most common operations are summarized below:

Multiplication or Division of a Vector by a Scalar. As seen in the example we just did, the multiplication (*) and division (/) operators can be used between vectors and a scalar. Note that not only can you divide a vector by a scalar, but you can also divide a scalar by a vector. In both cases, the result is a vector.

Addition and Subtraction of Vectors. The addition (+) and subtraction (-) operators can be used between vectors if and only if the two vectors are defined using the same set of Array Labels. GoldSim carries out the operation term-by-term and produces a vector with the same Array Label set. To see this, let’s edit the Equation for X and subtract one of the two vectors we created in the previous lesson (Crop2) from the other (Crop):

Recall that Crop was defined a [5kg, 7kg, 10kg] and Crops2 as [7 kg, 1 kg, 2kg].

Note that you cannot add or subtract a vector to/from a scalar (this would result in an error). If you wanted to subtract 1 kg from each item of Crop, you could do so by using the expression “Crop – vector(1 kg)”.

Multiplication and Division of Vectors. The multiplication (*) and division (/) operators can be used between vectors if and only if the two vectors are defined using the same set of Array Labels. GoldSim carries out the operation term-by-term and produces a vector with the same set of Array Labels as the original vectors. To see this, let’s edit the Equation for X and multiply Crop by Crop2:

When you first do this, you will likely get an error.  This is because, in order to carry out this operation, it is necessary to change the Display Units to kg2, since multiplying units of mass by units of mass results in units of mass^2.

Using Relational Operators with Vectors.  Relational operators (e.g., >, <, =, ==, >=) can be used between vectors if and only if the two vectors are defined using the same set of Array Labels. GoldSim carries out the operation term-by-term. To see this, let’s edit the Equation for X as follows:

Note that in to carry out this operation, it is necessary to press the Type… button and change the type to Condition instead of Value, since the output of a conditional expression is a Condition (not a Value).

Save this model to the “MyModels” subfolder in the “Contaminant Transport Course” folder on your Desktop again (by pressing File|Save from the main menu, Ctrl+S, or the Save button in the toolbar). We will experiment with it a bit more in the next Lesson.