public void Accuracy()
        {
            List <DPoint> points = new List <DPoint>();

            for (int i = -100; i < 101; i++)
            {
                double x = (double)i;
                double y = 3 * x * x - 2 * x + 1;
                points.Add(new DPoint(x, y));
            }
            Differentiator target = new Differentiator(points);

            DPoint[] diffPoints = target.Differentiate();
            //x=0, y' = -2
            Assert.AreEqual(0d, diffPoints[100].X);
            Assert.AreEqual(-2d, diffPoints[100].Y);
            //x=1, y'=4
            Assert.AreEqual(1d, diffPoints[101].X);
            Assert.AreEqual(4d, diffPoints[101].Y);
            //x=-1, y'=-8
            Assert.AreEqual(-1d, diffPoints[99].X);
            Assert.AreEqual(-8d, diffPoints[99].Y);
            //x=42, y'=250
            Assert.AreEqual(42d, diffPoints[142].X);
            Assert.AreEqual(250d, diffPoints[142].Y);
        }
        public void DifferentiateConstructorTest2()
        {
            DPoint[]       points = createPoints();
            Differentiator target = new Differentiator(points);

            Assert.AreEqual(points.Length, target.Points.Length);
        }
Esempio n. 3
0
        public void InvalidateReputationsFromFiles(IEnumerable <string> files)
        {
            lvLoadedDocuments.Items.Clear();
            lbTotalFiles.Text = TotalFormatter.FormatTotalFiles(0);
            rcGiven.Clear();
            rcReceived.Clear();
            rcDifference.Clear();

            var parsed = files.
                         Select(f => PageParser.Parse(f, PageReadType.FromFile));

            foreach (var page in parsed)
            {
                if (page.Type == TransactionType.Unknown)
                {
                    continue;
                }

                lvLoadedDocuments.Items.Add(page.ToPageViewItem());

                switch (page.Type)
                {
                case TransactionType.Given:
                    rcGiven.AddRange(page.Reputations);
                    break;

                case TransactionType.Received:
                    rcReceived.AddRange(page.Reputations);
                    break;
                }
            }

            rcDifference.AddRange(Differentiator.Differenciate(rcGiven.Reputations, rcReceived.Reputations).ToArray());
            lbTotalFiles.Text = TotalFormatter.FormatTotalFiles(lvLoadedDocuments.Items.Count);
        }
Esempio n. 4
0
        public void CalculateTurningPointsTest2()
        {
            //y = x*x*x - x*x - x +1
            //y' = 3x*x -2x -1 = (3x+1)(x-1)
            //Stationary points solutions x = -1/3 (peak) , +1 (trough)
            List <DPoint> points = new List <DPoint>();

            for (double x = -10; x < 10; x = x + 0.01)
            {
                double y = x * x * x - x * x - x + 1;
                points.Add(new DPoint(x, y));
            }
            Differentiator differentiator = new Differentiator(points);

            differentiator.SampleSize = 9;
            DPoint[]         diffPoints = differentiator.Differentiate();
            StationaryPoints target     = new StationaryPoints(points.ToArray(), diffPoints);

            target.Threshold = 0;
            TurningPoint[] actual;
            actual = target.CalculateTurningPoints();
            Assert.AreEqual(2, actual.Length);
            Assert.IsTrue(actual[0].IsPeak);
            Assert.AreEqual("-0.33", actual[0].Point.X.ToString("0.00"));
            Assert.IsFalse(actual[1].IsPeak);
            Assert.AreEqual("1.00", actual[1].Point.X.ToString("0.00"));
        }
Esempio n. 5
0
        public void TestDelVector()
        {
            var diff = new Differentiator();
            var simp = new Simplifier();
            var exp  = new Del(diff, simp, new Number(2));

            Test(exp, ResultType.Vector);
        }
Esempio n. 6
0
        public void TestDerivExpressionWithVar()
        {
            var diff = new Differentiator();
            var simp = new Simplifier();
            var exp  = new Derivative(diff, simp, Variable.X, Variable.X);

            Test(exp, ResultType.Expression);
        }
Esempio n. 7
0
        public void TestDerivNumber()
        {
            var diff = new Differentiator();
            var simp = new Simplifier();
            var exp  = new Derivative(diff, simp, Variable.X, Variable.X, new Number(2));

            Test(exp, ResultType.Number);
        }
