Matrix operation of simple algorithm

Not much to say about the code:
matrix object

class Matrix {
    
    
	// 矩阵的宽度
	private final int width;
	// 矩阵的高度
	private final int height;
	// 矩阵
	private final int[][] arr;
	
	Matrix(int width, int height) {
    
    
		this.width = width;
		this.height = height;
		arr = new int[height][width];
	}
	
	Matrix(int width, int height, int[][] arr) {
    
    
		this.width = width;
		this.height = height;
		this.arr = arr;
	}
	
	public int getWidth() {
    
    
		return width;
	}
	public int getHeight() {
    
    
		return height;
	}
	public int[][] getArr() {
    
    
		return arr;
	}
	public void setArrVal(int x, int y, int val) {
    
    
		arr[x][y] = val;
	}
	public boolean compareTo(Matrix matrix) {
    
    
		// 1 & 1 = true, 两个位置任何位置为0返回false,加快运算
		return this.width == matrix.width 
				& this.height == matrix.height;
	}
}

matrix object factory

/**
 * 创建对象
 * @author zygswo
 *
 */
public class MatrixFactory {
    
    
	private static final int MAX_SIZE = 1 << 30;
	/**
	 * 创建一个矩阵
	 */
	public static Matrix getInstance(int width, int height) {
    
    
		if (width < 0 || width > MAX_SIZE) {
    
    
			throw new IllegalArgumentException("矩阵宽度有误");
		}
		if (height < 0 || height > MAX_SIZE) {
    
    
			throw new IllegalArgumentException("矩阵高度有误");
		}
		return new Matrix(width, height);
	}
	
	public static Matrix getInstance(int width, int height, int[][] arr) {
    
    
		if (width < 0 || width > MAX_SIZE) {
    
    
			throw new IllegalArgumentException("矩阵宽度有误");
		}
		if (height < 0 || height > MAX_SIZE) {
    
    
			throw new IllegalArgumentException("矩阵高度有误");
		}
		return new Matrix(width, height, arr);
	}
}

Matrix manipulation tools

package matrixUtils;

import java.util.Arrays;

/**
 * 矩阵运算
 * @author zyg1
 */
public class MatrixUtils {
    
    
	
	/**
	 * add
	 * @param prevMatrix 上一个矩阵
	 * @param nextMatrix 下一个矩阵
	 * @return 添加后的矩阵
	 */
	public static Matrix add(Matrix prevMatrix, Matrix nextMatrix) {
    
    
		if (prevMatrix == null 
				|| nextMatrix == null) {
    
    
			throw new IllegalArgumentException("有矩阵为空");
		}
		if (!prevMatrix.compareTo(nextMatrix)) {
    
    
			throw new IllegalArgumentException("两个矩阵的尺寸不同,无法相加");
		}
		int width = prevMatrix.getWidth();
		int height = prevMatrix.getHeight();
		Matrix result = MatrixFactory
				.getInstance(width, height);
		for (int i = 0; i < height; i++) {
    
    
			for (int j = 0; j < width; j++) {
    
    
				result.setArrVal(i, j, 
						prevMatrix.getArr()[i][j] + nextMatrix.getArr()[i][j]);
			}
		}
		return result;
	}
	
	/**
	 * add
	 * @param matrix 矩阵
	 * @return 添加后的矩阵
	 */
	public static Matrix add(Matrix ... matrixList) {
    
    
		if (matrixList == null || matrixList.length < 2) {
    
    
			throw new IllegalArgumentException("矩阵为空或长度小于2");
		}
		Matrix base = matrixList[0];
		for (Matrix matrix: matrixList) {
    
    
			if (!matrix.compareTo(base)) {
    
    
				throw new IllegalArgumentException("矩阵的尺寸不同,无法相加");
			}
		}
		int width = base.getWidth();
		int height = base.getHeight();
		Matrix result = MatrixFactory
				.getInstance(width, height);
		for (int i = 0; i < height; i++) {
    
    
			for (int j = 0; j < width; j++) {
    
    
				int sum = 0;
				for(Matrix matrix: matrixList) {
    
    
					sum += matrix.getArr()[i][j];
				}
				result.setArrVal(i, j, sum);
			}
		}
		return result;
	}
	
	/**
	 * 矩阵相乘
	 * @param prevMatrix 第一个矩阵
	 * @param nextMatrix 第二个矩阵
	 * @return 相乘后的矩阵
	 */
	public static Matrix multiply(Matrix prevMatrix, Matrix nextMatrix) {
    
    
		if (prevMatrix == null 
				|| nextMatrix == null) {
    
    
			throw new IllegalArgumentException("有矩阵为空");
		}
		if (prevMatrix.getWidth() != nextMatrix.getHeight()) {
    
    
			throw new IllegalArgumentException("两个矩阵的尺寸不同,无法相乘");
		}
		// 新矩阵的宽度为第二个乘数矩阵的宽度,高度为第一个乘数矩阵的高度
		int width = nextMatrix.getWidth(); 
		int height = prevMatrix.getHeight();
		Matrix result = MatrixFactory
				.getInstance(width, height);
		int m = 0, sum = 0;
		// 注意,这里要根据第一个矩阵的宽、高来计算
		for (int i = 0; i < prevMatrix.getHeight();) {
    
    
			// 重置sum
			sum = 0;
			for (int j = 0; j < prevMatrix.getWidth(); j++) {
    
    
				sum += prevMatrix.getArr()[i][j] * nextMatrix.getArr()[j][m];
			}
			result.setArrVal(i, m, sum);
			m++;
			if (m == width) {
    
    
				m = 0;
				i++;
			}
		}
		return result;
	}
	
