示例#1
0
        private bool Invert_4x4(VdsMatrixd mat)
        {
            if (mat == this)
            {
                VdsMatrixd tm = new VdsMatrixd(mat);
                return(Invert_4x4(tm));
            }
            int[]  indxc = new int[4];
            int[]  indxr = new int[4];
            int[]  ipiv = new int[4];
            int    i, j, k, l, ll;
            int    icol = 0;
            int    irow = 0;
            double pivinv, dum, big;

            Mat = mat.Mat;
            for (j = 0; j < 4; j++)
            {
                ipiv[j] = 0;
            }
            for (i = 0; i < 4; i++)
            {
                big = 0.0;
                for (j = 0; j < 4; j++)
                {
                    if (ipiv[j] != 1)
                    {
                        for (k = 0; k < 4; k++)
                        {
                            if (ipiv[k] == 0)
                            {
                                if (Math.Abs(Mat[j, k]) >= big)
                                {
                                    big  = Math.Abs(Mat[j, k]);
                                    irow = j;
                                    icol = k;
                                }
                            }
                            else if (ipiv[k] > 1)
                            {
                                return(false);
                            }
                        }
                    }
                }
                ++(ipiv[icol]);
                if (irow != icol)
                {
                    for (l = 0; l < 4; l++)
                    {
                        StaticMethod.Swap(ref Mat[irow, l], ref Mat[icol, l]);
                    }
                }
                indxr[i] = irow;
                indxc[i] = icol;
                if (Mat[icol, icol] == 0)
                {
                    return(false);
                }
                pivinv          = 1.0 / Mat[icol, icol];
                Mat[icol, icol] = 1;
                for (l = 0; l < 4; l++)
                {
                    Mat[icol, l] *= pivinv;
                }
                for (ll = 0; ll < 4; ll++)
                {
                    if (ll != icol)
                    {
                        dum           = Mat[ll, icol];
                        Mat[ll, icol] = 0;
                        for (l = 0; l < 4; l++)
                        {
                            Mat[ll, l] -= Mat[icol, l] * dum;
                        }
                    }
                }
            }
            for (int lx = 4; lx > 0; --lx)
            {
                if (indxr[lx - 1] != indxc[lx - 1])
                {
                    for (k = 0; k < 4; k++)
                    {
                        StaticMethod.Swap(ref Mat[k, indxr[lx - 1]], ref Mat[k, indxc[lx - 1]]);
                    }
                }
            }
            return(true);
        }