Day31 —— IntMatrix
1. Background
今天是学习java的第31天了,终于从树的魔掌中逃出来了,今天学习的内容是矩阵及其运算。
今天的内容虽然多,但是还是蛮好理解的,所以学起来非常的顺畅,没有前两天学习时的那种无力感。
所以就直接上代码了,毕竟矩阵的运算啥的都是线性代数的基础,差不多是常识一样的东西,就不过多讲了。至于代码中需要注意的地方,我则是直接在代码上留了注释。所以就不单独介绍了。
2. Code
package matrix;
import java.util.Arrays;
public class IntMatrix {
/**
* The data.
*/
int[][] data;
/**
*********************
* The first constructor.创建矩阵。
*
* @param paraRows The number of rows.
* @param paraColumns The number of columns.
*********************
*/
public IntMatrix(int paraRows, int paraColumns) {
data = new int[paraRows][paraColumns];
}// Of the first constructor
/**
*********************
* The second constructor. 复制给定矩阵到data中.
*
* @param paraMatrix The given matrix.
*********************
*/
public IntMatrix(int[][] paraMatrix) {
data = new int[paraMatrix.length][paraMatrix[0].length];
// Copy elements.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = paraMatrix[i][j];
} // Of for j
} // Of for i
}// Of the second constructor
/**
*********************
* The third constructor. 效果同上,是上面方法的override.
*
* @param paraMatrix The given matrix.
*********************
*/
public IntMatrix(IntMatrix paraMatrix) {
this(paraMatrix.getData());
}// Of the third constructor
/**
*********************
* Get identity matrix. 获取单位矩阵.
*
* @param paraRows The given rows.
*********************
*/
public static IntMatrix getIdentityMatrix(int paraRows) {
IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
for (int i = 0; i < paraRows; i++) {
resultMatrix.data[i][i] = 1; // 默认初始化的矩阵元素都是0,所以此处只需要管主对角线上的元素即可。
} // Of for i
return resultMatrix;
}// Of getIdentityMatrix
/**
*********************
* 一个简单的toString。
*********************
*/
public String toString() {
return Arrays.deepToString(data); // 此方法设计用于将多维数组转换为字符串。
}// Of toString
/**
*********************
* 返回data,此处是对data的直接引用。
*
* @return The data matrix.
*********************
*/
public int[][] getData() {
return data;
}// Of getData
/**
*********************
* 返回data的长度,即行数。
*
* @return The number of rows.
*********************
*/
public int getRows() {
return data.length;
}// Of getRows
/**
*********************
* 返回列数.
*
* @return The number of columns.
*********************
*/
public int getColumns() {
return data[0].length;
}// Of getColumns
/**
*********************
* 改矩阵中的一个元素.
*
* @param paraRow The row of the element.
* @param paraColumn The column of the element.
* @param paraValue The new value.
*********************
*/
public void setValue(int paraRow, int paraColumn, int paraValue) {
data[paraRow][paraColumn] = paraValue;
}// Of setValue
/**
*********************
* 增删改查中的查.
*
* @param paraRow The row of the element.
* @param paraColumn The column of the element.
*********************
*/
public int getValue(int paraRow, int paraColumn) {
return data[paraRow][paraColumn];
}// Of getValue
/**
*********************
* 一个矩阵加到data这个矩阵上面.
*
* @param paraMatrix The other matrix.
*********************
*/
public void add(IntMatrix paraMatrix) throws Exception {
// Step 1. Get the data of the given matrix.
int[][] tempData = paraMatrix.getData();
// Step 2. 元素检查.由于矩阵加法的规则,相加的两个矩阵形状必须相等
if (data.length != tempData.length) {
throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. " + tempData.length + ".");
} // Of if
if (data[0].length != tempData[0].length) {
throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. " + tempData[0].length + ".");
} // Of if
// Step 3. Add.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] += tempData[i][j];
} // Of for j
} // Of for i
}// Of add
/**
*********************
* 两个矩阵中元素相加.
*
* @param paraMatrix1 The first matrix.
* @param paraMatrix2 The second matrix.
* @return A new matrix.
*********************
*/
public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
// Step 1. Clone the first matrix.
IntMatrix resultMatrix = new IntMatrix(paraMatrix1);
// Step 2. Add the second one.
resultMatrix.add(paraMatrix2);
return resultMatrix;
}// Of add
/**
*********************
* Multiply two existing matrices.
*
* @param paraMatrix1 The first matrix.
* @param paraMatrix2 The second matrix.
* @return A new matrix.
*********************
*/
public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2)
throws Exception {
// Step 1. 元素检查.
int[][] tempData1 = paraMatrix1.getData();
int[][] tempData2 = paraMatrix2.getData();
if (tempData1[0].length != tempData2.length) {
throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. "
+ tempData2.length + ".");
} // Of if
// Step 2. 新建返回矩阵.
int[][] resultData = new int[tempData1.length][tempData2[0].length];
// Step 3. 相乘.
for (int i = 0; i < tempData1.length; i++) {
for (int j = 0; j < tempData2[0].length; j++) {
for (int k = 0; k < tempData1[0].length; k++) {
resultData[i][j] += tempData1[i][k] * tempData2[k][j];
} // Of for k
} // Of for j
} // Of for i
// Step 4. emmmm,这里需要注意,这里不是无意义的一步,这里是将int型的矩阵转换为了我们的IntMatrix类型.
IntMatrix resultMatrix = new IntMatrix(resultData);
return resultMatrix;
}// Of multiply
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
IntMatrix tempMatrix1 = new IntMatrix(3, 3);
tempMatrix1.setValue(0, 1, 1);
tempMatrix1.setValue(1, 0, 1);
tempMatrix1.setValue(1, 2, 1);
tempMatrix1.setValue(2, 1, 1);
System.out.println("The original matrix is: " + tempMatrix1);
IntMatrix tempMatrix2 = null;
try {
tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The square matrix is: " + tempMatrix2);
IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
try {
tempMatrix3.add(tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The connectivity matrix is: " + tempMatrix3);
}// Of main
}
package matrix;
import java.util.Arrays;
public class IntMatrix {
/**
* The data.
*/
int[][] data;
/**
*********************
* The first constructor.创建矩阵。
*
* @param paraRows The number of rows.
* @param paraColumns The number of columns.
*********************
*/
public IntMatrix(int paraRows, int paraColumns) {
data = new int[paraRows][paraColumns];
}// Of the first constructor
/**
*********************
* The second constructor. 复制给定矩阵到data中.
*
* @param paraMatrix The given matrix.
*********************
*/
public IntMatrix(int[][] paraMatrix) {
data = new int[paraMatrix.length][paraMatrix[0].length];
// Copy elements.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = paraMatrix[i][j];
} // Of for j
} // Of for i
}// Of the second constructor
/**
*********************
* The third constructor. 效果同上,是上面方法的override.
*
* @param paraMatrix The given matrix.
*********************
*/
public IntMatrix(IntMatrix paraMatrix) {
this(paraMatrix.getData());
}// Of the third constructor
/**
*********************
* Get identity matrix. 获取单位矩阵.
*
* @param paraRows The given rows.
*********************
*/
public static IntMatrix getIdentityMatrix(int paraRows) {
IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
for (int i = 0; i < paraRows; i++) {
resultMatrix.data[i][i] = 1; // 默认初始化的矩阵元素都是0,所以此处只需要管主对角线上的元素即可。
} // Of for i
return resultMatrix;
}// Of getIdentityMatrix
/**
*********************
* 一个简单的toString。
*********************
*/
public String toString() {
return Arrays.deepToString(data); // 此方法设计用于将多维数组转换为字符串。
}// Of toString
/**
*********************
* 返回data,此处是对data的直接引用。
*
* @return The data matrix.
*********************
*/
public int[][] getData() {
return data;
}// Of getData
/**
*********************
* 返回data的长度,即行数。
*
* @return The number of rows.
*********************
*/
public int getRows() {
return data.length;
}// Of getRows
/**
*********************
* 返回列数.
*
* @return The number of columns.
*********************
*/
public int getColumns() {
return data[0].length;
}// Of getColumns
/**
*********************
* 改矩阵中的一个元素.
*
* @param paraRow The row of the element.
* @param paraColumn The column of the element.
* @param paraValue The new value.
*********************
*/
public void setValue(int paraRow, int paraColumn, int paraValue) {
data[paraRow][paraColumn] = paraValue;
}// Of setValue
/**
*********************
* 增删改查中的查.
*
* @param paraRow The row of the element.
* @param paraColumn The column of the element.
*********************
*/
public int getValue(int paraRow, int paraColumn) {
return data[paraRow][paraColumn];
}// Of getValue
/**
*********************
* 一个矩阵加到data这个矩阵上面.
*
* @param paraMatrix The other matrix.
*********************
*/
public void add(IntMatrix paraMatrix) throws Exception {
// Step 1. Get the data of the given matrix.
int[][] tempData = paraMatrix.getData();
// Step 2. 元素检查.由于矩阵加法的规则,相加的两个矩阵形状必须相等
if (data.length != tempData.length) {
throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. " + tempData.length + ".");
} // Of if
if (data[0].length != tempData[0].length) {
throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. " + tempData[0].length + ".");
} // Of if
// Step 3. Add.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] += tempData[i][j];
} // Of for j
} // Of for i
}// Of add
/**
*********************
* 两个矩阵中元素相加.
*
* @param paraMatrix1 The first matrix.
* @param paraMatrix2 The second matrix.
* @return A new matrix.
*********************
*/
public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
// Step 1. Clone the first matrix.
IntMatrix resultMatrix = new IntMatrix(paraMatrix1);
// Step 2. Add the second one.
resultMatrix.add(paraMatrix2);
return resultMatrix;
}// Of add
/**
*********************
* Multiply two existing matrices.
*
* @param paraMatrix1 The first matrix.
* @param paraMatrix2 The second matrix.
* @return A new matrix.
*********************
*/
public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2)
throws Exception {
// Step 1. 元素检查.
int[][] tempData1 = paraMatrix1.getData();
int[][] tempData2 = paraMatrix2.getData();
if (tempData1[0].length != tempData2.length) {
throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. "
+ tempData2.length + ".");
} // Of if
// Step 2. 新建返回矩阵.
int[][] resultData = new int[tempData1.length][tempData2[0].length];
// Step 3. 相乘.
for (int i = 0; i < tempData1.length; i++) {
for (int j = 0; j < tempData2[0].length; j++) {
for (int k = 0; k < tempData1[0].length; k++) {
resultData[i][j] += tempData1[i][k] * tempData2[k][j];
} // Of for k
} // Of for j
} // Of for i
// Step 4. emmmm,这里需要注意,这里不是无意义的一步,这里是将int型的矩阵转换为了我们的IntMatrix类型.
IntMatrix resultMatrix = new IntMatrix(resultData);
return resultMatrix;
}// Of multiply
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
IntMatrix tempMatrix1 = new IntMatrix(3, 3);
tempMatrix1.setValue(0, 1, 1);
tempMatrix1.setValue(1, 0, 1);
tempMatrix1.setValue(1, 2, 1);
tempMatrix1.setValue(2, 1, 1);
System.out.println("The original matrix is: " + tempMatrix1);
IntMatrix tempMatrix2 = null;
try {
tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The square matrix is: " + tempMatrix2);
IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
try {
tempMatrix3.add(tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The connectivity matrix is: " + tempMatrix3);
}// Of main
} // Of IntMatrix
运行结果: