0
点赞
收藏
分享

微信扫一扫

JAVA第31天——整数矩阵及其运算

_刘彦辉 2022-02-02 阅读 60
矩阵java

IntMatrix

  • 矩阵相加
	/**
	 * 矩阵相加
	 * @param m1
	 * @param m2
	 * @return
	 */
	public static IntMatrix add(IntMatrix m1, IntMatrix m2) {

		// 检查异常
		int r1 = m1.matrix.length;
		int c1 = m1.matrix[0].length;
		int r2 = m2.matrix.length;
		int c2 = m2.matrix[0].length;
		if (r1 != r2) {
			throw new RuntimeException("两个矩阵的行高都不相等!");
		}
		if (c1 != c2) {
			throw new RuntimeException("两个矩阵的列宽都不相等!");
		}

		// 初始大小
		IntMatrix resultMatrix = new IntMatrix(r1, c1);

		// 计算
		for (int i = 0; i < m1.matrix.length; i++) {

			for (int j = 0; j < m2.matrix.length; j++) {

				resultMatrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
			}
		}

		return resultMatrix;
	}
  • 矩阵相乘
	/**
	 * 矩阵相乘
	 * @param m1
	 * @param m2
	 * @return
	 */
	public static IntMatrix multiply(IntMatrix m1, IntMatrix m2) {

		// 判断是否能够相乘
		if (m2.matrix[0].length != m1.matrix.length) {
			throw new RuntimeException("第一个矩阵的列宽不等于第二个矩阵的行高!");
		}

		// 初始resultMatrix的大小
		int r = m1.matrix.length;
		int c = m2.matrix[0].length;
		IntMatrix resultMatrix = new IntMatrix(r, c);

		// 计算
		for (int i = 0; i < m1.matrix.length; i++) {

			for (int j = 0; j < m2.matrix.length; j++) {

				for (int k = 0; k < m1.matrix[0].length; k++) {

					resultMatrix.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
				}
			}
		}

		return resultMatrix;
	}
  • 效果图

在这里插入图片描述

  • 源码
import java.util.Arrays;

public class IntMatrix {
	int[][] matrix;

	/**
	 * 自己设置宽高
	 * 
	 * @param row
	 * @param col
	 */
	public IntMatrix(int row, int col) {
		matrix = new int[row][col];
	}

	/**
	 * 以IntMatrix构造矩阵
	 * 
	 * @param paraMatrix
	 */
	public IntMatrix(IntMatrix paraMatrix) {
		for (int i = 0; i < paraMatrix.matrix.length; i++) {
			for (int j = 0; j < paraMatrix.matrix[0].length; j++) {
				matrix[i][j] = paraMatrix.matrix[i][j];
			}
		}
	}

	/**
	 * toString()
	 */
	public String toString() {
		return Arrays.deepToString(matrix);
	}

	public void setValue(int row, int col, int value) {
		matrix[row][col] = value;
	}

	public int getValue(int row, int col) {
		return matrix[row][col];
	}

	/**
	 * 矩阵相加
	 * @param m1
	 * @param m2
	 * @return
	 */
	public static IntMatrix add(IntMatrix m1, IntMatrix m2) {

		// 检查异常
		int r1 = m1.matrix.length;
		int c1 = m1.matrix[0].length;
		int r2 = m2.matrix.length;
		int c2 = m2.matrix[0].length;
		if (r1 != r2) {
			throw new RuntimeException("两个矩阵的行高都不相等!");
		}
		if (c1 != c2) {
			throw new RuntimeException("两个矩阵的列宽都不相等!");
		}

		// 初始大小
		IntMatrix resultMatrix = new IntMatrix(r1, c1);

		// 计算
		for (int i = 0; i < m1.matrix.length; i++) {

			for (int j = 0; j < m2.matrix.length; j++) {

				resultMatrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
			}
		}

		return resultMatrix;
	}
	
	/**
	 * 矩阵相乘
	 * @param m1
	 * @param m2
	 * @return
	 */
	public static IntMatrix multiply(IntMatrix m1, IntMatrix m2) {

		// 判断是否能够相乘
		if (m2.matrix[0].length != m1.matrix.length) {
			throw new RuntimeException("第一个矩阵的列宽不等于第二个矩阵的行高!");
		}

		// 初始resultMatrix的大小
		int r = m1.matrix.length;
		int c = m2.matrix[0].length;
		IntMatrix resultMatrix = new IntMatrix(r, c);

		// 计算
		for (int i = 0; i < m1.matrix.length; i++) {

			for (int j = 0; j < m2.matrix.length; j++) {

				for (int k = 0; k < m1.matrix[0].length; k++) {

					resultMatrix.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
				}
			}
		}

		return resultMatrix;
	}

}
举报

相关推荐

0 条评论