C#下的矩阵算法类


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

namespace MyMath.Matrix
{
    public class Matrix
    {
        private double[,] MatrixData;
        private int row;
        private double module;

        public double Module
        {
            get { return module; }
            set { module = value; }
        }

        public int Row
        {
            get { return row; }
            //set { row = value; }
        }
        private int column;

        public int Column
        {
            get { return column; }
            //set { column = value; }
        }
        /// <summary>
        /// 构造方阵
        /// </summary>
        /// <param name="Row"></param>
        public Matrix(int Row)
        {
            if (Row > 0)
            {
                this.row = Row;
                this.column = Row;
                this.MatrixData = new double[Row, column];
            }
            else
            {
                throw new Exception("矩阵大小不能为负");
            }

        }
        /// <summary>
        /// 构造矩阵
        /// </summary>
        /// <param name="Row"></param>
        public Matrix(int Row, int Column)
        {
            if (Row > 0 && Column > 0)
            {
                this.row = Row;
                this.column = Column;
                this.MatrixData = new double[Row, Column];
            }
            else
            {
                throw new Exception("矩阵大小不能为负");
            }
        }
        /// <summary>
        /// 复制矩阵
        /// </summary>
        /// <param name="m">要复制的矩阵</param>
        public Matrix(Matrix m)
        {
            this.row = m.Row;
            this.column = m.Column;
            this.MatrixData = new double[row, column];
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    this.MatrixData[i, j] = m[i, j];
                }
            }
        }
        /// <summary>
        /// 获取和设置某行某列的值
        /// </summary>
        /// <param name="Row">行</param>
        /// <param name="Column">列</param>
        /// <returns>对应的值</returns>
        public double this[int Row, int Column]
        {
            get
            {
                return this.MatrixData[Row, Column];
            }
            set
            {
                this.MatrixData[Row, Column] = Convert.ToDouble(value);
            }
        }
        /// <summary>
        /// 将该矩阵设置为单位阵
        /// </summary>
        public void SetUnit()
        {
            if (MatrixData != null)
            {
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < column; j++)
                    {
                        if (i == j)
                        {
                            this.MatrixData[i, j] = 1;
                        }
                        else
                        {
                            this.MatrixData[i, j] = 0;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("该矩阵对象尚未初始化,请初始化后再使用本方法");
            }
        }
        /// <summary>
        /// 矩阵初等变换,调换两行的值
        /// </summary>
        /// <param name="row1"></param>
        /// <param name="row2"></param>
        public Matrix RowExchange(int row1, int row2)
        {
            if (row1 < this.row && row2 < this.row && row1 > 0 && row2 > 0)
            {
                double temp = 0;
                for (int i = 0; i < this.column; i++)
                {
                    temp = this.MatrixData[row1, i];
                    this.MatrixData[row1, i] = this.MatrixData[row2, i];
                    this.MatrixData[row2, i] = temp;
                }
                return this;
            }
            else
            {
                throw new Exception("给定索引超出范围");
            }
        }
        /// <summary>
        /// 矩阵初等变换,调换两列的值
        /// </summary>
        /// <param name="column1"></param>
        /// <param name="column2"></param>
        //public Matrix RowExchange(int column1, int column2)
        //{
        //    if (column1 < this.row && column2 < this.row && column1 > 0 && column2 > 0)
        //    {
        //        double temp = 0;
        //        for (int i = 0; i < this.row; i++)
        //        {
        //            temp = this.MatrixData[i, column1];
        //            this.MatrixData[i, column1] = this.MatrixData[column2, column2];
        //            this.MatrixData[i, column2] = temp;
        //        }
        //        return this;
        //    }
        //    else
        //    {
        //        throw new Exception("给定索引超出范围");
        //    }
        //}

        /// <summary>
        /// 矩阵初等变换,将第row行乘以mul
        /// </summary>
        /// <param name="row">行数</param>
        /// <param name="mul">乘数</param>
        /// <returns></returns>
        public Matrix RowMultiple(int row, double mul)
        {
            if (row > 0 && row < this.row)
            {
                for (int i = 0; i < column; i++)
                {
                    this.MatrixData[row, i] *= mul;
                }
                return this;
            }
            else
            {
                throw new Exception("给定的索引超出范围");
            }
        }
        /// <summary>
        /// 矩阵初等变换,将第row2行数据乘以mul加到第row1行
        /// </summary>
        /// <param name="row1"></param>
        /// <param name="row2"></param>
        /// <param name="mul"></param>
        /// <returns></returns>
        public Matrix MultipleAdd(int row1, int row2, double mul)
        {
            if (row1 > 0 && row1 < this.row && row2 < 0 && row2 > this.row)
            {
                for (int i = 0; i < this.column; i++)
                {
                    this.MatrixData[row1, i] += (this.MatrixData[row2, i] * mul);
                }
                return this;
            }
            else
            {
                throw new Exception("给定的索引超出范围");
            }
        }
        /// <summary>
        /// 求当前矩阵的转置矩阵
        /// </summary>
        /// <returns></returns>
        public Matrix Transpose()
        {
            Matrix TransposeMatrix = new Matrix(this.column, this.row);
            for (int i = 0; i < this.column; i++)
            {
                for (int j = 0; j < this.row; j++)
                {
                    TransposeMatrix[i, j] = this[j, i];
                }
            }
            return TransposeMatrix;
        }
        /// <summary>
        /// 操作符重载 +
        /// </summary>
        /// <param name="m1"></param>
        /// <param name="m2"></param>
        /// <returns></returns>
        public static Matrix operator +(Matrix m1, Matrix m2)
        {
            if (m1.Row != m2.Row || m1.Column != m2.Column)
            {
                throw new Exception("两个矩阵的大小不同,不能进行加法操作");
            }
            Matrix ResultMatrix = new Matrix(m1.Row, m1.Column);
            for (int i = 0; i < m1.Row; i++)
            {
                for (int j = 0; j < m1.Column; j++)
                {
                    ResultMatrix[i, j] = m1[i, j] + m2[i, j];
                }
            }
            return ResultMatrix;
        }
        /// <summary>
        /// 操作符重载 -
        /// </summary>
        /// <param name="m1"></param>
        /// <param name="m2"></param>
        /// <returns></returns>
        public static Matrix operator -(Matrix m1, Matrix m2)
        {
            if (m1.Row != m2.Row || m1.Column != m2.Column)
            {
                throw new Exception("两个矩阵的大小不同,不能进行减法操作");
            }
            Matrix ResultMatrix = new Matrix(m1.Row, m1.Column);
            for (int i = 0; i < m1.Row; i++)
            {
                for (int j = 0; j < m1.Column; j++)
                {
                    ResultMatrix[i, j] = m1[i, j] - m2[i, j];
                }
            }
            return ResultMatrix;
        }
        /// <summary>
        /// 操作符重载,两个矩阵相乘
        /// </summary>
        /// <param name="m1"></param>
        /// <param name="m2"></param>
        /// <returns></returns>
        public static Matrix operator *(Matrix m1, Matrix m2)
        {
            if (m1.Column != m2.Row)
            {
                throw new Exception("两矩阵行列关系不满足,不能进行矩阵乘法操作");
            }
            Matrix ResultMatrix = new Matrix(m1.Row, m2.Column);
            for (int i = 0; i < m1.Row; i++)
            {
                for (int j = 0; j < m2.Column; j++)
                {
                    for (int k = 0; k < m1.Column; k++)
                    {
                        try
                        {
                            ResultMatrix[i, j] += (m1[i, k] * m2[k, j]);
                        }
                        catch 
                        {
                            
                            throw;
                        }
                        
                        //Console.WriteLine("i-------->"+i+"   j------>"+j+"    k------->"+k);
                    }
                    //Console.WriteLine("j-------->"+j);
                }
                //Console.WriteLine("i----------->"+i);
            }
            return ResultMatrix;
        }
        /// <summary>
        /// 操作符重载,常数乘以矩阵
        /// </summary>
        /// <param name="mul"></param>
        /// <param name="m"></param>
        /// <returns></returns>
        public static Matrix operator *(double mul, Matrix m)
        {
            Matrix ResultMatrix = new Matrix(m.Row, m.Column);
            for (int i = 0; i < m.Row; i++)
            {
                for (int j = 0; j < m.Column; j++)
                {
                    ResultMatrix[i, j] = m[i, j] * mul;
                }
            }
            return ResultMatrix;
        }
        /// <summary>
        /// 操作符重载 /,矩阵除以常数
        /// </summary>
        /// <param name="m"></param>
        /// <param name="div"></param>
        /// <returns></returns>
        public static Matrix operator /(Matrix m, double div)
        {
            Matrix ResultMatrix = new Matrix(m.Row, m.Column);
            for (int i = 0; i < m.Row; i++)
            {
                for (int j = 0; j < m.Column; j++)
                {
                    ResultMatrix[i, j] = m[i, j] / div;
                }
            }
            return ResultMatrix;
        }
        /// <summary>
        /// 操作符重载,一个数除以矩阵等于乘以矩阵的逆矩阵
        /// </summary>
        /// <param name="div"></param>
        /// <param name="m"></param>
        /// <returns></returns>
        public static Matrix operator /(double div, Matrix m)
        {
            return div * m.Inverse();
        }

        /// <summary>
        /// 查找矩阵的列主元
        /// </summary>
        /// <param name="startrow">开始查找的行数</param>
        /// <param name="serchcolumn">要寻找列主元的列数</param>
        /// <returns></returns>
        public int ColumnPivot(int startrow, int serchcolumn)
        {
            int indextemp = startrow;
            double datatemp = this.MatrixData[startrow, serchcolumn];
            for (int i = startrow; i < this.row; i++)
            {
                if (this.MatrixData[i, serchcolumn] > datatemp)
                {
                    datatemp = this.MatrixData[i, serchcolumn];
                    indextemp = i;
                }
            }
            return indextemp;
        }
        /// <summary>
        /// 计算矩阵的模
        /// </summary>
        /// <returns></returns>
        public double CculateModule()
        {
            return new Determinant(this).Det;
        }
        /// <summary>
        /// 矩阵的代数余子式矩阵
        /// </summary>
        /// <returns></returns>
        public Matrix AlgebraicComplementMinorMatrix()
        {
            Matrix ResultMatrix = new Matrix(this.row, this.column);
            Determinant deter=new Determinant(this);
            for (int i = 0; i < this.row; i++)
            {
                for (int j = 0; j < this.column; j++)
                {
                    ResultMatrix[i, j] = deter.AlgebraicComplementMinor(i, j).Det*Math.Pow(-1,i+j);
                }
            }
            return ResultMatrix;
        }
        /// <summary>
        /// 计算矩阵的逆
        /// </summary>
        /// <returns></returns>
        public Matrix Inverse()
        {
            return this.AlgebraicComplementMinorMatrix() / this.CculateModule();
        }
        /// <summary>
        /// 判断是否是对称矩阵
        /// </summary>
        /// <returns></returns>
        public bool isMirror()
        {
            if (this.row!=this.column)
            {
                throw new Exception("不是方阵");
            }
            for (int i = 0; i < this.row; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (this[i,j]!=this[j,i])
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 覆写字符串函数
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string s = "";
            for (int i = 0; i < this.row; i++)
            {
                for (int j = 0; j < this.column; j++)
                {
                    s += (this[i, j].ToString() + "\t");
                }
                s += "\n";
            }
            return s;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值