예제 #1
0
        public static void TestInterpolation2()
        {
            double[] x = new double[] { 0, 1 };
            double[] y = new double[] { 0, 1 };

            ACQ.Math.Interpolation.InterpolationInterface interpolator = new ACQ.Math.Interpolation.LinearInterpolation(x, y);

            double xi = 0.5;
            double yi = interpolator.Eval(xi);
        }
예제 #2
0
        /// <summary>
        /// Construct Lowess
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="nsteps"> number of robustifying iterations which should be performed. (default = 3) </param>
        /// <param name="span">smoother span [0, 1], default = 2/3. This gives the proportion of points in the plot which influence the smooth at each value. Larger values give more smoothness</param>
        /// <param name="delta">Defaults to 1/100th of the range of x</param>
        public Lowess(double[] x, double[] y, double span = 2.0 / 3.0, int nsteps = 3, double delta = 0.0)
        {
            if (x == null || y == null)
            {
                throw new ArgumentNullException("Lowess input arrays can not be null");
            }

            if (x.Length != y.Length)
            {
                throw new ArgumentException("Lowess input arrays x and y should have the same length");
            }

            if (x.Length < 2)
            {
                throw new ArgumentException("Lowess input arrays should have at least 2 nodes");
            }

            if (nsteps < 0)
            {
                throw new ArgumentException("Lowess nsteps must be >= 0");
            }

            //the code below actually will work for any value of span (because it checks later), but R checks for negative span, so we as well for consistency
            if (span < 0)
            {
                throw new ArgumentException("Lowess span must be > 0");
            }

            int n = x.Length;

            //check that x is sorted
            bool ordered = true;

            for (int i = 0; i < n - 1; i++)
            {
                if (x[i + 1] < x[i]) //same values are allowed
                {
                    ordered = false;
                    break;
                }
            }

            double[] x_in = x;
            double[] y_in = y;

            if (!ordered)
            {
                //make copy and sort if provided array is not ordered
                x_in = (double[])x.Clone();
                y_in = (double[])y.Clone();

                Array.Sort <double, double>(x_in, y_in);
            }

            if (delta <= 0.0)
            {
                delta = 0.01 * (x_in[n - 1] - x_in[0]); //same default as in R
            }

            double[] ys = new double[n];

            clowess(x_in, y_in, ys, span, nsteps, delta); //this is main r - function

            //check that all values of x_in are uniques
            bool unique = true;

            for (int i = 0; i < n - 1; i++)
            {
                if (x[i + 1] == x[i]) //same values are allowed
                {
                    unique = false;
                    break;
                }
            }

            if (unique)
            {
                m_interpolator = new ACQ.Math.Interpolation.LinearInterpolation(x_in, ys);
            }
            else
            {
                List <double> xu = new List <double>(n);
                List <double> yu = new List <double>(n);

                xu.Add(x_in[0]);
                yu.Add(ys[0]);

                double prev_x = x_in[0];

                for (int i = 1; i < n; i++)
                {
                    if (x_in[i] > prev_x)
                    {
                        xu.Add(x_in[i]);
                        yu.Add(ys[i]);
                        prev_x = x_in[i];
                    }
                }
                m_interpolator = new ACQ.Math.Interpolation.LinearInterpolation(xu.ToArray(), yu.ToArray());
            }
        }