Beispiel #1
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"));
        }
        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 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 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 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"));
 }
Beispiel #7
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 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 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 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 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 static void TestDifferentiation(string expression, string answer)
 {
     Assert.Equal(answer, Differentiator.Differentiate(expression));
 }