一: 矩阵的加法与减法

规则:矩阵的加法与减法要求两个矩阵的行列完全相等,方可以完成两个矩阵的之间的运算。

举例说明如下

二:矩阵的乘法

规则:矩阵的乘法要求两个矩阵符合A(mx k),  B( k x n)即矩阵A的列数与矩阵B的行数相等,否

则无法完成矩阵运算。举例说明如下:

Java代码如下:

[java] view plaincopy

 
  1. package pet.shop;
  2. public class BasicMatrixMath {
  3. public final static int OPERATION_ADD = 1;
  4. public final static int OPERATION_SUB = 2;
  5. public final static int OPERATION_MUL = 4;
  6. /**
  7. * To be able to add two matrices, they must be of the same size
  8. * @param matrixa
  9. * @param matrixb
  10. */
  11. public int[][] add(int[][] matrixa, int[][] matrixb) {
  12. if(legalOperation(matrixa, matrixb, OPERATION_ADD)) {
  13. for(int i=0; i<matrixa.length; i++) {
  14. for(int j=0; j<matrixa[0].length; j++) {
  15. matrixa[i][j] = matrixa[i][j] + matrixb[i][j];
  16. }
  17. }
  18. }
  19. return matrixa;
  20. }
  21. /**
  22. * To be able to substract two matrices, they must be of the same size
  23. *
  24. * @param matrixa
  25. * @param matrixb
  26. */
  27. public int[][] substract(int[][] matrixa, int[][] matrixb) {
  28. if(legalOperation(matrixa, matrixb, OPERATION_SUB)) {
  29. for(int i=0; i<matrixa.length; i++) {
  30. for(int j=0; j<matrixa[0].length; j++) {
  31. matrixa[i][j] = matrixa[i][j] - matrixb[i][j];
  32. }
  33. }
  34. }
  35. return matrixa;
  36. }
  37. /**
  38. *
  39. * @param matrixa
  40. * @param matrixb
  41. */
  42. public int[][] multiplication(int[][] matrixa, int[][] matrixb) {
  43. if(legalOperation(matrixa, matrixb, OPERATION_SUB)) {
  44. int[][] result = new int[matrixa.length][matrixb[0].length];
  45. for(int i=0; i<matrixa.length; i++) {
  46. for(int j=0; j<matrixb[0].length; j++) {
  47. // i will complete this tomorrow @2012/09/17
  48. result[i][j] = calculateSingleResult(matrixa, matrixb, i, j);
  49. }
  50. }
  51. return result;
  52. }
  53. else
  54. {
  55. return null;
  56. }
  57. }
  58. private int calculateSingleResult(int[][] matrixa, int[][] matrixb, int row, int col) {
  59. int result = 0;
  60. for(int k=0; k<matrixa[0].length; k++) {
  61. result += matrixa[row][k] * matrixb[k][col];
  62. }
  63. return result;
  64. }
  65. /**
  66. *
  67. * @param matrixa
  68. * @param b
  69. */
  70. public int[][] multiplication(int[][] matrixa, int b) {
  71. for(int i=0; i<matrixa.length; i++) {
  72. for(int j=0; j<matrixa[0].length; j++) {
  73. matrixa[i][j] = matrixa[i][j] * b;
  74. }
  75. }
  76. return matrixa;
  77. }
  78. /**
  79. * validate whether the parameters is valid parameters.
  80. *
  81. * @param a
  82. * @param b
  83. * @param type
  84. * @return
  85. */
  86. private boolean legalOperation(int[][] a, int[][] b, int type) {
  87. boolean legal = true;
  88. if(type == OPERATION_ADD || type == OPERATION_SUB)
  89. {
  90. if(a.length != b.length || a[0].length != b[0].length) {
  91. legal = false;
  92. }
  93. }
  94. else if(type == OPERATION_MUL)
  95. {
  96. if(a[0].length != b.length) {
  97. legal = false;
  98. }
  99. }
  100. return legal;
  101. }
  102. /**
  103. *  test code here !!!!
  104. * @param args
  105. */
  106. public static void main(String[] args) {
  107. int[][] a = new int[][]{{1,2},{3,4}};
  108. int[][] b = new int[][]{{7, 8}, {6, 5}};
  109. BasicMatrixMath bmm = new BasicMatrixMath();
  110. System.out.println("addition two matrix");
  111. int[][] result = bmm.add(a, b);
  112. for(int i=0; i<result.length; i++) {
  113. for(int j=0; j<result[0].length; j++) {
  114. System.out.print("\t" + result[i][j]);
  115. }
  116. System.out.println();
  117. }
  118. System.out.println("substract two matrix");
  119. result = bmm.substract(a, b);
  120. for(int i=0; i<result.length; i++) {
  121. for(int j=0; j<result[0].length; j++) {
  122. System.out.print("\t" + result[i][j]);
  123. }
  124. System.out.println();
  125. }
  126. System.out.println("multiplex one matrix");
  127. result = bmm.multiplication(a, 3);
  128. for(int i=0; i<result.length; i++) {
  129. for(int j=0; j<result[0].length; j++) {
  130. System.out.print("\t" + result[i][j]);
  131. }
  132. System.out.println();
  133. }
  134. System.out.println("multiplex two matrix");
  135. result = bmm.multiplication(a, b);
  136. for(int i=0; i<result.length; i++) {
  137. for(int j=0; j<result[0].length; j++) {
  138. System.out.print("\t" + result[i][j]);
  139. }
  140. System.out.println();
  141. }
  142. }
  143. }