Esempio n. 8
0
        public void TestDerivException()
        {
            var diff = new Differentiator();
            var simp = new Simplifier();
            var exp  = new Derivative(diff, simp, new IExpression[] { Variable.X, new Number(1) });

            TestException(exp);
        }
Esempio n. 9
0
        public void Deriv()
        {
            var diff     = new Differentiator();
            var simpl    = new Simplifier();
            var simp     = new Derivative(diff, simpl, new Add(new Number(2), new Number(3)));
            var expected = new Derivative(diff, simpl, new Number(5));

            SimpleTest(simp, expected);
        }
        public void AddTest1()
        {
            DPoint[]       points = createPoints();
            Differentiator target = new Differentiator(points);

            target.Add(2, -2);
            target.Add(5, 31);
            target.Add(10, 166);
            Assert.AreEqual(points.Length + 3, target.Points.Length);
        }
        public void EndPoints()
        {
            DPoint[]       points = createPoints();
            Differentiator target = new Differentiator(points);

            DPoint[] diffPoints = target.Differentiate();
            Assert.AreEqual(0, diffPoints[0].X);
            Assert.AreEqual(1, diffPoints[0].Y);
            Assert.AreEqual(999, diffPoints[999].X);
            Assert.AreEqual(-1, diffPoints[999].Y);
        }
