C # Matrix

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace psp3
{
Public struct nnmatrix
{

Public int row, Col;
Public double [,] matrix;

Public nnmatrix (INT mrow, int mcol) // specify the number of rows and columns to create a matrix. The initial value is 0.
{
Row = mrow;
Col = mcol;
Matrix = new double [row, Col];
For (INT I = 0; I <row; I ++)
For (Int J = 0; j <Col; j ++)
Matrix [I, j] = 0;
}

 

Public static nnmatrix operator + (nnmatrix M1, nnmatrix m2) // matrix addition
{
If (m1.row = m2.row & m1.col = m2.col)
For (INT I = 0; I <m1.row; I ++)
For (Int J = 0; j <m2.col; j ++)
M1.matrix [I, j] + = m2.matrix [I, j];
Return (M1 );
}

 

Public static nnmatrix operator + (nnmatrix M1, double m2) // Add a constant to the matrix
{
For (INT I = 0; I <m1.row; I ++)
For (Int J = 0; j <m1.col; j ++)
M1.matrix [I, j] + = m2;
Return (M1 );
}

 

Public static nnmatrix operator-(nnmatrix M1, nnmatrix m2) // matrix Subtraction
{
If (m1.row = m2.row & m1.col = m2.col)
For (INT I = 0; I <m1.row; I ++)
For (Int J = 0; j <m2.col; j ++)
M1.matrix [I, j]-= m2.matrix [I, j];
Return (M1 );
}

 

Public static nnmatrix operator * (nnmatrix M1, nnmatrix m2) // Matrix Multiplication
{
Int M3R = m1.row;
Int m3c = m2.col;
Nnmatrix m3 = new nnmatrix (M3R, m3c );

If (m1.col = m2.row)
{
Double value = 0.0;
For (INT I = 0; I <M3R; I ++)
For (Int J = 0; j <m3c; j ++)
{
For (int ii = 0; II <m1.col; II ++)
Value + = m1.matrix [I, ii] * m2.matrix [II, J];

M3.matrix [I, j] = value;
}
}
Else
Throw new exception ("the rows/columns of the matrix do not match. ");

Return m3;
}

 

Public static nnmatrix operator * (nnmatrix M1, double m2) // multiply the matrix by the constant
{
For (INT I = 0; I <m1.row; I ++)
For (Int J = 0; j <m1.col; j ++)
M1.matrix [I, j] * = m2;
Return (M1 );
}

Public static nnmatrix transpos (nnmatrix SRCM) // rank conversion of Matrix
{

Nnmatrix tmpm = new nnmatrix (SRCM. Col, SRCM. Row );
For (INT I = 0; I <SRCM. Row; I ++)
For (Int J = 0; j <SRCM. Col; j ++)
{
If (I! = J)
{
Tmpm. Matrix [J, I] = SRCM. Matrix [I, j];
}
Else
Tmpm. Matrix [I, j] = SRCM. Matrix [I, j];
}
Return tmpm;
}

Private Static void swaper (double M1, double m2) // exchange
{
Double SW;
Sw = m1; M1 = m2; M2 = Sw;
}

 

/**
* Real matrix inverse all-selected Principal Component Gaussian-appointment Method
*
*/
Public static nnmatrix invers (nnmatrix SRCM) // returns the inverse of the matrix.
{
Int RHC = SRCM. row;
If (SRCM. Row = SRCM. col)
{
Int [] ISS = new int [RHC];
Int [] JSS = new int [RHC];
Double fdet = 1;
Double F = 1;
// Cancel the RMB
For (int K = 0; k <RHC; k ++)
{
Double Fmax = 0;
For (INT I = K; I <RHC; I ++)
{
For (Int J = K; j <RHC; j ++)
{
F = math. Abs (SRCM. Matrix [I, j]);
If (F> fmax)
{
Fmax = F;
ISS [k] = I;
JSS [k] = J;
}
}
}

If (ISS [k]! = K)
{
F =-F;
For (int ii = 0; II <RHC; II ++)
{
Swaper (SRCM. Matrix [K, ii], SRCM. Matrix [ISS [K], ii]);
}
}

If (JSS [k]! = K)
{
F =-F;
For (int ii = 0; II <RHC; II ++)
{
Swaper (SRCM. Matrix [K, ii], SRCM. Matrix [JSS [K], ii]);
}
}

Fdet * = SRCM. Matrix [K, K];
SRCM. Matrix [K, K] = 1.0/SRCM. Matrix [K, K];
For (Int J = 0; j <RHC; j ++)
If (J! = K)
SRCM. Matrix [K, J] * = SRCM. Matrix [K, K];

For (INT I = 0; I <RHC; I ++)
If (I! = K)
For (Int J = 0; j <RHC; j ++)
If (J! = K)
SRCM. Matrix [I, j] = SRCM. Matrix [I, j]-SRCM. Matrix [I, K] * SRCM. Matrix [K, J];

For (INT I = 0; I <RHC; I ++)
If (I! = K)
SRCM. Matrix [I, K] * =-SRCM. Matrix [K, K];
}
// Adjust the order of restored rows and columns
For (int K = RHC-1; k> = 0; k --)
{
If (JSS [k]! = K)
For (int ii = 0; II <RHC; II ++)
Swaper (SRCM. Matrix [K, ii], SRCM. Matrix [JSS [K], ii]);
If (ISS [k]! = K)
For (int ii = 0; II <RHC; II ++)
Swaper (SRCM. Matrix [K, ii], SRCM. Matrix [ISS [K], ii]);
}
}

Return SRCM;

}

Public String matrixprint () // matrix output
{
String tmprst;
Tmprst = "/N ";
For (INT I = 0; I <row; I ++)
{
For (Int J = 0; j <Col; j ++)
{
Tmprst + = matrix [I, j]. tostring () + "/t ";
}
Tmprst + = "/N ";
}
Return tmprst;
}

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.