Exemplo n.º 1
0
        public void Should_Return_0_25_Integral_0_To_1_From_x_Power_3()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 0, UpperBound = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Integral,
                    Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type = FunctionType.Power;
            functionPlotter.Function.Args.ElementAt(0).Args = new List <Function> {
                new Function(), new Function()
            };
            functionPlotter.Function.Args.ElementAt(0).Args.ElementAt(0).Type  = FunctionType.Variable;
            functionPlotter.Function.Args.ElementAt(0).Args.ElementAt(1).Type  = FunctionType.Constant;
            functionPlotter.Function.Args.ElementAt(0).Args.ElementAt(1).Value = 3;
            var solver = new Solver(functionPlotter);

            // When
            var result = solver.ComputeIntegral(functionPlotter.Function, 1000000);

            // Then
            var expectedResult = 0.25;

            Assert.Less(expectedResult - result, 0.001);
        }
Exemplo n.º 2
0
        public void Should_Return_0_From_0_Power_By_2()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 0, UpperBound = 0, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Power,
                    Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type  = FunctionType.Variable;
            functionPlotter.Function.Args.ElementAt(1).Type  = FunctionType.Constant;
            functionPlotter.Function.Args.ElementAt(1).Value = 1;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(0, 0)
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 3
0
        public void Should_Invalidate_If_Step_Is_0()
        {
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 6, Step = 0
                },
                Function = new Function
                {
                    Type = FunctionType.Power,
                    Args = new List <Function>
                    {
                        new Function
                        {
                            Type  = FunctionType.Constant,
                            Value = 2
                        },
                        new Function
                        {
                            Type = FunctionType.Variable
                        }
                    }
                }
            };

            var validationResult = new FunctionPlotterValidator().Validate(functionPlotter);

            Assert.IsFalse(validationResult.IsValid);
        }
Exemplo n.º 4
0
        public void Should_Invalidate_If_Integral_Function_Has_No_Range()
        {
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 6, Step = 0
                },
                Function = new Function
                {
                    Type = FunctionType.Integral,
                    Args = new List <Function>
                    {
                        new Function
                        {
                            Type  = FunctionType.Constant,
                            Value = 2
                        },
                        new Function
                        {
                            Type = FunctionType.Variable
                        }
                    }
                }
            };

            var validationResult = _validator.Validate(functionPlotter);

            Assert.IsFalse(validationResult.IsValid);
        }
Exemplo n.º 5
0
        public void Should_Return_Array_From_0_4_To_0_4_Variable()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 0, UpperBound = 4, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Variable,
                }
            };
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(0, 0),
                new Pair(1, 1),
                new Pair(2, 2),
                new Pair(3, 3),
                new Pair(4, 4),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 6
0
        public void Should_Validate_If_Function_Plotter_Is_Correct()
        {
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 6, Step = 0.1
                },
                Function = new Function
                {
                    Type = FunctionType.Addition,
                    Args = new List <Function>
                    {
                        new Function
                        {
                            Type  = FunctionType.Constant,
                            Value = 2
                        },
                        new Function
                        {
                            Type = FunctionType.Variable
                        }
                    }
                }
            };

            var validationResult = _validator.Validate(functionPlotter);

            Assert.IsTrue(validationResult.IsValid);
        }
Exemplo n.º 7
0
        public void Should_Return_Array_Of_7_Constant()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 0, UpperBound = 4, Step = 1
                },
                Function = new Function
                {
                    Type  = FunctionType.Constant,
                    Value = 7
                }
            };
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(0, 7),
                new Pair(1, 7),
                new Pair(2, 7),
                new Pair(3, 7),
                new Pair(4, 7),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 8
0
        public IActionResult Post(FunctionPlotter functionPlotter)
        {
            var validationResult = _validator.Validate(functionPlotter);

            if (!validationResult.IsValid)
            {
                string message = string.Empty;

                foreach (var error in validationResult.Errors)
                {
                    message += $"{error}/n";
                }

                return(BadRequest(message));
            }

            var solver = new Solver(functionPlotter);
            var result = solver.Solve();

            if (!string.IsNullOrEmpty(solver.Error))
            {
                return(BadRequest(solver.Error));
            }

            return(Ok(result));
        }
Exemplo n.º 9
0
    void Start()
    {
        plotter = new AreaPlotter(handler.integrator, gameObject, handler.lineMesh, axisMaterial,
                                  functionMaterial, planeMaterial);

        parent = gameObject.transform.parent;
    }
Exemplo n.º 10
0
        public void Should_Return_Cosine_Of_Array_1_To_4()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 4, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Cosine,
                    Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type = FunctionType.Variable;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(1, Cos(1)),
                new Pair(2, Cos(2)),
                new Pair(3, Cos(3)),
                new Pair(4, Cos(4)),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 11
0
        private ActionResult plot(IFunctionWithDerivative f, PlotViewModel model)
        {
            List <Tuple <Color, List <Vector <double> > > > points = new List <Tuple <Color, List <Vector <double> > > >();
            double minDeriv = model.MinDerivCompMaxMagn;
            var    result   = new DenseVector(f.DimensionsCount);
            {
                result[0] = model.InitialX;
                result[1] = model.InitialY;
                var ps = new List <Vector <double> >();
                points.Add(new Tuple <Color, List <Vector <double> > >(Color.Lime, ps));
                var sdImpl = new SteepestDescentBasicOptmizer(model.BasicStep, model.MaxIters);
                sdImpl.Optimize(result, f, model.MinDerivCompMaxMagn, (i, pFunc) => ps.Add(pFunc()), CancellationToken.None);
            }
            {
                result[0] = model.InitialX;
                result[1] = model.InitialY;
                var ps = new List <Vector <double> >();
                points.Add(new Tuple <Color, List <Vector <double> > >(Color.Red, ps));
                var sdImpl = new SteepestDescentBasicOptmizer(model.MomentumStep,
                                                              model.MomentumStart, model.MomentumEnd, model.MaxIters);
                sdImpl.Optimize(result, f, model.MinDerivCompMaxMagn, (i, pFunc) => ps.Add(pFunc()), CancellationToken.None);
            }
            {
                result[0] = model.InitialX;
                result[1] = model.InitialY;
                var ps = new List <Vector <double> >();
                points.Add(new Tuple <Color, List <Vector <double> > >(Color.Blue, ps));
                var sdImpl = new SteepestDescentAdvancedOptmizer(model.MomentumStep,
                                                                 model.MomentumStart, model.MomentumEnd, model.MaxIters);
                sdImpl.Optimize(result, f, model.MinDerivCompMaxMagn, (i, pFunc) => ps.Add(pFunc()), CancellationToken.None);
            }
            {
                result[0] = model.InitialX;
                result[1] = model.InitialY;
                var ps = new List <Vector <double> >();
                points.Add(new Tuple <Color, List <Vector <double> > >(Color.Magenta, ps));
                var sdImpl = new RpropPlusOptmizer(model.BasicStep, 10, 1.2, 0.5, model.MaxIters);
                sdImpl.Optimize(result, f, model.MinDerivCompMaxMagn, (i, pFunc) => ps.Add(pFunc()), CancellationToken.None);
            }
            {
                result[0] = model.InitialX;
                result[1] = model.InitialY;
                var ps = new List <Vector <double> >();
                points.Add(new Tuple <Color, List <Vector <double> > >(Color.Cyan, ps));
                var sdImpl = new ImprovedRpropMinusOptmizer(model.BasicStep, 10, 1.2, 0.5, model.MaxIters);
                sdImpl.Optimize(result, f, model.MinDerivCompMaxMagn, (i, pFunc) => ps.Add(pFunc()), CancellationToken.None);
            }

            var fp = new FunctionPlotter();

            var img = fp.AutoContourPlot(f, points, 0.1f, 0, 1, model.Width, model.Height,
                                         model.ContoursCount, model.ScalePower);
            var ms = new MemoryStream();

            img.Save(ms, ImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);

            return(File(ms, "image/png"));
        }
Exemplo n.º 12
0
        public void Should_Invalidate_If_Object_Is_Null()
        {
            var functionPlotter = new FunctionPlotter();

            var validationResult = _validator.Validate(functionPlotter);

            Assert.IsFalse(validationResult.IsValid);
        }
