• 代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System....namespace ConsoleApp3_两个矩阵相乘_二维数组_{ class Program { static void Main(st...

    代码如下:

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

    namespace ConsoleApp3_两个矩阵相乘_二维数组_
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[,] a = new int[3, 3] { { 1, 2, 3 }, { 9, 5, 7 }, { 12, -1, 0 } }; //定义矩阵a
                int[,] b = { { 8, 1, 1 }, { 0, 1, 1 }, { 0, -4, 1 } };  //定义矩阵b
                int[,] c = new int[3, 3];  //动态定义矩阵c
                Console.WriteLine("数组a为:");
                for (int i = 0; i < 3; i++)  //输出矩阵a
                { 
                    for(int j = 0; j < 3; j++)
                    {
                        Console.Write("{0, 5:d}", a[i, j]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("数组b为:");
                for (int i = 0; i < 3; i++)  //输出矩阵b
                { 
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write("{0, 5:d}", b[i, j]);
                    }
                    Console.WriteLine();
                }
                for (int i = 0; i < 3; i++)  //进行矩阵乘法
                {
                    for (int j = 0; j < 3; j++) {
                        for(int k = 0; k < 3; k++)
                        {
                            c[i, j] += a[i, k] * b[k, j];
                        }
                    }
                }
                Console.WriteLine("矩阵a*b后为:");
                for(int i = 0; i < 3; i++)  //输出矩阵c
                {
                    for(int j = 0; j < 3; j++)
                    {
                        Console.Write("{0 ,5:d}",c[i,j]);
                    }
                    Console.WriteLine();
                }
            }
        }
    }

    展开全文
  • C#矩阵类,包含矩阵的加减乘除,求逆,转置,赋值等
  • C#实现矩阵基本运算

    2018-02-13 01:17:15
    //矩阵打包成类,矩阵为m * n,直接调用 public class Matrix { double[,] A; int m, n; string name; public Matrix(int am, int an) { ...
        //矩阵打包成类,矩阵为m * n,直接调用
            public class Matrix
            {
                double[,] A;
                int m, n;
                string name;
                public Matrix(int am, int an)
                {
                    m = am;
                    n = an;
                    A = new double[m, n];
                    name = "Result";
                }
                public Matrix(int am, int an, string aName)
                {
                    m = am;
                    n = an;
                    A = new double[m, n];
                    name = aName;
                }
    
                public int getM
                {
                    get { return m; }
                }
                public int getN
                {
                    get { return n; }
                }
                public double[,] Detail
                {
                    get { return A; }
                    set { A = value; }
                }
                public string Name
                {
                    get { return name; }
                    set { name = value; }
                }
            }
    
            /***********矩阵通用操作打包*************/
    
            class MatrixOperator
            {
                //矩阵加法
                public static Matrix MatrixAdd(Matrix Ma, Matrix Mb)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    int m2 = Mb.getM;
                    int n2 = Mb.getN;
    
                    if ((m != m2) || (n != n2))
                    {
                        Exception myException = new Exception("数组维数不匹配");
                        throw myException;
                    }
    
                    Matrix Mc = new Matrix(m, n);
                    double[,] c = Mc.Detail;
                    double[,] a = Ma.Detail;
                    double[,] b = Mb.Detail;
    
                    for (int i = 0; i < m; i++)
                        for (int j = 0; j < n; j++)
                            c[i, j] = a[i, j] + b[i, j];
                    return Mc;
                }
    
                //矩阵减法
                public static Matrix MatrixSub(Matrix Ma, Matrix Mb)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    int m2 = Mb.getM;
                    int n2 = Mb.getN;
                    if ((m != m2) || (n != n2))
                    {
                        Exception myException = new Exception("数组维数不匹配");
                        throw myException;
                    }
                    Matrix Mc = new Matrix(m, n);
                    double[,] c = Mc.Detail;
                    double[,] a = Ma.Detail;
                    double[,] b = Mb.Detail;
    
                    for (int i = 0; i < m; i++)
                        for (int j = 0; j < n; j++)
                            c[i, j] = a[i, j] - b[i, j];
                    return Mc;
                }
    
                //矩阵乘法
                public static Matrix MatrixMulti(Matrix Ma, Matrix Mb)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    int m2 = Mb.getM;
                    int n2 = Mb.getN;
    
                    if (n != m2)
                    {
                        Exception myException = new Exception("数组维数不匹配");
                        throw myException;
                    }
    
                    Matrix Mc = new Matrix(m, n2);
                    double[,] c = Mc.Detail;
                    double[,] a = Ma.Detail;
                    double[,] b = Mb.Detail;
    
                    for (int i = 0; i < m; i++)
                        for (int j = 0; j < n2; j++)
                        {
                            c[i, j] = 0;
                            for (int k = 0; k < n; k++)
                                c[i, j] += a[i, k] * b[k, j];
                        }
                    return Mc;
    
                }
    
                //矩阵数乘
                public static Matrix MatrixSimpleMulti(double k, Matrix Ma)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    Matrix Mc = new Matrix(m, n);
                    double[,] c = Mc.Detail;
                    double[,] a = Ma.Detail;
    
                    for (int i = 0; i < m; i++)
                        for (int j = 0; j < n; j++)
                            c[i, j] = a[i, j] * k;
                    return Mc;
                }
    
                //矩阵转置
                public static Matrix MatrixTrans(Matrix MatrixOrigin)
                {
                    int m = MatrixOrigin.getM;
                    int n = MatrixOrigin.getN;
                    Matrix MatrixNew = new Matrix(n, m);
                    double[,] c = MatrixNew.Detail;
                    double[,] a = MatrixOrigin.Detail;
                    for (int i = 0; i < n; i++)
                        for (int j = 0; j < m; j++)
                            c[i, j] = a[j, i];
                    return MatrixNew;
                }
    
                //矩阵求逆(伴随矩阵法)
                public static Matrix MatrixInvByCom(Matrix Ma)
                {
                    double d = MatrixOperator.MatrixDet(Ma);
                    if (d == 0)
                    {
                        Exception myException = new Exception("没有逆矩阵");
                        throw myException;
                    }
                    Matrix Ax = MatrixOperator.MatrixCom(Ma);
                    Matrix An = MatrixOperator.MatrixSimpleMulti((1.0 / d), Ax);
                    return An;
                }
                //对应行列式的代数余子式矩阵
                public static Matrix MatrixSpa(Matrix Ma, int ai, int aj)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    if (m != n)
                    {
                        Exception myException = new Exception("矩阵不是方阵");
                        throw myException;
                    }
                    int n2 = n - 1;
                    Matrix Mc = new Matrix(n2, n2);
                    double[,] a = Ma.Detail;
                    double[,] b = Mc.Detail;
    
                    //左上
                    for (int i = 0; i < ai; i++)
                        for (int j = 0; j < aj; j++)
                        {
                            b[i, j] = a[i, j];
                        }
                    //右下
                    for (int i = ai; i < n2; i++)
                        for (int j = aj; j < n2; j++)
                        {
                            b[i, j] = a[i + 1, j + 1];
                        }
                    //右上
                    for (int i = 0; i < ai; i++)
                        for (int j = aj; j < n2; j++)
                        {
                            b[i, j] = a[i, j + 1];
                        }
                    //左下
                    for (int i = ai; i < n2; i++)
                        for (int j = 0; j < aj; j++)
                        {
                            b[i, j] = a[i + 1, j];
                        }
                    //符号位
                    if ((ai + aj) % 2 != 0)
                    {
                        for (int i = 0; i < n2; i++)
                            b[i, 0] = -b[i, 0];
    
                    }
                    return Mc;
    
                }
    
                //矩阵的行列式,矩阵必须是方阵
                public static double MatrixDet(Matrix Ma)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    if (m != n)
                    {
                        Exception myException = new Exception("数组维数不匹配");
                        throw myException;
                    }
                    double[,] a = Ma.Detail;
                    if (n == 1) return a[0, 0];
    
                    double D = 0;
                    for (int i = 0; i < n; i++)
                    {
                        D += a[1, i] * MatrixDet(MatrixSpa(Ma, 1, i));
                    }
                    return D;
                }
    
                //矩阵的伴随矩阵
                public static Matrix MatrixCom(Matrix Ma)
                {
                    int m = Ma.getM;
                    int n = Ma.getN;
                    Matrix Mc = new Matrix(m, n);
                    double[,] c = Mc.Detail;
                    double[,] a = Ma.Detail;
    
                    for (int i = 0; i < m; i++)
                        for (int j = 0; j < n; j++)
                            c[i, j] = MatrixDet(MatrixSpa(Ma, j, i));
    
                    return Mc;
                }
    
    展开全文
  • 一、基础知识 1、Math.net是什么 ...简单的矩阵运算,其效率可以达到C#代码的10倍以上。 开源地址:http://mathnetnumerics.codeplex.com Math.NETNumerics是核心功能是数值计算。主要是提供日常科学工程计...

    一、基础知识

    1、 Math.net是什么

      Math.net计算库是专为.NET开发的开源科学计算库。包含了大部分矩阵运算、线性运算等功能。效率高,且可以在C#中直接使用。简单的矩阵运算,其效率可以达到C#代码的10倍以上。

    开源地址:http://mathnetnumerics.codeplex.com

    Math.NET Numerics是核心功能是数值计算。主要是提供日常科学工程计算相关的算法,包括一些特殊函数,线性代数,概率论,随机函数,微积分,插值,最优化等相关计算功能。它是在 Math.NET Iridium和dnAnalytics 的基础上合并而来。该组件里面包括了一个读取Matlab数据格式的功能。

    2、科学计算库 Math.NET Numerics库的安装方法

    方法一:在线安装:https://blog.csdn.net/heray1990/article/details/72467304

    方法二:先下载后安装

    (1)在以下地址下载Math.NET Numerics库包

    https://www.nuget.org/packages/MathNet.Numerics/

     (2)S2012添加Math.NET Numerics库包

    参考地址:https://blog.csdn.net/linjf520/article/details/79585081

    方法三:添加dll文件的方式

    前两种方法,我都没有实验成功,找到了一种比较简单的方法

    (1)下载一个MathNet.Numerics.dll文件

    下载地址:https://www.jb51.net/dll/MathNet.Numerics.dll.html#downintro2

    (2)将dll文件拷到工程的bin debug文件或release文件下,添加引用即可使用

    3、类的用法

    https://numerics.mathdotnet.com/api/MathNet.Numerics.Integration/GaussLegendreRule.htm

    二、简单应用

    参考:https://blog.csdn.net/c914620529/article/details/50393223

    1、首先添加引用名称空间

    using MathNet.Numerics.LinearAlgebra.Double;
    using MathNet.Numerics.LinearAlgebra.Generic;
    

    2、矩阵

    (1)矩阵的定义

    注意:var来自名称空间System,只能定义在局部变量,不能定义在全局变量

    //矩阵的定义和初始化
    var matrix1 = new DenseMatrix(3);//3维方阵
    var matrix2 = new DenseMatrix(2,2);  //2维方阵
    var matrix3 = new DenseMatrix(2, 3); //2×3矩阵
    var matrix4 = new DenseMatrix(3, 2); //3×2矩阵
    var matrix5= new DenseMatrix(2, 3, 3.0); //2×3矩阵,所有值为3.0
    var matrixI = DenseMatrix.Identity(5); //5维单位矩阵
    //利用C#中的double[,]直接创建,2×3矩阵
    double[,] d_matrix = new double[2, 3];
    var matrix6 = new DenseMatrix(d_matrix); 

    (2)矩阵的操作

    var submatrix = matrix2.SubMatrix(2, 2, 3, 3); //取从第二行开始的2行,第三列开始的三列 子矩阵
    var row = matrix2.Row(5, 3, 4); //取从第5行第3列开始的4个行元素
    var column = matrix2.Column(2, 6, 3); //取从第2列第6行开始的3个列元素
    var columndata = matrix2.ColumnEnumerator(2, 4); //取从第2列开始的4列
    var rowdata = matrix2.RowEnumerator(4, 3);//取从第4行开始的3行
    var data1 = matrix2.ToRowWiseArray();//矩阵变为行向量
    var data2 = matrix2.ToColumnWiseArray();//矩阵变为列向量
    var data3 = matrix2.Diagonal()//取矩阵的对角线元素向量

     

    展开全文
  • C#矩阵运算类库

    2019-06-24 09:04:15
    这个类库是本人参考许多相关资料之后做出的C#矩阵运算类库,因为C#的数值计算库相对比较少,所以希望这个类库能够给大家带来一些帮助。 源码github网址:https://github.com/JoshuaHe2015/MatrixLibrary 功能介绍...

    这个类库是本人参考许多相关资料之后做出的C#矩阵运算类库,因为C#的数值计算库相对比较少,所以希望这个类库能够给大家带来一些帮助。

    源码github网址:https://github.com/JoshuaHe2015/MatrixLibrary

    功能介绍:(持续更新中)

    1、矩阵的基本运算:

      矩阵的加、减、乘、除、求逆、求幂、求秩、求行列式、转置。运算包括矩阵与矩阵的运算,矩阵与向量的运算和矩阵与标量的运算。

     1 using System;
     2 using LinearAlgebra;
     3 namespace MatrixLibraryTest
     4 {
     5     class Program
     6     {
     7         static void Main(string[] args)
     8         {
     9             Matrix A = Matrix.Create(2, 2, new double[] { 1, 2, 3, 4 });
    10             Matrix B = new double[,] {
    11                 { 5, 6 },
    12                 { 7, 8 } };
    13             Matrix C = A + B;
    14             Matrix D = A * 2;
    15             Matrix E = A * B;
    16             Matrix F = E.Inverse();
    17             Console.WriteLine(C);
    18             Console.WriteLine(D);
    19             Console.WriteLine(E);
    20             Console.WriteLine(F);
    21             Console.ReadKey();
    22         }
    23     }
    24 }

     

    2、矩阵分解:

      LU分解、QR分解

     1 using System;
     2 using LinearAlgebra;
     3 namespace MatrixLibraryTest
     4 {
     5     class Program
     6     {
     7         static void Main(string[] args)
     8         {
     9             Matrix A = new double[,]
    10             {
    11               {1,2,3 },
    12               {2,5,2 },
    13               {3,1,5 }
    14             };
    15             var lu = A.LU();
    16             Console.WriteLine(lu.L);
    17             Console.WriteLine(lu.U);
    18             Matrix B = new double[,]
    19             {
    20                 {1,1,-1 },
    21                 {2,1,0 },
    22                 {1,-1,0 },
    23                 {-1,2,1 }
    24             };
    25             var qr = B.QR();
    26             Console.WriteLine(qr.Q);
    27             Console.WriteLine(qr.R);
    28             Console.ReadKey();
    29         }
    30     }
    31 }

     

    3、IO操作:

      支持从文本文件中读取矩阵、将矩阵写入文本文件

     1 using System;
     2 using LinearAlgebra;
     3 namespace MatrixLibraryTest
     4 {
     5     class Program
     6     {
     7         static void Main(string[] args)
     8         {
     9             Matrix A = Matrix.Load("D:\\mat_A.txt");
    10             Console.WriteLine(A);
    11             Matrix B = Matrix.Random(3, 3);
    12             B.Save("D:\\mat_B");
    13             Console.ReadKey();
    14         }
    15     }
    16 }

     

    4、特殊运算:

      求Hessen Berg矩阵,求解矩阵特征值

    5、线性方程组的求解:

      高斯消元法求解线性方程组、QR分解求最小二乘解、共轭梯度法求对称正定方程组6、特殊矩阵:

      生成零矩阵、一矩阵、单位矩阵

    7、提取矩阵子集:

      可以提取矩阵的行、列或对角

    8、其他:

      支持复数运算与向量运算

     

    参考文献:

      1、数值分析(第5版)/李庆扬 著/清华大学出版社

      2、C#数值计算算法编程/周长发 著/电子工业出版社

     

    转载于:https://www.cnblogs.com/HeYanjie/p/5707283.html

    展开全文
  • C#矩阵运算

    2019-12-25 22:26:39
    命名空间:matrix 定义类:Matrix ...定义矩阵A: Matrix A=new Matrix(int mm, int nn); public double read(int i, int j) 获取Aij: A.read(i,j); public int write(int i, int j, double val) 将数据...

    命名空间:matrix
    定义类:Matrix
    方法:
    public Matrix(int mm, int nn)
    定义矩阵A:
    Matrix A=new Matrix(int mm, int nn);

    public double read(int i, int j)
    获取Aij:
    A.read(i,j);

    public int write(int i, int j, double val)
    将数据b写入Aij:
    A.read(i,j,b);

    释放矩阵:
    public void freeMatrix()
    A.freeMatrix();

    方法:
    //C = A + B
    //成功返回1,失败返回-1
    public int add(ref Matrix A, ref Matrix B, ref Matrix C)

    //C = A - B
    //成功返回1,失败返回-1
    public int subtract(ref Matrix A, ref Matrix B, ref Matrix C)

    //C = A * B
    //成功返回1,失败返回-1
    public int multiply(ref Matrix A, ref Matrix B, ref Matrix C)

    //行列式的值,只能计算2 * 2,3 * 3
    //失败返回-31415,成功返回值
    public double det(ref Matrix A)

    //求转置矩阵,B = AT
    //成功返回1,失败返回-1
    public int transpos(ref Matrix A, ref Matrix B)

    //求逆矩阵,B = A^(-1)
    //成功返回1,失败返回-1
    public int inverse(ref Matrix A, ref Matrix B)

    //矩阵输出//
    public string Out(ref Matrix A)

    //矩阵格式化输出//
    public string Outt(ref Matrix A, string format)

    //矩阵一维数组赋值//
    public void Fuzhi( ref Matrix A , double[] arr)

    //方阵行列式值//
    public double Det(ref Matrix A)

    //矩阵的伴随矩阵//
    public void Accompany(ref Matrix A, ref Matrix B)

    //伴随矩阵法求矩阵的逆//
    public void Inverse(ref Matrix A, ref Matrix B)

    //矩阵相等//
    public void Equal(ref Matrix A, ref Matrix B)

    //C = A + B
    //成功返回1,失败返回-1
    A.add(ref Matrix A, ref Matrix B, ref Matrix C);
    //C = A - B
    //成功返回1,失败返回-1
    A.subtract(ref Matrix A, ref Matrix B, ref Matrix C)

    //C = A * B
    //成功返回1,失败返回-1
    A.multiply(ref Matrix A, ref Matrix B, ref Matrix C)

    //行列式的值,只能计算2 * 2,3 * 3
    //失败返回-31415,成功返回值
    A.det(ref Matrix A)

    //求转置矩阵,B = AT
    //成功返回1,失败返回-1
    A.transpos(ref Matrix A, ref Matrix B)

    //求逆矩阵,B = A^(-1)
    //成功返回1,失败返回-1
    A.inverse(ref Matrix A, ref Matrix B)

    //矩阵输出//
    A.Out(ref Matrix A)
    //矩阵6位小数输出//
    A.Outt(ref Matrix A)

    //矩阵一维数组赋值//
    A.Fuzhi( ref Matrix A , double[] arr)

    //方阵行列式值//
    A.Det(ref Matrix A)

    //矩阵格式化输出//
    public string Outt(ref Matrix A, string format)
    A.Outt(ref Matrix A, string format);

    //矩阵的伴随矩阵//
    public void Accompany(ref Matrix A, ref Matrix B)
    A.Accompany(ref Matrix A, ref Matrix B);

    //伴随矩阵法求矩阵的逆//
    public void Inverse(ref Matrix A, ref Matrix B)
    A.Inverse(ref Matrix A, ref Matrix B);

    //矩阵相等//
    public void Equal(ref Matrix A, ref Matrix B)
    A.Equal(ref Matrix A, ref Matrix B);

    格式说明符说明 示例 输出

    C 货币 2.5.ToString(“C”) ¥2.50

    D 十进制数 25.ToString(“D5”) 00025

    E 科学型 25000.ToString(“E”) 2.500000E+005

    F 固定点 25.ToString(“F2”) 25.00

    G 常规 2.5.ToString(“G”) 2.5

    N 数字 2500000.ToString(“N”) 2,500,000.00

    X 十六进制 255.ToString(“X”) FF

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace matrix
    {
    
        //矩阵数据结构  
        //二维矩阵  
        public class Matrix
        {
            public int m;
            public int n;
            public double[] arr;
    
            //初始化  
            public Matrix()
            {
                m = 0;
                n = 0;
            }
    
            public Matrix(int mm, int nn)
            {
                m = mm;
                n = nn;
                arr = new double[m * n];
            }
    
            //设置m  
            public void set_mn(int mm, int nn)
            {
                m = mm;
                n = nn;
            }
    
    
            //设置m  
            public void set_m(int mm)
            {
                m = mm;
            }
    
            //设置n  
            public void set_n(int nn)
            {
                n = nn;
            }
    
            //初始化  
            public void initMatrix()
            {
                arr = new double[m * n];
            }
    
            //释放  
            public void freeMatrix()
            {
                //delete [] arr;
            }
    
            //读取i,j坐标的数据  
            //失败返回-31415,成功返回值  
            public double read(int i, int j)
            {
                if (i >= m || j >= n)
                {
                    return -31415;
                }
    
                //return *(arr + i * n + j);
                return arr[i * n + j];
            }
    
            //写入i,j坐标的数据  
            //失败返回-1,成功返回1  
            public int write(int i, int j, double val)
            {
                if (i >= m || j >= n)
                {
                    return -1;
                }
    
                arr[i * n + j] = val;
                return 1;
            }
    
    
            //二维运算类  
    
            //初始化
         
    
            //C = A + B  
            //成功返回1,失败返回-1  
            public int add(ref Matrix A, ref Matrix B, ref Matrix C)
            {
                int i = 0;
                int j = 0;
    
                //判断是否可以运算  
                if (A.m != B.m || A.n != B.n ||
                    A.m != C.m || A.n != C.n)
                {
                    return -1;
                }
                //运算  
                for (i = 0; i < C.m; i++)
                {
                    for (j = 0; j < C.n; j++)
                    {
                        C.write(i, j, A.read(i, j) + B.read(i, j));
                    }
                }
    
                return 1;
            }
    
            //C = A - B  
            //成功返回1,失败返回-1  
            public int subtract(ref Matrix A, ref Matrix B, ref Matrix C)
            {
                int i = 0;
                int j = 0;
    
                //判断是否可以运算  
                if (A.m != B.m || A.n != B.n ||
                    A.m != C.m || A.n != C.n)
                {
                    return -1;
                }
                //运算  
                for (i = 0; i < C.m; i++)
                {
                    for (j = 0; j < C.n; j++)
                    {
                        C.write(i, j, A.read(i, j) - B.read(i, j));
                    }
                }
    
                return 1;
            }
    
            //C = A * B  
            //成功返回1,失败返回-1  
            public int multiply(ref Matrix A, ref Matrix B, ref Matrix C)
            {
                int i = 0;
                int j = 0;
                int k = 0;
                double temp = 0;
    
                //判断是否可以运算  
                if (A.m != C.m || B.n != C.n ||
                    A.n != B.m)
                {
                    return -1;
                }
                //运算  
                for (i = 0; i < C.m; i++)
                {
                    for (j = 0; j < C.n; j++)
                    {
                        temp = 0;
                        for (k = 0; k < A.n; k++)
                        {
                            temp += A.read(i, k) * B.read(k, j);
                        }
                        C.write(i, j, temp);
                    }
                }
    
                return 1;
            }
    
            //行列式的值,只能计算2 * 2,3 * 3  
            //失败返回-31415,成功返回值  
            public double det(ref Matrix A)
            {
                double value = 0;
    
                //判断是否可以运算  
                if (A.m != A.n || (A.m != 2 && A.m != 3))
                {
                    return -31415;
                }
                //运算  
                if (A.m == 2)
                {
                    value = A.read(0, 0) * A.read(1, 1) - A.read(0, 1) * A.read(1, 0);
                }
                else
                {
                    value = A.read(0, 0) * A.read(1, 1) * A.read(2, 2) +
                            A.read(0, 1) * A.read(1, 2) * A.read(2, 0) +
                            A.read(0, 2) * A.read(1, 0) * A.read(2, 1) -
                            A.read(0, 0) * A.read(1, 2) * A.read(2, 1) -
                            A.read(0, 1) * A.read(1, 0) * A.read(2, 2) -
                            A.read(0, 2) * A.read(1, 1) * A.read(2, 0);
                }
    
                return value;
            }
    
            //求转置矩阵,B = AT  
            //成功返回1,失败返回-1  
            public int transpos(ref Matrix A, ref Matrix B)
            {
                int i = 0;
                int j = 0;
    
                //判断是否可以运算  
                if (A.m != B.n || A.n != B.m)
                {
                    return -1;
                }
                //运算  
                for (i = 0; i < B.m; i++)
                {
                    for (j = 0; j < B.n; j++)
                    {
                        B.write(i, j, A.read(j, i));
                    }
                }
    
                return 1;
            }
    
            //求逆矩阵,B = A^(-1)  
            //成功返回1,失败返回-1  
            public int inverse(ref Matrix A, ref Matrix B)
            {
                int i = 0;
                int j = 0;
                int k = 0;
                Matrix m = new Matrix(A.m, 2 * A.m);
                double temp = 0;
                double b = 0;
    
                //判断是否可以运算  
                if (A.m != A.n || B.m != B.n || A.m != B.m)
                {
                    return -1;
                }
    
                /* 
                //如果是2维或者3维求行列式判断是否可逆 
                if (A.m == 2 || A.m == 3) 
                { 
                    if (det(A) == 0) 
                    { 
                        return -1; 
                    } 
                } 
                */
    
                //增广矩阵m = A | B初始化  
                m.initMatrix();
                for (i = 0; i < m.m; i++)
                {
                    for (j = 0; j < m.n; j++)
                    {
                        if (j <= A.n - 1)
                        {
                            m.write(i, j, A.read(i, j));
                        }
                        else
                        {
                            if (i == j - A.n)
                            {
                                m.write(i, j, 1);
                            }
                            else
                            {
                                m.write(i, j, 0);
                            }
                        }
                    }
                }
    
                //高斯消元  
                //变换下三角  
                for (k = 0; k < m.m - 1; k++)
                {
                    //如果坐标为k,k的数为0,则行变换  
                    if (m.read(k, k) == 0)
                    {
                        for (i = k + 1; i < m.m; i++)
                        {
                            if (m.read(i, k) != 0)
                            {
                                break;
                            }
                        }
                        if (i >= m.m)
                        {
                            return -1;
                        }
                        else
                        {
                            //交换行  
                            for (j = 0; j < m.n; j++)
                            {
                                temp = m.read(k, j);
                                m.write(k, j, m.read(k + 1, j));
                                m.write(k + 1, j, temp);
                            }
                        }
                    }
    
                    //消元  
                    for (i = k + 1; i < m.m; i++)
                    {
                        //获得倍数  
                        b = m.read(i, k) / m.read(k, k);
                        //行变换  
                        for (j = 0; j < m.n; j++)
                        {
                            temp = m.read(i, j) - b * m.read(k, j);
                            m.write(i, j, temp);
                        }
                    }
                }
                //变换上三角  
                for (k = m.m - 1; k > 0; k--)
                {
                    //如果坐标为k,k的数为0,则行变换  
                    if (m.read(k, k) == 0)
                    {
                        for (i = k + 1; i < m.m; i++)
                        {
                            if (m.read(i, k) != 0)
                            {
                                break;
                            }
                        }
                        if (i >= m.m)
                        {
                            return -1;
                        }
                        else
                        {
                            //交换行  
                            for (j = 0; j < m.n; j++)
                            {
                                temp = m.read(k, j);
                                m.write(k, j, m.read(k + 1, j));
                                m.write(k + 1, j, temp);
                            }
                        }
                    }
    
                    //消元  
                    for (i = k - 1; i >= 0; i--)
                    {
                        //获得倍数  
                        b = m.read(i, k) / m.read(k, k);
                        //行变换  
                        for (j = 0; j < m.n; j++)
                        {
                            temp = m.read(i, j) - b * m.read(k, j);
                            m.write(i, j, temp);
                        }
                    }
                }
                //将左边方阵化为单位矩阵  
                for (i = 0; i < m.m; i++)
                {
                    if (m.read(i, i) != 1)
                    {
                        //获得倍数  
                        b = 1 / m.read(i, i);
                        //行变换  
                        for (j = 0; j < m.n; j++)
                        {
                            temp = m.read(i, j) * b;
                            m.write(i, j, temp);
                        }
                    }
                }
                //求得逆矩阵  
                for (i = 0; i < B.m; i++)
                {
                    for (j = 0; j < B.m; j++)
                    {
                        B.write(i, j, m.read(i, j + m.m));
                    }
                }
                //释放增广矩阵  
                m.freeMatrix();
    
                return 1;
            }
            //矩阵输出//
            public string Out(ref Matrix A)
            {
                string s = null;
                int i, j;
                for (i = 0; i < A.m; i++)
                {
                    for (j = 0; j < A.n; j++)
                        s += A.read(i, j).ToString() + "    ";
                    s += "\r\n";
                }
                return s;
    
            }
            public string Outt(ref Matrix A, string format)
            {
                string s = null, m = null;
                m = "{" + "0:" + format + "}";
                int i, j;
                for (i = 0; i < A.m; i++)
                {
                    for (j = 0; j < A.n; j++)
                        s += A.read(i, j).ToString(format) + "    ";
                    s += "\r\n";
                }
                return s;
    
            }
            public void Fuzhi(ref Matrix A, double[] arr)
            {
                int i, j, k = 0;
                for (i = 0; i < A.m; i++)
                {
                    for (j = 0; j < A.n; j++)
                        A.write(i, j, arr[k++]);
    
                }
    
            }
    
            public void Accompany(ref Matrix A, ref Matrix B)
            {
                int m = 0; int n = 0;
                Matrix Q = new Matrix(A.m - 1, A.n - 1);//用于求伴随矩阵过程中过渡矩阵//
                //Matrix_Calc C = new Matrix_Calc();
                for (int i = 0; i < A.m; i++)
                    for (int j = 0; j < A.n; j++)//遍历矩阵A中元素//
                    {
                        for (int p = 0; p < A.m - 1; p++)
                        {
                            for (int q = 0; q < A.n - 1; q++)//写入过渡矩阵//                             
                            {
                                if (m == i)
                                    m++;
                                if (n == j)
                                    n++;
                                Q.write(p, q, A.read(m, n++));
                            }
                            n = 0;
                            m++;
                        }
                        //   Console.Write("\r\n   {0}    \r\n",C.Out(ref Q));//验证过渡矩阵结果是否正确//
                        m = 0; n = 0;
                        B.write(i, j, Math.Pow(-1.0, Convert.ToDouble(i + j)) * Q.Det(ref Q));
                    }
                //  Console.Write("\r\n   {0}    \r\n", C.Out(ref B));//验证伴随矩阵结果是否正确//
            }
            public void Inverse(ref Matrix A, ref Matrix B)
            {
                //Matrix_Calc Q = new Matrix_Calc();
                Matrix C = new Matrix(A.m, A.n);
                C.Accompany(ref A, ref C);
                for (int i = 0; i < A.m; i++)
                    for (int j = 0; j < A.n; j++)
                        B.write(i, j, C.read(i, j) / C.Det(ref A));
               // Console.Write("{0}", C.Out(ref B));//验证逆阵结果
            }
            public void Equal(ref Matrix A, ref Matrix B)
            {
                for (int i = 0; i < A.m; i++)
                    for (int j = 0; j < A.n; j++)
                        B.write(i, j, A.read(i, j));
            }
            public double Det(ref Matrix A)
            {
                Matrix R = new Matrix(A.m, A.n);
                R.Equal(ref A, ref R);
                // string s = null;
                double f = 0.0;
                int j, n = 0;
                double B, D;
                double[] huanhangi = new double[A.n];//定义一个交换行的中间数组//
                double[] huanhangj = new double[A.n];//定义一个交换行的中间数组//
                n = A.m;
                for (j = 0; j < n - 1; j++)
                {
                    if (A.read(j, j) == 0)
                    {
                        for (int i = j; i < A.m;)
                        {
                            if (A.read(i, j) == 0)
                                i++;//往下找不为零的元素
    
                            if (A.read(i, j) != 0)
                            {
                                for (int l = 0; l < A.n; l++)
                                {
                                    huanhangi[l] = A.read(i, l);
                                    huanhangj[l] = A.read(j, l);
                                    A.write(j, l, huanhangi[l]);
                                    A.write(i, l, huanhangj[l]);
                                    //交换第i行和第j行//
                                    //  Console.Write("换行i:{0}     ", huanhangi[l]);
                                    //  Console.Write("换行j:{0}     ", huanhangj[l]);    
                                    f++;//记录交换多少次//
                                }
                                break;
    
                            }       //如果第i行不为零,则交换i,j两行//
                            if (i == A.m)
                                return 0;//如果以下都为零则行列式为零//
                        }
                    }
                    int k = j + 1; int m = j;//行变换元素之间对应关系
                    for (; k < n; k++)
                    {
                        D = A.read(k, j) / A.read(j, j);
                        for (; m < n; m++)
                        {
                            //Matrix_Calc C = new Matrix_Calc();
                            // String S = null;  //初始化对象
    
                            B = A.read(k, m) - A.read(j, m) * D;
                            A.write(k, m, B);
                            // Console.Write("\r\n {0} \r\n", A.Out(ref A));//观察矩阵运行变化
    
                            //   Console.Write("\r\n {0} {1} {2} \r\n", j, k, m); //观察变量是否符合要求
    
                        }
                        m = j;//注意如果不重新置m,k内循环将不会继续
                    }
                    k = j + 1;//同上原理                  
                }
                double result = 1;
                for (int Q = 0; Q < A.n; Q++)
                    result *= Math.Pow(-1.0, f) * A.read(Q, Q);
                R.Equal(ref R, ref A);//将A变回原来形式//
                return result;
    
    
            }
            public void Shangsanjiao(ref Matrix A)
            {
                int j, n = 0;
    
                double C, D;
                double[] huanhangi = new double[A.n];//定义一个交换行的中间数组//
                double[] huanhangj = new double[A.n];//定义一个交换行的中间数组//
                n = A.m;
                for (j = 0; j < n - 1; j++)
                {
                    if (A.read(j, j) == 0)
                    {
                        for (int i = j; i < A.m;)
                        {
                            if (A.read(i, j) == 0)
                                i++;//往下找不为零的元素
    
                            if (A.read(i, j) != 0)
                            {
                                for (int l = 0; l < A.n; l++)
                                {
                                    huanhangi[l] = A.read(i, l);
                                    huanhangj[l] = A.read(j, l);
                                    A.write(j, l, huanhangi[l]);
                                    A.write(i, l, huanhangj[l]);
                                    //交换第i行和第j行//
                                    //  Console.Write("换行i:{0}     ", huanhangi[l]);
                                    //  Console.Write("换行j:{0}     ", huanhangj[l]); 
    
                                }
                                break;
    
                            }       //如果第i行不为零,则交换i,j两行//
                            if (i == A.m)
                                j++;//如果以下都为零则行列式为零//
                        }
                    }
                    int k = j + 1; int m = j;//行变换元素之间对应关系
                    for (; k < n; k++)
                    {
                        D = A.read(k, j) / A.read(j, j);
                        for (; m < n; m++)
                        {
                            //Matrix_Calc C = new Matrix_Calc();
                            // String S = null;  //初始化对象
    
                            C = A.read(k, m) - A.read(j, m) * D;
                            A.write(k, m, C);
                            //  Console.Write("\r\n {0} \r\n", C.Out(ref A));//观察矩阵运行变化
    
                            //   Console.Write("\r\n {0} {1} {2} \r\n", j, k, m); //观察变量是否符合要求
    
                        }
                        m = j;//注意如果不重新置m,k内循环将不会继续
                    }
                    k = j + 1;//同上原理
                }
            }
        }
    }
    
    
    
    
    展开全文
  • C#中实现矩阵运算

    2012-08-27 11:24:14
    C#中实现矩阵运算 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:XP 开发环境:VS2008 功能: 在C#中实现矩阵运算 源代码: using System; ...
  • c#矩阵运算

    2020-06-14 17:35:50
    可用于定义矩阵,并进行常用的矩阵运算
  • 包含了大部分矩阵运算、线性运算等功能。 1.安装 项目 - 右键 - 管理NuGet程序包 2.矩阵定义 //矩阵的定义和初始化 var matrix1 = new DenseMatrix(3); //3维方阵 var matrix2 = new DenseMatrix(2, 2); //2...
  • 一、基础知识 1、Math.net是什么 ...简单的矩阵运算,其效率可以达到C#代码的10倍以上。 开源地址:http://mathnetnumerics.codeplex.com Math.NETNumerics是核心功能是数值计算。主要是提供日常科学工程计...
  • C# Winform实现矩阵运算

    2020-05-29 23:33:07
    Winform实现矩阵运算,根据输入的行列数随机生成矩阵,实现加减乘运算,也可更换皮肤
  • 定义了一个矩阵的储存方法CMatrix 定义了一个矩阵的预算方法CMatrix_...3.矩阵乘法 4.矩阵行列式 5.矩阵转置 6.矩阵的伴随矩阵 7.矩阵的逆(只限于3x3) 8.向量的单位化 此代码为本人做3D建模(空间旋转)时用的基础代码
  • C#矩阵运算(加减乘、转置、求逆)   class Matrix { /// &lt;summary&gt; /// 矩阵的转置 /// &lt;/summary&gt; /// &lt;param name= "iMatrix "&gt; &lt;/...
  • 对一个 C# 矩阵库的使用作的总结 和 使用方法介绍
  • C# 简单的矩阵运算

    2018-05-14 16:54:46
    软件界面图 代码文件Matrix.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;...using System.Windo...
  • 矩阵乘法 C#

    2012-03-19 10:56:25
    这里使用最传统的方法:  矩阵A的行 乘以矩阵B的列 来计算矩阵的乘法的,所以比较简单 using System; using System.Collections.Generic;...namespace 矩阵乘法 { class Program { static void
  • C# 矩阵乘法实现

    2019-07-29 18:23:25
    矩阵乘法是一种高效的算法可以把一些一维递推优化到log( n ),还可以求路径方案等,所以更是是一种应用性极强的算法。矩阵,是线性代数中的基本概念之一。一个m×n的矩阵就是m×n个数排成m行n列的一个数阵。由于它...
  • C#本身不支持矩阵,所以不适合数值运算,但是C#的优点又在于界面编写简单,如果需要用到界面,且必须用到矩阵,那么就就需要引用一个矩阵类来帮助矩阵的实现。本次矩阵类主要是基于Python以及Matlab矩阵的格式规范,...
  • 下面就有一种实现矩阵乘法和矩阵快速幂的方法。 首先我们先来看一下矩阵乘法的定义: 设A为的矩阵,B为的矩阵,那么称的矩阵C为矩阵A与B的乘积,记作,其中矩阵C中的第 行第列元素可以表示为: 简...
1 2 3 4 5 ... 20
收藏数 5,370
精华内容 2,148
热门标签