📄 matrix.java
字号:
matrix.value[i][j] = 1;
}
else
{
matrix.value[i][j] = 0;
}
}
}
return matrix;
}
/**
* Give the transpose matrix for the parameter matrix.
*
* @param matrix The given matrix.
* @return The transpose matrix for the given matrix.
*/
public static Matrix transpose( Matrix matrix )
{
Matrix result = new Matrix( matrix.column(), matrix.row() );
for ( int i = 0; i < matrix.column(); i++ )
{
for ( int j = 0; j < matrix.row(); j++ )
{
result.value[i][j] = matrix.value[j][i];
}
}
return result;
}
/**
* Give the negative value for the float number.
*
* @param number The given number.
* @return The negative value for the given number.
*/
public static Double negative( double number )
{
return new Double( number * ( -1 ) );
}
/**
* Return the negative matrix for the given matrix. <p>
* Call method multi( matrix ,-1 ) to do the job.
*
* @param matrix The given matrix.
* @return The negative matrix for the given matrix.
*/
public static Matrix negative( Matrix matrix )
{
return multi( matrix, -1 );
}
/**
* Give the inverse number for the given float number.
*
* @param x The given number.
* @return The inverse number for the parameter.
*/
public static Double inverse( double x )
{
return new Double( 1/x );
}
/**
* Give the inverse matrix for the given matrix. <p>
* Use the formula:The inverse matrix of A = A* / |A|. <p>
* Here A* is the transpose matrix of Aij. <p>
* Aij = (-1)^(i+j) * det(Aij); <p>
* det(ij) = Matrix.detValue( aij ); <p>
* Call the method Matrix.detValue() to calculate |A|. <p>
* At last,call the method Matrix.divide( A*,|A| ) and return the result. <p>
*
* @param matrix The given matrix.
* @return The inverse matrix for the given matrix.
*/
public static Matrix inverse( Matrix matrix )
{
double[][] ranks;
double det;
double[] temp;
int count = 0;
double[][] ranksValue = new double[matrix.row()][matrix.column()];
for ( int m = 0; m < matrix.row(); m++ )
{
for ( int n = 0; n < matrix.column(); n++ )
{
temp = new double[matrix.row() * matrix.column()];
ranks = new double[matrix.row() - 1][matrix.column() - 1];
count = 0;
//划去Aij所在行列后所得的行列式。
for ( int i = 0; i < matrix.row(); i++ )
{
for ( int j = 0; j < matrix.column(); j++ )
{
if ( i != m && j != n )
{
temp[count++] = matrix.value[i][j];
}
}
}
count = 0;
for ( int i = 0; i < matrix.row() - 1; i++ )
{
for ( int j = 0; j < matrix.column() - 1; j++ )
{
ranks[i][j] = temp[count++];
}
}
int judge = ( m + n ) % 2;
//计算(-1)^( i + j )
if ( judge == 0 )
{
judge = 1;
}
else
{
judge = -1;
}
ranksValue[m][n] = judge * Matrix.detValue( ranks );
}
}
Matrix resultMatrix = new Matrix( ranksValue );
//计算代数余子式的转置。
resultMatrix = Matrix.transpose( resultMatrix );
resultMatrix = Matrix.divide( resultMatrix, Matrix.detValue( matrix.value ) );
return resultMatrix;
}
/**
* Check whether the matrix has the inverse matrix. <p>
* First check whether it is a square matrix, if not return false. <p>
* Then calculate det( matrix ).If it equals zero,then it is <p>
* a singular matrix,and return false. Otherwise return true.
*
* @param matrix The given matrix.
* @return True if the matrix has an inverse matrix,false otherwise.
*/
public static boolean hasInvMatrix( Matrix matrix )
{
if ( matrix.value.length != matrix.value[0].length )
{
System.out.println( "Error: It is not a square matrix!" );
return false;
}
else if ( detValue( matrix.value ) == 0)
{
System.out.println( "Error: It is a singular matrix!" );
return false;
}
return true;
}
/**
* Given the det value of the given matrix. <p>
* Use the first row of the matrix to calculate the det value <p>
* of the given matrix. <p>
*
* @param x The given matrix.
* @return The det value of the given matrix.
*/
public static double detValue ( double[][] x )
{
double[][] ranks; //存储划去同行同列后的矩阵
double[] value; //存储每一个元素的余子式
double[] temp;
int count;
double finalValue = 0;
//1*1的行列式返回原值。
if ( x.length == 1 && x[0].length == 1)
{
return x[0][0];
}
//2*2的行列式计算值。
else if ( x.length == 2 && x[0].length == 2 )
{
return x[0][0] * x[1][1] - x[0][1] * x[1][0];
}
//其余使用递归。
else
{
value = new double[x.length];
for ( int m = 0; m < x.length; m++ )
{
ranks = new double[x.length - 1][x.length - 1];
temp = new double[x.length * x.length];
count = 0;
//获得余子式的过渡体
for ( int i = 0; i < x.length; i++ )
{
for ( int j = 0; j < x.length; j++ )
{
if ( i != 0 && j != m )
{
temp[count++] = x[i][j];
}
}
}
count = 0;
for ( int i = 0; i < x.length - 1; i++ )
{
for ( int j = 0; j < x.length - 1; j++ )
{
ranks[i][j] = temp[count++];
}
}
value[m] = detValue( ranks );
}
for ( int i = 0; i < x.length; i++ )
{
int bias;
if ( i % 2 == 0 )
{
bias = 1;
}
else
{
bias = -1;
}
finalValue += x[0][i] * value[i] * bias;
}
return finalValue;
}
}
/**
* Given the String version for the matrix to be printed.
*
* @param matrix The given matrix.
* @return The string representation for the given matrix.
*/
public static String toString( Matrix matrix )
{
String output = "";
if ( matrix.column > 1 )
{
if ( matrix.row > 1)
{
output += "[ ";
}
for ( int i = 0; i < matrix.row(); i++ )
{
output += "[ ";
for ( int j = 0; j < matrix.column(); j++ )
{
output += matrix.value[i][j] + " ";
}
output += "] ";
if ( i != matrix.row() - 1 )
{
output += " \n " ;
}
}
if ( matrix.row > 1)
{
output += "] ";
}
}
else if ( matrix.column == 1 )
{
if ( matrix.row == 1)
{
output += matrix.value[0][0];
}
else
{
output += "[ ";
for ( int i = 0; i < matrix.row; i++ )
{
output += matrix.value[i][0] + " ";
}
output += "] t";
}
}
return output;
}
//这个私有函数可以将包含矩阵的矩阵对象转换为包含二维数组的矩阵对象.
/**
* The private mathod for changing the matrix of elements[] to the matrix
* of value[][].<p> Then it can be used to do the operations.
*/
private void parseMatrix()
{
if ( this.row == 1 )
{
this.value = new double[1][this.column];
for ( int i = 0; i < this.column; i++ )
{
this.value[0][i] =
Double.parseDouble( this.elements[i].toString() );
}
return;
}
this.value = new double[this.row][this.column];
for ( int i = 0; i < this.row; i++ )
{
Matrix temp = ( Matrix )this.elements[i];
for ( int j = 0; j < this.column; j++ )
{
this.value[i][j] =
Double.parseDouble( ( temp.elements[j]).toString() );
}
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -