作者:青樽有酒_585_587 | 来源:互联网 | 2023-09-25 17:27
在数学上,矩阵是由方程组的系数及常数所构成的方阵.用在解析性方程组上既方便又直观.生活中通过矩阵多因素探索解决问题.要点: 1.使用二维数组表示矩阵 2.对矩阵的操作前,需要进
在数学上, 矩阵是由方程组的系数及常数所构成的方阵.用在解析性方程组上既方便又直观.生活中通过矩阵多因素探索解决问题.
要点:
1.使用二维数组表示矩阵
2.对矩阵的操作前,需要进行合法性验证,判断他们是否能进行运算
/***
* 矩阵的加减和转置
* @author Power
*
*/
public class TextMatrix {
//矩阵数据
private double[][] data;
//默认构造函数
public TextMatrix() {
}
//初始化矩阵
public TextMatrix(double[][] data) {
if(CanTransToMatrix(data)) {
this.data = this.cloneArray(data);
}
}
//克隆一个二维数组
private double[][] cloneArray(double[][] data2) {
// TODO Auto-generated method stub
if(data == null) {
return null;
}
return (double[][])data.clone();
}
//判断二维数组能够转换成矩阵
public static boolean CanTransToMatrix(double[][] data) {
if(data == null) {
return false;
}
for(int i = 0; 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可以修改数据
StringBuffer buffer = new StringBuffer("");
for(int i = 0; i for(int j = 0; j //将数组元素转换为指定格式
buffer.append(format.format(data[i][j])).append(" ");
}
buffer.append("\n");
}
return buffer.toString();
}
//调用方法显示二维数组
public void showData() {
System.out.println(showArray(this.data));
}
//获得矩阵
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 for(int j = 0; 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 for(int j = 0; j result[i][j] = this.data[i][j] + tmData[i][j];
}
}
text = new TextMatrix(result);
return text;
}
}
//矩阵转置,格式为a[i][j] = b[j][i]
public TextMatrix transposeMatrix() {
int Row = this.data[0].length;
int Colume = this.data.length;
double[][] change = new double[Row][Colume];
for(int i = 0; i for(int j = 0; j change[i][j] = this.data[j][i];
}
}
return new TextMatrix(change);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
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("SHOW:");
matrix1.showData();
matrix2.showData();
System.out.println("ADD:");
matrix1.add(matrix2).showData();
System.out.println("SUB:");
matrix1.subtration(matrix2).showData();
System.out.println("TRAN:");
matrix1.transposeMatrix().showData();
}
}