首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#兑现矩阵的转置,相乘等

2012-11-25 
c#实现矩阵的转置,相乘等比较简单,适合 初学者题目:定义矩阵类,完成矩阵的产生,转置和相乘(控制台程序)步

c#实现矩阵的转置,相乘等

比较简单,适合 初学者

题目:

定义矩阵类,完成矩阵的产生,转置和相乘(控制台程序)

步骤:

1.可以新创建一个控制台程序,也可在原工程中添加新类

右键工程名

ADD->new items->class,名称为Matrix

具体的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassTest
{
    class Matrix
    {
        private double[,] matrix;//一个二维数组,保存矩阵中的值
        private int col;//矩阵的列数
        private int row;//矩阵的行数
        private string name;//矩阵的名字
        //构造函数
        public Matrix()
        {

        }
        public Matrix(int row,int col)
        {
            this.col = col;
            this.row = row;
            this.name = "";
            this.matrix = new double[row, col];
        }
        public Matrix(int row,int col,string name)
        {
            this.col = col;
            this.row = row;
            this.name=name;
            this.matrix=new double[row,col];
        }
        public Matrix(int row, int col, double[] data)
        {
            this.col = col;
            this.row = row;
            this.name = "";
            this.matrix = new double[row, col];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    this.matrix[i,j] = data[i * col + j];
                }
            }
        }
        public Matrix(int row, int col,string name, double[] data)//用一个数组中的值为矩阵赋值
        {
            this.col = col;
            this.row = row;
            this.name = name;
            this.matrix = new double[row, col];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    this.matrix[i, j] = data[i * col + j];
                }
            }
        }
       
        public int Column
        {
            get
            {
                return col;
            }
            set
            {
                this.col = value;
            }
        }
        public int Rows
        {
            get
            {
                return row;
            }
            set
            {
                this.row = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                this.name = value;
            }
        }
        //输出矩阵
        public void Show()
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    Console.Write(matrix[i, j].ToString() + "  ");
                }
                Console.Write("\n");
            }
        }
        //对矩阵的某一位置赋值
        public void SetValue(double v)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); i++)
                {
                    matrix[i, j] = v;
                }
            }
        }
        //初等变换:对调任意两行
        public Matrix Exchange(int i,int j)
        {
            double temp;
            for (int k = 0; k < col; k++)
            {
                temp = matrix[i, k];
                matrix[i, k] = matrix[j, k];
                matrix[j, k] = temp;
            }
            return this;
        }
        //初等变换:矩阵第i行乘以常数
        public Matrix MulCount(int i,double count)
        {
            for (int j = 0; j < col; j++)
            {
                this.matrix[i, j] *= count;
            }
            return this;
        }
        //转置
        public Matrix Transpose()
        {
            int rows = this.col;
            int cols = this.row;
            Matrix trans = new Matrix(rows, cols);
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    trans.matrix[i,j] = this.matrix[j,i];
                }
            }
            return trans;
        }
        //矩阵相加
        public static Matrix operator +(Matrix m1, Matrix m2)
        {
            if (m1.row != m2.row || m1.col != m2.col)
            {
                System.Exception e = new Exception("两矩阵的行数或列数不相等");
                throw e;
            }
            Matrix mat = new Matrix(m1.row, m2.col);
            for (int i = 0; i < m1.row; i++)
            {
                for (int j = 0; j < m1.col; j++)
                {
                    mat.matrix[i, j] = m1.matrix[i, j] + m2.matrix[i, j];
                }
            }
            return mat;
        }
        public static Matrix operator -(Matrix m1, Matrix m2)
        {
            if (m1.row != m2.row || m1.col != m2.col)
            {
                System.Exception e = new Exception("两矩阵的行数或列数不相等");
                throw e;
            }
            Matrix mat = new Matrix(m1.row, m2.col);
            for (int i = 0; i < m1.row; i++)
            {
                for (int j = 0; j < m1.col; j++)
                {
                    mat.matrix[i, j] = m1.matrix[i, j] - m2.matrix[i, j];
                }
            }
            return mat;
        }
        public static Matrix operator *(Matrix m1, Matrix m2)
        {
            if (m1.col!=m2.row)
            {
                System.Exception e = new Exception("两矩阵的行列数不相等");
                throw e;
            }
            Matrix mat = new Matrix(m1.row, m2.col);
            for (int i = 0; i < m1.row; i++)
            {
                for (int j = 0; j < m2.col; j++)
                {
                    double temp=0;
                    for (int k = 0; k < m1.col; k++)
                    {
                        temp += m1.matrix[i, k] * m2.matrix[k, j];
                    }
                    mat.matrix[i, j] = temp;
                }
            }
            return mat;
        }
    }
}

然后在program.cs中调用矩阵类,进行测试,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassTest
{
    class Program
    {
        static void Main(string[] args)
        {
             Matrix mA=new Matrix();
            Matrix mB=new Matrix();
            for (int t = 0; t < 2; t++)
            {
                string name;
                if (t == 0) name = "矩阵A";
                else name = "矩阵B";
                int r, c;
                double[] data = new double[10000];
                Console.WriteLine("请输入"+name+"的行数:");
                r = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入" + name + "的列数:");
                c = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入"+name+"的数值(请按行列式的形式输入):");
                for (int i = 0; i < r; i++)
                {
                    string input = Console.ReadLine();
                    string[] str = new string[10000];
                    str = input.Split(' ');
                    for (int j = 0; j < c; j++)
                    {
                        data[i * c + j] = double.Parse(str[j]);
                    }
                }
                if (t == 0)
                {
                    mA= new Matrix(r, c, name, data);
                    Matrix mA2=mA.Transpose();
                    Console.WriteLine("矩阵A转置后变成:");
                    mA2.Show();
                }
                else
                {
                    mB = new Matrix(r, c, name, data);
                    Matrix mB2 = mB.Transpose();
                    Console.WriteLine("矩阵B转置后变成:");
                    mB2.Show();
                } 
            }
            Matrix mAB = mA * mB;
            Console.WriteLine("矩阵A,B相乘的结果:");
            mAB.Show();
        }
    }
}

ctrl+F5运行,如图:
c#兑现矩阵的转置,相乘等

 

具体工程在我的资源中,可下载。

 

 

热点排行