GF(2 ^ 16)中的Lagrange插值多项式的计算

时间:2017-03-19 17:17:27

标签: c# polynomials galois-field

我有一组x,y数据,我需要在GF(2 ^ 16)中计算拉格朗日插值多项式,并以正确的格式返回系数。我从How to calculate coefficients of polynomial using Lagrange interpolation找到了一些我转换为c#的javascript代码。在计算f(x)之后,我发现该方法返回正确的系数,但是它不属于GF(2 ^ 16)产生可能是负的或非常长的系数。我如何才能使系数落在GF(2 ^ 16)之内? 这是我用来从集合数据创建系数的代码:

// initialize array
    public double[] zeros(int n)
    {
        double[] array = new double[n];
        for (int i = n - 1; i >= 0; i--)
        {
            array[i] = 0;
        }
        return array;
    }

    public double denominator(int i, double[] xpoints)
    {
        double result = 1;
        double x_i = xpoints[i];
        for (int j = xpoints.Count() - 1; j >= 0; j--)
        {
            if (i != j)
            {
                result = result * (x_i - xpoints[j]);
            }
        }
        return result;
    }

    // calculate coefficients for Li polynomial
    public double[] interpolation_polynomial(int i, double[] xpoints)
    {
        double[] coefficients = zeros(xpoints.Length);
        coefficients[0] =((double)1 / denominator(i, xpoints)); 
        double[] new_coefficients;

        for (int k = 0; k < xpoints.Count(); k++)
        {
            if (k == i)
            {
                continue;
            }
            new_coefficients = zeros(xpoints.Count());

            for (int j = (k < i) ? k + 1 : k; j >= 0; j--)
            {
                if (j + 1 < xpoints.Count())
                {
                    new_coefficients[j + 1] = new_coefficients[j + 1] + coefficients[j]  ;
                }
                new_coefficients[j] = new_coefficients[j] - (xpoints[k] * coefficients[j]);
            }  
            coefficients = new_coefficients;
        }
        return coefficients;
    }

    // calculate coefficients of polynomial
    public double[] Lagrange(double[] xpoints, double[] ypoints)
    {
        double[] polynomial = zeros(ypoints.Count());
        double[] coefficients;
        for (int i = 0; i < ypoints.Count(); ++i)
        {
            coefficients = interpolation_polynomial(i, xpoints);
            for (int k = 0; k < ypoints.Count(); ++k)
            {
                polynomial[k] = polynomial[k] + (ypoints[i] * coefficients[k]);
            }
        }         
        return polynomial;
    }

0 个答案:

没有答案