//TODO: get this near functiontest. This is not a general function. public static IFunction CreateSimpleFunction(IFunctionStore store) { var function = new Function("test"); store.Functions.Add(function); // initialize schema IVariable x = new Variable<double>("x", 3); IVariable y = new Variable<double>("y", 2); IVariable f1 = new Variable<double>("f1"); function.Arguments.Add(x); function.Arguments.Add(y); function.Components.Add(f1); // write some data var xValues = new double[] {0, 1, 2}; var yValues = new double[] {0, 1}; var fValues = new double[] {100, 101, 102, 103, 104, 105}; function.SetValues(fValues, new VariableValueFilter<double>(x, xValues), new VariableValueFilter<double>(y, yValues), new ComponentFilter(f1)); return function; }
public void InterpolateLinear1D() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); var function = new Function(); function.Arguments.Add(x); function.Components.Add(y); var xValues = new[] {1.0, 2.0, 3.0}; var yValues = new[] {100.0, 200.0, 300.0}; function.SetValues(yValues, new VariableValueFilter<double>(x, xValues), new ComponentFilter(y)); var value = function.Evaluate<double>(new VariableValueFilter<double>(x, 1.5)); Assert.AreEqual(150.0, value); value = function.Evaluate<double>(new VariableValueFilter<double>(x, 2.5)); Assert.AreEqual(250.0, value); value = function.Evaluate<double>(new VariableValueFilter<double>(x, 1.75)); Assert.AreEqual(175, value); }
public void InterpolateConstant1D() { //defines a piece-wise-constant function. Value is always equals to the value of the neareast smaller //argument IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); Function function = new Function(); function.Arguments.Add(x); function.Components.Add(y); var xValues = new[] { 1.0, 2.0, 3.0 }; var yValues = new[] { 100.0, 200.0, 300.0 }; function.SetValues(yValues, new VariableValueFilter<double>(x, xValues), new ComponentFilter(y)); x.InterpolationType = ApproximationType.Constant; var value = function.Evaluate<double>(new VariableValueFilter<double>(x, 1.5)); Assert.AreEqual(100.0, value); value = function.Evaluate<double>(new VariableValueFilter<double>(x, 2.5)); Assert.AreEqual(200.0, value); }
public void Clear() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction f = new Function(); f.Arguments.Add(x); f.Components.Add(y); f.SetValues(new[] {100.0, 200.0, 300.0}, new VariableValueFilter<double>(x, new[] {1.0, 2.0, 3.0})); IList values = f.GetValues(); Assert.AreEqual(3, values.Count); f.Clear(); Assert.AreEqual(0, values.Count); }
public void SetValuesNonExistingFilter() { IVariable<float> f = new Variable<float>("f"); IVariable<float> x = new Variable<float>("x1"); IFunction function = new Function("Fail Test"); function.Components.Add(f); function.Arguments.Add(x); function.SetValues(new float[] {100}, new VariableValueFilter<float>(x, new[] {0.0f})); }
public void AssignFloatValuesToIntVariable() { IVariable f = new Variable<int>("f"); IVariable x = new Variable<float>("x1"); IFunction function = new Function("Fail Test"); function.Components.Add(f); function.Arguments.Add(x); x.Values.Add(0.0f); function.SetValues(new[] {1.7}, // should be rounded to int new VariableValueFilter<float>(x, new[] {0.0f})); Assert.AreEqual(2, f.Values[0]); }
public void SetMultiValues() { object defaultValue = -1.0f; var x = new Variable<float>("x"); var y = new Variable<float>("y"); var z = new Variable<float>("z"); var t = new Variable<int>("t"); var vx = new Variable<float>("vx"); var vy = new Variable<float>("vy"); var vz = new Variable<float>("vz"); vx.DefaultValue = defaultValue; vy.DefaultValue = defaultValue; vz.DefaultValue = defaultValue; var function = new Function("3D Test"); function.Arguments.Add(x); function.Arguments.Add(y); function.Arguments.Add(z); function.Arguments.Add(t); function.Components.Add(vx); function.Components.Add(vy); function.Components.Add(vz); IList values; // (vx, vy, vz)(0.0, 0.0, 0.0, 0) = (0.0, 0.0, 0.0) function[0.0f, 0.0f, 0.0f, 0] = new[] {0.0f, 0.0f, 0.0f}; // 0.0 0.0 0.0 0 0.0 0.0 0.0 function.SetValues( new[] {3.0f, 4.0f, 5.0f}, new VariableValueFilter<float>(x, 1.0F), new VariableValueFilter<float>(y, 2.0F)); // 0.0 0.0 0.0 0 0.0 0.0 0.0 // 0.0 2.0 0.0 0 default default default // 1.0 0.0 0.0 0 default default default // 1.0 2.0 0.0 0 3.0 4.0 5.0 Assert.AreEqual(0.0F, vx.Values[0, 0, 0, 0]); Assert.AreEqual(0.0F, vy.Values[0, 0, 0, 0]); Assert.AreEqual(0.0F, vz.Values[0, 0, 0, 0]); Assert.AreEqual(defaultValue, vx.Values[0, 1, 0, 0]); Assert.AreEqual(defaultValue, vy.Values[0, 1, 0, 0]); Assert.AreEqual(defaultValue, vz.Values[0, 1, 0, 0]); Assert.AreEqual(defaultValue, vx.Values[1, 0, 0, 0]); Assert.AreEqual(defaultValue, vx.Values[1, 0, 0, 0]); Assert.AreEqual(defaultValue, vx.Values[1, 0, 0, 0]); Assert.AreEqual(3.0F, vx.Values[1, 1, 0, 0]); Assert.AreEqual(4.0F, vy.Values[1, 1, 0, 0]); Assert.AreEqual(5.0F, vz.Values[1, 1, 0, 0]); function.SetValues(new[] {7.0f, 8.0f, 9.0f}, new VariableValueFilter<int>(t, 1)); // 0.0 0.0 0.0 0 0.0 0.0 0.0 // 0.0 0.0 0.0 1 7.0 8.0 9.0 // 0.0 2.0 0.0 0 default default default // 0.0 2.0 0.0 1 7.0 8.0 9.0 // 1.0 0.0 0.0 0 default default default // 1.0 0.0 0.0 1 7.0 8.0 9.0 // 1.0 2.0 0.0 0 3.0 4.0 5.0 // 1.0 2.0 0.0 1 7.0 8.0 9.0 Assert.AreEqual(0.0F, vx.Values[0]); Assert.AreEqual(0.0F, vy.Values[0]); Assert.AreEqual(0.0F, vz.Values[0]); Assert.AreEqual(7.0F, vx.Values[1]); Assert.AreEqual(8.0F, vy.Values[1]); Assert.AreEqual(9.0F, vz.Values[1]); Assert.AreEqual(defaultValue, vx.Values[2]); Assert.AreEqual(defaultValue, vy.Values[2]); Assert.AreEqual(defaultValue, vz.Values[2]); Assert.AreEqual(7.0F, vx.Values[3]); Assert.AreEqual(8.0F, vy.Values[3]); Assert.AreEqual(9.0F, vz.Values[3]); Assert.AreEqual(defaultValue, vx.Values[4]); Assert.AreEqual(defaultValue, vy.Values[4]); Assert.AreEqual(defaultValue, vz.Values[4]); Assert.AreEqual(7.0F, vx.Values[5]); Assert.AreEqual(8.0F, vy.Values[5]); Assert.AreEqual(9.0F, vz.Values[5]); Assert.AreEqual(3.0F, vx.Values[6]); Assert.AreEqual(4.0F, vy.Values[6]); Assert.AreEqual(5.0F, vz.Values[6]); Assert.AreEqual(7.0F, vx.Values[7]); Assert.AreEqual(8.0F, vy.Values[7]); Assert.AreEqual(9.0F, vz.Values[7]); function.SetValues( new[] {10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f}, new VariableValueFilter<int>(t, 1)); // 0.0 0.0 0.0 0 0.0 0.0 0.0 // 0.0 0.0 0.0 1 10.0 11.0 12.0 // 0.0 2.0 0.0 0 default default default // 0.0 2.0 0.0 1 13.0 14.0 15.0 // 1.0 0.0 0.0 0 default default default // 1.0 0.0 0.0 1 16.0 17.0 18.0 // 1.0 2.0 0.0 0 3.0 4.0 5.0 // 1.0 2.0 0.0 1 19.0 20.0 21.0 Assert.AreEqual(0.0F, vx.Values[0]); Assert.AreEqual(0.0F, vy.Values[0]); Assert.AreEqual(0.0F, vz.Values[0]); Assert.AreEqual(10.0F, vx.Values[1]); Assert.AreEqual(11.0F, vy.Values[1]); Assert.AreEqual(12.0F, vz.Values[1]); Assert.AreEqual(defaultValue, vx.Values[2]); Assert.AreEqual(defaultValue, vy.Values[2]); Assert.AreEqual(defaultValue, vz.Values[2]); Assert.AreEqual(13.0F, vx.Values[3]); Assert.AreEqual(14.0F, vy.Values[3]); Assert.AreEqual(15.0F, vz.Values[3]); Assert.AreEqual(defaultValue, vx.Values[4]); Assert.AreEqual(defaultValue, vy.Values[4]); Assert.AreEqual(defaultValue, vz.Values[4]); Assert.AreEqual(16.0F, vx.Values[5]); Assert.AreEqual(17.0F, vy.Values[5]); Assert.AreEqual(18.0F, vz.Values[5]); Assert.AreEqual(3.0F, vx.Values[6]); Assert.AreEqual(4.0F, vy.Values[6]); Assert.AreEqual(5.0F, vz.Values[6]); Assert.AreEqual(19.0F, vx.Values[7]); Assert.AreEqual(20.0F, vy.Values[7]); Assert.AreEqual(21.0F, vz.Values[7]); function.SetValues(new[] {6.0f, 7.0f, 8.0f}, new VariableValueFilter<int>(t, 1), new VariableValueFilter<float>(x, 1.0f)); // 0.0 0.0 0.0 0 0.0 0.0 0.0 // 0.0 0.0 0.0 1 10.0 11.0 12.0 // 0.0 2.0 0.0 0 default default default // 0.0 2.0 0.0 1 13.0 14.0 15.0 // 1.0 0.0 0.0 0 default default default // 1.0 0.0 0.0 1 6.0 7.0 8.0 // 1.0 2.0 0.0 0 3.0 4.0 5.0 // 1.0 2.0 0.0 1 6.0 7.0 8.0 Assert.AreEqual(0.0F, vx.Values[0]); Assert.AreEqual(0.0F, vy.Values[0]); Assert.AreEqual(0.0F, vz.Values[0]); Assert.AreEqual(10.0F, vx.Values[1]); Assert.AreEqual(11.0F, vy.Values[1]); Assert.AreEqual(12.0F, vz.Values[1]); Assert.AreEqual(defaultValue, vx.Values[2]); Assert.AreEqual(defaultValue, vy.Values[2]); Assert.AreEqual(defaultValue, vz.Values[2]); Assert.AreEqual(13.0F, vx.Values[3]); Assert.AreEqual(14.0F, vy.Values[3]); Assert.AreEqual(15.0F, vz.Values[3]); Assert.AreEqual(defaultValue, vx.Values[4]); Assert.AreEqual(defaultValue, vy.Values[4]); Assert.AreEqual(defaultValue, vz.Values[4]); Assert.AreEqual(6.0F, vx.Values[5]); Assert.AreEqual(7.0F, vy.Values[5]); Assert.AreEqual(8.0F, vz.Values[5]); Assert.AreEqual(3.0F, vx.Values[6]); Assert.AreEqual(4.0F, vy.Values[6]); Assert.AreEqual(5.0F, vz.Values[6]); Assert.AreEqual(6.0F, vx.Values[7]); Assert.AreEqual(7.0F, vy.Values[7]); Assert.AreEqual(8.0F, vz.Values[7]); function.SetValues(new[] {20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f}, new VariableValueFilter<int>(t, 1), new VariableValueFilter<float>(x, 1.0f)); // 0.0 0.0 0.0 0 0.0 0.0 0.0 // 0.0 0.0 0.0 1 10.0 11.0 12.0 // 0.0 2.0 0.0 0 default default default // 0.0 2.0 0.0 1 13.0 14.0 15.0 // 1.0 0.0 0.0 0 default default default // 1.0 0.0 0.0 1 20.0 21.0 22.0 // 1.0 2.0 0.0 0 3.0 4.0 5.0 // 1.0 2.0 0.0 1 23.0 24.0 25.0 Assert.AreEqual(0.0F, vx.Values[0]); Assert.AreEqual(0.0F, vy.Values[0]); Assert.AreEqual(0.0F, vz.Values[0]); Assert.AreEqual(10.0F, vx.Values[1]); Assert.AreEqual(11.0F, vy.Values[1]); Assert.AreEqual(12.0F, vz.Values[1]); Assert.AreEqual(defaultValue, vx.Values[2]); Assert.AreEqual(defaultValue, vy.Values[2]); Assert.AreEqual(defaultValue, vz.Values[2]); Assert.AreEqual(13.0F, vx.Values[3]); Assert.AreEqual(14.0F, vy.Values[3]); Assert.AreEqual(15.0F, vz.Values[3]); Assert.AreEqual(defaultValue, vx.Values[4]); Assert.AreEqual(defaultValue, vy.Values[4]); Assert.AreEqual(defaultValue, vz.Values[4]); Assert.AreEqual(20.0F, vx.Values[5]); Assert.AreEqual(21.0F, vy.Values[5]); Assert.AreEqual(22.0F, vz.Values[5]); Assert.AreEqual(3.0F, vx.Values[6]); Assert.AreEqual(4.0F, vy.Values[6]); Assert.AreEqual(5.0F, vz.Values[6]); Assert.AreEqual(23.0F, vx.Values[7]); Assert.AreEqual(24.0F, vy.Values[7]); Assert.AreEqual(25.0F, vz.Values[7]); }
public void SetValues() { object defaultValue = default(float); var f = new Variable<float>("f"); var x1 = new Variable<float>("x1"); var x2 = new Variable<float>("x2"); var function = new Function("OneComponentTwoArguments Test"); function.Components.Add(f); function.Arguments.Add(x1); function.Arguments.Add(x2); function[0.0f, 0.0f] = 0.0f; // x1 x2 f // 0.0 0.0 0.0 Assert.AreEqual(1, x1.Values.Count); var x2values = new[] {0.1f, 0.2f}; // set multiple values function.SetValues(new[] {5.0f}, new VariableValueFilter<float>(x1, 1.0f), new VariableValueFilter<float>(x2, x2values)); // x1 x2 f // 0.0 0.0 0.0 // 1.0 0.0 default // 0.0 0.1 default // 1.0 0.1 5.0 // 0.0 0.2 default // 1.0 0.2 5.0 Assert.AreEqual(2, x1.Values.Count); Assert.AreEqual(3, x2.Values.Count); Assert.AreEqual(6, function.GetValues().Count); Assert.AreEqual(0.0F, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(defaultValue, function.GetValues( new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.0F))[0] ); Assert.AreEqual(defaultValue, function.GetValues( new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.1F))[0] ); Assert.AreEqual(5.0F, function.GetValues( new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.1F))[0] ); Assert.AreEqual(defaultValue, function.GetValues( new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.2F))[0] ); Assert.AreEqual(5.0F, function.GetValues( new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.2F))[0] ); function.SetValues(new[] { 6.0f }, new VariableValueFilter<float>(x1, 2.0f)); // x1 x2 f // 0.0 0.0 0.0 // 1.0 0.0 default // 0.0 0.1 default // 1.0 0.1 5.0 // 0.0 0.2 default // 1.0 0.2 5.0 // 2.0 0.0 6.0 // 2.0 0.1 6.0 // 2.0 0.2 6.0 Assert.AreEqual(x1.Values.Count, 3); Assert.AreEqual(x2.Values.Count, 3); Assert.AreEqual(function.GetValues().Count, 9); Assert.AreEqual(0.0F, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(defaultValue, function.GetValues(new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(defaultValue, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.1F))[0]); Assert.AreEqual(5.0F, function.GetValues(new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.1F))[0]); Assert.AreEqual(defaultValue, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.2F))[0]); Assert.AreEqual(5.0F, function.GetValues(new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.2F))[0]); Assert.AreEqual(6.0F, function.GetValues(new VariableValueFilter<float>(x1, 2.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(6.0F, function.GetValues(new VariableValueFilter<float>(x1, 2.0F), new VariableValueFilter<float>(x2, 0.1F))[0]); Assert.AreEqual(6.0F, function.GetValues(new VariableValueFilter<float>(x1, 2.0F), new VariableValueFilter<float>(x2, 0.2F))[0]); function.SetValues(new[] {3.0f}, new VariableValueFilter<float>(x1, 0.0f)); // x1 x2 f // 0.0 0.0 3.0 // 1.0 0.0 default // 0.0 0.1 3.0 // 1.0 0.1 5.0 // 0.0 0.2 3.0 // 1.0 0.2 5.0 // 2.0 0.0 6.0 // 2.0 0.1 6.0 // 2.0 0.2 6.0 Assert.AreEqual(x1.Values.Count, 3); Assert.AreEqual(x2.Values.Count, 3); Assert.AreEqual(function.GetValues().Count, 9); Assert.AreEqual(3.0F, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(defaultValue, function.GetValues(new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(3.0F, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.1F))[0]); Assert.AreEqual(5.0F, function.GetValues(new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.1F))[0]); Assert.AreEqual(3.0F, function.GetValues(new VariableValueFilter<float>(x1, 0.0F), new VariableValueFilter<float>(x2, 0.2F))[0]); Assert.AreEqual(5.0F, function.GetValues(new VariableValueFilter<float>(x1, 1.0F), new VariableValueFilter<float>(x2, 0.2F))[0]); Assert.AreEqual(6.0F, function.GetValues(new VariableValueFilter<float>(x1, 2.0F), new VariableValueFilter<float>(x2, 0.0F))[0]); Assert.AreEqual(6.0F, function.GetValues(new VariableValueFilter<float>(x1, 2.0F), new VariableValueFilter<float>(x2, 0.1F))[0]); Assert.AreEqual(6.0F, function.GetValues(new VariableValueFilter<float>(x1, 2.0F), new VariableValueFilter<float>(x2, 0.2F))[0]); }
public void SetValuesPreconditions() { var arg1 = new Variable<double>(); var comp1 = new Variable<double>(); var function = new Function(); function.Arguments.Add(arg1); function.Components.Add(comp1); arg1.SetValues(new[]{1.0, 2.0}); Assert.AreEqual(2, arg1.Values.Count); Assert.AreEqual(2, comp1.Values.Count); try { function.SetValues(new double[] { }); Assert.Fail("Should have thrown an ArgumentOutOfRangeException"); } catch (Exception e) { Assert.AreEqual(typeof(ArgumentOutOfRangeException), e.GetType(), "Expected an ArgumentOutOfRangeException to be thrown"); Assert.AreEqual("Size of 'values' argument must be greater than 0.\r\nParameter name: values", e.Message); } // Exception should not have affected specified data Assert.AreEqual(2, arg1.Values.Count); Assert.AreEqual(2, comp1.Values.Count); try { function.SetValues(new[] { 1.1, 2.2, 3.3, 4.4, 5.5 }); Assert.Fail("Should have thrown an ArgumentException"); } catch (Exception e) { Assert.AreEqual(typeof(ArgumentException), e.GetType(), "Expected an ArgumentException to be thrown"); Assert.AreEqual("Number of values to be written to dependent variable 'variable' exceeds argument values range. Got 5 values expected at most 2.", e.Message); } // Exception should not have affected specified data Assert.AreEqual(2, arg1.Values.Count); Assert.AreEqual(2, comp1.Values.Count); // TODO: If possible, add test case to cover scenario when collection of IVariableFilters results in a would return a subspace size of 0 permutations }
public void RemoveValues() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction f = new Function(); f.Arguments.Add(x); f.Components.Add(y); f.SetValues(new[] { 100.0, 200.0, 300.0 }, new VariableValueFilter<double>(x, new[] { 1.0, 2.0, 3.0 })); //update argument f.Store.RemoveFunctionValues(x); //component resizes Assert.AreEqual(0,y.Values.Count); }
public void InterpolatedLinear2D() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IVariable<double> f1 = new Variable<double>("f1"); var function = new Function("testfunction"); function.Arguments.Add(x); function.Arguments.Add(y); function.Components.Add(f1); var xValues = new[] { 1.0, 2.0, 3.0 }; var yValues = new[] { 10.0, 20.0, 30.0, 40.0 }; var fValues = new[,] { {100.0, 200.0, 300.0, 400.0}, {1000.0, 2000.0, 3000.0, 4000.0}, {10000.0, 20000.0, 30000.0, 40000.0} }; function.SetValues(fValues, new VariableValueFilter<double>(x, xValues), new VariableValueFilter<double>(y, yValues), new ComponentFilter(f1)); //no interpolation..on a defined spot Assert.AreEqual(100, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.0), (new VariableValueFilter<double>(y, 10.0)))); //interpolation among first argument Assert.AreEqual(640.0, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.6), (new VariableValueFilter<double>(y, 10.0))), 0.001); //interpolation among second argument Assert.AreEqual(260.0, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.0), (new VariableValueFilter<double>(y, 26.0))), 0.001); //interpolation among two arguments Assert.AreEqual(1664.0, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.6), (new VariableValueFilter<double>(y, 26.0))), 0.001); }
public void GetExtrapolatedNoneThrowsException() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction f1 = new Function(); f1.Arguments.Add(x); f1.Components.Add(y); // set (fx, fy) values to (100.0, 200.0) for a combination of x and y values. f1.SetValues( new[] { 100.0, 200.0, 300.0 }, new VariableValueFilter<double>(x, new[] { 1.0, 2.0, 3.0 })); IFunction f = f1; //No extrapolation x.ExtrapolationType = ExtrapolationType.None; //x0 < f.Arguments[0], extrapolation at begin is set to true var value = f.Evaluate<double>(new VariableValueFilter<double>(x, 0.5)); Assert.AreEqual(f.Components[0].Values[0], value); }
public void GetExtrapolatedValuesLinear1D() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction function = new Function(); function.Arguments.Add(x); function.Components.Add(y); // set (fx, fy) values to (100.0, 200.0) for a combination of x and y values. function.SetValues( new[] { 100.0, 200.0, 300.0 }, new VariableValueFilter<double>(x, new[] { 1.0, 2.0, 3.0 })); //Extrapolate linear x.ExtrapolationType = ApproximationType.Linear; //before the 1st argument value Assert.AreEqual(50, function.Evaluate<double>(new VariableValueFilter<double>(x, 0.5))); //after the las Assert.AreEqual(350, function.Evaluate<double>(new VariableValueFilter<double>(x, 3.5))); }
public void GetExceptionExtrapolatingValuesAtBeginAndEnd1ArgsFunction() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction f1 = new Function(); f1.Arguments.Add(x); f1.Components.Add(y); // set (fx, fy) values to (100.0, 200.0) for a combination of x and y values. f1.SetValues( new[] { 100.0, 200.0, 300.0 }, new VariableValueFilter<double>(x, new[] { 1.0, 2.0, 3.0 })); IFunction f = f1; x.ExtrapolationType = ApproximationType.None; //x0 < f.Arguments[0], extrapolation at begin is set to false var value = f.Evaluate<double>(new VariableValueFilter<double>(x, 0.5)); }
public void GetExtrapolatedValuesConstant1D() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction function = new Function(); function.Arguments.Add(x); function.Components.Add(y); // set (fx, fy) values to (100.0, 200.0) for a combination of x and y values. function.SetValues( new[] { 100.0, 200.0, 300.0 }, new VariableValueFilter<double>(x, new[] { 1.0, 2.0, 3.0 })); //No extrapolation x.ExtrapolationType = ApproximationType.Constant; //x0 < f.Arguments[0], extrapolation at begin is set to true var value = function.Evaluate<double>(new VariableValueFilter<double>(x, 0.5)); Assert.AreEqual(100, value); value = function.Evaluate<double>(new VariableValueFilter<double>(x, 3.5)); Assert.AreEqual(300, value); }
public void InterpolatedConstant2D() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IVariable<double> f1 = new Variable<double>("f1"); Function function = new Function("testfunction"); function.Arguments.Add(x); function.Arguments.Add(y); function.Components.Add(f1); x.InterpolationType = ApproximationType.Constant; y.InterpolationType = ApproximationType.Constant; var xValues = new double[] { 1.0, 2.0, 3.0 }; var yValues = new double[] { 10.0, 20.0, 30.0, 40.0 }; var fValues = new[,] { {100.0, 200.0, 300.0, 400.0}, {1000.0, 2000.0, 3000.0, 4000.0}, {10000.0, 20000.0, 30000.0, 40000.0} }; function.SetValues(fValues, new VariableValueFilter<double>(x, xValues), new VariableValueFilter<double>(y, yValues), new ComponentFilter(f1)); //interpolation among first argument Assert.AreEqual(100.0, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.6), (new VariableValueFilter<double>(y, 10.0)))); //interpolation among second argument Assert.AreEqual(200.0, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.0), (new VariableValueFilter<double>(y, 26.0)))); //interpolation among two arguments Assert.AreEqual(200.0, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.6), (new VariableValueFilter<double>(y, 26.0))), 0.001); }
public void ClearFunction_ShouldNotClearAttributes() { // setup IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IFunction f = new Function(); f.Arguments.Add(x); f.Components.Add(y); string kie = "kie"; string expected = "veljoe"; f.Attributes[kie] = expected; f.Components[0].Attributes[kie] = expected; f.SetValues(new[] { 100.0, 200.0, 300.0 }, new VariableValueFilter<double>(x, new[] { 1.0, 2.0, 3.0 })); // call f.Clear(); // check Assert.AreEqual(0, f.GetValues().Count); var funcVal = f.Attributes[kie]; Assert.AreEqual(expected,funcVal); var val = f.Components[0].Attributes[kie]; Assert.AreEqual(expected,val); }
public void GetValuesGeneric() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IVariable<double> fx = new Variable<double>("fx"); IVariable<double> fy = new Variable<double>("fy"); IFunction f = new Function(); f.Arguments.Add(x); f.Arguments.Add(y); f.Components.Add(fx); f.Components.Add(fy); // set (fx, fy) values to (100.0, 200.0) for a combination of x and y values. f.SetValues( new[] {100.0, 200.0}, new VariableValueFilter<double>(x, new[] {1.0, 2.0}), new VariableValueFilter<double>(y, new[] { 10.0, 20.0 })); IMultiDimensionalArray<double> values = fx.GetValues(); int expectedValuesCount = 4; Assert.AreEqual(expectedValuesCount, values.Count); Assert.AreEqual(expectedValuesCount, x.Values.Count*y.Values.Count); double expectedFxValue = 100.0; double expectedFyValue = 200.0; Assert.AreEqual(expectedFxValue, fx.Values[0, 0]); Assert.AreEqual(expectedFyValue, fy.Values[0, 0]); }
public void Clone() { IVariable<int> x = new Variable<int>("x") { Values = { 1, 2, 3 } }; IVariable<double> y = new Variable<double>("y"); IFunction f = new Function { Arguments = {x}, Components = {y} }; f.SetValues(new[] { 100.0, 200.0, 300.0 }); var store = (MemoryFunctionStore)f.Store; var clone = (MemoryFunctionStore)store.Clone(); // clone it! clone.Functions.Count .Should().Be.EqualTo(3); clone.Functions[0] .Should("check f").Be.OfType<Function>(); clone.Functions[1] .Should("check x").Be.OfType<Variable<int>>(); clone.Functions[2] .Should("check y").Be.OfType<Variable<double>>(); Assert.AreEqual(new [] { 100.0, 200.0, 300.0}, clone.Functions[0].GetValues()); }
public void SetFunctionValuesUsingComponentFilter() { //Setup a function with 2 components and 1 argument var f = new Function(); IVariable vX = new Variable<int>("vX"); IVariable vY = new Variable<int>("vY"); f.Arguments.Add(new Variable<int>("x")); f.Components.Add(vX); f.Components.Add(vY); int[] xValues = {1, 2, 3, 4}; int[] vXValues = {0, -20, -20, 0}; f.Arguments[0].SetValues(xValues); f.SetValues(vXValues, new ComponentFilter(vX)); Assert.AreEqual(4, f.Components[0].Values.Count); //TODO: make this syntax fly //Assert.IsTrue(new[]{0,-20,-20,0}.SequenceEqual<int>(f.Components[0].Values)); Assert.AreEqual(0, vX.Values[0]); Assert.AreEqual(-20, vX.Values[1]); Assert.AreEqual(-20, vX.Values[2]); Assert.AreEqual(0, vX.Values[3]); }
public void CopyConstructor() { IVariable<int> x = new Variable<int>("x") { Values = { 1, 2, 3 } }; IVariable<double> y = new Variable<double>("y"); IFunction f = new Function { Arguments = { x }, Components = { y } }; f.SetValues(new[] { 100.0, 200.0, 300.0 }); var store = (MemoryFunctionStore)f.Store; var copy = new MemoryFunctionStore(store); // copy copy.Functions.Count .Should().Be.EqualTo(3); copy.Functions[0] .Should("check f").Be.OfType<Function>(); copy.Functions[1] .Should("check x").Be.OfType<Variable<int>>(); copy.Functions[2] .Should("check y").Be.OfType<Variable<double>>(); var variables = copy.Functions.OfType<IVariable>(); variables.ForEach(v => v.Values.Count.Should().Be.EqualTo(0)); }
public void GetInterpolatedValues2ArgsFunctionDateTime() { IVariable<double> x = new Variable<double>("x"); IVariable t = new Variable<DateTime>("t"); IVariable<double> f1 = new Variable<double>("f1"); IFunction function = new Function("testfunction"); function.Arguments.Add(t); function.Arguments.Add(x); function.Components.Add(f1); var tValue = DateTime.Now; var tValues = new[] { tValue, tValue.AddSeconds(1), tValue.AddSeconds(2), tValue.AddSeconds(3) }; var xValues = new[] { 1.0, 2.0, 3.0 }; var fValues = new[,] { {100.0, 200.0, 300.0}, {1000.0, 2000.0, 3000.0}, {10000.0, 20000.0, 30000.0}, {100000.0, 200000.0, 300000.0} }; function.SetValues(fValues, new VariableValueFilter<DateTime>(t, tValues), new VariableValueFilter<double>(x, xValues), new ComponentFilter(f1)); // now get interpolated value on 2d function var value = function.Evaluate<double>( new VariableValueFilter<double>(x, 2.5), new VariableValueFilter<DateTime>(t, tValue.AddSeconds(1.5))); Assert.AreEqual(13750, value); var value1 = function.Evaluate<double>( new VariableValueFilter<double>(x, 1.5), new VariableValueFilter<DateTime>(t, tValue.AddSeconds(2.5))); Assert.AreEqual(82500, value1); var value2 = function.Evaluate<double>( new VariableValueFilter<double>(x, 1.6), new VariableValueFilter<DateTime>(t, tValue.AddSeconds(2.5))); Assert.AreEqual(88000, value2); }
public void AddManyFunctionValuesWithBindingListShouldBeFast() { var f = new Function { Arguments = {new Variable<double>("x")}, Components = {new Variable<double>("y")} }; var values = Enumerable.Range(0, 1000).Select(i => (double)i); // first time is slower so we add/clear values a few times first f.SetValues(values, new VariableValueFilter<double>(f.Arguments[0], values)); f.Clear(); f.SetValues(values, new VariableValueFilter<double>(f.Arguments[0], values)); f.Clear(); // measure overhead (add values to function) var stopwatch = new Stopwatch(); stopwatch.Start(); AddValuesWithoutBindingList(f, values); stopwatch.Stop(); var overhead = stopwatch.ElapsedMilliseconds; log.DebugFormat("Added 1000 values to function in {0} ms", overhead); f.Clear(); // add values with FunctionBindingList new FunctionBindingList(f); stopwatch.Reset(); stopwatch.Start(); AddValuesWithBindingList(f, values); stopwatch.Stop(); log.DebugFormat("Added 1000 values to function wrapped with a binding list in {0} ms, {1}x slower", stopwatch.ElapsedMilliseconds, (stopwatch.ElapsedMilliseconds / (double)overhead)); (stopwatch.ElapsedMilliseconds - overhead) .Should("Updating binding list should be fast").Be.LessThan(50); (stopwatch.ElapsedMilliseconds / (double)overhead) .Should("Setting values to function with function binding list should be almost as fast as without it") .Be.LessThan(4); }
public void GetExceptionExtrapolatingValues2ArgsFunction() { IVariable<double> x = new Variable<double>("x"); IVariable<double> y = new Variable<double>("y"); IVariable<double> f1 = new Variable<double>("f1"); IFunction function = new Function("testfunction"); function.Arguments.Add(x); function.Arguments.Add(y); function.Components.Add(f1); var xValues = new[] {1.0, 2.0, 3.0}; var yValues = new[] {10.0, 20.0, 30.0, 40.0}; var fValues = new[,] { {100.0, 200.0, 300.0, 400.0}, {1000.0, 2000.0, 3000.0, 4000.0}, {10000.0, 20000.0, 30000.0, 40000.0} }; function.SetValues(fValues, new VariableValueFilter<double>(x, xValues), new VariableValueFilter<double>(y, yValues), new ComponentFilter(f1)); //no interpolation..on a defined spot Assert.AreEqual(100, f1.Evaluate<double>(new VariableValueFilter<double>(x, 0.5), (new VariableValueFilter<double>(y, 10.0)))); Assert.AreEqual(100, f1.Evaluate<double>(new VariableValueFilter<double>(x, 1.5), (new VariableValueFilter<double>(y, 10.0)))); }
/// <summary> /// Use separate method for profiling /// </summary> /// <param name="f"></param> /// <param name="values"></param> private void AddValuesWithoutBindingList(Function f, IEnumerable<double> values) { f.SetValues(values, new VariableValueFilter<double>(f.Arguments[0], values)); }
public void FilterUsingComponent() { IFunction f = new Function(); IVariable c1 = new Variable<int>("c1"); IVariable c2 = new Variable<int>("c2"); IVariable x = new Variable<int>("x"); IVariable y = new Variable<int>("y"); f.Components.Add(c1); f.Components.Add(c2); f.Arguments.Add(x); f.Arguments.Add(y); f.SetValues( new[] { 100, 200 }, new VariableValueFilter<int>(x, new[] { 1, 2, 3, 4, 5 }), new VariableValueFilter<int>(y, new[] { 1, 2, 3 }) ); IFunction filtered = f.Filter(new ComponentFilter(c1)); IMultiDimensionalArray<int> values = filtered.GetValues<int>(new VariableValueFilter<int>(x, new[] { 1, 2, 3 })); Assert.IsTrue(values.Shape.SequenceEqual(new[] { 3, 3 })); Assert.AreEqual(100, values[2, 2]); }
public void BranchSegmentBoundaryAsFunctionArgument() { // Test with only 1 FeatureVariable as argument; could be usefull as initial conditions Function initialFlow = new Function("initial flow"); Variable<SimpleFeature> branchSegmentBoundaryVariable = new Variable<SimpleFeature>("cell"); branchSegmentBoundaryVariable.AutoSort = false; initialFlow.Arguments.Add(branchSegmentBoundaryVariable); initialFlow.Components.Add(new Variable<double>("depth")); // save results back to Functions initialFlow[boundary11] = 1.0; initialFlow[boundary12] = 2.0; initialFlow[boundary13] = 3.0; initialFlow[boundary23] = 4.0; initialFlow[boundary22] = 5.0; initialFlow[boundary21] = 6.0; IList<double> values = initialFlow.GetValues<double>(); Assert.AreEqual(1.0, values[0]); Assert.AreEqual(2.0, values[1]); Assert.AreEqual(3.0, values[2]); Assert.AreEqual(4.0, values[3]); Assert.AreEqual(5.0, values[4]); Assert.AreEqual(6.0, values[5]); values = initialFlow.GetValues<double>(new VariableValueFilter<SimpleFeature>(branchSegmentBoundaryVariable, boundary23)); Assert.AreEqual(4.0, values[0]); double[] initialFlowValues = { 11, 12, 13, 14, 15, 16 }; // get from model engine initialFlow.SetValues(initialFlowValues); values = initialFlow.GetValues<double>(); Assert.AreEqual(11.0, values[0]); Assert.AreEqual(12.0, values[1]); Assert.AreEqual(13.0, values[2]); Assert.AreEqual(14.0, values[3]); Assert.AreEqual(15.0, values[4]); Assert.AreEqual(16.0, values[5]); List<SimpleFeature> branchSegmentBoundaries2 = new List<SimpleFeature>(); branchSegmentBoundaries2.Add(boundary21); branchSegmentBoundaries2.Add(boundary22); branchSegmentBoundaries2.Add(boundary23); values = initialFlow.GetValues<double>(new VariableValueFilter<SimpleFeature>(branchSegmentBoundaryVariable, branchSegmentBoundaries2)); Assert.AreEqual(3, values.Count); Assert.AreEqual(14.0, values[0]); Assert.AreEqual(15.0, values[1]); Assert.AreEqual(16.0, values[2]); }