Java详细剖析矩阵乘法和加法运算

1.核心算法(在第三点会结合代码讲解):

1.1加法:

第一步我们要判断两个矩阵能不能相加,应该列出不能相加的异常情况,行和列不相同就不能相加。第二步行和列相同就把对应元素相加。

1.2乘法:

也要判断,只有第一个矩阵的列 = 第二个矩阵的行 才能相乘。对数学里面矩阵乘法相当熟悉,明白相乘因子位置,结果放到结果矩阵的对应位置。

2.实现

2.1准备工作

IntMatrix类

data数组 装数据
IntMatrix 三个构造方法
add 加法有关的两个方法
multiply 方法

代码:

/**
 * 
 */
package matrix;

import java.util.Arrays;

/**
 ****************************
 * TODO
 * 
 * @author Chen Fan
 * @version 1.0 time 2022年1月14日
 ****************************
 */
public class IntMatrix {

	// 一个二维数组来装数据
	int[][] data;

	// 第一个构造方法,按要求创建一个二维数组
	public IntMatrix(int pararows, int paraColumns) {
		data = new int[pararows][paraColumns];
	}

	// 第二个构造方法,复制一个矩阵
	public IntMatrix(int[][] paraMatrix) {
		data = new int[paraMatrix.length][paraMatrix[0].length];

		// 复制元素
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[0].length; j++) {
				data[i][j] = paraMatrix[i][j];
			} // Of j
		} // Ofi
	} // Of 第二个构造方法

	// 第三个构造方法,复制构造一个被给的矩阵
	public IntMatrix(IntMatrix paraMatrix) {
		this(paraMatrix.getData());
	} // Of 第三个构造方法

	// 得到一个单位矩阵,对角线的值全是一
	public static IntMatrix geIdentityMatrix(int paraRows) {
		IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
		for (int i = 0; i < paraRows; i++) {
			// 根据访问控制,可以直接访问resultMatrix的数据
			resultMatrix.data[i][i] = 1;
		} // Of i
		return resultMatrix;
	}

	// 重写toString方法
	public String toString() {
		return Arrays.deepToString(data);
	} // Of toSting

	// 获取我们的数据,注意返回是引用而非数据的副本
	public int[][] getData() {
		return data;
	}// Of getData

	// 获取行和列的方法
	public int getRows() {
		return data.length;
	} // Of getRows

	public int getColumns() {
		return data[0].length;
	}

	public void setValue(int paraRow, int paraColumn, int paraValue) {
		data[paraRow][paraColumn] = paraValue;
	} // Of getValue

	public void add(IntMatrix paraMatrix) throws Exception {
		// 第一步获取给定矩阵的数据
		int[][] tempData = paraMatrix.getData();

		// 第二步尺寸检查
		if (data.length != tempData.length) {
			throw new Exception(
					"Cannot add matrices. Rows not match: " + data.length + " vs. " + tempData.length + ".");
		} // Of if

		if (data[0].length != tempData[0].length) {
			throw new Exception(
					"Cannot add matrices. Rows not match: " + data[0].length + " vs. " + tempData[0].length + ".");
		} // Of if

		// 第三步添加元素
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[0].length; j++) {
				data[i][j] += tempData[i][j];
			} // Of for j
		} // Of for i
	} // Of add

	// 矩阵加法
	public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
		// 第一步克隆第一个矩阵
		IntMatrix resultMatrix = new IntMatrix(paraMatrix1);

		// 第二步,两个矩阵相加
		resultMatrix.add(paraMatrix2);

		return resultMatrix;
	} // Of add

	// 矩阵乘法
	public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
		// 第一步尺寸检测
		int[][] tempData1 = paraMatrix1.getData();
		int[][] tempData2 = paraMatrix2.getData();
		if (tempData1[0].length != tempData2.length) {
			throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. " + tempData2.length + ".");
		} // Of if

		// 第二步 创建结果矩阵
		int[][] resultData = new int[tempData1.length][tempData2[0].length];

		// 第三步相乘
		for (int i = 0; i < tempData1.length; i++) {
			for (int j = 0; j < tempData2[0].length; j++) {
				for (int k = 0; k < tempData1[0].length; k++) {
					resultData[i][j] += tempData1[i][k] * tempData2[k][j];
				} // Of for k
			} // Of for j
		} // Of for i

		// 第四步,构造矩阵对象
		IntMatrix resultMatrix = new IntMatrix(resultData);

		return resultMatrix;
	} // Of mutiply

	// 程序入口
	public static void main(String[] args) {
		IntMatrix tempMatrix1 = new IntMatrix(3, 3);
		tempMatrix1.setValue(0, 1, 1);
		tempMatrix1.setValue(1, 0, 1);
		tempMatrix1.setValue(1, 2, 1);
		tempMatrix1.setValue(2, 1, 1);
		System.out.println("The original matrix is: " + tempMatrix1);

		IntMatrix tempMatrix2 = null;
		try {
			tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
		} catch (Exception e) {
			System.out.println(e);
		} // try

		System.out.println("The square matrix is: " + tempMatrix2);

		IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
		try {
			tempMatrix3.add(tempMatrix1);
		} catch (Exception e) {
			System.out.println(e);
		} // Of try

		System.out.println("The connectivity matrix is: " + tempMatrix3);
	} // Of main

} // Of class IntMatrix

