用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

矩阵类

2018-10-14 作者: 不吃鱼举报

[java]代码库

import java.util.*; 
public class Main{ 
    public static void main(String[] args) { 
         Scanner scan = new Scanner(System.in);
         int row, col, i, j;
         double val;
         row = scan.nextInt();
         col = scan.nextInt();
         double [][] m = new double[row][col];
         for(i = 0; i < row; i++) {
        	 for(j = 0; j < col; j++) {
        		 val = scan.nextDouble();
        		 m[i][j] = val;
        	 }
         }
         System.out.println("row:" +row +" column:" +col);
         Matrix ma = new Matrix(row, col, m);
         
         row = scan.nextInt();
         col = scan.nextInt();
         val = scan.nextDouble();
         ma.set(row, col, val);
         System.out.println("after set value:");
         ma.print();
         
         row = scan.nextInt();
         col = scan.nextInt();
         System.out.print("value on (" +row +"," +col +"):");
         System.out.printf("%.0f", ma.get(row, col));
         System.out.println();
         
         row = scan.nextInt();
         col = scan.nextInt();
         double [][] m1 = new double[row][col];
         for(i = 0; i < row; i++) {
        	 for(j = 0; j < col; j++) {
        		 val = scan.nextDouble();
        		 m1[i][j] = val;
        	 }
         }
         Matrix ma1 = new Matrix(row, col, m1);
         System.out.println("after add:");
         ma.add(ma1).print();
         
         row = scan.nextInt();
         col = scan.nextInt();
         double [][] m2 = new double[row][col];
         for(i = 0; i < row; i++) {
        	 for(j = 0; j < col; j++) {
        		 val = scan.nextDouble();
        		 m2[i][j] = val;
        	 }
         }
         Matrix ma2 = new Matrix(row, col, m2);
         System.out.println("after multiply:");
         ma.multiply(ma2).print();
         
         System.out.println("after transpose:");
         ma.transpose().print();
         
         scan.close();  
    } 
}  

class Matrix{
	int row;
	int col;
	int i, j;
	double [][] ma;
	public Matrix(int r, int c, double [][]m) {
		row = r;
		col = c;
		ma = new double[row][col];
		ma = m;
	}
	public void set(int r, int c, double val) {
		ma[r-1][c-1] = val;
	}
	public double get(int r, int c) {
		return ma[r-1][c-1];
	}
	public int width() {
		return col;
	}
	public int height() {
		return row;
	}
	public Matrix add(Matrix b) {
		Matrix t = new Matrix(b.row, b.col, b.ma);
		for(i = 0; i < row; i++) {
			for(j = 0; j < col; j++)
				t.ma[i][j] = this.ma[i][j] + b.ma[i][j];
		}
		return t;
	}
	public Matrix multiply(Matrix b) {
		double [][] a= new double[row][b.col];
		for(i = 0; i < row; i++) {
			for(j = 0; j < b.col; j++) {
				double sum = 0;
				int k;
				for(k = 0; k < row; k++) {
					sum += this.ma[i][k]*b.ma[k][j];
				}
				a[i][j] = sum;
			}
		}
		Matrix t = new Matrix(row, b.col, a);
		return t;
		
	}
	public Matrix transpose(){
		double [][] a = new double[col][row];
		for(i = 0; i < col; i++) {
			for(j = 0; j < row; j++) {
				a[i][j] = this.ma[j][i];
			}
		}
		Matrix t = new Matrix(col, row, a);
		return t;
	}
	public void print(){
		for(i = 0; i < row; i++) {
			for(j = 0; j <col; j++) {
				if(j == 0) 
					System.out.printf("%.0f", ma[i][j]);
				else
				    System.out.printf(" %.0f", ma[i][j]);
			}
			System.out.println();
		}
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...