Home > Net >  [] pure couple save the children wrote a matrix calculation of class how to use in Windows (I am usi
[] pure couple save the children wrote a matrix calculation of class how to use in Windows (I am usi

Time:09-30

The class Matrix
{
Private int numColumns=0;//matrix columns
Private int numRows=0;//matrix rows
Private double eps=0.0;//the default accuracy
Private double [] elements=null;//matrix data buffer
Public int Columns
{
The get
{
Return numColumns;
}

}
Public int Rows
{
The get
{
Return numRows.
}
}
Public double Eps
{
The get
{
Return eps;
}
Set
{
Eps=value;
}
}
Public double this [int row, int col]//index,
{
The get
{
Return elements [col numColumns + row *];
}
Set
{
Elements [col + row * numColumns]=value;
}
}
Public Matrix ()
{

}
Public bool Init (int nRows, int nCols)//allocate memory
{
NumRows=nRows;
NumColumns=nCols;
Int nSize nCols=nRows *;
If (nSize & lt; 0)
return false;
The else
{
Elements=new double [nSize];
return true;
}
}
Public Matrix (int row, int col, string [] STR)
{
If (row * col!=STR. Length) return;
Enclosing numRows=row;
Enclosing numColumns=col;
Elements=new double [row * col];
For (int I=0; i {
Elements [I]=double. Parse (STR [I]);
}
}
Public Matrix (double [and] m)
{

Enclosing numRows=m.G etLength (0);
Enclosing numColumns=m.G etLength (1);
Elements=new double [numRows * numColumns];
int count=0;
For (int I=0; i {
For (int j=0; J & lt; NumColumns; J++)
{
Elements [count++]=m (I, j),
}
}
}
Public static Matrix operator + (Matrix a, Matrix b)//Matrix addition
{
If (a.n umColumns!=b.n umColumns | | a.n umRows!=b.n umRows)
{
Throw new Exception (" matrix row/column number does not match!" );
}
The else
{
Double [and] res=new double [a.n umRows, a.R ows];
For (int I=0; i {
For (int j=0; J & lt; A.n umColumns; J++)
{
Res [I, j]=[I, j) + b (I, j),
}
}
The return of new Matrix (res);
}
}
Public static Matrix operator - (Matrix a, Matrix b)//Matrix subtraction
{
If (a.n umColumns!=b.n umColumns | | a.n umRows!=b.n umRows)
{
Throw new Exception (" matrix row/column number does not match!" );
}
The else
{
Double [and] res=new double [a.n umRows, a.R ows];
For (int I=0; i {
For (int j=0; J & lt; A.n umColumns; J++)
{
Res [I, j]=[I, j] - b (I, j),
}
}
The return of new Matrix (res);
}
}
Public static Matrix Multiply (double a, Matrix a)//number of Matrix multiplication
{
Double [and] res=new double [A.n umRows, A.n umColumns];
For (int I=0; i {
For (int j=0; J & lt; A.n umColumns; J++)
{
Res [I, j)=a * a (I, j),
}
}
The return of new Matrix (res);
}
Public static Matrix Multiply (Matrix A, Matrix B)//Matrix multiplication
{
If (A.n umColumns!=B.n umRows)
{
Throw new Exception (" matrix row/column number does not match!" );
}
The else
{
Double [and] res=new double [A.n umRows, B.n umColumns];
A double value.
For (int I=0; i {
For (int j=0; J & lt; B.n umColumns; J++)
{
Value=https://bbs.csdn.net/topics/0.0;
For (int k=0; K & lt; B.n umRows; K++)
{
Value=https://bbs.csdn.net/topics/value + A/I, k * B [k, j];
}
Res [I, j]=value;
}
}
The return of new Matrix (res);
}
}
Public static Matrix Transpose Matrix (A)//Matrix Transpose
{
Double [and] res=new double [A.n umColumns, A.n umRows];
For (int I=0; i {
For (int j=0; J & lt; A.n umRows; J++)
{
Res [I, j)=A (j, I),
}
}
The return of new Matrix (res);
}
Public static bool InvertGaussJordan//Matrix inversion (Matrix A)
{
Int I, j, k, l, u, v.
Double d=0, p=0;

//allocate memory

Int [] pnRow=new int [A.n umColumns];
Int [] pnCol=new int [A.n umColumns];

//elimination

For (k=0; K & lt;=A.n umColumns - 1; K++)
{
D=0.0;
For (I=k; i <=A.n umColumns - 1; I++)
{
For (j=k; J & lt;=A.n umColumns - 1; J++)
{
L=I * A.n umColumns + j.
P=Math. Abs (A.e lements [l]);
If (p & gt; D)
{
D=p;
PnRow [k]=I;
PnCol [k]=j;
}
}
}
//failure
If (d==0.0)
{
return false;
}
If (pnRow [k]!=k)
{
For (j=0; J & lt;=A.n umColumns - 1; J++)
{
U=k * A.n umColumns + j.
V=pnRow [k] * A.n umColumns + j.
P=A.e lements [u];
A.e lements [u]=A.e lements [v];
A.e lements [v]=p;
}
}
If (pnCol [k]!=k)
{
For (I=0; i <=A.n umColumns - 1; I++)
{
U=I * A.n umColumns + k;
V=I * A.n umColumns + pnCol [k].
P=A.e lements [u];
A.e lements [u]=A.e lements [v];
A.e lements [v]=p;
}
}
L=k * A.n umColumns + k;
A.e lements [l]=1.0/A.e lements [l];
For (j=0; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related