|
<< Click to Display Table of Contents >> Math operators |
![]() ![]()
|
Gekko mathematical operators like the + in %x1+%x2 all have their equivalents in corresponding methods in these IVariables (the variables all implement the IVariable interface). For instance, + corresponds to the Add() method that all IVariables implement, so in a sense, the expression %x1+%x2 gets translated into %x1.Add(%x2).
The following will address how such operator math is handled. Timeseries and math involving timeseries are dealt with in a special way, cf. the section on timeseries "arithmetics" here.
As an example, consider this:
%x1 = 'a'; |
Here, the Add() method of the ScalarString class gets called, because it is a string that has a value added to it. The call inside ScalarString.cs looks like this:
Operators.StringVal.Add(this, (ScalarVal)x, false); |
Here, this is the ScalarString (with value 'a'), and x is the ScalarVal (with value 5). Operators.StringVal() gets called, and inside this method, the 'a' is first extracted, then 5 is converted to the string '5', and finally these are concatenated into a5. The point of this is that there is a symmetrical use of the same logic, namely this:
%x1 = 5; %x2 = 'a'; |
Instead of having to create a very similar Add() method for the ScalarVal object, when adding a ScalarString to the ScalarVal object, the same Operators.StringVal.Add() method gets called but this time like this:
return Operators.StringVal.Add((ScalarString)input, this, true); |
So again, the first argument is a ScalarString, and the second argument is a ScalarVal, but the last argument (true) tells the method to swap the result. Using the Operators class avoids some repetitive code.