Beispiel #1
0
        /// <summary>
        /// 二维矩阵乘法运算,矩阵相乘
        /// </summary>
        /// <param name="martix1"></param>
        /// <param name="martix2"></param>
        /// <returns></returns>
        public static Martix operator *(Martix martix1, Martix martix2)
        {
            double[,] array = new double[martix1.Rows, martix2.Columns];
            Martix result = new Martix(array);

            try
            {
                if (martix1.Columns == martix2.Rows)
                {
                    for (int row1 = 0; row1 < martix1.Rows; row1++)
                    {
                        int row2 = 0;
                        for (int column2 = 0; column2 < martix2.Columns; column2++)
                        {
                            double Sum = 0;
                            for (int column1 = 0; column1 < martix1.Columns; column1++)
                            {
                                Sum += martix1[row1, column1] * martix2[column1, row2];
                            }
                            result[row1, column2] = Sum;
                            row2++;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("矩阵乘法运算两个矩阵行列不匹配!");
                }
            }
            catch
            {
                MessageBox.Show("矩阵乘法运算错误!");
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// 二维矩阵减法运算
        /// </summary>
        /// <param name="martix1"></param>
        /// <param name="martix2"></param>
        /// <returns></returns>
        public static Martix operator -(Martix martix1, Martix martix2)
        {
            double[,] array = new double[martix1.Rows, martix1.Columns];
            Martix result = new Martix(array);

            try
            {
                if (martix1.Rows == martix2.Rows && martix1.Columns == martix2.Columns)
                {
                    for (int i = 0; i < martix1.Rows; i++)
                    {
                        for (int j = 0; j < martix1.Columns; j++)
                        {
                            result[i, j] = martix1[i, j] - martix2[i, j];
                        }
                    }
                }
            }
            catch
            {
                if (martix1.Rows != martix2.Rows)
                {
                    MessageBox.Show("减法运算行数不匹配!");
                }
                if (martix1.Columns != martix2.Columns)
                {
                    MessageBox.Show("减法运算列数不匹配!");
                }
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// 求解七参数
        /// </summary>
        /// <param name="knownPoints">已知点构成的点集</param>
        /// <param name="B"></param>
        /// <param name="N"></param>
        /// <returns>七参数构成的数组</returns>
        public static double[,] ComputeSevenParameter(List <Point> knownPoints, out double[,] B, out double[,] N)
        {
            Martix tempB = new Martix(GetB(knownPoints));
            Martix l     = new Martix(Getl(knownPoints));

            Martix tempN = tempB.Transpose() * tempB;
            Martix W     = tempB.Transpose() * l;
            Martix x     = tempN.Inverse(tempN.Element) * W;
            Martix V     = tempB * x - l;

            string str = FileHandle.ArrayToStr(tempN.Element);

            B = tempB.Element;
            N = tempN.Element;

            return(x.Element);
        }
Beispiel #4
0
        /// <summary>
        /// 计算方阵伴随矩阵
        /// </summary>
        /// <param name="martix"></param>
        /// <returns></returns>
        private Martix complement(Martix martix)
        {
            double[,] array = new double[martix.Rows, martix.Columns];
            Martix result = new Martix(array);

            try
            {
                if (martix.Rows == martix.Columns)//方阵
                {
                    for (int i = 0; i < martix.Rows; i++)
                    {
                        for (int j = 0; j < martix.Columns; j++)
                        {
                            //生成aij的余子式矩阵
                            double[,] complement = new double[martix.Rows - 1, martix.Columns - 1]; //n-1阶
                            Martix martix1 = new Martix(complement);                                //aij的余子式矩阵
                            int    row     = 0;
                            for (int k = 0; k < martix.Rows; k++)
                            {
                                int column = 0;
                                if (k == i)//去除第i行
                                {
                                    continue;
                                }
                                for (int l = 0; l < martix.Rows; l++)
                                {
                                    if (l == j)//去除第j列
                                    {
                                        continue;
                                    }
                                    martix1[row, column++] = martix[k, l];
                                }
                                row++;
                            }
                            result[i, j] = Math.Pow(-1, i + j) * Determinant(martix1);
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("非方阵无法求伴随矩阵!");
            }
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// 递归计算方阵的行列式,代数余子式法
        /// </summary>
        /// <param name="martix"></param>
        /// <returns></returns>
        public double Determinant(Martix martix)
        {
            //注:运用此方法求解行列式,有时结果错误。下例:det(test1)求解结果正确,但det(test2)结果错误。原因不明。
            //double[,] test1 = { { 1, 2, 3, 5,1,2,5 }, { 3, 4, 5, 9,5,9,7 }, { 8, 6, 9, 10,10,25,36 }, { 12, 23, 15, 20 ,15,20,12} ,
            //    { 12,14,15,52,36,25,14},{ 48,56,19,47,23,14,15},{ 42,20,23,12,14,15,26} };
            //double[,] test2 = {
            //    { 6, 0, 0, 0, -24457908.6589, 26916348.4769, -11760075.3881},
            //    { 0,6 ,0,24457908.6589,0,11760075.3881,26916348.4769},
            //    { 0,0,6 ,-26916348.4769,-11760075.3881,0,24457908.6589},
            //    { 0,24457908.6589,-26916348.476900,22044684068435,52756432634080.4,47937688228728.8,0},
            //    { -24457908.6589,0,-11760075.388100,52756432634080.4,122748456455284 ,-109719457014783 ,0},
            //    { 26916348.4769,11760075.3881,0,47937688228728.8,-109719457014783 ,143798427582131 ,0},
            //    { -11760075.3881,26916348.4769,24457908.6589,0,0,0,243496862360883 } };


            double sum = 0;

            try
            {
                int sign = 1;
                if (martix.Rows == 1)//递归出口
                {
                    return(martix[0, 0]);
                }
                for (int i = 0; i < martix.Rows; i++)
                {
                    double[,] _tempmatrix = new double[martix.Rows - 1, martix.Columns - 1];
                    Martix tempmatrix = new Martix(_tempmatrix);
                    for (int j = 0; j < martix.Rows - 1; j++)
                    {
                        for (int k = 0; k < martix.Columns - 1; k++)
                        {
                            tempmatrix[j, k] = martix[j + 1, k >= i ? k + 1 : k];
                        }
                    }
                    sum += sign * martix[0, i] * Determinant(tempmatrix);//递归,调用函数自身
                    sign = sign * (-1);
                }
            }
            catch
            {
                MessageBox.Show("矩阵求行列式错误!");
            }
            return(sum);
        }
Beispiel #6
0
        /// <summary>
        /// 二维矩阵转置运算
        /// </summary>
        /// <returns></returns>
        public Martix Transpose()
        {
            double[,] array = new double[Element.GetLength(1), Element.GetLength(0)];
            Martix result = new Martix(array);

            try
            {
                for (int i = 0; i < Element.GetLength(0); i++)
                {
                    for (int j = 0; j < Element.GetLength(1); j++)
                    {
                        result[j, i] = Element[i, j];
                    }
                }
            }
            catch
            {
                MessageBox.Show("矩阵转置错误!");
            }
            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// 二维矩阵乘法运算,矩阵右乘一个数
        /// </summary>
        /// <param name="num"></param>
        /// <param name="martix1"></param>
        /// <returns></returns>
        public static Martix operator *(int num, Martix martix1)
        {
            double[,] array = new double[martix1.Rows, martix1.Columns];
            Martix result = new Martix(array);

            try
            {
                for (int i = 0; i < martix1.Rows; i++)
                {
                    for (int j = 0; j < martix1.Columns; j++)
                    {
                        result[i, j] = martix1[i, j] * num;
                    }
                }
            }
            catch
            {
                MessageBox.Show("矩阵右乘一个数错误!");
            }
            return(result);
        }
Beispiel #8
0
        /// <summary>
        /// 二维矩阵求逆运算
        /// </summary>
        /// <returns></returns>
        public Martix Inverse()
        {
            double[,] array = new double[Element.GetLength(1), Element.GetLength(0)];
            Martix result = new Martix(array);

            try
            {
                if (Element.GetLength(0) == Element.GetLength(1))//方阵
                {
                    Martix martix = new Martix(Element);
                    if (Determinant(martix) != 0)
                    {
                        if (martix.Rows > 1)
                        {
                            result = complement(martix).Transpose() * (1 / Determinant(martix));
                        }
                        else
                        {
                            result.Element[0, 0] = 1 / martix[0, 0];
                        }
                    }
                    else
                    {
                        MessageBox.Show("矩阵行列式为0!");
                    }
                }
                else
                {
                    MessageBox.Show("非方阵矩阵无法求逆!");
                }
            }
            catch
            {
                MessageBox.Show("矩阵求逆错误!");
            }
            return(result);
        }