本文实例讲述了Java实现矩阵加减乘除及转制等运算功能。分享给大家供大家参考,具体如下:
Java初学,编写矩阵预算程序,当做工具,以便以后写算法时使用。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
public class MatrixOperation {
public static int [][] add( int [][] matrix_a, int [][] matrix_b) {
int row = matrix_a.length;
int col = matrix_a[ 0 ].length;
int [][] result = new int [row][col];
if (row != matrix_b.length || col != matrix_b[ 0 ].length) {
System.out.println( "Fault" );
} else {
for ( int i = 0 ; i < row; i++) {
for ( int j = 0 ; j < col; j++) {
result[i][j] = matrix_a[i][j] + matrix_b[i][j];
}
}
}
return result;
}
public static int [][] sub( int [][] matrix_a, int [][] matrix_b) {
int row = matrix_a.length;
int col = matrix_a[ 0 ].length;
int [][] result = new int [row][col];
if (row != matrix_b.length || col != matrix_b[ 0 ].length) {
System.out.println( "Fault" );
} else {
for ( int i = 0 ; i < row; i++) {
for ( int j = 0 ; j < col; j++) {
result[i][j] = matrix_a[i][j] - matrix_b[i][j];
}
}
}
return result;
}
public static int [][] dot( int [][] matrix_a, int [][] matrix_b) {
/*
* matrix_a's dimention m*p matrix_b's dimention p*n. return dimention
* m*n
*/
int row = matrix_a.length;
int col = matrix_a[0].length;
int[][] result = new int[row][col];
if (col != matrix_b.length) {
System.out.println("Fault");
} else {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = 0;
for (int k = 0; k < col; k++) {
result[i][j] += matrix_a[i][k] * matrix_b[k][j];
}
}
}
}
return result;
}
public static int[][] dot(int[][] matrix_a, int b) {
int row = matrix_a.length;
int col = matrix_a[0].length;
int[][] result = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = matrix_a[i][j] * b;
}
}
return result;
}
public static int[][] mul(int[][] matrix_a, int[][] matrix_b) {
/*
* matrix_a's dimention m*n matrix_b's dimention m*n. return dimention
* m*n
*/
int row = matrix_a.length;
int col = matrix_a[ 0 ].length;
int [][] result = new int [row][col];
if (row != matrix_b.length || col != matrix_b[ 0 ].length) {
System.out.println( "Fault" );
} else {
for ( int i = 0 ; i < row; i++) {
for ( int j = 0 ; j < col; j++) {
result[i][j] = matrix_a[i][j] * matrix_b[i][j];
}
}
}
return result;
}
public static int [][] transport( int [][] matrix_a) {
int row = matrix_a.length;
int col = matrix_a[ 0 ].length;
int [][] result = new int [row][col];
for ( int i = 0 ; i < row; i++) {
for ( int j = 0 ; j < col; j++) {
result[j][i] = matrix_a[i][j];
}
}
return result;
}
public static void print( int [][] matrix) {
int row = matrix.length;
int col = matrix[ 0 ].length;
for ( int i = 0 ; i < row; i++) {
System.out.print( "[" );
for ( int j = 0 ; j < col; j++) {
System.out.print(matrix[i][j]);
if (j != col - 1 ) {
System.out.print( ", " );
}
}
System.out.print( "]\\n" );
}
}
public static void main(String[] args) {
int [][] a = { { 1 , 2 }, { 3 , 4 } };
int [][] b = { { 7 , 8 }, { 6 , 5 } };
int [][] c = add(a, b);
System.out.println( "快网idc测试结果如下:" );
System.out.println( "matrix a = " );
print(a);
System.out.println( "matrix b = " );
print(b);
System.out.println( "matrix a + b = " );
print(c);
c = sub(a, b);
System.out.println( "matrix a - b = " );
print(c);
int [][] d = dot(a, b);
System.out.println( "matrix a dot b = " );
print(d);
int [][] e = dot(a, 3 );
System.out.println( "matrix a * 3 = " );
print(e);
int [][] f = transport(a);
System.out.println( "matrix a.T = " );
print(f);
int [][] g = mul(a, b);
System.out.println( "matrix a * b = " );
print(g);
}
}
|
运行结果:
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/miangangzhen/article/details/52275480
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 17
-
Linux系统如何安装和使用shell编写的工具supportconfig
2025-05-27 41 -
2025-05-29 90
-
2025-05-25 27
-
2025-05-29 87
热门评论