public OneDVectorInterpolator(double[] x, double[][] y, InterpolatonType type)
        {
            componentInterp = new OneDInterpolator[y[0].Length];
            var tempY = new double[y.Length];

            for (int i = 0; i < componentInterp.Length; i++)
            {
                for (int j = 0; j < y.Length; j++)
                {
                    tempY[j] = y[j][i];
                }

                componentInterp[i] = new OneDInterpolator(x, tempY, type);
            }
        }
Beispiel #2
0
        public bool TryGetValue(double x, double y, out double z)
        {
            if (type == InterpolatonType.Nearest)
            {
                if (GetNearestIndex(xs, x, xsReversed, out int i) && GetNearestIndex(ys, y, ysReversed, out int j))
                {
                    z = rawValues[i][j];
                    return(true);
                }

                z = 0;
                return(false);
            }
            else
            {
                if (!cachedValues.TryGetValue(y, out OneDInterpolator inter))
                {
                    if (!this.values[0].GetKLoHi(y, out int klo, out int khi))
                    {
                        z = 0;
                        return(false);
                    }

                    double[] interpValues = new double[xs.Length];
                    for (int j = 0; j < xs.Length; j++)
                    {
                        interpValues[j] = this.values[j].GetValue(y, klo, khi);
                    }

                    inter = new OneDInterpolator(xs, interpValues, type);
                    cachedValues.Add(y, inter);
                }

                return(inter.TryGetValue(x, out z));
            }
        }
        public static void Test()
        {
            int    n  = 10;
            double y0 = 5.0;

            double[] xs = new double[n];
            double[] ys = new double[n];
            for (int i = 0; i < n; i++)
            {
                xs[i] = i;
                ys[i] = y0;
            }

            var  t1      = new OneDInterpolator(xs, ys, InterpolatonType.Cubic);
            bool success = t1.TryGetValue(n / 2.0 + 0.5, out double y);

            if (!success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y - y0) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }

            success = t1.TryGetValue(0.0, out y);
            if (!success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y - y0) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }

            success = t1.TryGetValue((n - 1) * 1.0, out y);
            if (!success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y - y0) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }

            success = t1.TryGetValue(-0.01, out y);
            if (success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }

            success = t1.TryGetValue((n - 1) * 1.0 + 0.01, out y);
            if (success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }

            double cubic(double x_) => (x_ - 2) * (x_ + 3) * (x_ - 1);

            for (int i = 0; i < n; i++)
            {
                xs[i] = i;
                ys[i] = cubic(i);
            }

            t1 = new OneDInterpolator(xs, ys, InterpolatonType.Cubic);

            double x = n / 2.0 + 0.5;

            y0      = cubic(x);
            success = t1.TryGetValue(x, out y);
            if (!success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y - y0) > 0.05)
            {
                throw new InvalidOperationException();
            }

            x       = 0.05;
            y0      = cubic(x);
            success = t1.TryGetValue(x, out y);
            if (!success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y - y0) > 1.0e-4)
            {
                throw new InvalidOperationException();
            }

            x       = (n - 1) * 1.0 - 0.5;
            y0      = cubic(x);
            success = t1.TryGetValue(x, out y);
            if (!success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y - y0) > 2.5)
            {
                throw new InvalidOperationException();
            }

            x       = -0.01;
            success = t1.TryGetValue(x, out y);
            if (success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }

            success = t1.TryGetValue((n - 1) * 1.0 + 0.01, out y);
            if (success)
            {
                throw new InvalidOperationException();
            }
            if (Math.Abs(y) > 1.0e-10)
            {
                throw new InvalidOperationException();
            }
        }