矩阵相乘的代码java java 矩阵相乘( 二 )


for(int k=0;klt;a;k++){
sum += x[i][k]*y[k][j] ;
}
result[i][j] = sum ;
}
}
}
return result ;
}
public void print(double[][] x){
System.out.println("矩阵为:");
for(int i=0;ilt;x.length;i++){
for(int j=0;jlt;x[i].length;j++){
System.out.print(x[i][j] + " ") ;
}
System.out.println();
}
}
}
测试类:
public class TestMatrix {
public static void main(String[] args) {
Matrix m = new Matrix() ;
//double[][] x = {{1,2},{3,2}} ;
//double[][] y = {{1,2,1},{2,3,3}} ;
System.out.println("创建第一个数组:") ;
double[][] x = m.create() ;
m.print(x) ; //用来验证输入的是否和你一样的,没啥作用
System.out.println("创建第二个数组:");
double[][] y = m.create() ;
m.print(y) ; //用来验证输入的是否和你一样的,没啥作用
double[][] result = m.multiply(x, y) ;
if(result == null){
return ; //如果输入的矩阵不能运算就不输出结果了 。
}
m.print(result) ;
}
}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念 , 因此Java语言具有功能强大和简单易用两个特征 。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论 , 允许程序员以优雅的思维方式进行复杂的编程 。Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等 。
要编写一个两个矩阵相乘的JAVA方法,本人不懂,求高手帮忙?。⊥蚍指屑ぃ 。?/h2>你好,按照你的要求代码如下,给出了注释和运行结果,可以直接运行:
public class test2 {
public static int[][] multiplyMatrix(int[][] a, int[][] b) {
// 判断是否合法
if (a == null || a == null || a.length == 0 || b.length == 0
|| a[0].length != b.length) {
return null;
}
// 计算相乘
int[][] c = new int[a.length][b[0].length];
for (int i = 0; ia.length; i++) {
for (int j = 0; jb[0].length; j++) {
for (int k = 0; ka[0].length; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
public static void main(String[] args) {
int[][] a = new int[][] { { 1, 2, 3 }, { 1, 2, 3 } };
int[][] b = new int[][] { { 1, 2 }, { 1, 2 }, { 1, 2 } };
int[][] c = multiplyMatrix(a, b);
printMatrix(a);
printMatrix(b);
printMatrix(c);
}
// 打印矩阵
public static void printMatrix(int[][] c) {
if (c != null) {
for (int i = 0; ic.length; i++) {
for (int j = 0; jc[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("无效");
}
System.out.println();
}
}
运行结果:
1 2 3
1 2 3
1 2
1 2
1 2
6 12
6 12
【矩阵相乘的代码java java 矩阵相乘】矩阵相乘的代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java 矩阵相乘、矩阵相乘的代码java的信息别忘了在本站进行查找喔 。

推荐阅读