Exemplo n.º 13
0
        //private void performTrainTestWithPlots(Net net, Matrix<double> inputs, int[] outputIndices,
        //		double learningRate, double regularizationLambda, int maxIters, double stepPerPixel) {
        //	var optimizer = new SteepestDescentAdvancedOptmizer(learningRate, 0.6, 0.99, 1e-4, maxIters);
        //	var trainer = new NetTrainer(net, optimizer, regularizationLambda);

        //	var cf = new NetCostFunction(net, inputs, outputIndices, regularizationLambda);
        //	trainer.InitializeCoefs(net);
        //	double costBefore = cf.Evaluate(net.Coefficients.Pack());

        //	var optimizationSteps = new List<Vector<double>>();
        //	bool result = trainer.Train(inputs, outputIndices, p => optimizationSteps.Add(p));

        //	new FunctionPlotter()
        //		.FunctionPlot(optimizationSteps.Select(pt => cf.Evaluate(pt)).ToArray(), 1024, 512, 0, 2)
        //		.Save("err.png", ImageFormat.Png);

        //	int dims = cf.DimensionsCount;
        //	var plotOrigin = net.Coefficients.Pack();
        //	for (int dx = 0; dx < dims; ++dx) {
        //		for (int dy = dx + 1; dy < dims; ++dy) {
        //			plotOptimization(cf, plotOrigin, dx, dy, stepPerPixel, optimizationSteps);
        //		}
        //	}

        //	double costAfter = cf.Evaluate(net.Coefficients.Pack());
        //	Assert.IsTrue(costAfter < costBefore);

        //	var actualOutputIndices = trainer.Predict(inputs);
        //	CollectionAssert.AreEqual(outputIndices, actualOutputIndices);

        //	Assert.IsTrue(result);
        //}

        private void plotOptimization(IFunctionWithDerivative fun, Vector <double> origin, int xDimIndex, int yDimIndex,
                                      double stepPerPixel, List <Vector <double> > points)
        {
            var plotter = new FunctionPlotter();
            var img     = plotter.ContourPlot(fun, origin, xDimIndex, yDimIndex, 512, 512, stepPerPixel, 10, 0.5,
                                              new List <Tuple <Color, List <Vector <double> > > >()
            {
                new Tuple <Color, List <Vector <double> > >(Color.DarkGreen, points)
            });

            img.Save(string.Format("x={0};y={1}.png", xDimIndex, yDimIndex), ImageFormat.Png);
        }
Exemplo n.º 14
0
        private static void RunTest_DataFunctionPlotter()
        {
            GameObject cameraGO = new GameObject("Camera", new Vector2(0, 0));
            Viewport   viewport = cameraGO.AddComponent <Viewport>();

            viewport.RenderTarget = null;
            viewport.Width        = 200;
            viewport.Height       = 200;
            cameraGO.AddComponent <GUIDebugDisplay>();

            string          filePath        = "E:\\Coding\\C#\\PhysicsCalculator\\PhysicsCalculator\\bin\\Debug\\summary.csv";
            FunctionPlotter functionPlotter = FunctionPlotBuilder.BuildFromData(filePath, cameraGO);
        }
Exemplo n.º 15
0
        public void Should_Invalidate_If_Function_Is_Null()
        {
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 2, Step = 0.2
                }
            };

            var validationResult = _validator.Validate(functionPlotter);

            Assert.IsFalse(validationResult.IsValid);
        }
Exemplo n.º 16
0
        public void Should_Invalidate_If_Range_Is_Null()
        {
            var functionPlotter = new FunctionPlotter
            {
                Function = new Function
                {
                    Type  = FunctionType.Constant,
                    Value = 3
                },
            };

            var validationResult = _validator.Validate(functionPlotter);

            Assert.IsFalse(validationResult.IsValid);
        }
Exemplo n.º 17
0
        public void Init()
        {
            _goodFunctionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 6, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Division,
                    Args = new List <Function>
                    {
                        new Function
                        {
                            Type  = FunctionType.Constant,
                            Value = 2
                        },
                        new Function
                        {
                            Type = FunctionType.Variable
                        }
                    }
                }
            };

            _badFunctionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 6, Step = -1
                },
                Function = new Function
                {
                    Type = FunctionType.Integral,
                    Args = new List <Function>
                    {
                        new Function
                        {
                            Type  = FunctionType.Constant,
                            Value = 2
                        },
                        new Function
                        {
                            Type = FunctionType.Variable
                        }
                    }
                }
            };
        }
Exemplo n.º 18
0
        public void Should_Return_Empty_For_LowerBound_Grater_Than_UpperBound()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 10, UpperBound = -1, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Variable,
                }
            };
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            Assert.IsEmpty(resultList);
        }
Exemplo n.º 19
0
        public void Should_Return_Array_From_1_5_Integral_2_4()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 10, Step = 2
                },
                Function = new Function
                {
                    Type  = FunctionType.Integral,
                    Range = new List <int> {
                        2, 4
                    },
                    Args = new List <Function> {
                        new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type = FunctionType.Variable;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(1, 1),
                new Pair(2, 2),
                new Pair(3, 3),
                new Pair(4, 4),
                new Pair(5, 5),
                new Pair(7, 7),
                new Pair(9, 9),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 20
0
        public void Should_Timeout_For_Step_0()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 0, UpperBound = 1, Step = 0
                },
                Function = new Function
                {
                    Type = FunctionType.Variable,
                }
            };
            var solver = new Solver(functionPlotter);

            // When
            var task            = Task.Run(() => solver.Solve());
            var completedInTime = task.Wait(5000);

            // Then
            Assert.AreEqual(completedInTime, false);
        }
