C#有关矩阵运算代码

个人日记

网上有些利用类进行运算的代码,这里直接用数组

 

        /// <summary>

        /// 矩阵相乘

        /// </summary>

        /// <param name="a"></param>

        /// <param name="b"></param>

        /// <returns></returns>

        public double[,] zucheng(double[,] a, double[,] b)

        {

//定义一个以a行数为行数,b列数为列数的数组

            double[,] c = new double[a.GetLength(0), b.GetLength(1)];

            if (a.GetLength(1) != b.GetLength(0))

            {

                MessageBox.Show("数组维数不匹配");

            }

            for (int i = 0; i < c.GetLength(0); i++)

            {

                for (int j = 0; j < c.GetLength(1); j++)

                    {

                        c[i, j] = 0;

                        for (int k = 0; k < a.GetLength(1); k++)

                        {

                            c[i, j] += a[i, k] * b[k, j];          //i行j列元素结果

                        }

                    }

             }

                return c;

        }

        /// <summary>

        /// 矩阵转置

        /// </summary>

        /// <param name="a"></param>

        /// <returns></returns>

        public static double[,] zuzhuan(double[,] a)

        {

            double[,] b = new double[a.GetLength(1), a.GetLength(0)];

            for (int i = 0; i < a.GetLength(1); i++)

            {

                for (int j = 0; j < a.GetLength(0); j++)

                {

                    b[i, j] = a[j, i];

                }

            }

            return b;

        }

        /// <summary>

        /// 矩阵求逆

        /// </summary>

        /// <param name="a"></param>

        /// <returns></returns>

        public static double[,] zuni(double[,] a)

        {

            double[,] b=new double[a.GetLength(0),a.GetLength(1)];

            if (a.GetLength(0) != a.GetLength(1))

            {

                MessageBox.Show("数组行列数不相等!");

            }

            int i, j, row, k;

            double max, temp;

 

            //单位矩阵

            for (i = 0; i < a.GetLength(1); i++)

            {

                b[i, i] = 1;

            }

            for (k = 0; k < a.GetLength(1); k++)

            {

                max = 0; row = k;

                //找最大元,其所在行为row

                for (i = k; i < a.GetLength(1); i++)

                {

                    temp = Math.Abs(a[i, k]);

                    if (max < temp)

                    {

                        max = temp;

                        row = i;

                    }

                }

                if (max == 0)

                {

                    MessageBox.Show("没有逆矩阵");

                }

                //交换k与row行

                if (row != k)

                {

                    for (j = 0; j < a.GetLength(0); j++)

                    {

                        temp = a[row, j];

                        a[row, j] = a[k, j];

                        a[k, j] = temp;

 

                        temp = b[row, j];

                        b[row, j] = b[k, j];

                        b[k, j] = temp;

                    }

 

                }

 

                //首元化为1

                for (j = k + 1; j < a.GetLength(0); j++) a[k, j] /= a[k, k];

                for (j = 0; j < a.GetLength(0); j++) b[k, j] /= a[k, k];

 

                a[k, k] = 1;

 

                //k列化为0

                //对a

                for (j = k + 1; j < a.GetLength(0); j++)

                {

                    for (i = 0; i < k; i++) a[i, j] -= a[i, k] * a[k, j];

                    for (i = k + 1; i < a.GetLength(0); i++) a[i, j] -= a[i, k] * a[k, j];

                }

                //对b

                for (j = 0; j < a.GetLength(0); j++)

                {

                    for (i = 0; i < k; i++) b[i, j] -= a[i, k] * b[k, j];

                    for (i = k + 1; i < a.GetLength(0); i++) b[i, j] -= a[i, k] * b[k, j];

                }

                for (i = 0; i < a.GetLength(0); i++) a[i, k] = 0;

                a[k, k] = 1;

            }

            return b;

        }

 


文章评论