/// <summary>
 /// Create a rational pole-free interpolation based on arbitrary points. This is the default interpolation scheme.
 /// </summary>
 /// <param name="points">The sample points t. Supports both lists and arrays.</param>
 /// <param name="values">The sample point values x(t). Supports both lists and arrays.</param>
 /// <returns>
 /// An interpolation scheme optimized for the given sample points and values,
 /// which can then be used to compute interpolations and extrapolations
 /// on arbitrary points.
 /// </returns>
 public static IInterpolationMethod Create(
     IList<double> points,
     IList<double> values)
 {
     RationalPoleFreeInterpolation method = new RationalPoleFreeInterpolation();
     method.Init(points, values);
     return method;
 }
        Create(
            IList <double> points,
            IList <double> values)
        {
            RationalPoleFreeInterpolation method = new RationalPoleFreeInterpolation();

            method.Init(points, values);
            return(method);
        }
        public void TestInterpolationMethod_RationalPoleFreeBarycentric()
        {
            /**************************************************************************************************
            *  1st: polynomial case (equidistant polynomial generates the same values; rational would have pole)
            **************************************************************************************************/

            double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
            double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 };

            IInterpolationMethod method = Interpolation.Create(t, x);

            Assert.That(method, Is.TypeOf(typeof(RationalPoleFreeInterpolation)), "Type");

            for (int i = 0; i < t.Length; i++)
            {
                // verify the interpolated values exactly at the sample points.
                Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "A Exact Point " + i.ToString());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "PolynomialInterpolation([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x);"
            Assert.That(method.Interpolate(-2.4), NumericIs.AlmostEqualTo(-4.5968, 1e-15), "A -2.4");
            Assert.That(method.Interpolate(-0.9), NumericIs.AlmostEqualTo(1.65395, 1e-15), "A -0.9");
            Assert.That(method.Interpolate(-0.5), NumericIs.AlmostEqualTo(0.21875, 1e-15), "A -0.5");
            Assert.That(method.Interpolate(-0.1), NumericIs.AlmostEqualTo(-0.84205, 1e-15), "A -0.1");
            Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(-1.10805, 1e-15), "A 0.1");
            Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(-1.1248, 1e-15), "A 0.4");
            Assert.That(method.Interpolate(1.2), NumericIs.AlmostEqualTo(0.5392, 1e-15), "A 1.2");
            Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo((double)(-4431), 1e-12), "A 10.0");
            Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo((double)(-5071), 1e-12), "A -10.0");

            /******************************************************************************
            *  2nd: x(t) = 1/(1+t^2), t=-5..5 (polynomial can' t interpolate that function!)
            ******************************************************************************/

            t = new double[40];
            x = new double[40];

            const double step = 10.0 / 39.0;

            for (int i = 0; i < t.Length; i++)
            {
                double tt = -5 + (i * step);
                t[i] = tt;
                x[i] = 1.0 / (1.0 + (tt * tt));
            }

            RationalPoleFreeInterpolation methodTyped = (RationalPoleFreeInterpolation)method;

            methodTyped.Init(t, x); // re-initialize for another set of points/values.

            for (int i = 0; i < t.Length; i++)
            {
                // verify the interpolated values exactly at the sample points.
                Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "B Exact Point " + i.ToString());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "tt := [seq(-5+(i-1)*10/39,i=1..40)]: xx := [seq(1/(1+tt[i]*tt[i]),i=1..40)]:"
            // Maple: "RationalInterpolation(tt, xx, x);"

            // Test Linear Case
            for (int k = 2; k < 7; k++)
            {
                double[] linx, liny, linxtest, linytest;
                BuildLinearCase(2, k, out linx, out liny, out linxtest, out linytest);
                IInterpolationMethod linearMethod = Interpolation.Create(linx, liny);
                for (int i = 0; i < linxtest.Length; i++)
                {
                    Assert.That(linearMethod.Interpolate(linxtest[i]), NumericIs.AlmostEqualTo(linytest[i], 1e-12), String.Format("Linear k={0} i={1}", k, i));
                }
            }
        }
Example #4
0
        public void TestInterpolationMethod_RationalPoleFreeBarycentric()
        {
            // *************************************************************************************************
            // 1st: polynomial case (equidistant polynomial generates the same values; rational would have pole)
            // *************************************************************************************************

            double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
            double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 };

            IInterpolationMethod method = Interpolation.Create(t, x);

            Assert.IsInstanceOfType(typeof(RationalPoleFreeInterpolation), method, "Type");

            for (int i = 0; i < t.Length; i++)
            {
                // verify the interpolated values exactly at the sample points.
                Assert.AreEqual(x[i], method.Interpolate(t[i]), "A Exact Point " + i.ToString());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "PolynomialInterpolation([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x);"
            NumericAssert.AreAlmostEqual(-4.5968, method.Interpolate(-2.4), 1e-15, "A -2.4");
            NumericAssert.AreAlmostEqual(1.65395, method.Interpolate(-0.9), 1e-15, "A -0.9");
            NumericAssert.AreAlmostEqual(0.21875, method.Interpolate(-0.5), 1e-15, "A -0.5");
            NumericAssert.AreAlmostEqual(-0.84205, method.Interpolate(-0.1), 1e-15, "A -0.1");
            NumericAssert.AreAlmostEqual(-1.10805, method.Interpolate(0.1), 1e-15, "A 0.1");
            NumericAssert.AreAlmostEqual(-1.1248, method.Interpolate(0.4), 1e-15, "A 0.4");
            NumericAssert.AreAlmostEqual(0.5392, method.Interpolate(1.2), 1e-15, "A 1.2");
            NumericAssert.AreAlmostEqual(-4431, method.Interpolate(10.0), 1e-12, "A 10.0");
            NumericAssert.AreAlmostEqual(-5071, method.Interpolate(-10.0), 1e-12, "A -10.0");

            // *****************************************************************************
            // 2nd: x(t) = 1/(1+t^2), t=-5..5 (polynomial can' t interpolate that function!)
            // *****************************************************************************

            t = new double[40];
            x = new double[40];

            double step = 10.0 / 39.0;

            for (int i = 0; i < t.Length; i++)
            {
                double tt = -5 + i * step;
                t[i] = tt;
                x[i] = 1.0 / (1.0 + tt * tt);
            }

            RationalPoleFreeInterpolation methodTyped = (RationalPoleFreeInterpolation)method;

            methodTyped.Init(t, x); // re-initialize for another set of points/values.

            for (int i = 0; i < t.Length; i++)
            {
                // verify the interpolated values exactly at the sample points.
                Assert.AreEqual(x[i], method.Interpolate(t[i]), "B Exact Point " + i.ToString());
            }

            // Maple: "with(CurveFitting);"
            // Maple: "tt := [seq(-5+(i-1)*10/39,i=1..40)]: xx := [seq(1/(1+tt[i]*tt[i]),i=1..40)]:"
            // Maple: "RationalInterpolation(tt, xx, x);"
        }