Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不稳定计算误差_C#_Matrix - Fatal编程技术网

C# 不稳定计算误差

C# 不稳定计算误差,c#,matrix,C#,Matrix,我需要计算矩阵:(X^(T)*X)^(-1)。 代码和注释的图例: x is double[,] array; xT - transposed matrix ^(-1) - inverted matrix 每次我生成新的随机矩阵来处理它时,我发现程序非常不稳定,因为它不能正确处理任何输入数据。我确信这一点,因为如果一切正常的话,我最终需要得到单位矩阵,但有时我得到的是一个非常糟糕的无因次矩阵,所以我得不到单位矩阵。我不被任命,因为我总是使用相同类型的数据,不转换任何内容。编译器是MVS2010

我需要计算矩阵:(X^(T)*X^(-1)。 代码和注释的图例:

x is double[,] array;
xT - transposed matrix
^(-1) - inverted matrix
每次我生成新的随机矩阵来处理它时,我发现程序非常不稳定,因为它不能正确处理任何输入数据。我确信这一点,因为如果一切正常的话,我最终需要得到单位矩阵,但有时我得到的是一个非常糟糕的无因次矩阵,所以我得不到单位矩阵。我不被任命,因为我总是使用相同类型的数据,不转换任何内容。编译器是MVS2010。希望你能帮助我

这是我的程序。cs

    static void Main(string[] args)
    {

        Matrix x = new Matrix(5, 4);
        //Matrix temp = new Matrix(x.Row, x.Col);
        //double[] y = new double[x.Row];
        //double[] b = new double[x.Row];

        //this data isn't calculated correctly. used for debugging
        x.MatrixX[0, 0] = 7; x.MatrixX[0, 1] = 6; x.MatrixX[0, 2] = 5; x.MatrixX[0, 3] = 8;
        x.MatrixX[1, 0] = 7; x.MatrixX[1, 1] = 5; x.MatrixX[1, 2] = 8; x.MatrixX[1, 3] = 5;
        x.MatrixX[2, 0] = 6; x.MatrixX[2, 1] = 8; x.MatrixX[2, 2] = 6; x.MatrixX[2, 3] = 8;
        x.MatrixX[3, 0] = 8; x.MatrixX[3, 1] = 5; x.MatrixX[3, 2] = 8; x.MatrixX[3, 3] = 7;
        x.MatrixX[4, 0] = 8; x.MatrixX[4, 1] = 5; x.MatrixX[4, 2] = 6; x.MatrixX[4, 3] = 7;
        /*
        7,00000  6,00000  5,00000  8,00000
        7,00000  5,00000  8,00000  5,00000
        6,00000  8,00000  6,00000  8,00000
        8,00000  5,00000  8,00000  7,00000
        8,00000  5,00000  6,00000  7,00000
        */


        //random matrix generation
        /*
        Random rnd = new Random();
        for (int i = 0; i < x.Row; i++)
            for (int j = 0; j < x.Col; j++)
                x.MatrixX[i, j] = rnd.Next(5, 10);
         */


        /*i'm going to calculate: (  X^(T)  *  X  )^(-1)
         * 1. transpose X
         * 2. multiply X and (1)
         * 3. invert matrix (2)
         * +4. i wanna check the results: Multilate of (2) and (3) = Identity_matrix.
         * */
        Matrix.Display(x);

        //1
        Matrix xt = Matrix.Transpose(x);
        Matrix.Display(xt);

        //2
        Matrix xxt = Matrix.Multiply(x, xt);
        Matrix.Display(xxt);

        //3
        Matrix xxtinv = Matrix.Invert(Matrix.Multiply(x, xt));
        Matrix.Display(xxtinv);

        //4
        Console.WriteLine("Invert(xxt) * xxt. IdentityMatrix:");
        Matrix IdentityMatrix = Matrix.Multiply(xxtinv, xxt);
        Matrix.Display(IdentityMatrix);

        Console.ReadKey();
    }
static void Main(字符串[]args)
{
矩阵x=新矩阵(5,4);
//矩阵温度=新矩阵(x行,x列);
//双精度[]y=新双精度[x.行];
//double[]b=新的double[x.Row];
//此数据计算不正确。用于调试
x、 MatrixX[0,0]=7;x.MatrixX[0,1]=6;x.MatrixX[0,2]=5;x.MatrixX[0,3]=8;
x、 MatrixX[1,0]=7;x.MatrixX[1,1]=5;x.MatrixX[1,2]=8;x.MatrixX[1,3]=5;
x、 MatrixX[2,0]=6;x.MatrixX[2,1]=8;x.MatrixX[2,2]=6;x.MatrixX[2,3]=8;
x、 MatrixX[3,0]=8;x.MatrixX[3,1]=5;x.MatrixX[3,2]=8;x.MatrixX[3,3]=7;
x、 MatrixX[4,0]=8;x.MatrixX[4,1]=5;x.MatrixX[4,2]=6;x.MatrixX[4,3]=7;
/*
7,00000  6,00000  5,00000  8,00000
7,00000  5,00000  8,00000  5,00000
6,00000  8,00000  6,00000  8,00000
8,00000  5,00000  8,00000  7,00000
8,00000  5,00000  6,00000  7,00000
*/
//随机矩阵生成
/*
随机rnd=新随机();
对于(int i=0;i
下面是具有所有功能的矩阵.cs

public class Matrix
    {
        private double[,] matrix;
        private int row;
        private int col;

        #region constructors
        public Matrix(int Row, int Col)
        {
            this.row = Row;
            this.col = Col;
            matrix = new double[Row, Col];
        }

        public Matrix()
        {
            Random rnd = new Random();
            Row = rnd.Next(3, 7);
            Col = rnd.Next(3, 7);
            matrix = new double[Row, Col];
            for (int i = 0; i < Row; i++)
                for (int j = 0; j < Col; j++)
                    matrix[i, j] = rnd.Next(5, 10);
        }

        public Matrix(Matrix a)
        {
            this.Col = a.Col;
            this.Row = a.Row;
            this.matrix = a.matrix;
        }
        #endregion

        #region properties
        public int Col
        {
            get { return col; }
            set { col = value; }
        }
        public int Row
        {
            get { return row; }
            set { row = value; }
        }
        public double[,] MatrixX
        {
            get { return matrix; }
            set { matrix = value; }
        }
        #endregion

        static public Matrix Transpose(Matrix array)
        {
            Matrix temp = new Matrix(array.Col, array.Row);
            for (int i = 0; i < array.Row; i++)
                for (int j = 0; j < array.Col; j++)
                    temp.matrix[j, i] = array.matrix[i, j];
            return temp;
        }

        static public void Display(Matrix array)
        {
            for (int i = 0; i < array.Row; i++)
            {
                for (int j = 0; j < array.Col; j++)
                    Console.Write("{0,5:f2}\t", array.matrix[i, j]);
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        static public Matrix Multiply(Matrix a, Matrix b)
        {
            if (a.Col != b.Row) throw new Exception("multiplication is impossible: a.Col != b.Row");

            Matrix r = new Matrix(a.Row, b.Col);
            for (int i = 0; i < a.Row; i++)
            {
                for (int j = 0; j < b.Col; j++)
                {
                    double sum = 0;
                    for (int k = 0; k < b.Row; k++)
                        sum += a.matrix[i, k] * b.matrix[k, j];
                    r.matrix[i, j] = sum;
                }
            }
            return r;
        }

        static public Matrix Invert(Matrix a)
        {
            Matrix E = new Matrix(a.Row, a.Col);
            double temp = 0;
            int n = a.Row;


            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    E.matrix[i, j] = 0.0;

                    if (i == j)
                        E.matrix[i, j] = 1.0;
                }

            for (int k = 0; k < n; k++)
            {
                temp = a.matrix[k, k];

                for (int j = 0; j < n; j++)
                {
                    a.matrix[k, j] /= temp;
                    E.matrix[k, j] /= temp;
                }

                for (int i = k + 1; i < n; i++)
                {
                    temp = a.matrix[i, k];

                    for (int j = 0; j < n; j++)
                    {
                        a.matrix[i, j] -= a.matrix[k, j] * temp;
                        E.matrix[i, j] -= E.matrix[k, j] * temp;
                    }
                }
            }

            for (int k = n - 1; k > 0; k--)
            {
                for (int i = k - 1; i >= 0; i--)
                {
                    temp = a.matrix[i, k];

                    for (int j = 0; j < n; j++)
                    {
                        a.matrix[i, j] -= a.matrix[k, j] * temp;
                        E.matrix[i, j] -= E.matrix[k, j] * temp;
                    }
                }
            }


            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    a.matrix[i, j] = E.matrix[i, j];
                }

            return a;
        }
    }
公共类矩阵
{
私有双[,]矩阵;
私人int row;
私人国际学院;
#区域构造函数
公共矩阵(整数行,整数列)
{
this.row=行;
this.col=col;
矩阵=新的双精度[行,列];
}
公共矩阵()
{
随机rnd=新随机();
Row=rnd.Next(3,7);
Col=rnd.Next(3,7);
矩阵=新的双精度[行,列];
对于(int i=0;i