Lesson 6 - Manipulating Vectors Using Built-In Functions
In the previous Lesson we showed how you can reference vectors and manipulate them using mathematical operators. In addition to using mathematical operators, most of the built-in functions in GoldSim can also operate on arrays. (GoldSim’s built-in functions were discussed in the Unit 5, Lesson 5 of the Basic GoldSim Course).
In most cases, these functions carry out the operation term-by-term and produce an array of the same order as the input argument. For example, if A was a vector based on the set “Days”, sin(A) would also produce a vector based on the set “Days”. The first item would be the sine of the first item of A, the second item would be the sine of the second item of A, and so on.
Several functions (e.g., max, min, mod) accept multiple vector arguments. In this case, the arguments must be defined using the same set of Array Labels. GoldSim carries out the operation term-by-term and produces an array of the same order. To see this, let’s open the Example model we created in the previous Lesson (Vectors.gsm). Create a new Expression (Y), give it Display Units of kg and define it as a vector of Fruit. Let’s next find the minimum value for each item of the vectors Crop and Crop2:
Recall that Crop was defined a [5kg, 7kg, 10kg] and Crops2 as [7 kg, 1 kg, 2kg].
Vectors can also be used when using the If function in GoldSim. Recall that the If function is one of the built-in functions provided by GoldSim, and it requires three arguments: If(C, X, Y). The first argument must be a condition, the second is the result if the condition is True, and the third is the result if the condition is False.
To see how this works, create a new vector (named C), define it as a vector of Fruit, and a Condition, and define it as [True, False, True]:
Now let’s edit the Y Expression and create an If function using the Crop and Crop2 vectors:
In this case, GoldSim carries out a term-by-term if, then calculation to produce the output array. That is, since the first item of C is True, the first item of the output is equal to the first item of Crop, since the second item of C is False, the second item of the output is equal to the second item of Crop2, and since the third item of C is True, the third item of the output is equal to the third item of Crop.
Note: If C was a scalar condition, the output of the If function would either be the vector Crop (if C was true) or the vector Crops (if C was false).
In addition to the standard functions, GoldSim also provides a wide variety of special array functions which allow you to manipulate arrays. These are accessible from the context menu for an input field:
These functions are described in detail in GoldSim Help.
Most are self-explanatory. For example, sumv() returns a scalar value consisting of the sum of all of the items of the vector argument, minv() returns a scalar value consisting of the minimum value of all of the items of the vector argument, and meanv() returns a scalar value consisting of the mean value of all of the items of the vector argument.
In the next Lesson, we will work on the first of two Exercises to ensure you are comfortable creating and manipulating vectors. As mentioned previously, this is critical, as a good understanding of vectors is required in order to use the Contaminant Transport Module (which utilizes them extensively).