📄 matrix.java
字号:
import java.util.*;
import java.io.*;
/**
* Title:Matrix
* @author 刘志强
*/
/**
* class Matrix
*
*/
public class Matrix {
/**
* a private array data
*/
private int data[][];
/**
* @param m the row of Matrix
* @param n the column of Matrix
*/
public Matrix(int m,int n){
data = new int[m][n];
}
/**
* @param data the 2-D array is initialized
*/
public Matrix(int data[][]){
setData(data);
}
/**
* @param data the 2-D array is setted
*/
public void setData(int data[][]){
for(int i=0;i<data.length;i++){
for(int j=0;j<data[0].length;j++){
this.data = data;
}
}
}
/**
* @return the 2-D array
*/
public int[][] getData(){
return this.data;
}
/**
* @param row the row where the new element is placed
* @param column the column where the new element is placed
* @param element the new element
*/
public void setData(int row,int column,int element){
this.data[row][column] = element;
}
/**
* @return the element
*/
public int getData(int row,int column){
return this.data[row][column];
}
/**
* transpose the Matrix
* @return the result
*/
public Matrix transpose(){
int [][]data1 = new int[data[0].length][data.length];
for(int i=0;i<this.data.length;i++){
for(int j=0;j<this.data[0].length;j++){
data1[i][j] = this.data[j][i];
}
}
Matrix mt = new Matrix(data1);
return mt;
}
/**
* transpose the Matrix
* @return the result
*/
public static Matrix transpose(Matrix matrix){
return matrix.transpose();
}
/**
* Matrix addition
* @param other Matrix which is used to added to the Matrix
* @return the result
*/
public Matrix add(Matrix other){
int [][]data2 = new int[this.data.length][other.data[0].length];
if(this.data.length == other.data.length && this.data[0].length == other.data[0].length){
for(int i=0;i<this.data.length;i++){
for(int j=0;j<this.data[0].length;j++){
data2[i][j] = this.data[i][j] + other.data[i][j];
}
}
}
else
System.out.println("不符合矩阵相加条件");
Matrix mt = new Matrix(data2);
return mt;
}
/**
* Matrix addition
* @param first the first Matrix
* @param second the second Matrix
* @return the result
*/
public static Matrix add(Matrix first,Matrix second){
return first.add(second);
}
/**
* Matrix multiplication
* @param other Matrix which is used to multiplied to the Matrix
* @return the result
*/
public Matrix multiply(Matrix other){
int [][]data3 = new int[this.data.length][other.data[0].length];
if(data[0].length == other.data.length){
for(int i=0;i<this.data.length;i++){
for(int j=0;j<other.data[0].length;j++){
data3[i][j] = 0;
for(int k=0;k<other.data.length;k++){
data3[i][j] += this.data[i][k] * other.data[k][j];
}
}
}
}
else
System.out.println("不符合矩阵相乘条件");
Matrix mt=new Matrix(data3);
return mt;
}
/**
* Matrix multiplication
* @param first the first Matrix
* @param second the second Matrix
* @return the result
*/
public static Matrix multiply(Matrix first,Matrix second){
return first.multiply(second);
}
/**
* transform the Matrix to String type
* @return the stlye of String
*/
public String toString(){
String output = new String();
output = "[" + this.data.length + "][" + this.data[0].length + "] =" + "\n";
for(int i=0;i<this.data.length;i++){
for(int j=0;j<this.data[0].length;j++){
output += this.data[i][j] + "\t";
}
output += "\n";
}
return output;
}
/**
* get the 2-D array by random
*/
public void randomMatrix(){
Random ran = new Random();
for(int i=0;i<this.data.length;i++){
for(int j=0;j<this.data[0].length;j++)
this.setData(i, j, (int)(ran.nextInt(10)+ 1));
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
/**
* get the rows and columns from keyboard
* print the result
*/
System.out.print("输入矩阵的行列:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int rows = Integer.parseInt(s);
int columns= rows;
Matrix Mat1= new Matrix(rows,columns);
Mat1.randomMatrix();
System.out.println("矩阵A :" + Mat1);
System.out.println();
System.out.println("转置后的矩阵A:" + Mat1.transpose());
System.out.println();
Matrix Mat2 = new Matrix(rows,columns);
Mat2.randomMatrix();
System.out.println( "矩阵B :" + Mat2);
System.out.println();
System.out.println( "矩阵A + 矩阵B =" + Matrix.add(Mat1,Mat2));
System.out.println( "矩阵A * 矩阵B =" + Matrix.multiply(Mat1,Mat2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -