ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

高数 矩阵的基本运算

2019-11-14 18:01:15  阅读:250  来源: 互联网

标签:运算 int double 矩阵 System length print 高数


一、介绍

1、介绍。

        矩阵的运算,本质上就是二维数组的运算。

2、公共代码。

  1.   /**
  2.   * 打印二维数组
  3.   *
  4.   * @param a 二维数组a
  5.   */
  6.   public static void print(double[][] a) {
  7.   print(a, 5, 0);
  8.   }
  9.    
  10.   /**
  11.   * 打印二维数组
  12.   *
  13.   * @param a 二维数组a
  14.   * @param precision 如果是浮点型的话,保留几位小数
  15.   */
  16.   public static void print(int[][] a, int width, int precision) {
  17.   String str = "%" + width + "d ";
  18.   for (int i = 0; i < a.length; i++) {
  19.   int[] aItem = a[i];
  20.   for (int j = 0; j < aItem.length; j++) {
  21.   System.out.print(String.format(str, aItem[j]));
  22.   }
  23.   System.out.println();
  24.   }
  25.   }
  26.    
  27.   /**
  28.   * 打印二维数组
  29.   *
  30.   * @param a 二维数组a
  31.   * @param precision 如果是浮点型的话,保留几位小数
  32.   */
  33.   public static void print(double[][] a, int width, int precision) {
  34.   String str = "%" + width + "." + precision + "f ";
  35.   for (int i = 0; i < a.length; i++) {
  36.   double[] aItem = a[i];
  37.   for (int j = 0; j < aItem.length; j++) {
  38.   System.out.print(String.format(str, aItem[j]));
  39.   }
  40.   System.out.println();
  41.   }
  42.   }

二、减法。

1、介绍。

        \LARGE \begin{bmatrix}a&b\\ c&d\end{bmatrix} - \begin{bmatrix}e&f\\ g&h\end{bmatrix} = \begin{bmatrix}a-e&b-f\\ c-g&d-h\end{bmatrix}

2、代码。

  1.   /**
  2.   * 矩阵a和b对应位置的数相减
  3.   *
  4.   * @param a 矩阵a[m][n]
  5.   * @param b 矩阵b[n][p]
  6.   */
  7.   public static double[][] sub(double[][] a, double[][] b) throws Exception {
  8.   int row = a.length;
  9.   int column = a[0].length;
  10.   int bRow = b.length;
  11.   int bColumn = b[0].length;
  12.   if (row != bRow || column != bColumn) {
  13.   throw new Exception("两个矩阵的大小不一致");
  14.   }
  15.   double[][] result = new double[row][column];
  16.   for (int i = 0; i < row; i++)
  17.   for (int j = 0; j < column; j++)
  18.   result[i][j] = a[i][j] - b[i][j];
  19.   return result;
  20.   }
  1.   public static void main(String[] args) {
  2.   try {
  3.   double[][] a = new double[][]{
  4.   {1, 2, 3, 4},
  5.   {5, 6, 7, 8},
  6.   {1, 1, 2, 2},
  7.   {3, 3, 4, 4},
  8.   };
  9.   System.out.println("矩阵a:");
  10.   ArrayUtils.print(a);
  11.   double[][] b = new double[][]{
  12.   {3, 2, 1, 4},
  13.   {8, 6, 5, 8},
  14.   {1, 2, 3, 4},
  15.   {8, 7, 6, 5},
  16.   };
  17.   System.out.println("矩阵b:");
  18.   ArrayUtils.print(b);
  19.   double[][] c = MatrixUtils.sub(a, b);
  20.   System.out.println("矩阵a-b=c:");
  21.   ArrayUtils.print(c);
  22.   } catch (Exception e) {
  23.   e.printStackTrace();
  24.   }
  25.   }

 

 

三、加法

1、介绍。

        \LARGE \begin{bmatrix}a&b\\ c&d\end{bmatrix} +\begin{bmatrix}e&f\\ g&h\end{bmatrix} = \begin{bmatrix}a+e&b+f\\ c+g&d+h\end{bmatrix}

2、代码。

  1.   /**
  2.   * 矩阵a和b对应位置的数相加
  3.   *
  4.   * @param a 矩阵a[m][n]
  5.   * @param b 矩阵b[n][p]
  6.   */
  7.   public static double[][] add(double[][] a, double[][] b) throws Exception {
  8.   int row = a.length;
  9.   int column = a[0].length;
  10.   int bRow = b.length;
  11.   int bColumn = b[0].length;
  12.   if (row != bRow || column != bColumn) {
  13.   throw new Exception("两个矩阵的大小不一致");
  14.   }
  15.   double[][] result = new double[row][column];
  16.   for (int i = 0; i < row; i++)
  17.   for (int j = 0; j < column; j++)
  18.   result[i][j] = a[i][j] + b[i][j];
  19.   return result;
  20.   }
  1.   public static void main(String[] args) {
  2.   try {
  3.   double[][] a = new double[][]{
  4.   {1, 2, 3, 4},
  5.   {5, 6, 7, 8},
  6.   {1, 1, 2, 2},
  7.   {3, 3, 4, 4},
  8.   };
  9.   System.out.println("矩阵a:");
  10.   ArrayUtils.print(a);
  11.   double[][] b = new double[][]{
  12.   {3, 2, 1, 4},
  13.   {8, 6, 5, 8},
  14.   {1, 2, 3, 4},
  15.   {8, 7, 6, 5},
  16.   };
  17.   System.out.println("矩阵b:");
  18.   ArrayUtils.print(b);
  19.   //加法
  20.   double[][] c = MatrixUtils.add(a, b);
  21.   System.out.println("矩阵a+b=c:");
  22.   ArrayUtils.print(c);
  23.   } catch (Exception e) {
  24.   e.printStackTrace();
  25.   }
  26.   }

 

四、数乘

1、介绍。

        \LARGE \begin{bmatrix}a&b\\ c&d\end{bmatrix} *3 = \begin{bmatrix}3a&3b\\ 3c&3d\end{bmatrix}

n和m是数,A和B是矩阵:n(mA)=(nm)A  ,  n(AB)=(nA)B=A(nB) , (n+m)A=nA+mA , n(A+B)=nA+nB

2、代码。

  1.   /**
  2.   * 矩阵数乘
  3.   *
  4.   * @param a 矩阵a
  5.   * @param multiplier 乘数multiplier
  6.   */
  7.   public static double[][] mul(double[][] a, double multiplier) {
  8.   int row = a.length;
  9.   int column = a[0].length;
  10.   double[][] result = new double[row][column];
  11.   for (int i = 0; i < row; i++)
  12.   for (int j = 0; j < column; j++)
  13.   result[i][j] = a[i][j] * multiplier;
  14.   return result;
  15.   }
  1.   public static void main(String[] args) {
  2.   try {
  3.   double[][] a = new double[][]{
  4.   {1, 2, 3, 4},
  5.   {5, 6, 7, 8},
  6.   {1, 1, 2, 2},
  7.   {3, 3, 4, 4},
  8.   };
  9.   System.out.println("矩阵a:");
  10.   ArrayUtils.print(a);
  11.   //数乘
  12.   double[][] c = MatrixUtils.mul(a, 3);
  13.   System.out.println("矩阵a*3=c:");
  14.   ArrayUtils.print(c);
  15.   } catch (Exception e) {
  16.   e.printStackTrace();
  17.   }
  18.   }

五、转置

1、介绍。

        \LARGE \begin{bmatrix}a&b\\ c&d\\ e&f\end{bmatrix} ^T= \begin{bmatrix}a&c&e\\ b&d&f\end{bmatrix}