基本矩阵运算的Java实现的更多相关文章

  1. Java第三方工具库/包汇总

    一.科学计算或矩阵运算库 科学计算包: JMathLib是一个用于计算复杂数学表达式并能够图形化显示计算结果的Java开源类库.它是Matlab.Octave.FreeMat.Scilab的一个克隆, ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. Java调用jama实现矩阵运算

    Java调用jama实现矩阵运算 一.jama简介 Jama是一个基本的线性代数java包.包括一个基本的Matrix类和5个矩阵分解类. Matrix类提供了基本的线性代数数值运算的功能,不同的构造 ...

  4. Java读取mat文件

    概述 使用ujmp中的jmatio模块读取.mat文件到java程序中. 其实,ujmp主要是在模块core中实现了矩阵运算,其余模块都是复用了已有的开源库.模块jmatio是复用了已有的JMatIo ...

  5. JAMA:Java矩阵包

    原文链接:JAMA:Java矩阵包 API文档链接:线性代数Java包 JAMA jama是一个非常好用的java的线性代数软件包.适用于日常编程可能碰到的各种矩阵运算问题,提供了一个优雅的简便的解决 ...

  6. 用java写bp神经网络(一)

    根据前篇博文<神经网络之后向传播算法>,现在用java实现一个bp神经网络.矩阵运算采用jblas库,然后逐渐增加功能,支持并行计算,然后支持输入向量调整,最后支持L-BFGS学习算法. ...

  7. 158个JAVA免豆精品资料汇总

    附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...

  8. 【转】【JAVA资料免费下载】158个JAVA免豆精品资料汇总——下载目录

    附件完整版下载地址: http://down.51cto.com/data/431561 附件部分预览~ java中国移动收费系统[源代码] http://down.51cto.com/data/70 ...

  9. JavaSE基础之矩阵运算

    JavaSE基础之矩阵运算 1.矩阵类:Matrix.java 包括矩阵的加.乘运算,行列式的求解,最大最小元素等 package cn.com.zfc.help; import java.text. ...

随机推荐

  1. json.stringfy()和json.parse()

    json.stringfy()将对象.数组转换成字符串:json.parse()将字符串转成json对象. json.stringfy(): 语法:  JSON.stringify(value [, ...

  2. 慕课网__css3__3D

  3. mybatis mysql 调用视图

    java代码 @RequestMapping(value = "/testView", method = RequestMethod.GET) public @ResponseBo ...

  4. MyISAM与InnoDB区别

    两种类型最主要的差别就是Innodb 支持事务处理与外键和行级锁.而MyISAM不支持.所以MyISAM往往就容易被人认为只适合在小项目中使用. 我作为使用MySQL的用户角度出发,Innodb和My ...

  5. css学习记录

    1 !important 表示此属性需要优先考虑: <head>    <title>Page Title</title>    <style type=&q ...

  6. GoF--观察者模式

    观察者模式定义了对象间的一对多依赖关系,让一个或多个观察者对象观察一个主题对象.当主题对象的状态发生变化时,系统恩那个通知所有的依赖于此对象观察者对象,从而使得观察者对象能够自动更新. 在观察者模式中 ...

  7. API -- java.lang.Integer

    java.lang Class Integer static Integer valueOf(int i) Returns an Integer instance representing the s ...

  8. Inverted sentences

    And ever has it been that love knows not its own depth until the hour of separation.  除非临到了别离的时候,爱永远 ...

  9. 破解YunFile下载间隔10分钟/下载等待30秒

    [破解10分钟间隔] 可以采用断网重连等方法重新获取IP地址,就不用再等十分钟了 [破解30秒等待] 收藏一个书签,书签地址如下 javascript:var downpage_link = docu ...

  10. Windows添加和取消右键管理员权限

    亲测可用 新建文本文档,粘贴下列代码 Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\runas]@="管理员取 ...