📄 matrix.java.bak
字号:
public class Matrix{
private int h;//成员变量,矩阵行数
private int w;//成员变量,矩阵列数
private int[][] matrix;
public Matrix(int h,int w){//构造函数,h为行数,w为列数
if(!(h > 0 && w > 0))
throw new ArrayIndexOutOfBoundsException("h or w < " + 1);
matrix = new int[h][w]; //创建有h行的对象
this.h = h;
this.w = w;
}
public void set(int row,int col,int value){//置元素
if(!(row >= 0 && w >= 0 && row < h && col <w))
throw new ArrayIndexOutOfBoundsException("h or w < " + "-1");
matrix[row][col]=(int)value;
}
public Object get(int row,int col){ //取元素
if(!(row >= 0 && w >= 0 && row < h && col <w))
throw new ArrayIndexOutOfBoundsException("h or w < " + "-1");
return matrix[row][col];
}
public int width(){ //矩阵列数
return w;
}
public int height(){//矩阵行数
return h;
}
public Matrix add(Matrix b){//矩阵加
if(height() != b.height() || width()!= b.width()){
throw new ArrayIndexOutOfBoundsException("Matrix h and w error");
}
Matrix result = new Matrix(height(),width());
for(int i = 0;i < height();i ++){
for(int j = 0;j < width(); j ++){
Integer i1 = (Integer)get(i,j);
Integer i2 = (Integer)(b.get(i,j));
result.set(i,j,new Integer(i1.intValue()+i2.intValue()));
}
}
return result;
}
public Matrix minus(Matrix b){//矩阵减
if(height() != b.height() || width()!= b.width()){
throw new ArrayIndexOutOfBoundsException("Matrix h and w error");
}
Matrix result = new Matrix(height(),width());
for(int i = 0;i < height();i ++){
for(int j = 0;j < width(); j ++){
Integer i1 = (Integer)get(i,j);
Integer i2 = (Integer)(b.get(i,j));
result.set(i,j,new Integer(i1.intValue()-i2.intValue()));
}
}
return result;
}
public Matrix mul(Matrix b){//矩阵乘
if(width()!=b.height()){
throw new ArrayIndexOutOfBoundsException("Matrix h and w error");
}
Matrix result = new Matrix(height(),b.width());
for (int i = 0; i < height(); i++) {
for (int j = 0; j < b.width(); j++) {
int sum = 0;
for (int k = 0; k < width(); k++) {
Integer i1 = (Integer)get(i,k);
Integer i2 = (Integer)(b.get(k,j));
sum =sum+i1.intValue()*i2.intValue();
}
result.set(i,j,sum);
}
}
return result;
}
public Matrix turn(){//矩阵转置
Matrix result = new Matrix(width(),height());
for(int i = 0;i < height();i ++){
for(int j = 0;j < width(); j ++){
Integer i1 = (Integer)get(j,i);
result.set(i,j,new Integer(i1.intValue()));
}
}
return result;
}
public Matrix det(int m, int n){ //求行列式的amn的余子式
Matrix result = new Matrix(h-1,w-1);
if (m > h || n > w){
throw new ArrayIndexOutOfBoundsException("求行列式的a" + m + n + "的余子式时参数错误.");
}
if(h!=w){
throw new ArrayIndexOutOfBoundsException("不是方阵,不存在余子式。");
}
for (int i = 0,a = 0; i < h; i++){ //a为新行
if ((m-1) != i){
for (int j = 0, b = 0; j <w; j++){ //b为新列
if ((n - 1) != j){
Integer i1 = (Integer)get(i,j);
result.set(a,b,new Integer(i1.intValue()));
b++;
}
}
a++;
}
}
return result;
}
public double getvalue(){ //求行列式的值
int result=0;
if(h==w){
if(h==1){
return matrix[0][0];
}
else{
for(int i=0;i<w;i++){
Matrix temp=det(1,i+1);
if ((i+2) % 2 == 1){
result += -1*matrix[0][i] * temp.getvalue(); }
else{
result += matrix[0][i] * temp.getvalue();
}
}
return result;
}
}
else{
throw new ArrayIndexOutOfBoundsException("不是方阵,不能求值。");
}
}
public Matrix bansui(){ //求伴随矩阵
if(h!=w){
throw new ArrayIndexOutOfBoundsException("不是方阵,不存在伴随矩阵。");
}
Matrix result=new Matrix(h,w);
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
int temp=(int)(this.det(i+1,j+1).getvalue());
if((i+j+2)%2==1){
result.set(j,i,-1*temp);}
else{
result.set(j,i,temp);}
}
}
return result;
}
public Matrix ni(){ //求逆矩阵
if(h!=w){
throw new ArrayIndexOutOfBoundsException("不是方阵,不存在逆矩阵。");
}
if(this.getvalue()==0){
throw new ArrayIndexOutOfBoundsException("行列式值为0,逆矩阵不存在。");
}
Matrix result=new Matrix(h,w);
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
int temp=(int)(this.matrix[i][j]/(this.getvalue()));
result.set(i,j,temp);
}
}
return result;
}
public void print(){ //输出矩阵元素
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -