C#数组列表生成双线性插值

时间:2016-09-22 18:04:56

标签: c# arrays algorithm bilinear-interpolation

在开始之前,请接受我的道歉,即我是初级开发人员。

我想要的是一个包含数据的网格。只有一个包含7个点(x,y,值)的数组列表。我想在5x5的网格中执行此操作。只有我错过了许多价值观。我想借助插值函数生成缺失值。搜索多维(或插值)(x,y)以生成其他网格的值的算法。

网格示例:

18 - 0 - 0 - 35 - 0
0 - 20 - 0 - 0 - 29
25 - 0 - 0 - 30 - 0
0 - 25 - 0 - 32 - 35
0 - 0 - 28 - 0 - 0

我使用了这个方法(来自c#book中的数值方法,算法和工具)并且它正在工作,但我不明白如何将我的值传递给函数并生成随机的5x5缺失价值观,因为书中没有解释这些功能和方法。

什么是zdata [,]双2D数组?,这是从点数的值? 什么是xdate和ydata?

例如,我有一个7点(x,y,值)=>的数组0,0,18等 如何正确传递给此方法以生成缺失值?

这是整个功能:

class InterpolationTest
{
    static void Main(string[] args)
    {
        double[] xdata = new double[] { 0, 2 };  //x-coordinate 0 and 2?
        double[] ydata = new double[] { 0, 2 };  //y-coordinate 0 and 2?
        double[,] zdata = new double[,] { { 0, 20 }, { 20, 10 } }; //what is a zdata??
        double[] x = new double[5]; 
        double[] y = new double[5];
        RMatrix z = new RMatrix(5, 5);
        for (int i = 0; i < 4; i++)
        {
            x[i] = (i + 2.0) / 20.0;
            y[i] = (i + 2.0) / 20.0;
        }
        RVector xvec = new RVector(x);
        RVector yvec = new RVector(y);
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                z[i, j] = BilinearInterpolation(xdata, ydata, zdata, x[i], y[j]);
            }
        }
        Console.Clear();
        Console.WriteLine("Running Bilinear Interpolation Test\n\n");
        Console.WriteLine("x = " + xvec.ToString());
        Console.WriteLine("y = " + yvec.ToString());
        Console.WriteLine("\nResults z = \n" + z.ToString() + "\n\n");
        Console.WriteLine("Press ENTER key to continue...");
        Console.ReadLine();

    }

    public static double BilinearInterpolation(double[] x, double[] y, double[,] z, double xval, double yval)
    {
        double zval = 0.0;
        for (int i = 0; i < x.Length - 1; i++)
        {
            for (int j = 0; j < y.Length - 1; j++)
            {
                if (xval >= x[i] && xval < x[i + 1] && yval >= y[j] && yval < y[j + 1])
                {
                    zval = z[i, j] * (x[i + 1] - xval) * (y[j + 1] - yval) / (x[i + 1] - x[i]) / (y[j + 1] - y[j]) +
                    z[i + 1, j] * (xval - x[i]) * (y[j + 1] - yval) / (x[i + 1] - x[i]) / (y[j + 1] - y[j]) +
                    z[i, j + 1] * (x[i + 1] - xval) * (yval - y[j]) / (x[i + 1] - x[i]) / (y[j + 1] - y[j]) +
                    z[i + 1, j + 1] * (xval - x[i]) * (yval - y[j]) / (x[i + 1] - x[i]) / (y[j + 1] - y[j]);
                }
            }
        }
        return zval;
    }

    public static double[] BilinearInterpolation(double[] x, double[] y, double[,] z, double[] xvals, double[] yvals)
    {
        double[] zvals = new double[xvals.Length];
        for (int i = 0; i < xvals.Length; i++)
            zvals[i] = BilinearInterpolation(x, y, z, xvals[i], yvals[i]);
        return zvals;
    }
}

public struct RMatrix
{
    private int nRows;
    private int nCols;
    private double[,] matrix;

    public RMatrix(int nCols, int nRows)
    {
        this.nRows = nRows;
        this.nCols = nCols;
        this.matrix = new double[nRows, nCols];
        for (int i = 0; i < nRows; i++)
        {
            for (int j = 0; j < nCols; j++)
            {
                matrix[i, j] = 0.0;
            }
        }
    }

    public RMatrix(double[,] matrix)
    {
        this.nRows = matrix.GetLength(0);
        this.nCols = matrix.GetLength(1);
        this.matrix = matrix;
    }

    public RMatrix(RMatrix m)
    {
        nRows = m.GetnRows;
        nCols = m.GetnCols;
        matrix = m.matrix;
    }

    public RMatrix IdentityMatrix()
    {
        RMatrix m = new RMatrix(nRows, nCols);
        for (int i = 0; i < nRows; i++)
        {
            for (int j = 0; j < nCols; j++)
            {
                if (i == j)
                {
                    m[i, j] = 1;
                }
            }
        }
        return m;
    }

    public int GetnRows
    {
        get { return nRows; }
    }
    public int GetnCols
    {
        get { return nCols; }
    }

    public override string ToString()
    {
        string strMatrix = "(";
        for (int i = 0; i < nRows; i++)
        {
            string str = "";
            for (int j = 0; j < nCols - 1; j++)
            {
                str += matrix[i, j].ToString() + ", ";
            }
            str += matrix[i, nCols - 1].ToString();
            if (i != nRows - 1 && i == 0)
                strMatrix += str + "\n";
            else if (i != nRows - 1 && i != 0)
                strMatrix += " " + str + "\n";
            else
                strMatrix += " " + str + ")";
        }
        return strMatrix;
    }
}

public struct RVector
{
    private int ndim;
    private double[] vector;

    public RVector(int ndim)
    {
        this.ndim = ndim;
        this.vector = new double[ndim];
        for (int i = 0; i < ndim; i++)
        {
            vector[i] = 0.0;
        }
    }

    public RVector(double[] vector)
    {
        this.ndim = vector.Length;
        this.vector = vector;
    }

    public override string ToString()
    {
        string str = "(";
        for (int i = 0; i < ndim - 1; i++)
        {
            str += vector[i].ToString() + ", ";
        }
        str += vector[ndim - 1].ToString() + ")";
        return str;
    }
}

由于

0 个答案:

没有答案