Exemplo n.º 1
0
        public static bool Inverse(float[,] A, out float[,] invA)
        {
            if (A.GetLength(0) != A.GetLength(1))
            {
                invA = null;
                return(false);
            }
            int length = A.GetLength(0);

            invA = new float[length, length];
            Buffer.BlockCopy(A, 0, invA, 0, A.Length * 4);
            int[]  array  = new int[length];
            int[]  array2 = new int[length];
            bool[] array3 = new bool[length];
            int    num    = 0;
            int    num2   = 0;

            for (int i = 0; i < length; i++)
            {
                float num3 = 0f;
                for (int j = 0; j < length; j++)
                {
                    if (!array3[j])
                    {
                        for (int k = 0; k < length; k++)
                        {
                            if (!array3[k])
                            {
                                float num4 = Mathf.Abs(invA[j, k]);
                                if (num4 > num3)
                                {
                                    num3 = num4;
                                    num  = j;
                                    num2 = k;
                                }
                            }
                        }
                    }
                }
                if (num3 == 0f)
                {
                    invA = null;
                    return(false);
                }
                array3[num2] = true;
                if (num != num2)
                {
                    LinearSystem.SwapRows(invA, num, num2, length);
                }
                array2[i] = num;
                array[i]  = num2;
                float num5 = 1f / invA[num2, num2];
                invA[num2, num2] = 1f;
                for (int k = 0; k < length; k++)
                {
                    invA[num2, k] *= num5;
                }
                for (int j = 0; j < length; j++)
                {
                    if (j != num2)
                    {
                        float num6 = invA[j, num2];
                        invA[j, num2] = 0f;
                        for (int k = 0; k < length; k++)
                        {
                            invA[j, k] -= invA[num2, k] * num6;
                        }
                    }
                }
            }
            for (int j = length - 1; j >= 0; j--)
            {
                if (array2[j] != array[j])
                {
                    for (int k = 0; k < length; k++)
                    {
                        float num6 = invA[k, array2[j]];
                        invA[k, array2[j]] = invA[k, array[j]];
                        invA[k, array[j]]  = num6;
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public static bool Solve(float[,] A, float[] B, out float[] X)
        {
            if (A.GetLength(0) != A.GetLength(1) || A.GetLength(0) != B.Length)
            {
                X = null;
                return(false);
            }
            int length = A.GetLength(1);

            float[,] array = new float[A.GetLength(0), A.GetLength(1)];
            Buffer.BlockCopy(A, 0, array, 0, A.Length * 4);
            X = new float[length];
            Buffer.BlockCopy(B, 0, X, 0, length * 4);
            int[]  array2 = new int[length];
            int[]  array3 = new int[length];
            bool[] array4 = new bool[length];
            int    num    = 0;
            int    num2   = 0;

            for (int i = 0; i < length; i++)
            {
                float num3 = 0f;
                for (int j = 0; j < length; j++)
                {
                    if (!array4[j])
                    {
                        for (int k = 0; k < length; k++)
                        {
                            if (!array4[k])
                            {
                                float num4 = Mathf.Abs(array[j, k]);
                                if (num4 > num3)
                                {
                                    num3 = num4;
                                    num  = j;
                                    num2 = k;
                                }
                            }
                        }
                    }
                }
                if (num3 == 0f)
                {
                    X = null;
                    return(false);
                }
                array4[num2] = true;
                if (num != num2)
                {
                    LinearSystem.SwapRows(array, num, num2, length);
                    float num5 = X[num];
                    X[num]  = X[num2];
                    X[num2] = num5;
                }
                array3[i] = num;
                array2[i] = num2;
                float num6 = 1f / array[num2, num2];
                array[num2, num2] = 1f;
                for (int k = 0; k < length; k++)
                {
                    array[num2, k] *= num6;
                }
                X[num2] *= num6;
                for (int j = 0; j < length; j++)
                {
                    if (j != num2)
                    {
                        float num5 = array[j, num2];
                        array[j, num2] = 0f;
                        for (int k = 0; k < length; k++)
                        {
                            array[j, k] -= array[num2, k] * num5;
                        }
                        X[j] -= X[num2] * num5;
                    }
                }
            }
            return(true);
        }