随笔 - 30  文章 - 3  评论 - 5  阅读 - 8572

矩阵运算——矩阵加法+乘法+转置+求逆

矩阵运算——矩阵加法+乘法+转置+求逆__控制台程序

double[,] matrix;

        public int row = 0, col = 0;

        //定义三个不同情况下的构造函数

        public Matrix() { }

        public Matrix(int row)

        {

            matrix = new double[row, row];

        }

        public Matrix(int row, int col)

        {

            this.row = row;

            this.col = col;

            matrix = new double[row, col];

        }

        //复制构造函数  

        public Matrix(Matrix m)

        {

            int row = m.row;

            int col = m.col;

            matrix = new double[row, col];

            for (int i = 0; i < row; i++)

            {

                for (int j = 0; j < col; j++)

                {

                    matrix[i, j] = m.getNum(i, j);

                }

            }

        }

        //输入相应的值,对矩阵进行设置

        public void SetNum(int i, int j, double num)

        {

            matrix[i, j] = num;

        }

        //得到相应的矩阵某个数  

        public double getNum(int i, int j)

        {

            return matrix[i, j];

        }

        //输出矩阵 

        public void OutputM()

        {

            Console.WriteLine("矩阵为:");

            for (int p = 0; p < row; p++)

            {

                for (int q = 0; q < col; q++)

                {

                    Console.Write("\t" + matrix[p, q]);

                }

                Console.Write("\n");

            }

        }

 

        public void InputM(int Row, int Col)

        {

            for (int a = 0; a < Row; a++)//col——Michael

            {

                for (int b = 0; b < Col; b++)

                {

                    Console.WriteLine("第{0}行,第{1}列", a + 1, b + 1);

                    double value = Convert.ToDouble(Console.ReadLine());

                    this.SetNum(a, b, value);

                }

            }

        }

 

        //得到matrix

        public double[,] Detail

        {

            get

            {

                return matrix;

            }

            set { matrix = value; }

        }

        //矩阵转置实现 

        public Matrix Transpose()

        {

            Matrix another = new Matrix(row, col);

            for (int i = 0; i < row; i++)

            {

                for (int j = 0; j < col; j++)

                {

                    another.SetNum(j, i, matrix[i, j]);

                }

            }

            return another;

        }

        /// <summary>

        /// 矩阵相乘

        /// </summary>

        /// <param name="X">第一个矩阵</param>

        /// <param name="Y">第二个矩阵</param>

        /// <returns></returns>

        public double[,] matrixMulti(double[,] X, double[,] Y)

        {

            double[,] A = X; double[,] B = Y;

            double[,] C = new double[A.GetLength(0), B.GetLength(1)];

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

                for (int j = 0; j < B.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];

                    }

                }

            return C;

        }

        //矩阵相乘

        public static Matrix Multiply(Matrix lm, Matrix rm)

        {

            Matrix another = new Matrix(lm.row, rm.col);

            if (lm.col == rm.row)

            {

                for (int i = 0; i < lm.row; i++)

                {

                    for (int j = 0; j < rm.col; j++)

                    {

                        double temp=0;

                        for (int k = 0; k < lm.col; k++)

                        {

                            temp += lm.getNum(i, k) * rm.getNum(k, j);

                        }

                        another.SetNum(i, j, temp);

                       

                    }

                }

            }

            else

            {

                Console.WriteLine("左矩阵的列数不等于右矩阵的行数,不能参与计算");

            }

            return another;

        }

        //矩阵相加实现

        public static Matrix Add(Matrix lm, Matrix rm)

        {

            //行出错  

            if (lm.row != rm.row)

            {

                System.Exception e = new Exception("相加的两个矩阵的行数不等");

                throw e;

            }

            //列出错

            if (lm.col != rm.col)

            {

                System.Exception e = new Exception("相加的两个矩阵的列数不等");

                throw e;

            }

            Matrix another = new Matrix(lm.row, lm.col);

            for (int i = 0; i < lm.row; i++)

            {

                for (int j = 0; j < lm.col; j++)

                {

                    double temp = lm.getNum(i, j) + rm.getNum(i, j);

                    another.SetNum(i, j, temp);

                }

            }

            return another;

        }

        //矩阵求逆实现

        public static Matrix Inverse(Matrix M)

        {

            int m = M.row;

            int n = M.col;

            if (m != n)

            {

                Exception myException = new Exception("求逆的矩阵不是方阵");

                throw myException;

            }

            Matrix ret = new Matrix(m, n);

            double[,] a0 = M.Detail;

            double[,] a = (double[,])a0.Clone();

            double[,] b = ret.Detail;

            int i, j, row, k; double max, temp;

            //单位矩阵 

            for (i = 0; i < n; i++)

            {

                b[i, i] = 1;

            }

            for (k = 0; k < n; k++)

            {

                max = 0; row = k;

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

                for (i = k; i < n; i++)

                {

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

                    if (max < temp)

                    {

                        max = temp; row = i;

                    }

                }

                if (max == 0)

                {

                    Exception myException = new Exception("该矩阵无逆矩阵");

                    throw myException;

                }

                //交换k与row行

                if (row != k)

                {

                    for (j = 0; j < n; 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 < n; j++)

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

                for (j = 0; j < n; j++)

                    b[k, j] /= a[k, k];

                a[k, k] = 1;

                //k列化为0 

                //对a 

                for (j = k + 1; j < n; j++)

                {

                    for (i = 0; i < k; i++)

                        a[i, j] -= a[i, k] * a[k, j];

                    for (i = k + 1; i < n; i++)

                        a[i, j] -= a[i, k] * a[k, j];

                }

                //对b 

                for (j = 0; j < n; j++)

                {

                    for (i = 0; i < k; i++)

                        b[i, j] -= a[i, k] * b[k, j];

                    for (i = k + 1; i < n; i++)

                        b[i, j] -= a[i, k] * b[k, j];

                }

                for (i = 0; i < n; i++)

                    a[i, k] = 0;

                a[k, k] = 1;

            }

            return ret;

        }

 

        //主函数  

        public static void Main()

        {

            int Row, Col, choice;

            Console.WriteLine("请输入想要的矩阵行数与列数");

            Row = Convert.ToInt32(Console.ReadLine());

            Col = Convert.ToInt32(Console.ReadLine());

            Matrix m = new Matrix(Row, Col);

            Console.WriteLine("输入矩阵数据");

            m.InputM(Row, Col); m.OutputM();

            do

            {

                Console.WriteLine("请选择你想要进行的运算:\n(1)转置;\n(2)输入另一矩阵并相加;\n(3)求逆;\n(4)输入另一矩阵并相乘;\n(0)停止;");

                choice = Convert.ToInt32(Console.ReadLine());

                switch (choice)

                {

                    case 1:

                        Matrix n = m.Transpose();

                        n.OutputM();

                        break;

                    case 2:

                        Console.WriteLine("请输入第二个矩阵行数与列数");

                        Row = Convert.ToInt32(Console.ReadLine());

                        Col = Convert.ToInt32(Console.ReadLine());

                        Matrix m2 = new Matrix(Row, Col);

                        Console.WriteLine("输入矩阵数据");

                        m2.InputM(Row, Col);

                        Console.WriteLine("第二个矩阵为:");

                        m2.OutputM();

                        Matrix result = Add(m, m2);

                        result.OutputM();

                        break;

                    case 3:

                        Matrix m3 = Inverse(m);

                        m3.OutputM();

                        break;

                    case 4:

                        Console.WriteLine("请输入第二个矩阵行数与列数");

                        Row = Convert.ToInt32(Console.ReadLine());

                        Col = Convert.ToInt32(Console.ReadLine());

                        Matrix m4 = new Matrix(Row, Col);

                        Console.WriteLine("输入矩阵数据");

                        m4.InputM(Row, Col);

                        Console.WriteLine("第二个矩阵为:");

                        m4.OutputM();

                        Matrix result2 = Multiply(m, m4);

                        result2.OutputM();

                        break;

                    case 0:

                        break;

                }

            }

            while (choice != 0);

            Console.Write("请按任意键继续");

            Console.ReadLine();

        }

posted on   心有猛虎细嗅蔷薇  阅读(410)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示