在数学上,矩阵是由方程组的系数及常数所构成的方阵。用在解线性方程组上既方便又直观。生活中通过矩阵多因素探索解决问题。
程序如下:
package JAVA_Project_01_04;
import java.text.DecimalFormat;
public class TextMatrix {
private double[][] data;
public TextMatrix() {
}
public TextMatrix(double[][] data) {
if (CanTransTomatrix(data))
this.data = this.cloneArray(data);
}
private static boolean CanTransTomatrix(double[][] data) {
if (data == null)
return false;
for (int i = 0; i <= data.length - 2; i++) {
if (data[i].length != data[i + 1].length)
return false;
}
return true;
}
public String showArray(double[][] data) {
DecimalFormat format = new DecimalFormat("0.00");
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length; j++) {
buffer.append(format.format(data[i][j])).append(" ");
}
buffer.append("\n");
}
return buffer.toString();
}
public void showData() {
System.out.println(showArray(this.data));
}
private double[][] cloneArray(double[][] data) {
if (data == null)
return null;
return (double[][]) data.clone();
}
public double[][] getMatrixData() {
return cloneArray(this.data);
}
public TextMatrix add(TextMatrix t) {
if (t == null)
return null;
TextMatrix text = null;
double[][] tmData = t.getMatrixData();
if ((this.data.length != tmData.length) || (this.data[0].length != tmData[0].length)) {
System.out.println("两个矩阵大小不一");
return text;
} else {
double[][] result = new double[this.data.length][this.data[0].length];
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
result[i][j] = this.data[i][j] + tmData[i][j];
}
}
text = new TextMatrix(result);
return text;
}
}
public TextMatrix subtration(TextMatrix t) {
if (t == null)
return null;
TextMatrix text = null;
double[][] tmData = t.getMatrixData();
if ((this.data.length != tmData.length) || (this.data[0].length != tmData[0].length)) {
System.out.println("两个矩阵大小不一");
return text;
} else {
double[][] result = new double[this.data.length][this.data[0].length];
for (int i = 0; i < this.data.length; i++) {
for (int j = 0; j < this.data[0].length; j++) {
result[i][j] = this.data[i][j] - tmData[i][j];
}
}
text = new TextMatrix(result);
return text;
}
}
public TextMatrix transposeMatrix() {
int Row = this.data[0].length;
int Column = this.data.length;
double[][] change = new double[Row][Column];
for (int i = 0; i < Row; i++) {
for (int j = 0; j < Column; j++) {
change[i][j] = this.data[j][i];
}
}
return new TextMatrix(change);
}
public static void main(String[] args) {
double[][] data1 = new double[][]{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}};
double[][] data2 = new double[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
data2[i][j] = 2 * i + j;
}
}
TextMatrix matrix1 = new TextMatrix(data1);
TextMatrix matrix2 = new TextMatrix(data2);
System.out.println("两组二维数组展示:");
matrix1.showData();
matrix2.showData();
System.out.println("矩阵加减法运算结果:");
matrix1.add(matrix2).showData();
System.out.println("矩阵减法运算结果:");
matrix1.subtration(matrix2).showData();
System.out.println("矩阵matrix1的转置结果:");
matrix1.transposeMatrix().showData();
}
}