The functions library has a lot of functionality to get values from functions. It includes filtering, 'slicing' of data and data aggregation. The exact 'query' is specified using various filter which allow you to get the data you need. I will show a few examples of independent variables first
Getting values from independent variables
We've set-up a variables x like this:
var x = new Variable<int>(); x.SetValues(new[]{1,2,3,4});
Now we can get all the values like this:
var values = x.GetValues(); //get all values var values2 = x.Values(); //this is exactly the same a first line
Or we can do some filtering based on index
var values = x.GetValues(new VariableIndexRangeFilter(x, 1, 2)); //get the values from index 1 up and including 2 Assert.AreEqual(new[]{2,3},values); //so that will be 2,3
Getting values from dependent variables (1-D)
Getting values from dependent variables is where the functions library really 'comes to live'. We can get values based on argument for example. We set a 1D function like this
var y = new Variable<int>(); x = new Variable<int>(); y.Arguments.Add(x); x.SetValues(new[]{1,2,3,4}); y.SetValues(new[]{100,200,300,400});
Getting a value for y based on x can be done like this
var yValues = y.GetValues(new VariableValueFilter<int>(x, new[] { 1 })); //get value for y based on value of x Assert.AreEqual(new[] { 100 }, yValues); yValues = y.GetValues(new VariableValueFilter<int>(x, new[] { 1,2 })); //get value for y based on value of x Assert.AreEqual(new[] { 100,200 }, yValues);
Or use the indexer to get a single value based on argument values
Assert.AreEqual(100,y[1]); //this will work
Apart from VariableValueFilters one can also use index filters, or index range filters. The filters are explained on the filters page.
Getting values from dependent variables (2-D)
We can also specify 2D,3D or actually n-D functions using functions and variables. For example a regular grid F(x,y) or a timedependent network coverage F(networklocation,t) (where a networklocation is a branch,offset combination) are 2D functions.
Setting up a grid Fxy with x and y argument:
var Fxy = new Variable<int>(); var y = new Variable<int>(); var x = new Variable<int>(); Fxy.Arguments.Add(x); Fxy.Arguments.Add(y); x.SetValues(new[]{1,2,3}); y.SetValues(new[]{100,200,300}); Fxy.SetValues(new[]{1,2,3,4,5,6,7,8,9});
x\y |
100 |
200 |
300 |
---|---|---|---|
1 |
1 |
2 |
3 |
2 |
4 |
5 |
6 |
3 |
7 |
8 |
9 |
To get the first row from the grid (where x == 1) we can write :
Assert.AreEqual(new[]{1,2,3},Fxy[1]);
This is equal to
Assert.AreEqual(new[]{1,2,3},Fxy.GetValues(new VariableValueFilter<int>(x,new[]{1})));