简单算法之矩阵运算

2023-11-09

不多说上代码:
矩阵对象

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;
	}
}

矩阵对象工厂

/**
 * 创建对象
 * @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);
	}
}

矩阵操作工具类

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);
	}
	
}

最后是运算结果
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

简单算法之矩阵运算 的相关文章

  • 从文本文件中读取阿拉伯字符

    我完成了一个项目 在该项目中我读取了用记事本编写的文本文件 我的文本文件中的字符是阿拉伯语 文件编码类型是UTF 8 当在 Netbeans 7 0 1 中启动我的项目时 一切似乎都正常 但是当我将项目构建为 jar 文件时 字符以这种方式
  • 将 MouseListener 添加到面板

    我正在尝试将鼠标操作添加到我的面板中 这就是程序应该做的事情 编写一个程序 允许用户通过按三下鼠标来指定一个三角形 第一次按下鼠标后 画一个小点 第二次按下鼠标后 绘制一条连接前两个点的线 第三次按下鼠标后 绘制整个三角形 第四次按下鼠标会
  • 最快的高斯模糊实现

    如何以最快的速度实施高斯模糊 http en wikipedia org wiki Gaussian blur算法 我要用Java来实现它 所以GPU http en wikipedia org wiki Graphics processi
  • Java 卡布局。多张卡中的一个组件

    一个组件 例如JLabel 在多张卡中使用CardLayout 目前看来该组件仅出现在它添加到的最后一张卡上 如果有办法做到这一点 我应该吗 这是不好的做法吗 或者有其他选择吗 你是对的 它只出现在 添加到的最后一张卡 中 但这与CardL
  • 本地开发的 Azure Functions 扩展包版本问题

    我有一个带有队列触发器的 Java 11 Azure 函数 该函数在部署到 Azure 时按预期工作 并正确从定义的服务总线主题中提取消息 但是 运行相同的功能locally除非我回滚版本 否则不起作用Azure Functions 绑定扩
  • 我需要在 JFileChooser(打开模式)中显示不带扩展名的文件名。如何?

    我在打开模式下使用 JFileChooser 我需要显示不带扩展名的 文件名 字段 如何 我知道文件视图 它删除文件系统文件中的扩展名 但将所选文件中的扩展名保留在 文件名 字段中解释 http saveimg ru show image
  • 记录共享和映射的诊断上下文

    据我所知 其他人做了什么来解决 Commons Logging 项目 针对 NET 和 Java 不支持映射或嵌套诊断上下文这一事实 执行摘要 我们选择直接使用实现者日志框架 在我们的例子中为 log4j 长答案 您是否需要一个抽象日志框架
  • 以有效的方式从 Map 中删除多个键?

    我有一个Map
  • 如何将 Java 地图转换为在 Scala 中使用?

    我正在开发一个 Scala 程序 该程序调用 Java 库中的函数 处理结果并生成 CSV 有问题的 Java 函数如下所示 Map
  • Struts 1 到 Spring 迁移 - 策略

    我有一个legacy银行应用程序编码为Struts 1 JSP现在的要求是迁移后端 目前为 MVC to Springboot MVC 后续UI JSP 将迁移到angular Caveats 1 后端不是无状态的 2 会话对象中存储了大量
  • 生成 equals 和 hashcode 时忽略属性

    假设我有一个类 Customer public class Customer private String firstName private String lastName private String doNotAddMeToEqual
  • 在 java 中运行外部应用程序但不要等待它完成

    我正在用java编写一个应用程序 允许我运行其他应用程序 为此 我使用了 Process 类对象 但当我这样做时 应用程序会等待进程结束 然后再退出 有没有办法在 Java 中运行外部应用程序 但不等待它完成 public static v
  • 为什么无法从 WEB-INF 文件夹内加载 POSModel 文件?

    我在我的 Web 项目中使用 Spring MVC 我将模型文件放在 WEB INF 目录中 String taggerModelPath WEB INF lib en pos maxent bin String chunkerModelP
  • C 与 C++ 中的 JNI 调用不同?

    所以我有以下使用 Java 本机接口的 C 代码 但是我想将其转换为 C 但不知道如何转换 include
  • 如何以编程方式创建 CardView

    我正在开发一个 Android 应用程序Java Android Studio 我想在活动中创建CardView以编程方式 我想将以下属性设置为CardView layout width wrap content layout row 0
  • Android UnityPlayerActivity 操作栏

    我正在构建一个 Android 应用程序 其中包含 Unity 3d 交互体验 我已将 Unity 项目导入 Android Studio 但启动时该 Activity 是全屏的 并且不显示 Android 操作栏 我怎样才能做到这一点 整
  • 使用 PC/SC 读卡器验证 Ultralight EV1

    我在尝试使用 Java 中的 PC SC 读卡器 特别是 ACR1222L 验证 Ultralight EV1 卡时遇到问题 我能够使用 ISO 14443 3 标签的相应 APDU 在不受保护的标签上进行写入和读取 但是 我找不到运行 P
  • java.lang.IllegalStateException - 提交响应后无法创建会话

    我在我的项目中使用 JSF PrimeFaces 我为此准备了一个Maven项目 当我编译项目并加载主页后 我收到以下异常 java lang IllegalStateException Cannot create a session af
  • 决策树和规则引擎 (Drools)

    In the application that I m working on right now I need to periodically check eligibility of tens of thousands of object
  • Java中单例的其他方式[重复]

    这个问题在这里已经有答案了 只是我在考虑编写单例类的其他方法 那么这个类是否被认为是单例类呢 public class MyClass static Myclass myclass static myclass new MyClass pr

随机推荐

  • redissonclient类_Redisson入门教程

    Redisson入门 Author RickyDate 2017 04 24 Redisson概述 Redisson是架设在Redis基础上的一个Java驻内存数据网格 In Memory Data Grid 充分的利用了Redis键值数据
  • MySQL数据库学习(保姆级教程)(1.7W字)

    1 初识MySQL JavaEE 企业级Java开发 Web 前端 页面 展示 数据 后台 连接点 连接数据库JDBC 链接前端 控制 控制视图跳转 和给前端传递数据 数据库 存数据 Txt Excel Word 只会写代码 学好数据库 基
  • buuctf web 前5题

    目录 一 极客大挑战 2019 EasySQL 总结 二 极客大挑战 2019 Havefun 总结 三 HCTF 2018 WarmUp 总论 四 ACTF2020 新生赛 Include 总结 五 ACTF2020 新生赛 Exec 总
  • 电脑cpu排名_2019年12月最新CPU天梯图 CPU性能排行榜

    参考国外评测机构PassMark的数据 下面排行榜比较了笔记本和台式电脑CPU的性能 截止更新时间为2019年12月5日 下方为排名前30的CPU天梯图 为方便大家查看更多CPU具体型号的排名和评分 请看天梯图后面的图表 注 电脑端可以使用
  • 投影变换 到 uv坐标 xy/w ---齐次坐标

    float3 vScreenPos In ClipPos xyz vScreenPos In ClipPos w vScreenPos xy 1 f vScreenPos xy 0 5f vScreenPos y 1 f vScreenPo
  • word 插入 高亮代码

    word 插入高亮代码 方法1 直接复制 IDE 中的内容 优 随时随地复制 保留vscode格式 缺 其他IDE的格式可能就不好看了 方法2 代码复制到网站 highlightcode com 高亮后再复制到word 缺 高亮做的不好看
  • 双向BFS搜索和A*算法

    双向BFS适合给出起点和终点 求最短路径的问题 分别从起点和终点扩展 找交点 每次选择待扩展节点少的那个方向进行扩展 一次扩展一层 扩展一个节点的时候 如果节点也在另一个方向的待扩展队列里 找到交点 int doubleBFS vector
  • 1001 害死人不偿命的(3n+1)猜想 (15 分)

    标题 include
  • 【Vue3】学习笔记-reactive响应式

    Vue3 学习笔记 reactive响应式 用ref 设置响应式对象 用reactive 设置响应式对象 总结 用ref 设置响应式对象 JS中设置对象 import ref from vue var user ref username W
  • 2022年美国大学生数学建模-【美赛】A题:Game Theory in Cycling(附获奖论文)

    目录 Summary 1 Introduction 1 1 Problem Background 1 2 Restatement of the Problem 1 3 Our Work 2 Assuptions and Justifific
  • comsol学习中心:几何建模

    创建二维几何 我们打算创建这样的二维模型 这里演示创建 因此不考虑物理场等的设置 创建空白模型 创建的是二维几何 所以在组件中选择天剑二维组件 也可以通过在功能树上右键进行此操作 接着在几何选项卡下找到体素开始构建几何 先添加一个圆形 在功
  • mybaits如何防止SQL注入:mybatis的${}和#{}

    其实这个mybatis的 和 区别和使用 算是很古早很常见的一个基础问题了 先说结论 尽可能使用 不使用 因为 可以防止SQL注入 如果记不清楚 就记一句话或者是口诀 不是所有事都能靠钱能解决 号是货币符号 为什么 可以防止SQL注入 占位
  • Source Insight设置黑色背景

    今天使用Source Insight看C代码 觉得背景白色太亮 觉得应该可以调背景颜色 通过百度 搜索到了CSDN上的相关文章 受益良多 但是文章后面附的style文件下载需30积分 无奈囊中羞涩 只好自己按照文章的说明调颜色 首先将背景调
  • 【基于python实现UI自动化】4.1 selenium发送163邮箱邮件

    python UI自动化 1 0 selenium工具介绍 2 0 selenium环境搭建 3 Selenium的元素定位 3 0 selenium常见8大元素定位 3 1 selenium通过By定位元素 3 2 selenium通过J
  • MySQL出现:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)问题解决

    本文mysql的安装环境为win7 64位 mysql版本为MySQL5 7 问题描述 在命令行输入 mysql u root p 登录mysql 返回 Can t connect to MySQL server on localhost
  • 【不忘初心】Windows11 22000.168 X64 四合一[纯净精简版][2.77G](2021.8.29)

    此版可正常更新补丁 WIN11全新的UI界面出炉 可以说这一次Windows 11全新升级 无论是从Logo上还是UI界面设计 都有很大的变化 母版来自UUP WIN11 22000 168 为了保证稳定初心的系统全部都是离线精简和优化 非
  • MySQL常用配置详解

    目录 一 MySQL 查看配置信息 二 MySQL 查看服务器当前运行状态的信息 三 MySQL 常用配置详解 1 mysql 使用mysql命令登录数据库时的默认的设置 2 client 客户端默认设置内容 3 mysqld 服务端端配置
  • centos 6.4/redhat 6.4 安装gitlab

    为什么80 的码农都做不了架构师 gt gt gt 一 把所有包升级到最新版本 yum y upgrade 二 安装最新版ruby 2 1 5 步骤http my oschina net duolus blog 348353 三 安装官方给
  • MATLAB对RGB彩色图像进行加马赛克处理

    简单实现MATLAB对RGB彩色图像进行加马赛克处理 为了加深对图像中像素块操作的记忆 利用像素块内均值方式对RGB彩色图像进行马赛克效果的处理 为了能后比较简单得实现 所以采用了n n像素块大小的均值的方式 为了比较简单实现 这里对RGB
  • 简单算法之矩阵运算

    不多说上代码 矩阵对象 class Matrix 矩阵的宽度 private final int width 矩阵的高度 private final int height 矩阵 private final int arr Matrix in