Esempio n. 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Processor"/> class.
        /// </summary>
        public Processor()
        {
            Lexer          = new Lexer();
            Simplifier     = new Simplifier();
            Differentiator = new Differentiator();
            Parser         = new Parser(new ExpressionFactory(Differentiator, Simplifier));
            TypeAnalyzer   = new TypeAnalyzer();

            Parameters    = new ExpressionParameters(AngleMeasurement.Degree, new ParameterCollection(), new FunctionCollection());
            NumeralSystem = NumeralSystem.Decimal;
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Processor"/> class.
        /// </summary>
        public Processor()
        {
            Lexer          = new Lexer();
            Simplifier     = new Simplifier();
            Differentiator = new Differentiator();
            Parser         = new Parser(new ExpressionFactory(
                                            new DefaultDependencyResolver(new Type[] { typeof(ISimplifier), typeof(IDifferentiator) },
                                                                          new object[] { Simplifier, Differentiator })
                                            ));

            Parameters    = new ExpressionParameters(AngleMeasurement.Degree, new ParameterCollection(), new FunctionCollection());
            NumeralSystem = NumeralSystem.Decimal;
            DoSimplify    = true;
        }
        public void SampleSize()
        {
            Differentiator target = new Differentiator();

            target.SampleSize = 9;
            //y=2x+1
            target.Add(new DPoint(0, 1));
            target.Add(new DPoint(1, 3));
            target.Add(new DPoint(2, 5));
            DPoint[] diffPoints = target.Differentiate();
            Assert.AreEqual(3, diffPoints.Length);
            //expect best line fit, which has gadient of 2
            Assert.AreEqual(2, diffPoints[0].Y);
            Assert.AreEqual(2, diffPoints[1].Y);
            Assert.AreEqual(2, diffPoints[2].Y);
        }
        public void Differentiate1()
        {
            DPoint[]       points = createPoints();
            Differentiator target = new Differentiator(points);

            DPoint[] diffPoints = target.Differentiate();
            Assert.AreEqual(points.Length, diffPoints.Length);
            Assert.AreEqual(points[50].X, diffPoints[50].X);
            Assert.AreEqual(points[500].X, diffPoints[500].X);
            //gradient = 1
            Assert.AreEqual(1d, diffPoints[50].Y);
            //gradient  = -1
            Assert.AreEqual(-1d, diffPoints[150].Y);
            //turning point
            Assert.AreEqual(0d, diffPoints[100].Y);
        }
Esempio n. 16
0
        public void DoTestTransformToElementaryOperations(string functionName)
        {
            var model = GetSematicModel();

            var diffOperator = new Differentiator(model);
            var transformed  = diffOperator.TransformToElementaryOperations(functionName);

            var outputTree   = Helpers.SyntaxTreeFromFile(OutputFileName);
            var expectedTree = outputTree.GetRoot().DescendantNodes()
                               .OfType <MethodDeclarationSyntax>()
                               .Where(m => m.Identifier.Value.ToString() == ("T_" + functionName))
                               .Select(m => m.NormalizeWhitespace())
                               .Single();

            AreEqual(expectedTree.ToFullString(), transformed.ToFullString());
        }
        public void AddTest2()
        {
            Differentiator target = new Differentiator();

            //y = 2xx +3x + 4
            //y' = 4x +3
            target.Add(new DPoint(0, 4));
            target.Add(new DPoint(1, 9));
            target.Add(new DPoint(2, 18));
            DPoint[] points = target.Points;
            Assert.AreEqual(3, points.Length);
            Assert.AreEqual(0, points[0].X);
            Assert.AreEqual(4, points[0].Y);
            Assert.AreEqual(1, points[1].X);
            Assert.AreEqual(9, points[1].Y);
            Assert.AreEqual(2, points[2].X);
            Assert.AreEqual(18, points[2].Y);
        }
        public void AddTest3()
        {
            Differentiator     target = new Differentiator();
            List <Measurement> list   = new List <Measurement>();

            list.Add(new Measurement(2, -2));
            list.Add(new Measurement(5, 31));
            list.Add(new Measurement(10, 166));
            target.Add(list, "Wavelength", "Transmittance");
            DPoint[] points = target.Points;
            Assert.AreEqual(3, points.Length);
            Assert.AreEqual(2, points[0].X);
            Assert.AreEqual(-2, points[0].Y);
            Assert.AreEqual(5, points[1].X);
            Assert.AreEqual(31, points[1].Y);
            Assert.AreEqual(10, points[2].X);
            Assert.AreEqual(166, points[2].Y);
        }
Esempio n. 19
0
        public void DoTestSimpleExpressions(string functionName)
        {
            var model = GetSematicModel();

            var diffOperator = new Differentiator(model);
            var transformed  = diffOperator.Transform(functionName);

            var outputTree   = Helpers.SyntaxTreeFromFile(OutputFileName);
            var expectedTree = outputTree.GetRoot().DescendantNodes()
                               .OfType <MethodDeclarationSyntax>()
                               .Where(m => m.Identifier.Value.ToString() == ("D_" + functionName))
                               .Select(m => SyntaxFactory.SyntaxTree(m.NormalizeWhitespace()))
                               .Single();

            var comparer = new CompareLogic();
            var results  = comparer.Compare(expectedTree, transformed);

            IsTrue(results.AreEqual, results.DifferencesString);
        }
Esempio n. 20
0
        public void Test_diff_bettwen_two_inputs_differents_without_line_in_to_text()
        {
            string from = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam tristique massa sed semper lobortis.
In facilisis risus ut feugiat lacinia.
Pellentesque a dui ut nunc efficitur tempus nec sit amet metus.";

            string to = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam tristique massa sed semper loborytis.
In facilisis risus ut feugiat lacinia lacinia.";

            var differentiator = new Differentiator();
            var diffs          = differentiator.Diff(from, to);

            Assert.Equal(TypeDifference.Equal, diffs[0].Type);
            Assert.Equal(TypeDifference.Diff, diffs[1].Type);
            Assert.Equal(TypeDifference.Diff, diffs[2].Type);
            Assert.Equal(TypeDifference.Removed, diffs[3].Type);
        }
Esempio n. 21
0
        protected static void test(string oldFileName, string newFileName)
        {
            FolderName folder   = @"C:\Enterprise\Projects\Core\Core.Tests\test-data";
            var        oldFile  = folder + oldFileName;
            var        newFile  = folder + newFileName;
            var        oldLines = oldFile.Lines;
            var        newLines = newFile.Lines;

            var diff = new Differentiator(oldLines, newLines, true, false);

            if (diff.BuildModel().If(out var model, out var exception))
            {
                Console.WriteLine(model);

                var oldDifferences = model.OldDifferences();
                var newDifferences = model.NewDifferences();

                Console.WriteLine("old differences:");
                foreach (var difference in oldDifferences)
                {
                    Console.WriteLine(difference);
                }

                Console.WriteLine();

                Console.WriteLine("new differences:");
                foreach (var difference in newDifferences)
                {
                    Console.WriteLine(difference);
                }

                Console.WriteLine("merged differences:");
                foreach (var line in model.MergedDifferences())
                {
                    Console.WriteLine(line);
                }
            }
            else
            {
                Console.WriteLine($"exception: {exception.Message}");
            }
        }
Esempio n. 22
0
        static void Main(string[] args)
        {
            string input;

            do
            {
                try
                {
                    Console.Clear();
                    Console.Write("Zadajte vzorec:");
                    input = Console.ReadLine();
                    ILexer        lexer        = Setup.Instance.GetLexer();
                    ILexerContext lexerContext = Setup.Instance.CreateLexerContext(input);
                    LexerResult   lexerResult  = lexer.Evaluate(lexerContext);

                    IParser        parser        = Setup.Instance.GetParser();
                    IParserContext parserContext = Setup.Instance.CreateParserContext(lexerResult.PostfixNotation);
                    ParserResult   parserResult  = parser.Evaluate(parserContext);

                    Visitor evaluator      = new Evaluator(Setup.Instance.CreateEvaluationContext());
                    Visitor differentiator = new Differentiator(SyntaxToken.Variable("x"));

                    Console.WriteLine($"Vstup: {input}");
                    Console.WriteLine($"Výsledok: {evaluator.Visit(parserResult.Tree)}");
                    Console.WriteLine($"Derivácia: {differentiator.Visit(parserResult.Tree)}");
                }
                catch (Exception ex)
                {
                    Console.Clear();
                    Console.WriteLine("Vyskytla sa chyba!");
                    Console.WriteLine("Ohláste to prosím Maťovi.");
                    Console.WriteLine();
                    Console.WriteLine("Za pochopenie ďakujeme!");
                    Console.WriteLine("Váš kalkulátor");
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("Chybová správa");
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }while (true);
        }
 public void CalculateTurningPointsTest2()
 {
     //y = x*x*x - x*x - x +1
     //y' = 3x*x -2x -1 = (3x+1)(x-1)
     //Stationary points solutions x = -1/3 (peak) , +1 (trough)
     List<DPoint> points = new List<DPoint>();
     for (double x = -10; x < 10; x=x+0.01)
     {
         double y = x * x * x - x * x - x + 1;
         points.Add(new DPoint(x, y));
     }
     Differentiator differentiator = new Differentiator(points);
     differentiator.SampleSize = 9;
     DPoint[] diffPoints = differentiator.Differentiate();
     StationaryPoints target = new StationaryPoints(points.ToArray(), diffPoints);
     target.Threshold = 0;
     TurningPoint[] actual;
     actual = target.CalculateTurningPoints();
     Assert.AreEqual(2, actual.Length);
     Assert.IsTrue(actual[0].IsPeak);
     Assert.AreEqual("-0.33", actual[0].Point.X.ToString("0.00"));
     Assert.IsFalse(actual[1].IsPeak);
     Assert.AreEqual("1.00", actual[1].Point.X.ToString("0.00"));
 }
Esempio n. 24
0
 /// <summary>
 /// Calculates this mathemarical expression.
 /// </summary>
 /// <param name="parameters">An object that contains all parameters and functions for expressions.</param>
 /// <returns>
 /// A result of the calculation.
 /// </returns>
 /// <seealso cref="ExpressionParameters" />
 public override object Calculate(ExpressionParameters parameters)
 {
     var differentiator = new Differentiator ();
     var derivat = differentiator.Differentiate ( this.Expression, this.Variable );
     return derivat.Calculate ( parameters );
 }
 public static void TestDifferentiation(string expression, string answer)
 {
     Assert.Equal(answer, Differentiator.Differentiate(expression));
 }
 public void AddTest1()
 {
     DPoint[] points = createPoints();
     Differentiator target = new Differentiator(points);
     target.Add(2, -2);
     target.Add(5, 31);
     target.Add(10, 166);
     Assert.AreEqual(points.Length+3, target.Points.Length);
 }
 public void DifferentiateConstructorTest()
 {
     Differentiator target = new Differentiator();
 }
 public void AddTest2()
 {
     Differentiator target = new Differentiator();
     //y = 2xx +3x + 4
     //y' = 4x +3
     target.Add(new DPoint(0, 4));
     target.Add(new DPoint(1, 9));
     target.Add(new DPoint(2, 18));
     DPoint[] points = target.Points;
     Assert.AreEqual(3, points.Length);
     Assert.AreEqual(0, points[0].X);
     Assert.AreEqual(4, points[0].Y);
     Assert.AreEqual(1, points[1].X);
     Assert.AreEqual(9, points[1].Y);
     Assert.AreEqual(2, points[2].X);
     Assert.AreEqual(18, points[2].Y);
 }
 public void AddTest3()
 {
     Differentiator target = new Differentiator();
     List<Measurement> list = new List<Measurement>();
     list.Add(new Measurement(2, -2));
     list.Add(new Measurement(5, 31));
     list.Add(new Measurement(10, 166));
     target.Add(list, "Wavelength", "Transmittance");
     DPoint[] points = target.Points;
     Assert.AreEqual(3, points.Length);
     Assert.AreEqual(2, points[0].X);
     Assert.AreEqual(-2, points[0].Y);
     Assert.AreEqual(5, points[1].X);
     Assert.AreEqual(31, points[1].Y);
     Assert.AreEqual(10, points[2].X);
     Assert.AreEqual(166, points[2].Y);
 }
 public void Differentiate1()
 {
     DPoint[] points = createPoints();
     Differentiator target = new Differentiator(points);
     DPoint[] diffPoints = target.Differentiate();
     Assert.AreEqual(points.Length, diffPoints.Length);
     Assert.AreEqual(points[50].X, diffPoints[50].X);
     Assert.AreEqual(points[500].X, diffPoints[500].X);
     //gradient = 1
     Assert.AreEqual(1d, diffPoints[50].Y);
     //gradient  = -1
     Assert.AreEqual(-1d, diffPoints[150].Y);
     //turning point
     Assert.AreEqual(0d, diffPoints[100].Y);
 }
 public void DifferentiateConstructorTest()
 {
     Differentiator target = new Differentiator();
 }
 public void DifferentiateConstructorTest2()
 {
     DPoint[] points = createPoints();
     Differentiator target = new Differentiator(points);
     Assert.AreEqual(points.Length,target.Points.Length);
 }
 public void EndPoints()
 {
     DPoint[] points = createPoints();
     Differentiator target = new Differentiator(points);
     DPoint[] diffPoints = target.Differentiate();
     Assert.AreEqual(0, diffPoints[0].X);
     Assert.AreEqual(1, diffPoints[0].Y);
     Assert.AreEqual(999, diffPoints[999].X);
     Assert.AreEqual(-1, diffPoints[999].Y);
 }
 public void SampleSize()
 {
     Differentiator target = new Differentiator();
     target.SampleSize = 9;
     //y=2x+1
     target.Add(new DPoint(0, 1));
     target.Add(new DPoint(1, 3));
     target.Add(new DPoint(2, 5));
     DPoint[] diffPoints = target.Differentiate();
     Assert.AreEqual(3, diffPoints.Length);
     //expect best line fit, which has gadient of 2
     Assert.AreEqual(2, diffPoints[0].Y);
     Assert.AreEqual(2, diffPoints[1].Y);
     Assert.AreEqual(2, diffPoints[2].Y);
 }
 public void Accuracy()
 {
     List<DPoint> points = new List<DPoint>();
     for (int i = -100; i < 101; i++)
     {
         double x = (double)i;
         double y = 3 * x * x - 2 * x + 1;
         points.Add(new DPoint(x, y));
     }
     Differentiator target = new Differentiator(points);
     DPoint[] diffPoints = target.Differentiate();
     //x=0, y' = -2
     Assert.AreEqual(0d, diffPoints[100].X);
     Assert.AreEqual(-2d, diffPoints[100].Y);
     //x=1, y'=4
     Assert.AreEqual(1d, diffPoints[101].X);
     Assert.AreEqual(4d, diffPoints[101].Y);
     //x=-1, y'=-8
     Assert.AreEqual(-1d, diffPoints[99].X);
     Assert.AreEqual(-8d, diffPoints[99].Y);
     //x=42, y'=250
     Assert.AreEqual(42d, diffPoints[142].X);
     Assert.AreEqual(250d, diffPoints[142].Y);
 }
 public DifferentiationTests()
 {
     this.differentiator = new Differentiator();
     this.targetFunc     = x => x * x;
     this.inputValue     = 3;
 }