java实现矩阵相加、相乘,判断是否上(下)三角矩阵、对称矩阵、相等的算法
在Matrix.java添加这些操作:判断一个矩阵是否为上三角矩阵:publicbooleanisUpperTriangularMatrix()判断一个矩阵是否为下三角矩...
在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。 展开
判断一个矩阵是否为上三角矩阵: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。 展开
3个回答
展开全部
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];
for(i=1; i<this.value.length; i++)
for(j=0; j<i; j++)
if(this.value[i][j] != c)
break;
if(i>=this.value.length)
return true;
return false;
}
//判断一个矩阵是否为下三角矩阵
public boolean isLowerTriangularMatrix() {
int i, j = 0;
int c = this.value[0][1];
for(i=0; i<this.value.length-1; i++)
for(j=i+1; j<this.value[0].length; j++)
if(this.value[i][j] != c)
break;
if(i>=this.value.length-1)
return true;
return false;
}
//判断一个矩阵是否为对称矩阵
public boolean isSymmetricMatrix () {
int i, j = 0;
for(i=1; i<this.value.length; i++)
for(j=0; j<i; j++)
if(this.value[i][j] != this.value[j][i])
break;
if(i>=this.value.length)
return true;
return false;
}
//比较两个矩阵是否相等
public boolean equals(Matrix b) {
int i, j = 0;
if(this.value.length != b.value.length || this.value[0].length != b.value[0].length)
return false;
for(i=0; i<this.value.length; i++)
for(j=0; j<this.value[0].length; j++)
if(this.value[i][j] != b.value[j][i])
break;
if(i>=this.value.length)
return true;
return false;
}
//计算两个矩阵的乘积
public Matrix multiply(Matrix b){
int i, j, k;
int sum;
Matrix mtr;
if(this.value[0].length != b.value.length) {
return null;
}
mtr = new Matrix(this.value.length, b.value[0].length);
for(i=0; i<this.value.length; i++)
{
for(k=0; k<b.value[0].length; k++){
for(sum=0,j=0; j<this.value[0].length; j++){
sum += this.value[i][j] * b.value[j][k];
mtr.value[i][k] = sum;
}
}
}
return mtr;
}
}
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());
if(mtr2.isLowerTriangularMatrix())
System.out.println("下三角矩阵:\n" + mtr2.toString());
if(mtr3.isSymmetricMatrix())
System.out.println("对称矩阵:\n" + mtr3.toString());
System.out.println(mtr1.toString() + "\n乘以\n" + mtr2.toString() + "\n=\n");
Matrix tempM = mtr1.multiply(mtr2);
System.out.println(tempM.toString());
}
}
{
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];
for(i=1; i<this.value.length; i++)
for(j=0; j<i; j++)
if(this.value[i][j] != c)
break;
if(i>=this.value.length)
return true;
return false;
}
//判断一个矩阵是否为下三角矩阵
public boolean isLowerTriangularMatrix() {
int i, j = 0;
int c = this.value[0][1];
for(i=0; i<this.value.length-1; i++)
for(j=i+1; j<this.value[0].length; j++)
if(this.value[i][j] != c)
break;
if(i>=this.value.length-1)
return true;
return false;
}
//判断一个矩阵是否为对称矩阵
public boolean isSymmetricMatrix () {
int i, j = 0;
for(i=1; i<this.value.length; i++)
for(j=0; j<i; j++)
if(this.value[i][j] != this.value[j][i])
break;
if(i>=this.value.length)
return true;
return false;
}
//比较两个矩阵是否相等
public boolean equals(Matrix b) {
int i, j = 0;
if(this.value.length != b.value.length || this.value[0].length != b.value[0].length)
return false;
for(i=0; i<this.value.length; i++)
for(j=0; j<this.value[0].length; j++)
if(this.value[i][j] != b.value[j][i])
break;
if(i>=this.value.length)
return true;
return false;
}
//计算两个矩阵的乘积
public Matrix multiply(Matrix b){
int i, j, k;
int sum;
Matrix mtr;
if(this.value[0].length != b.value.length) {
return null;
}
mtr = new Matrix(this.value.length, b.value[0].length);
for(i=0; i<this.value.length; i++)
{
for(k=0; k<b.value[0].length; k++){
for(sum=0,j=0; j<this.value[0].length; j++){
sum += this.value[i][j] * b.value[j][k];
mtr.value[i][k] = sum;
}
}
}
return mtr;
}
}
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());
if(mtr2.isLowerTriangularMatrix())
System.out.println("下三角矩阵:\n" + mtr2.toString());
if(mtr3.isSymmetricMatrix())
System.out.println("对称矩阵:\n" + mtr3.toString());
System.out.println(mtr1.toString() + "\n乘以\n" + mtr2.toString() + "\n=\n");
Matrix tempM = mtr1.multiply(mtr2);
System.out.println(tempM.toString());
}
}
追问
我要的是以a,b矩阵为例子的,而且要注意健壮性,不过还是谢谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |