This article describes the matrix operation method in C#. Share it for your reference. The specific analysis is as follows:
1. Test environment:
Host: XP
Development environment: VS2008
2. Function:
Implement matrix operation in C#
3. Source code:
using System; using ; using ; using ; using ; using ; using ; using ; //Matrix data structure//Two-dimensional matrixclass _Matrix { public int m; public int n; public float[] arr; //initialization public _Matrix() { m = 0; n = 0; } public _Matrix(int mm,int nn) { m = mm; n = nn; } //Set m public void set_mn(int mm,int nn) { m = mm; n = nn; } //Set m public void set_m(int mm) { m = mm; } //Set n public void set_n(int nn) { n = nn; } //initialization public void init_matrix() { arr = new float[m * n]; } //release public void free_matrix() { //delete [] arr; } //Read data of i and j coordinates //Return -31415 for failure, return value successfully public float read(int i,int j) { if (i >= m || j >= n) { return -31415; } //return *(arr + i * n + j); return arr[i * n + j]; } //Write data to i and j coordinates //Return -1 if failed, return 1 if successful public int write(int i,int j,float val) { if (i >= m || j >= n) { return -1; } arr[i * n + j] = val; return 1; } }; //Two-dimensional operation classclass _Matrix_Calc { //initialization public _Matrix_Calc() { } //C = A + B //Return 1 for success, return -1 for failure public int add(ref _Matrix A,ref _Matrix B,ref _Matrix C) { int i = 0; int j = 0; //Judge whether it can be calculated if ( != || != || != || != ) { return -1; } //Operation for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { (i,j,(i,j) + (i,j)); } } return 1; } //C = A - B //Return 1 for success, return -1 for failure public int subtract(ref _Matrix A,ref _Matrix B, ref _Matrix C) { int i = 0; int j = 0; //Judge whether it can be calculated if ( != || != || != || != ) { return -1; } //Operation for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { (i,j,(i,j) - (i,j)); } } return 1; } //C = A * B //Return 1 for success, return -1 for failure public int multiply(ref _Matrix A, ref _Matrix B, ref _Matrix C) { int i = 0; int j = 0; int k = 0; float temp = 0; //Judge whether it can be calculated if ( != || != || != ) { return -1; } //Operation for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { temp = 0; for (k = 0;k < ;k++) { temp += (i,k) * (k,j); } (i,j,temp); } } return 1; } //The value of the determinant can only be calculated 2 * 2, 3 * 3 //Return -31415 for failure, return value successfully public float det(ref _Matrix A) { float value = 0; //Judge whether it can be calculated if ( != || ( != 2 && != 3)) { return -31415; } //Operation if ( == 2) { value = (0,0) * (1,1) - (0,1) * (1,0); } else { value = (0,0) * (1,1) * (2,2) + (0,1) * (1,2) * (2,0) + (0,2) * (1,0) * (2,1) - (0,0) * (1,2) * (2,1) - (0,1) * (1,0) * (2,2) - (0,2) * (1,1) * (2,0); } return value; } //Find the transpose matrix, B = AT //Return 1 for success, return -1 for failure public int transpos(ref _Matrix A,ref _Matrix B) { int i = 0; int j = 0; //Judge whether it can be calculated if ( != || != ) { return -1; } //Operation for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { (i,j,(j,i)); } } return 1; } //Finding the inverse matrix, B = A^(-1) //Return 1 for success, return -1 for failure public int inverse(ref _Matrix A, ref _Matrix B) { int i = 0; int j = 0; int k = 0; _Matrix m = new _Matrix(,2 * ); float temp = 0; float b = 0; //Judge whether it can be calculated if ( != || != || != ) { return -1; } /* //If it is 2-dimensional or 3-dimensional, find out whether the determinant is reversible if ( == 2 || == 3) { if (det(A) == 0) { return -1; } } */ //Augmented matrix m = A | B initialization m.init_matrix(); for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { if (j <= - 1) { (i,j,(i,j)); } else { if (i == j - ) { (i,j,1); } else { (i,j,0); } } } } //Gauss extinguishes the yuan //Change the lower triangle for (k = 0;k < - 1;k++) { //If the coordinates are k and the number of k is 0, then the row transformation if ((k,k) == 0) { for (i = k + 1;i < ;i++) { if ((i,k) != 0) { break; } } if (i >= ) { return -1; } else { //Switch lines for (j = 0;j < ;j++) { temp = (k,j); (k,j,(k + 1,j)); (k + 1,j,temp); } } } //Xiaoyuan for (i = k + 1;i < ;i++) { //Get multiple b = (i,k) / (k,k); //Line Transformation for (j = 0;j < ;j++) { temp = (i,j) - b * (k,j); (i,j,temp); } } } //Transform the upper triangle for (k = - 1;k > 0;k--) { //If the coordinates are k and the number of k is 0, then the row transformation if ((k,k) == 0) { for (i = k + 1;i < ;i++) { if ((i,k) != 0) { break; } } if (i >= ) { return -1; } else { //Switch lines for (j = 0;j < ;j++) { temp = (k,j); (k,j,(k + 1,j)); (k + 1,j,temp); } } } //Xiaoyuan for (i = k - 1;i >= 0;i--) { //Get multiple b = (i,k) / (k,k); //Line Transformation for (j = 0;j < ;j++) { temp = (i,j) - b * (k,j); (i,j,temp); } } } //Create the left square matrix into a unit matrix for (i = 0;i < ;i++) { if ((i,i) != 1) { //Get multiple b = 1 / (i,i); //Line Transformation for (j = 0;j < ;j++) { temp = (i,j) * b; (i,j,temp); } } } //Finding the inverse matrix for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { (i,j,(i,j + )); } } //Release the augmented matrix m.free_matrix(); return 1; } }; namespace test { public partial class Form1 : Form { double zk; double xkg, pkg, kk, xk, pk, q, r; public Form1() { InitializeComponent(); xk = 0; pk = 0; q = 0.00001; r = 0.0001; int i = 0; int j = 0; int k = 0; _Matrix_Calc m_c = new _Matrix_Calc(); //_Matrix m1 = new _Matrix(3,3); //_Matrix m2 = new _Matrix(3,3); //_Matrix m3 = new _Matrix(3,3); _Matrix m1 = new _Matrix(2, 2); _Matrix m2 = new _Matrix(2, 2); _Matrix m3 = new _Matrix(2, 2); //Initialize memory m1.init_matrix(); m2.init_matrix(); m3.init_matrix(); //Initialize the data k = 1; for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { (i,j,k++); } } for (i = 0;i < ;i++) { for (j = 0;j < ;j++) { (i,j,k++); } } m_c.multiply(ref m1,ref m2, ref m3); // = ((1,1)); = (m_c.det(ref m1)); } /* private void button1_Click(object sender, EventArgs e) { zk = (); //Time equation xkg = xk; pkg = pk + q; //Equation of state kk = pkg / (pkg + r); xk = xkg + kk * (zk - xkg); pk = (1 - kk) * pkg; //Output = (xk); } private void textBox1_TextChanged(object sender, EventArgs e) { } * */ } }
I hope this article will be helpful to everyone's C# programming.