基本矩阵运算的Java实现
一: 矩阵的加法与减法
规则:矩阵的加法与减法要求两个矩阵的行列完全相等,方可以完成两个矩阵的之间的运算。
举例说明如下

二:矩阵的乘法
规则:矩阵的乘法要求两个矩阵符合A(mx k), B( k x n)即矩阵A的列数与矩阵B的行数相等,否
则无法完成矩阵运算。举例说明如下:

Java代码如下:
- package pet.shop;
- public class BasicMatrixMath {
- public final static int OPERATION_ADD = 1;
- public final static int OPERATION_SUB = 2;
- public final static int OPERATION_MUL = 4;
- /**
- * To be able to add two matrices, they must be of the same size
- * @param matrixa
- * @param matrixb
- */
- public int[][] add(int[][] matrixa, int[][] matrixb) {
- if(legalOperation(matrixa, matrixb, OPERATION_ADD)) {
- for(int i=0; i<matrixa.length; i++) {
- for(int j=0; j<matrixa[0].length; j++) {
- matrixa[i][j] = matrixa[i][j] + matrixb[i][j];
- }
- }
- }
- return matrixa;
- }
- /**
- * To be able to substract two matrices, they must be of the same size
- *
- * @param matrixa
- * @param matrixb
- */
- public int[][] substract(int[][] matrixa, int[][] matrixb) {
- if(legalOperation(matrixa, matrixb, OPERATION_SUB)) {
- for(int i=0; i<matrixa.length; i++) {
- for(int j=0; j<matrixa[0].length; j++) {
- matrixa[i][j] = matrixa[i][j] - matrixb[i][j];
- }
- }
- }
- return matrixa;
- }
- /**
- *
- * @param matrixa
- * @param matrixb
- */
- public int[][] multiplication(int[][] matrixa, int[][] matrixb) {
- if(legalOperation(matrixa, matrixb, OPERATION_SUB)) {
- int[][] result = new int[matrixa.length][matrixb[0].length];
- for(int i=0; i<matrixa.length; i++) {
- for(int j=0; j<matrixb[0].length; j++) {
- // i will complete this tomorrow @2012/09/17
- result[i][j] = calculateSingleResult(matrixa, matrixb, i, j);
- }
- }
- return result;
- }
- else
- {
- return null;
- }
- }
- private int calculateSingleResult(int[][] matrixa, int[][] matrixb, int row, int col) {
- int result = 0;
- for(int k=0; k<matrixa[0].length; k++) {
- result += matrixa[row][k] * matrixb[k][col];
- }
- return result;
- }
- /**
- *
- * @param matrixa
- * @param b
- */
- public int[][] multiplication(int[][] matrixa, int b) {
- for(int i=0; i<matrixa.length; i++) {
- for(int j=0; j<matrixa[0].length; j++) {
- matrixa[i][j] = matrixa[i][j] * b;
- }
- }
- return matrixa;
- }
- /**
- * validate whether the parameters is valid parameters.
- *
- * @param a
- * @param b
- * @param type
- * @return
- */
- private boolean legalOperation(int[][] a, int[][] b, int type) {
- boolean legal = true;
- if(type == OPERATION_ADD || type == OPERATION_SUB)
- {
- if(a.length != b.length || a[0].length != b[0].length) {
- legal = false;
- }
- }
- else if(type == OPERATION_MUL)
- {
- if(a[0].length != b.length) {
- legal = false;
- }
- }
- return legal;
- }
- /**
- * test code here !!!!
- * @param args
- */
- public static void main(String[] args) {
- int[][] a = new int[][]{{1,2},{3,4}};
- int[][] b = new int[][]{{7, 8}, {6, 5}};
- BasicMatrixMath bmm = new BasicMatrixMath();
- System.out.println("addition two matrix");
- int[][] result = bmm.add(a, b);
- for(int i=0; i<result.length; i++) {
- for(int j=0; j<result[0].length; j++) {
- System.out.print("\t" + result[i][j]);
- }
- System.out.println();
- }
- System.out.println("substract two matrix");
- result = bmm.substract(a, b);
- for(int i=0; i<result.length; i++) {
- for(int j=0; j<result[0].length; j++) {
- System.out.print("\t" + result[i][j]);
- }
- System.out.println();
- }
- System.out.println("multiplex one matrix");
- result = bmm.multiplication(a, 3);
- for(int i=0; i<result.length; i++) {
- for(int j=0; j<result[0].length; j++) {
- System.out.print("\t" + result[i][j]);
- }
- System.out.println();
- }
- System.out.println("multiplex two matrix");
- result = bmm.multiplication(a, b);
- for(int i=0; i<result.length; i++) {
- for(int j=0; j<result[0].length; j++) {
- System.out.print("\t" + result[i][j]);
- }
- System.out.println();
- }
- }
- }
基本矩阵运算的Java实现的更多相关文章
- Java第三方工具库/包汇总
一.科学计算或矩阵运算库 科学计算包: JMathLib是一个用于计算复杂数学表达式并能够图形化显示计算结果的Java开源类库.它是Matlab.Octave.FreeMat.Scilab的一个克隆, ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java调用jama实现矩阵运算
Java调用jama实现矩阵运算 一.jama简介 Jama是一个基本的线性代数java包.包括一个基本的Matrix类和5个矩阵分解类. Matrix类提供了基本的线性代数数值运算的功能,不同的构造 ...
- Java读取mat文件
概述 使用ujmp中的jmatio模块读取.mat文件到java程序中. 其实,ujmp主要是在模块core中实现了矩阵运算,其余模块都是复用了已有的开源库.模块jmatio是复用了已有的JMatIo ...
- JAMA:Java矩阵包
原文链接:JAMA:Java矩阵包 API文档链接:线性代数Java包 JAMA jama是一个非常好用的java的线性代数软件包.适用于日常编程可能碰到的各种矩阵运算问题,提供了一个优雅的简便的解决 ...
- 用java写bp神经网络(一)
根据前篇博文<神经网络之后向传播算法>,现在用java实现一个bp神经网络.矩阵运算采用jblas库,然后逐渐增加功能,支持并行计算,然后支持输入向量调整,最后支持L-BFGS学习算法. ...
- 158个JAVA免豆精品资料汇总
附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...
- 【转】【JAVA资料免费下载】158个JAVA免豆精品资料汇总——下载目录
附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...
- JavaSE基础之矩阵运算
JavaSE基础之矩阵运算 1.矩阵类:Matrix.java 包括矩阵的加.乘运算,行列式的求解,最大最小元素等 package cn.com.zfc.help; import java.text. ...
随机推荐
- json.stringfy()和json.parse()
json.stringfy()将对象.数组转换成字符串:json.parse()将字符串转成json对象. json.stringfy(): 语法: JSON.stringify(value [, ...
- 慕课网__css3__3D
- mybatis mysql 调用视图
java代码 @RequestMapping(value = "/testView", method = RequestMethod.GET) public @ResponseBo ...
- MyISAM与InnoDB区别
两种类型最主要的差别就是Innodb 支持事务处理与外键和行级锁.而MyISAM不支持.所以MyISAM往往就容易被人认为只适合在小项目中使用. 我作为使用MySQL的用户角度出发,Innodb和My ...
- css学习记录
1 !important 表示此属性需要优先考虑: <head> <title>Page Title</title> <style type=&q ...
- GoF--观察者模式
观察者模式定义了对象间的一对多依赖关系,让一个或多个观察者对象观察一个主题对象.当主题对象的状态发生变化时,系统恩那个通知所有的依赖于此对象观察者对象,从而使得观察者对象能够自动更新. 在观察者模式中 ...
- API -- java.lang.Integer
java.lang Class Integer static Integer valueOf(int i) Returns an Integer instance representing the s ...
- Inverted sentences
And ever has it been that love knows not its own depth until the hour of separation. 除非临到了别离的时候,爱永远 ...
- 破解YunFile下载间隔10分钟/下载等待30秒
[破解10分钟间隔] 可以采用断网重连等方法重新获取IP地址,就不用再等十分钟了 [破解30秒等待] 收藏一个书签,书签地址如下 javascript:var downpage_link = docu ...
- Windows添加和取消右键管理员权限
亲测可用 新建文本文档,粘贴下列代码 Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\runas]@="管理员取 ...