Beispiel #1
0
        public void TestInterpolationMethod_LinearSpline()
        {
            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.CreateLinearSpline(t, x);

            Assert.IsInstanceOfType(typeof(LinearSplineInterpolation), 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: "f := x -> piecewise(x<-1,3+x,x<0,-1-3*x,x<1,-1+x,-1+x);"
            // Maple: "f(x)"
            NumericAssert.AreAlmostEqual(.6, method.Interpolate(-2.4), 1e-15, "A -2.4");
            NumericAssert.AreAlmostEqual(1.7, method.Interpolate(-0.9), 1e-15, "A -0.9");
            NumericAssert.AreAlmostEqual(.5, method.Interpolate(-0.5), 1e-15, "A -0.5");
            NumericAssert.AreAlmostEqual(-.7, method.Interpolate(-0.1), 1e-15, "A -0.1");
            NumericAssert.AreAlmostEqual(-.9, method.Interpolate(0.1), 1e-15, "A 0.1");
            NumericAssert.AreAlmostEqual(-.6, method.Interpolate(0.4), 1e-15, "A 0.4");
            NumericAssert.AreAlmostEqual(.2, method.Interpolate(1.2), 1e-15, "A 1.2");
            NumericAssert.AreAlmostEqual(9.0, method.Interpolate(10.0), 1e-15, "A 10.0");
            NumericAssert.AreAlmostEqual(-7.0, method.Interpolate(-10.0), 1e-15, "A -10.0");
        }
        public void TestInterpolationMethod_LinearSpline()
        {
            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.CreateLinearSpline(t, x);

            Assert.That(method, Is.TypeOf(typeof(LinearSplineInterpolation)), "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: "f := x -> piecewise(x<-1,3+x,x<0,-1-3*x,x<1,-1+x,-1+x);"
            // Maple: "f(x)"
            Assert.That(method.Interpolate(-2.4), NumericIs.AlmostEqualTo(.6, 1e-15), "A -2.4");
            Assert.That(method.Interpolate(-0.9), NumericIs.AlmostEqualTo(1.7, 1e-15), "A -0.9");
            Assert.That(method.Interpolate(-0.5), NumericIs.AlmostEqualTo(.5, 1e-15), "A -0.5");
            Assert.That(method.Interpolate(-0.1), NumericIs.AlmostEqualTo(-.7, 1e-15), "A -0.1");
            Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(-.9, 1e-15), "A 0.1");
            Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(-.6, 1e-15), "A 0.4");
            Assert.That(method.Interpolate(1.2), NumericIs.AlmostEqualTo(.2, 1e-15), "A 1.2");
            Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo(9.0, 1e-15), "A 10.0");
            Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo(-7.0, 1e-15), "A -10.0");

            // Test Linear Case
            for (int k = 2; k < 6; k++)
            {
                double[] linx, liny, linxtest, linytest;
                BuildLinearCase(2, k + 1, out linx, out liny, out linxtest, out linytest);
                IInterpolationMethod linearMethod = Interpolation.CreateLinearSpline(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));
                }
            }
        }