2、代码。

  1.   /**
  2.   * 矩阵转置
  3.   *
  4.   * @param a 矩阵a
  5.   */
  6.   public static double[][] transform(double[][] a) {
  7.   int row = a.length;
  8.   int column = a[0].length;
  9.   double[][] result = new double[column][row];
  10.   for (int i = 0; i < column; i++)
  11.   for (int j = 0; j < row; j++)
  12.   result[i][j] = a[j][i];
  13.   return result;
  14.   }
  1.   public static void main(String[] args) {
  2.   try {
  3.   double[][] a = new double[][]{
  4.   {1, 2},
  5.   {5, 6},
  6.   {1, 1},
  7.   {3, 3},
  8.   };
  9.   System.out.println("矩阵a:");
  10.   ArrayUtils.print(a);
  11.   double[][] c = MatrixUtils.transform(a);
  12.   System.out.println("矩阵a转置c:");
  13.   ArrayUtils.print(c);
  14.   } catch (Exception e) {
  15.   e.printStackTrace();
  16.   }
  17.   }

六、乘法

1、介绍。

        \LARGE \begin{bmatrix}a&b\\ c&d\end{bmatrix} +\begin{bmatrix}e&f\\ g&h\end{bmatrix} = \begin{bmatrix}ae+bg&af+bh\\ ce+dg&cf+dh\end{bmatrix}

A、B和C都为矩阵: ABC=A(BC) ,  (A+B)C=AC+AB ,  (AB)^{T}=A^TB^T ,  AB != BA。

        两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如Am×n矩阵和Bn×p矩阵,它们的乘积C是一个m×p矩阵\LARGE c=(c_{ij}),它的一个元素表示为:c_{ij}=a_{i1}*b_{1j}+a_{i2}*b_{2j}+......+a_{in}*b_{nj}=\sum_{r=1}^{n}a_{ir}*b_{rj}

2、代码。

  1.   /**
  2.   * 矩阵a[m][n]和矩阵b[n][p]相乘,返回c[m][p]
  3.   *
  4.   * @param a 矩阵a[m][n]
  5.   * @param b 矩阵b[n][p]
  6.   */
  7.   public static double[][] mul(double[][] a, double[][] b) throws Exception {
  8.   int aRow = a.length;
  9.   int aColumn = a[0].length;
  10.   int bRow = b.length;
  11.   int bColumn = b[0].length;
  12.   if (aColumn != bRow) {
  13.   throw new Exception("a[m][n]和b[n][p],其中两个n不相等");
  14.   }
  15.   double[][] result = new double[aRow][bColumn];
  16.   //计算
  17.   for (int i = 0; i < aRow; i++) {
  18.   for (int j = 0; j < bColumn; j++) {
  19.   double sum = 0;
  20.   for (int k = 0; k < aColumn; k++) {
  21.   sum += (a[i][k] * b[k][j]);
  22.   }
  23.   result[i][j] = sum;
  24.   }
  25.   }
  26.   return result;
  27.   }
  1.   public static void main(String[] args) {
  2.   try {
  3.   double[][] a = new double[][]{
  4.   {1, 2, 3, 4},
  5.   {5, 6, 7, 8},
  6.   {1, 1, 2, 2},
  7.   {3, 3, 4, 4},
  8.   };
  9.   System.out.println("矩阵a:");
  10.   ArrayUtils.print(a);
  11.   double[][] b = new double[][]{
  12.   {3, 2, 1, 4},
  13.   {8, 6, 5, 8},
  14.   {1, 2, 3, 4},
  15.   {8, 7, 6, 5},
  16.   };
  17.   System.out.println("矩阵b:");
  18.   ArrayUtils.print(b);
  19.   //乘法
  20.   double[][] c = MatrixUtils.mul(a, b);
  21.   System.out.println("矩阵a*b=c:");
  22.   ArrayUtils.print(c);
  23.   } catch (Exception e) {
  24.   e.printStackTrace();
  25.   }
  26.   }

 

标签:运算,int,double,矩阵,System,length,print,高数
来源: https://www.cnblogs.com/ko88/p/11859188.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有