⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.cs

📁 C#矩阵运算类
💻 CS
📖 第 1 页 / 共 2 页
字号:
        #endregion
        #region 矩阵扩展初等变换
        public static NRMatrix Transpose(NRMatrix mat)    //矩阵的转置
        {
            NRMatrix temp = new NRMatrix(mat.cols, mat.rows);
            for (int j = 0; j < mat.cols; j++)
                for (int i = 0; i < mat.rows; i++)
                    temp[j, i] = mat[i, j];
            return temp;
        }
        public static NRMatrix ExchangeRows(NRMatrix mat, int i, int j)   //交换矩阵的i,j两行
        {
            if (i >= mat.rows || j >= mat.rows) throw (new NRException("要交换的行标超出范围!"));
            if (i < 0 || j < 0) throw (new NRException("行标出现负值!"));
            NRMatrix result = new NRMatrix(mat);
            for (int k = 0; k < result.cols; k++)
            {
                double t = result[i, k];
                result[i, k] = result[j, k];
                result[j, k] = t;
            }
            return result;
        }
        public static NRMatrix ExchangeCols(NRMatrix mat, int i, int j) //交换矩阵的i,j两列
        {
            if (i >= mat.cols || j >= mat.cols) throw (new NRException("要交换的列标超出范围!"));
            if (i < 0 || j < 0) throw (new NRException("列标出现负值!"));
            NRMatrix result = new NRMatrix(mat);
            for (int k = 0; k < result.rows; k++)
            {
                double t = result[k, i];
                result[k, i] = result[k, j];
                result[k, j] = t;
            }
            return result;
        }
        public static NRMatrix Multiply(NRMatrix mat, int i, double coe)  //矩阵的第i行乘以coe运算
        {
            if (i >= mat.rows || i < 0) throw (new NRException("行标超出范围!"));
            NRMatrix result = new NRMatrix(mat);
            for (int k = 0; k < result.cols; k++)
                result[i, k] *= coe;
            return result;
        }
        public static NRMatrix Multiply(NRMatrix mat, double coe, int j)  //矩阵的第j列乘以coe运算
        {
            if (j >= mat.cols || j < 0) throw (new NRException("列标超出范围!"));
            NRMatrix result = new NRMatrix(mat);
            for (int k = 0; k < result.rows; k++)
                result[k, j] *= coe;
            return result;

        }
        public static NRMatrix RowAdd2Row(NRMatrix mat, int i, int j, double coe)  //第i行乘coe加到第j行上去
        {
            if (i >= mat.rows || j >= mat.rows) throw (new NRException("行标超出范围!"));
            if (i < 0 || j < 0) throw (new NRException("行标出现负值!"));
            NRMatrix result = new NRMatrix(mat);
            for (int k = 0; k < result.cols; k++)
            {
                result[j, k] += result[i, k] * coe;
            }
            return result;
        }
        public static NRMatrix ColAdd2Col(NRMatrix mat, int i, int j, double coe)      //第i列乘coe加到第j列上去
        {
            if (i >= mat.cols || j >= mat.cols) throw (new NRException("列标超出范围!"));
            if (i < 0 || j < 0) throw (new NRException("列标出现负值!"));
            NRMatrix result = new NRMatrix(mat);
            for (int k = 0; k < result.rows; k++)
            {
                result[k, j] += result[k, i] * coe;
            }
            return result;
        }
        public static NRMatrix ExtractRows(NRMatrix mat, int i)        //抽取矩阵的第i行组成新的行矩阵
        {
            if (i >= mat.rows || i < 0) throw (new NRException("抽取的行标超出矩阵的行标范围!"));
            NRMatrix result = new NRMatrix(1, mat.cols);
            for (int j = 0; j < result.cols; j++)
                result[0, j] = mat[i, j];
            return result;
        }
        public static NRMatrix ExtractRows(NRMatrix mat, int i1, int i2)  //抽取第i1行到第i2行组成新矩阵
        {
            if (i1 >= mat.rows || i2 >= mat.rows || i1 < 0 || i2 < 0) throw (new NRException("抽取的行标超出矩阵的行标范围!"));
            int min = i1, max = i2;
            if (i1 > i2) { min = i2; max = i1; }
            NRMatrix result = new NRMatrix(max - min + 1, mat.cols);
            for (int k = min; k <= max; k++)
                for (int j = 0; j < result.cols; j++)
                    result[k - min, j] = mat[k, j];
            return result;
        }
        public static NRMatrix ExtractCols(NRMatrix mat, int j)   //抽取第j列组成新的列矩阵
        {
            if (j >= mat.cols || j < 0) throw (new NRException("抽取的列标超出矩阵的列标范围!"));
            NRMatrix result = new NRMatrix(mat.rows, 1);
            for (int i = 0; i < result.rows; i++)
                result[i, 0] = mat[i, j];
            return result;
        }
        public static NRMatrix ExtractCols(NRMatrix mat, int j1, int j2)  //抽取第j1列到第j2列组成新矩阵
        {
            if (j1 >= mat.cols || j2 >= mat.cols || j1 < 0 || j2 < 0) throw (new NRException("抽取的列标超出矩阵的列标范围!"));
            int min = j1, max = j2;
            if (j1 > j2) { min = j2; max = j1; }
            NRMatrix result = new NRMatrix(mat.rows, max - min + 1);
            for (int k = min; k <= max; k++)
                for (int i = 0; i < result.rows; i++)
                    result[i, k - min] = mat[i, k];
            return result;
        }
        public static NRMatrix SubMatrix(NRMatrix mat, int[] r, int[] c)  //获取矩阵的子矩阵
        {
            if (r.Length != c.Length) throw (new NRException("抽取的行数和列数不等!"));
            int M = r.Length;
            int rmin = r.GetLowerBound(0); int rmax = r.GetUpperBound(0);
            int cmin = c.GetLowerBound(0); int cmax = c.GetUpperBound(0);
            if (rmax >= mat.rows || cmax >= mat.cols) throw (new NRException("抽取的行标列标超出矩阵范围!"));
            NRMatrix result = new NRMatrix(M, M);
            for (int i = 0; i < M; i++)
                for (int j = 0; j < M; j++)
                    result[i, j] = mat[r[i], c[j]];
            return result;
        }
        public static NRMatrix Flipud(NRMatrix mat)        //上下对称交换
        {
            NRMatrix result = new NRMatrix(mat);
            for (int i = 0; i < result.rows / 2; i++)
                result = NRMatrix.ExchangeRows(result, i, result.rows - i - 1);
            return result;
        }
        public static NRMatrix Fliplr(NRMatrix mat)        //左右对称交换
        {
            NRMatrix result = new NRMatrix(mat);
            for (int j = 0; j < result.cols / 2; j++)
                result = NRMatrix.ExchangeCols(result, j, result.cols - j - 1);
            return result;
        }
        public static NRMatrix Rot90(NRMatrix mat)        //矩阵逆时针旋转90度
        {
            NRMatrix result = NRMatrix.Transpose(mat);
            result = NRMatrix.Flipud(mat);
            return result;
        }
        public static NRMatrix JointMatrix(NRMatrix A, NRMatrix B, string str) //矩阵A,B进行拼接;
        { //str=“Down”矩阵B接在下方;str=“Right”矩阵B接在右方;
            if (str == "Down" && A.cols == B.cols)
            {
                NRMatrix temp = new NRMatrix(A.rows + B.rows, A.cols);
                for (int i = 0; i < A.rows; i++)
                    for (int j = 0; j < A.cols; j++)
                        temp[i, j] = A[i, j];
                for (int i = A.rows; i < temp.rows; i++)
                    for (int j = 0; j < temp.cols; j++)
                        temp[i, j] = B[i - A.rows, j];
                return temp;
            }
            if (str == "Right" && A.rows == B.rows)
            {
                NRMatrix temp = new NRMatrix(A.rows, A.cols + B.cols);
                for (int i = 0; i < A.rows; i++)
                    for (int j = 0; j < A.cols; j++)
                        temp[i, j] = A[i, j];
                for (int i = 0; i < temp.rows; i++)
                    for (int j = A.cols; j < temp.cols; j++)
                        temp[i, j] = B[i, j - A.cols];
                return temp;
            }
            throw (new NRException("两矩阵不能进行拼接!"));
        }
        #endregion
        #region 矩阵对象转换为其它数据类型及存储到文件
        public static explicit operator NRMatrix(double[,] array) //显示转换二维数组为矩阵
        {
            NRMatrix result = new NRMatrix(array);
            return result;
        }
        public static explicit operator double[,](NRMatrix mat) //显示转换矩阵为二维数组
        {
            double[,] result = new double[mat.rows, mat.cols];
            for (int i = 0; i < mat.rows; i++)
                for (int j = 0; j < mat.cols; j++)
                    result[i, j] = mat.data[i, j];
            return result;
        }
        public void Save2Bin(string Filename) //矩阵二进制文件
        {
            FileStream fs = new FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write);
            // Create the writer for data.
            BinaryWriter bw = new BinaryWriter(fs);
            // Write data to Test.data.
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                    bw.Write(data[i, j]);
            bw.Close();
            fs.Close();
        }
        public void Save2Txt(string Filename) //矩阵存为文本文件
        {
            StreamWriter SW = File.CreateText(Filename);
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                    SW.Write("{0:F4}" + "  ", data[i, j]);
                SW.Write(SW.NewLine);
            }
            SW.Close();
        }

        #endregion
    }
    public class NRException : Exception
    {
        public NRException() : base() { }
        public NRException(string message) : base(message) { }
        public NRException(string message, Exception inner) : base(message, inner) { }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -