简体   繁体   English

通用线性分段查找表

[英]Generic Linear Piecewise Lookup Table

I am looking for a generic optimized lookup object that takes a function f(x) and creates a linear piecewise approximation with configurable parameters for the range of x and the intervals of population. 我正在寻找一个通用的优化查找对象,它采用函数f(x)并创建一个线性分段近似,其中包含x的范围和总体间隔的可配置参数。

Obviously this is not hard to write, but given that it is useful for a lot of expensive functions (trig, yield, distance), I thought a generic one might already exist. 显然这并不难写,但鉴于它对很多昂贵的函数(trig,yield,distance)很有用,我认为一个通用的函数可能已经存在。 Please let me know. 请告诉我。

Another useful feature would be to serialize / deserialize the lookup table as a rather precise 100,000 point+ table can take several minutes to construct. 另一个有用的功能是序列化/反序列化查找表,因为相当精确的100,000点+表可能需要几分钟才能构建。

I don't believe anything exists in the .NET class libraries directly. 我不相信.NET类库中直接存在任何内容。 Something may exist in a third party library ( like C5 perhaps ). 第三方库中可能存在某些东西( 比如C5 )。

Creating a generic version of a function that can accept ranges is a bit tricky in C#, since there is no unifying type or interface which provides arithmetic operators. 在C#中创建可以接受范围的函数的泛型版本有点棘手,因为没有统一类型或接口提供算术运算符。 However, with some creativity, it's possible to craft something: 然而,通过一些创造力,可以制作一些东西:

// generic linear lookup class, supports any comparable type T
public class LinearLookup<T>  where T : IComparable<T>
{
    private readonly List<T> m_DomainValues = new List<T>();

    public LinearLookup( Func<T,T> domainFunc, Func<T,T> rangeFunc, 
          T lowerBound, T upperBound )
    {
        m_DomainValues = Range( domainFunc, rangeFunc, 
                                lowerBound, upperBound )
                           .ToList();
    }

    public T Lookup( T rangeValue )
    {
        // this could be improved for numeric types
        var index = m_DomainValues.BinarySearch( rangeValue );
        if( index < 0 )
            index = (-index)-1;
        return m_DomainValues[index];
    }

    private static IEnumerable<T> Range( Func<T,T> domainFunc, 
         Func<T,T> rangeFunc, T lower, T upper )
    {
        var rangeVal = lower;
        do
        {
            yield return domainFunc( rangeVal );

            rangeVal = rangeFunc( rangeVal );

        } while( rangeVal.CompareTo( upper ) < 0 );
    }
}

This class will precompute a set of domain values for the function domainFunc over the range [lower,upper>. 此类将在[lower,upper>]范围内为函数domainFunc预计算一组域值。 It uses binary search for lookup - a compromise that allows any comparable type to be used - not just built in numeric types. 它使用二进制搜索进行查找 - 允许使用任何类似类型的折衷方案 - 不仅仅是以数字类型构建。 The function rangeFunc allows the increment to be controlled by external code. 函数rangeFunc允许增量由外部代码控制。 So, here's a linear lookup for Math.Sin over the range [0,PI/2> with an increment of 0.01: 因此,这里是Math.Sin在[0,PI / 2>范围内的增量为0.01的线性查找:

var sinLookup = new LinearLookup( Math.Sin, x => x + 0.01d, 0, Math.PI/2 );
var lookupPI4 = sinLookup[Math.PI/4]; // fetch the value sin(π/4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM