FunctionFilters are used in the functions library to specify which data you want to set or get. For example you can use filters to get a subset of a function. FunctionFilters can be quite complex and usually there is an alternative syntax to get the same result easier.

VariableValueFilter

For example if we have a grid f with 2 int arguments and one string component like this:

x\y

0

1

2

0

a

b

c

1

d

e

f

2

g

h

i

We can define a filtered function of this like this

f2 = f.Filter(new VariableValueFilter<int>(x,new[]{1,2}),new VariableValueFilter<int>(y,new[]{1,2}))

f2 look like this

x2\y2

1

2

1

e

f

2

h

i

VariableValueFilters can also be used to set values of arguments when setting values for components.

            var x = new Variable<int>("x");
            var y = new Variable<int>("y");
            var f = new Function{Components = {y},Arguments = {x}};

            y.SetValues(new[] { 1 }, new VariableValueFilter<int>(x, 0));

            //this is equivalent of the previous call
            f[0] = 1;

VariableIndexRangeFilters

VariableIndexRangeFilters work similar to VariableValueFilters but they used the indices in the argument variable values to make a selection.

For example the following grid

x\y

10

20

30

0

-

-

-

1

-

-

-

2

-

-

-

Can be generated like this

            var flow = new Variable<int>();
            var x = new Variable<int>();
            var y = new Variable<int>();
            flow.Arguments.Add(x);
            flow.Arguments.Add(y);

            x.AddValues(new[] {1, 2, 3});
            y.AddValues(new[] {10, 20, 30});

Now we can set the bottom row like this

            //we now have 3x3 array for flow..write the last 'slice'
            var xIndex = new VariableIndexRangeFilter(x, 2);
            var yIndex = new VariableIndexRangeFilter(y, 0, 2);
            flow.SetValues(new[] {1, 2, 3}, new[] {xIndex, yIndex});

And we get the following grid

x\y

10

20

30

0

-

-

-

1

-

-

-

2

1

2

3

VariableIndexRangesFilters VariableIndexRangesFilters
They are the same as VariableIndexRangeFilters except that now multiple ranges can be defined in one filter.They are the same as VariableIndexRangeFilters except that now multiple ranges can be defined in one filter.

VariableReduceFilter

Can be used to reduce function in dimensionality. For example a two dimension (two-argument) function can be reduced to a one dimensional (one-argument) function. This is only possible if the dimension that is being reduced has a single value.

    IVariable<int> x = new Variable<int>("x");
    IVariable<int> y = new Variable<int>("y");
    IVariable<int> fx = new Variable<int>("fx");
    fx.Arguments.Add(x);
    fx.Arguments.Add(y);
            
    x.SetValues(new[] {1, 2, 3});
    y.SetValues(new[] {1,2});
    fx[3, 1] = 20;
            
    var reducedFunction = fx.Filter(new VariableValueFilter<int>(y, 1), new VariableReduceFilter(y));
    //the function 'lost' an argument
    Assert.AreEqual(1, reducedFunction.Arguments.Count);
    Assert.AreEqual(3, reducedFunction.Values.Count);
    Assert.AreEqual(20, reducedFunction.Values[2]);

This filter is used for example to get a timeseries out of a timedependent coverage (reducing location) or to get a timeIndependent coverage out of a timedependent (reducing time)

  • No labels