在Matrix.java添加这些操作: 判断一个矩阵是否为上三角矩阵:public boolean isUpperTriangularMatrix() 判断一个矩阵是否为下三角矩阵:public boolean isLowerTriangularMatrix() 判断一个矩阵是否为对称矩阵:public boolean isSymmetricMatrix () 比较两个矩阵是否相等:public boolean equals(Matrix b) 计算两个矩阵的乘积:public void multiply(Matrix b) public class Matrix { private int value[][]; //存储矩阵元素的二维数组
public Matrix(int m, int n) //构造m行n列的空矩阵 { this.value=new int[m][n]; } public Matrix(int n) //构造n行n列的空矩阵 { this(n,n); } public Matrix() { this(10,10); } public Matrix(int mat[][]) //构造矩阵,由数组mat提供矩阵元素 { this(mat.length,mat[0].length); for (int i=0; i<mat.length; i++) for (int j=0; j<mat[i].length; j++) this.value[i][j] = mat[i][j]; }
public int get(int i, int j) //获得矩阵第i行第j列的元素,O(1) { return value[i][j]; } public void set(int i, int j, int k) //设置矩阵第i行第j列的元素,O(1) { value[i][j]=k; }
public void add(Matrix b) //this和b两个矩阵相加,改变当前矩阵 { for (int i=0; i<this.value.length; i++) for (int j=0; j<this.value[i].length; j++) this.value[i][j] += b.value[i][j]; }
public String toString() //行主序遍历,访问矩阵全部元素 { String str=""; for (int i=0; i<value.length; i++) { for (int j=0; j<value[i].length; j++) str += " "+value[i][j]; str += "\n"; } return str; }
public Matrix transpose() //矩阵的转置 { Matrix trans = new Matrix(value[0].length, value.length); for (int i=0; i<this.value.length; i++) for (int j=0; j<this.value[i].length; j++) trans.value[j][i]=this.value[i][j]; return trans; }
}
class Matrix_ex { public static void main(String args[]) { int m1[][]={{1,2,3},{4,5,6}}; Matrix a=new Matrix(m1); int m2[][]={{1,0,0},{0,1,0}}; Matrix b=new Matrix(m2); System.out.print("Matrix a:\n"+a.toString()); System.out.print("Matrix b:\n"+b.toString()); a.add(b); System.out.print("Matrix a:\n"+a.toString()); System.out.println("a的转置矩阵:\n"+a.transpose().toString()); } }
需要完整的算法答案,thanks。
最新回答
听说海能吞掉鱼的眼泪
2024-10-30 08:28:01
class Matrix { private int value[][]; //存储矩阵元素的
二维数组
public Matrix(int m, int n) //构造m行n列的空矩阵 { this.value=new int[m][n]; }
public Matrix(int n) //构造n行n列的空矩阵 { this(n,n); }
public Matrix() { this(10,10); }
public Matrix(int mat[][]) //构造矩阵,由数组mat提供矩阵元素 { this(mat.length,mat[0].length); for (int i=0; i<mat.length; i++) for (int j=0; j<mat[i].length; j++) this.value[i][j] = mat[i][j]; }
public int get(int i, int j) //获得矩阵第i行第j列的元素,O(1) { return value[i][j]; }
public void set(int i, int j, int k) //设置矩阵第i行第j列的元素,O(1) { value[i][j]=k; }
public void add(Matrix b) //this和b两个矩阵相加,改变当前矩阵 { for (int i=0; i<this.value.length; i++) for (int j=0; j<this.value[i].length; j++) this.value[i][j] += b.value[i][j]; }
public String toString() //行主序遍历,访问矩阵全部元素 { String str=""; for (int i=0; i<value.length; i++) { for (int j=0; j<value[i].length; j++) str += " "+value[i][j]; str += "\n"; } return str; }
public Matrix transpose() //矩阵的转置 { Matrix trans = new Matrix(value[0].length, value.length); for (int i=0; i<this.value.length; i++) for (int j=0; j<this.value[i].length; j++) trans.value[j][i]=this.value[i][j]; return trans; }
//判断一个矩阵是否为
上三角矩阵
public boolean isUpperTriangularMatrix() { int i, j = 0; int c = this.value[1][0];
public class Test { public static void main(String args[]) { int m1[][]={{1,2,3},{4,5,6}}; Matrix a=new Matrix(m1); int m2[][]={{1,0,0},{0,1,0}}; Matrix b=new Matrix(m2); System.out.print("Matrix a:\n"+a.toString()); System.out.print("Matrix b:\n"+b.toString()); a.add(b); System.out.print("Matrix a:\n"+a.toString()); System.out.println("a的
转置矩阵
:\n"+a.transpose().toString());
int m3[][] = {{1,2,1},{0,3,1},{0,0,2}}; int m4[][] = {{1,0,0},{2,1,0},{3,2,1}}; int m5[][] = {{1,0,2},{0,1,0},{2,0,2}}; Matrix mtr1 = new Matrix(m3); Matrix mtr2 = new Matrix(m4); Matrix mtr3 = new Matrix(m5); if(mtr1.isUpperTriangularMatrix()) System.out.println("上三角矩阵:\n" + mtr1.toString());