Generic linear interpolator: how to deal with DateTime?

I would like to write a LinearInterpolator class, where X is the type of the value of the X axis and Y is the type of the value of the Y axis. I don’t see how to do this so that X can be a DateTime or double. The class is something like below (which has not been verified):

class LinearInterpolator<X, Y>
{
    private List<X> m_xAxis;
    private List<Y> m_yAxis;

    public LinearInterpolator(List<X> x, List<Y> y)
    {
        m_xAxis = x;
        m_yAxis = y;
    }

    public Y interpolate(X x)
    {
        int i = m_xAxis.BinarySearch(x);
        if (i >= 0)
        {
            return m_yAxis[i];
        }
        else
        {
            // Must interpolate.
            int rightIdx = ~i;
            if (rightIdx >= m_xAxis.Count)
                --rightIdx;
            int leftIdx = rightIdx - 1;

            X xRight = m_xAxis[rightIdx];
            X xLeft = m_xAxis[leftIdx];
            Y yRight = m_yAxis[rightIdx];
            Y yLeft = m_yAxis[leftIdx];

            // This is the expression I'd like to write generically.
            // I'd also like X to be compilable as a DateTime.
            Y y = yLeft + ((x - xLeft) / (xRight - xLeft)) * (yRight - yLeft);
            return y;
        }
    }
}

}

That would be easy in C ++, but I'm new to C # generics, so any help would be appreciated.

+5
source share
4 answers

Use DateTime.Ticksas interpolated value. You can use the type longas your common to interpolate between moments.

+1
source

You should be aware that C # does not support operator overrides. Thus, such code does not work.

, spoulson, - generics int long T DateTime.Ticks.

+1

There is no good way to do the math from C # generators, so you need to do something like this:

Y y = FromDouble<Y>(ToDouble(yLeft) + ((ToDouble(x) - ToDouble(xLeft)) /
          (ToDouble(xRight) - ToDouble(xLeft))) *
          (ToDouble(yRight) - ToDouble(yLeft)));

double ToDouble(object val)
{
    if (val is DateTime)
        return (double)((DateTime)val).Ticks;
    else
        return Convert.ToDouble(val);
}

T FromDouble<T>(double val)
{
    if (typeof(T) == typeof(DateTime))
        return (T)Convert.ChangeType(new DateTime((long)val), typeof(T));
    else
        return (T)Convert.ChangeType(val, typeof(T));
}

I have not tested the code, so consider its pseudo-code.

+1
source

Thanks for the advice. I change my approach and write an interpolator that works only for doubles; I want interpolation to be fast, so prefer not to perform type checks at runtime.

0
source

All Articles