Java——矩阵类

时间:2025-02-17 10:23:44
  • import ;
  • import .*;
  • class Matrix{
  • private int row;//行
  • private int col;//列
  • //private double value;
  • double [][]Data;
  • public Matrix(int row, int col,double [][]Data) {
  • this.row = row;
  • this.col = col;
  • // = value;
  • this.Data = Data;
  • }
  • public void setMatrix(int row , int col, double value) {
  • this.Data[row - 1][col - 1] = value;
  • }
  • public double getMatrix(int row, int col) {
  • return Data[row - 1][col - 1] ;
  • }
  • public int width() {
  • return row;
  • }
  • public int height() {
  • return col;
  • }
  • public Matrix add(Matrix b) {
  • if(this.width() != () && this.height() != ()) {
  • return null;
  • }
  • double add[][] = new double[this.row][this.col];
  • for(int i = 0;i<col;i++) {
  • for(int j = 0;j<row;j++) {
  • add[i][j] = this.Data[i][j] + [i][j];
  • }
  • }
  • Matrix another = new Matrix(this.col,this.row,add);
  • ("after add:");
  • return another;
  • }
  • public Matrix multiply(Matrix b) {
  • if(this.col != ) {
  • return null;
  • }
  • double mul[][] = new double[this.row][];
  • double temp = 0;
  • for(int i = 0;i<this.row;i++) {
  • for(int k = 0;k<;k++) {
  • for(int j = 0;j<this.col;j++)
  • {
  • temp += this.Data[i][j] * [j][k];
  • }
  • mul[i][k] = temp;
  • temp = 0;
  • }
  • }
  • Matrix another = new Matrix(this.row, , mul);
  • ("after multiply:");
  • return another;
  • }
  • public Matrix transpose() {
  • double tran[][] = new double[this.row][this.col];
  • for(int i = 0;i<this.row;i++) {
  • for(int j = 0;j<this.col;j++) {
  • tran[j][i] = this.Data[i][j];
  • }
  • }
  • Matrix another = new Matrix(this.col,this.row,tran);
  • ("after transpose:");
  • return another;
  • }
  • public String toString() {
  • DecimalFormat df = new DecimalFormat("0");
  • String result = "";
  • //result += (Data[0][0]);
  • for(int i = 0;i<this.row;i++) {
  • result += (Data[i][0]);
  • for(int j = 1;j<this.col;j++) {
  • result += " " + (Data[i][j]);
  • }
  • result += "\n";
  • }
  • return result;
  • }
  • }
  • public class Main{
  • public static void main(String[] args) {
  • Scanner scan = new Scanner();
  • int row = ();
  • int col = ();
  • ("row:" + row +" column:" + col);
  • //set value
  • double data[][] = new double[row][col];
  • for(int i = 0;i<row;i++) {
  • for(int j = 0;j<col;j++) {
  • double d = ();
  • data[i][j] = d;
  • }
  • }
  • Matrix matrix = new Matrix(row,col,data);
  • int srow = ();
  • int scol = ();
  • double sv = ();
  • ("after set value:");
  • (srow, scol, sv);
  • (matrix);
  • //get value
  • DecimalFormat df = new DecimalFormat("0");
  • int vrow = ();
  • int vcol = ();
  • ("value on (" +vrow + "," + vcol +"):");
  • (((vrow, vcol)));
  • //add
  • int addrow = ();
  • int addcol = ();
  • double addMatrix[][] = new double[addrow][addcol];
  • for(int i = 0;i<addrow;i++) {
  • for(int j = 0;j<addcol;j++) {
  • double ad = ();
  • addMatrix[i][j] = ad;
  • }
  • }
  • Matrix add = new Matrix(addrow,addcol,addMatrix);
  • ((add));
  • // //mul
  • int mulrow = ();
  • int mulcol = ();
  • double mulMatrix[][] = new double[mulrow][mulcol];
  • for(int i = 0;i<mulrow;i++) {
  • for(int j = 0;j<mulcol;j++) {
  • double mu = ();
  • mulMatrix[i][j] = mu;
  • }
  • }
  • Matrix mul = new Matrix(mulrow,mulcol,mulMatrix);
  • //((add));
  • ((mul));
  • //transpose
  • (());
  • }
  • }