通用线性插值器:如何处理DateTime? [英] A generic linear interpolator: how to cope with DateTime?

查看:150
本文介绍了通用线性插值器:如何处理DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个LinearInterpolator类,其中X是X轴值的类型,Y是Y轴值的类型。我看不出如何做到这一点,以便X可以是DateTime或double。该类如下所示(未经测试):

 类LinearInterpolator< X,Y> 
{
私人列表< X> m_xAxis;
私人列表< 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
{
//必须内插。
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];

//这是我希望一般写的表达式。
//我也希望X可以编译为DateTime。
Y y = yLeft +((x - xLeft)/(xRight - xLeft))*(yRight - yLeft);
return y;
}
}
}

}



在C ++中很容易,但我是C#泛型的新手,所以任何帮助都不胜感激。

解决

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

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

$ b $ 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));
}

我没有测试过代码,所以认为它是伪代码。 / p>

I'd like to write a LinearInterpolator class, where X is the type of the X axis value, and Y the type of the Y axis value. I can't see how to do this such that X could be a DateTime or a double. The class is something like below (which is untested):

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;
        }
    }
}

}

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

解决方案

There's no good way to do math from C# generics, so you have 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 haven't tested the code, so consider it pseudo-code.

这篇关于通用线性插值器:如何处理DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