运行结果:

 三个元素是一行,需要我们自己写成矩阵。

2.2问答环节

2.2.1 为什么要用三个构造方法?

答:如果你能读懂程序,不难发现,我们三个构造方法分别是:先初始化二维数组,再向里面投入元素,然后再复制一个矩阵类。前两个可以写在一起,但是参数长不符合常规,所以选择分开,这也是java的多态思想。

2.2.2 为什么要用两个有关的add方法?

答:第一个add我们主要用来判断,符合条件可以将元素复制一遍,方便后面的加法方法运算。

2.2.3 Arrays.deepToString(data)是怎么用的

这就是为二维数组遍历用的,一行一行去读。输出入上图结果

3.加法乘法算法图解

3.1乘法运算

 

	// 矩阵乘法
	public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
		// 第一步尺寸检测
		int[][] tempData1 = paraMatrix1.getData();
		int[][] tempData2 = paraMatrix2.getData();
		if (tempData1[0].length != tempData2.length) {
			throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. " + tempData2.length + ".");
		} // Of if

		// 第二步 创建结果矩阵
		int[][] resultData = new int[tempData1.length][tempData2[0].length];

		// 第三步相乘
		for (int i = 0; i < tempData1.length; i++) {
			for (int j = 0; j < tempData2[0].length; j++) {
				for (int k = 0; k < tempData1[0].length; k++) {
					resultData[i][j] += tempData1[i][k] * tempData2[k][j];
				} // Of for k
			} // Of for j
		} // Of for i

		// 第四步,构造矩阵对象
		IntMatrix resultMatrix = new IntMatrix(resultData);

		return resultMatrix;
	} // Of mutiply

 三个for循环:第一个是控制第一个矩阵的行,第二个是控制第二个矩阵的列,第三个是第一个矩阵的行中的元素乘以第二个矩阵的列元素。注意 结果元素 充分利用三个循环,最好自己拿笔跟着程序算一下。

3.2加法

 

	public void add(IntMatrix paraMatrix) throws Exception {
		// 第一步获取给定矩阵的数据
		int[][] tempData = paraMatrix.getData();

		// 第二步尺寸检查
		if (data.length != tempData.length) {
			throw new Exception(
					"Cannot add matrices. Rows not match: " + data.length + " vs. " + tempData.length + ".");
		} // Of if

		if (data[0].length != tempData[0].length) {
			throw new Exception(
					"Cannot add matrices. Rows not match: " + data[0].length + " vs. " + tempData[0].length + ".");
		} // Of if

		// 第三步添加元素
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[0].length; j++) {
				data[i][j] += tempData[i][j];
			} // Of for j
		} // Of for i
	} // Of add

	// 矩阵加法
	public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
		// 第一步克隆第一个矩阵
		IntMatrix resultMatrix = new IntMatrix(paraMatrix1);

		// 第二步,两个矩阵相加
		resultMatrix.add(paraMatrix2);

		return resultMatrix;
	} // Of add

两个for循环:就是把相同位置相加,注意此处都用到了一个结果矩阵。目的就是为了不破坏原来的矩阵。如: a=a+b;a的值就改变了,以后要进行乘法就会出错,数据被覆盖了。所以我们一班采取c = a + b;

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值