Exemplo n.º 21
0
        public void Should_Return_Infinity_Division_By_0()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 2, UpperBound = 11, Step = 2
                },
                Function = new Function
                {
                    Type = FunctionType.Division,
                    Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type  = FunctionType.Variable;
            functionPlotter.Function.Args.ElementAt(1).Type  = FunctionType.Constant;
            functionPlotter.Function.Args.ElementAt(1).Value = 0;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(2, Double.PositiveInfinity),
                new Pair(4, Double.PositiveInfinity),
                new Pair(6, Double.PositiveInfinity),
                new Pair(8, Double.PositiveInfinity),
                new Pair(10, Double.PositiveInfinity),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 22
0
        public void Should_Return_Array_From_1_5_To_2_10_Multiply_By_2()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 1, UpperBound = 5, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Multiplication,
                    Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type  = FunctionType.Variable;
            functionPlotter.Function.Args.ElementAt(1).Type  = FunctionType.Constant;
            functionPlotter.Function.Args.ElementAt(1).Value = 2;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(1, 2),
                new Pair(2, 4),
                new Pair(3, 6),
                new Pair(4, 8),
                new Pair(5, 10),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 23
0
        public void Should_Return_Array_From_1_5_To_0dot5_2dot5_Division_By_2()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 2, UpperBound = 11, Step = 2
                },
                Function = new Function
                {
                    Type = FunctionType.Division,
                    Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type  = FunctionType.Variable;
            functionPlotter.Function.Args.ElementAt(1).Type  = FunctionType.Constant;
            functionPlotter.Function.Args.ElementAt(1).Value = 2;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(2, 1),
                new Pair(4, 2),
                new Pair(6, 3),
                new Pair(8, 4),
                new Pair(10, 5),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 24
0
        private static void RunTest_FunctionPlotter()
        {
            GameObject cameraGO = new GameObject("Camera", new Vector2(0, 0));
            Viewport   viewport = cameraGO.AddComponent <Viewport>();

            viewport.RenderTarget = null;
            viewport.Width        = 200;
            viewport.Height       = 200;
            cameraGO.AddComponent <GUIDebugDisplay>();

            //cameraGO.AddComponent<ForestFire>();
            //cameraGO.AddComponent<CellularEmpire>();
            //cameraGO.AddComponent<Wireworld>();
            FunctionPlotter functionPlotter = cameraGO.AddComponent <FunctionPlotter>();

            functionPlotter.SetBounds(-10.5f, -10.5f, 10.5f, 10.5f);
            functionPlotter.StepSize = 0.01f;
            //functionPlotter.AddFunction("F1",
            //    x => (float)(-2.0 / (1.0 + Math.Exp(-5.5 * x * x)) + 2), Color.Crimson);
            //functionPlotter.AddFunction("F2",
            //    x => (float)(1 / (1.0 + Math.Exp(-2.0 * Math.Log(999) * Math.Pow(x, 1) + Math.Log(999)))), Color.LawnGreen);
            //functionPlotter.AddFunction("F3",
            //    x => (float)(0.5 * Math.Tanh(6 * x - 3) + 0.5), Color.DodgerBlue);

            double I  = 300;
            double dm = 1;
            double g  = 9.81;
            double m0 = 220;

            double Mass(float t) => m0 - t * dm;

            functionPlotter.AddFunction("F4",
                                        x => (float)(I * dm * g / Mass(x) - g), Color.Gold);

            functionPlotter.AddFunction("F5",
                                        x => (float)(g * I * dm / (Mass(x) * g)), Color.Violet);
        }
Exemplo n.º 25
0
        public void Should_Return_Array_From_0_4_To_1_5_Addition_By_1()
        {
            // Given
            var functionPlotter = new FunctionPlotter
            {
                Range = new Range {
                    LowerBound = 0, UpperBound = 4, Step = 1
                },
                Function = new Function
                {
                    Type = FunctionType.Addition, Args = new List <Function> {
                        new Function(), new Function()
                    }
                }
            };

            functionPlotter.Function.Args.ElementAt(0).Type  = FunctionType.Variable;
            functionPlotter.Function.Args.ElementAt(1).Type  = FunctionType.Constant;
            functionPlotter.Function.Args.ElementAt(1).Value = 1;
            var solver = new Solver(functionPlotter);

            // When
            List <Pair> resultList = solver.Solve();

            // Then
            var expectedList = new List <Pair>
            {
                new Pair(0, 1),
                new Pair(1, 2),
                new Pair(2, 3),
                new Pair(3, 4),
                new Pair(4, 5),
            };

            CollectionAssert.AreEqual(expectedList, resultList);
        }
Exemplo n.º 26
0
 public Solver(FunctionPlotter functionPlotter)
 {
     _functionSolver = functionPlotter;
 }