用java怎么写矩阵乘法?

Python013

用java怎么写矩阵乘法?,第1张

import java.util.Scanner 

public class Matrix { 

public double[][] create() { 

Scanner sc = new Scanner(System.in)  

System.out.print("请输入矩阵的行高:"); 

int a = sc.nextInt()  

System.out.print("请输入矩阵的列宽:"); 

int b = sc.nextInt()  

double[][] x = new double[a][b]  

for(int i=0i&ltai++){ 

for(int j=0j&ltbj++){ 

System.out.print("请输入元素x["+i+"]["+j+"]的值:" ); 

x[i][j] = sc.nextDouble()  

return x  

public double[][] multiply(double[][] x,double[][] y){ 

double[][] result = null  

int a = x[0].length  

int b = y.length  

if(a != b){ 

System.out.println("输入的维数不匹配,不能进行运算"); 

}else{ 

int c = x.length  

int d = y[0].length  

result = new double[c][d]  

for(int i=0i&ltci++){ 

for(int j=0j&ltdj++){ 

double sum = 0  

for(int k=0k&ltak++){ 

sum += x[i][k]*y[k][j]  

result[i][j] = sum  

return result  

public void print(double[][] x){ 

System.out.println("矩阵为:"); 

for(int i=0i&ltx.lengthi++){ 

for(int j=0j&ltx[i].lengthj++){ 

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应用程序、分布式系统和嵌入式系统应用程序等。

/**

 * 矩阵:由 m × n 个数Aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵

 * 说白了就是一个二维数组,下面的程序用整形作为数据类型,其他类型运算大同小异

 * 

 */

public class MatrixUtils {

    /**

     * 矩阵运算:加(减法与之类似)

     */

    public static int[][] matrixAdd(int[][] addend, int[][] summand) {

        if (addend == null || addend.length == 0) {

            throw new IllegalArgumentException("addend matrix is empty!")

        }

        if (summand == null || summand.length == 0) {

            throw new IllegalArgumentException("summand matrix is empty!")

        }

        //矩阵加减要求两个矩阵类型一致,即行列数相同

        int row = addend.length

        int col = addend[0].length

        if (row != summand.length || col != summand[0].length) {

            throw new IllegalArgumentException("summand and summand not the same type!")

        }

        int[][] sum = new int[row][col]

        for (int i = 0 i < row i++) {

            for (int j = 0 j < col j++) {

                sum[i][j] = addend[i][j] + summand[i][j]

                // sum[i][j] = addend[i][j] - summand[i][j] //减法

            }

        }

        return sum

    }

    /**

     * 矩阵运算:乘法,没找到除法的运算规则

     */

    public static int[][] matrixMultiply(int[][] addend, int[][] summand) {

        if (addend == null || addend.length == 0) {

            throw new IllegalArgumentException("addend matrix is empty!")

        }

        if (summand == null || summand.length == 0) {

            throw new IllegalArgumentException("summand matrix is empty!")

        }

        //两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵 

        int row = addend.length

        int col = summand[0].length

        if (addend[0].length != summand.length) {

            throw new IllegalArgumentException("summand and summand not the same type!")

        } 

        int[][] sum = new int[row][col]

        for (int i = 0 i < row i++) {

            for (int j = 0 j < col j++) {

                for (int z = 0 z < addend[0].length z++) {

                    sum[i][j] += addend[i][z] * summand[z][j]

                    System.out.println("sum[" + i+  "]["+ j+"]= " + sum[i][j])

                }

            }

        }

        return sum

    }

}

有两个错误:

一、Matrix 类的构造方法写的有问题:改成这样:

public Matrix(int m, int n) {

this.m = m

this.n = n

this.ma = new int[m][n]

}

二、如果你发现输入和输出的不一致的话,把Matrix类的print()方法:

循环打印的那行代码改成 System.out.print(ma[i][j] + " ")

也就是单引号改成双引号 ,单引号空格 如果和数字相加回转成int,值为:32