	/**
	 * 矩阵转置
	 * @param matrix 矩阵
	 * @return 转置后的矩阵
	 */
	public static Matrix matrixReverse(Matrix matrix) {
    
    
		if (matrix == null) {
    
    
			throw new IllegalArgumentException("矩阵为空");
		}
		int width = matrix.getWidth(); 
		int height = matrix.getHeight();
		Matrix result = MatrixFactory.getInstance(height, width);
		for (int i = 0; i < width; i++) {
    
    
			for (int j = 0; j < height; j++) {
    
    
				result.setArrVal(i, j, matrix.getArr()[j][i]);
			}
		}
		return result;
	}
	
	/**
	 * 代数余子式
	 * @param matrix 矩阵
	 * @param x 行
	 * @param y 列
	 * @return 代数余子式
	 */
	public static Matrix cofactor(Matrix matrix, int x ,int y) {
    
    
		if (matrix == null) {
    
    
			throw new IllegalArgumentException("矩阵为空");
		}
		int width = matrix.getWidth(); 
		int height = matrix.getHeight();
		if (width != height) {
    
    
			throw new IllegalArgumentException("矩阵长度宽度不一致");
		}
		Matrix result = MatrixFactory.getInstance(width-1, height-1);
		for (int i = 0; i < width; i++) {
    
    
			for (int j = 0; j < height; j++) {
    
    
				if (i == x || j == y) {
    
    
					continue;
				}
				int m = i > x ? i - 1 : i;
				int n = j > y ? j - 1 : j;
				result.setArrVal(m, n, matrix.getArr()[i][j]);
			}
		}
		return result;
	}
	
	/**
	 * 求行列式
	 * @param matrix 矩阵
	 * @return 矩阵行列式
	 */
	public static int MatrixDet(Matrix matrix) {
    
    
		if (matrix == null) {
    
    
			throw new IllegalArgumentException("矩阵为空");
		}
		int width = matrix.getWidth(); 
		int height = matrix.getHeight();
		if (width != height) {
    
    
			throw new IllegalArgumentException("矩阵长度宽度不一致");
		}
		return getMatrixDet(matrix, width);
	}
	
	/**
	 * 计算高阶矩阵行列式(递归)
	 * @param matrix 矩阵
	 * @param size 阶级
	 * @return 行列式
	 */
	private static int getMatrixDet(Matrix matrix, int size) {
    
    
		//根据不同阶级的行列式进行计算
		if (size <= 1) {
    
    
			return matrix.getArr()[0][0];
		} else if (size == 2) {
    
    
			return matrix.getArr()[0][0] * matrix.getArr()[1][1]
					- matrix.getArr()[0][1] * matrix.getArr()[1][0];
		} else {
    
    
			int sum = 0;
			for (int i = 0; i < size; i++) {
    
    
				Matrix cofactor = cofactor(matrix, 0 , i);
				int matrixDet = (int)(Math.pow(-1, i)) 
						* getMatrixDet(cofactor, size - 1);
				sum += matrix.getArr()[0][i] * matrixDet;
			}
			return sum;
		}
	}

	public static void main(String[] args) {
    
    
		Matrix m1 = MatrixFactory.getInstance(4,2,
				new int[][]{
    
    {
    
    1,2,3,4},{
    
    1,2,3,4}});
		Matrix m2 = MatrixFactory.getInstance(2,4,
				new int[][]{
    
    {
    
    1,2},{
    
    1,2},{
    
    1,2},{
    
    1,2}});
		Matrix m3 = MatrixFactory.getInstance(4,4,
				new int[][]{
    
    {
    
    1,2,3,4},{
    
    1,2,3,4},{
    
    1,2,3,4},{
    
    1,2,3,4}});
		System.out.println("----------矩阵相加-----------");
//		Matrix result = MatrixUtils.add(m1,m2,m3);
//		for (int i = 0; i < result.getHeight(); i++) {
    
    
//			System.out.println(Arrays.toString(result.getArr()[i]));
//		}
//		
		Matrix m11 = MatrixFactory.getInstance(3,2,
				new int[][]{
    
    {
    
    1,9,1},{
    
    3,1,1}});
//		Matrix m21 = MatrixFactory.getInstance(2,3,
//				new int[][]{
    
    {1,9},{3,1},{6,3}});
		System.out.println("----------矩阵相乘-----------");
		Matrix result2 = MatrixUtils.multiply(m1, m2);
		for (int i = 0; i < result2.getHeight(); i++) {
    
    
			System.out.println(Arrays.toString(result2.getArr()[i]));
		}
		// 求转置矩阵
		System.out.println("----------转置矩阵-----------");
		Matrix rev = MatrixUtils.matrixReverse(m11);
		for (int i = 0; i < rev.getHeight(); i++) {
    
    
			System.out.println(Arrays.toString(rev.getArr()[i]));
		}
		// 求代数余子式
		System.out.println("----------代数余子式-----------");
		Matrix cofactor = MatrixUtils.cofactor(m3, 1, 1);
		for (int i = 0; i < cofactor.getHeight(); i++) {
    
    
			System.out.println(Arrays.toString(cofactor.getArr()[i]));
		}
		// 求行列式
		System.out.println("----------行列式-----------");
		Matrix matrix = MatrixFactory.getInstance(2, 2, 
				new int[][] {
    
    {
    
    1,2},{
    
    -3,5}});
		Matrix matrix2 = MatrixFactory.getInstance(5, 5, 
				new int[][] {
    
    {
    
    1,2,3,4,5},{
    
    2,2,2,1,1},{
    
    3,1,2,4,5},{
    
    1,1,1,2,2},{
    
    4,3,1,5,0}});
		int det = MatrixUtils.MatrixDet(matrix2);
		System.out.println(det);
	}
	
}

Finally, the result of the operation
insert image description here

Guess you like

Origin blog.csdn.net/qq_31236027/article/details/124529042