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(); |
} |
} |
} |