标签:运算 int double 矩阵 System length print 高数
一、介绍
1、介绍。
矩阵的运算,本质上就是二维数组的运算。
2、公共代码。
- /**
- * 打印二维数组
- *
- * @param a 二维数组a
- */
- public static void print(double[][] a) {
- print(a, 5, 0);
- }
- /**
- * 打印二维数组
- *
- * @param a 二维数组a
- * @param precision 如果是浮点型的话,保留几位小数
- */
- public static void print(int[][] a, int width, int precision) {
- String str = "%" + width + "d ";
- for (int i = 0; i < a.length; i++) {
- int[] aItem = a[i];
- for (int j = 0; j < aItem.length; j++) {
- System.out.print(String.format(str, aItem[j]));
- }
- System.out.println();
- }
- }
- /**
- * 打印二维数组
- *
- * @param a 二维数组a
- * @param precision 如果是浮点型的话,保留几位小数
- */
- public static void print(double[][] a, int width, int precision) {
- String str = "%" + width + "." + precision + "f ";
- for (int i = 0; i < a.length; i++) {
- double[] aItem = a[i];
- for (int j = 0; j < aItem.length; j++) {
- System.out.print(String.format(str, aItem[j]));
- }
- System.out.println();
- }
- }
二、减法。
1、介绍。
2、代码。
- /**
- * 矩阵a和b对应位置的数相减
- *
- * @param a 矩阵a[m][n]
- * @param b 矩阵b[n][p]
- */
- public static double[][] sub(double[][] a, double[][] b) throws Exception {
- int row = a.length;
- int column = a[0].length;
- int bRow = b.length;
- int bColumn = b[0].length;
- if (row != bRow || column != bColumn) {
- throw new Exception("两个矩阵的大小不一致");
- }
- double[][] result = new double[row][column];
- for (int i = 0; i < row; i++)
- for (int j = 0; j < column; j++)
- result[i][j] = a[i][j] - b[i][j];
- return result;
- }
- public static void main(String[] args) {
- try {
- double[][] a = new double[][]{
- {1, 2, 3, 4},
- {5, 6, 7, 8},
- {1, 1, 2, 2},
- {3, 3, 4, 4},
- };
- System.out.println("矩阵a:");
- ArrayUtils.print(a);
- double[][] b = new double[][]{
- {3, 2, 1, 4},
- {8, 6, 5, 8},
- {1, 2, 3, 4},
- {8, 7, 6, 5},
- };
- System.out.println("矩阵b:");
- ArrayUtils.print(b);
- double[][] c = MatrixUtils.sub(a, b);
- System.out.println("矩阵a-b=c:");
- ArrayUtils.print(c);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
三、加法
1、介绍。
2、代码。
- /**
- * 矩阵a和b对应位置的数相加
- *
- * @param a 矩阵a[m][n]
- * @param b 矩阵b[n][p]
- */
- public static double[][] add(double[][] a, double[][] b) throws Exception {
- int row = a.length;
- int column = a[0].length;
- int bRow = b.length;
- int bColumn = b[0].length;
- if (row != bRow || column != bColumn) {
- throw new Exception("两个矩阵的大小不一致");
- }
- double[][] result = new double[row][column];
- for (int i = 0; i < row; i++)
- for (int j = 0; j < column; j++)
- result[i][j] = a[i][j] + b[i][j];
- return result;
- }
- public static void main(String[] args) {
- try {
- double[][] a = new double[][]{
- {1, 2, 3, 4},
- {5, 6, 7, 8},
- {1, 1, 2, 2},
- {3, 3, 4, 4},
- };
- System.out.println("矩阵a:");
- ArrayUtils.print(a);
- double[][] b = new double[][]{
- {3, 2, 1, 4},
- {8, 6, 5, 8},
- {1, 2, 3, 4},
- {8, 7, 6, 5},
- };
- System.out.println("矩阵b:");
- ArrayUtils.print(b);
- //加法
- double[][] c = MatrixUtils.add(a, b);
- System.out.println("矩阵a+b=c:");
- ArrayUtils.print(c);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
四、数乘
1、介绍。
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、代码。
- /**
- * 矩阵数乘
- *
- * @param a 矩阵a
- * @param multiplier 乘数multiplier
- */
- public static double[][] mul(double[][] a, double multiplier) {
- int row = a.length;
- int column = a[0].length;
- double[][] result = new double[row][column];
- for (int i = 0; i < row; i++)
- for (int j = 0; j < column; j++)
- result[i][j] = a[i][j] * multiplier;
- return result;
- }
- public static void main(String[] args) {
- try {
- double[][] a = new double[][]{
- {1, 2, 3, 4},
- {5, 6, 7, 8},
- {1, 1, 2, 2},
- {3, 3, 4, 4},
- };
- System.out.println("矩阵a:");
- ArrayUtils.print(a);
- //数乘
- double[][] c = MatrixUtils.mul(a, 3);
- System.out.println("矩阵a*3=c:");
- ArrayUtils.print(c);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
五、转置
1、介绍。
2、代码。
- /**
- * 矩阵转置
- *
- * @param a 矩阵a
- */
- public static double[][] transform(double[][] a) {
- int row = a.length;
- int column = a[0].length;
- double[][] result = new double[column][row];
- for (int i = 0; i < column; i++)
- for (int j = 0; j < row; j++)
- result[i][j] = a[j][i];
- return result;
- }
- public static void main(String[] args) {
- try {
- double[][] a = new double[][]{
- {1, 2},
- {5, 6},
- {1, 1},
- {3, 3},
- };
- System.out.println("矩阵a:");
- ArrayUtils.print(a);
- double[][] c = MatrixUtils.transform(a);
- System.out.println("矩阵a转置c:");
- ArrayUtils.print(c);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
六、乘法
1、介绍。
A、B和C都为矩阵: ABC=A(BC) , (A+B)C=AC+AB , , AB != BA。
两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵,它的一个元素表示为:。
2、代码。
- /**
- * 矩阵a[m][n]和矩阵b[n][p]相乘,返回c[m][p]
- *
- * @param a 矩阵a[m][n]
- * @param b 矩阵b[n][p]
- */
- public static double[][] mul(double[][] a, double[][] b) throws Exception {
- int aRow = a.length;
- int aColumn = a[0].length;
- int bRow = b.length;
- int bColumn = b[0].length;
- if (aColumn != bRow) {
- throw new Exception("a[m][n]和b[n][p],其中两个n不相等");
- }
- double[][] result = new double[aRow][bColumn];
- //计算
- for (int i = 0; i < aRow; i++) {
- for (int j = 0; j < bColumn; j++) {
- double sum = 0;
- for (int k = 0; k < aColumn; k++) {
- sum += (a[i][k] * b[k][j]);
- }
- result[i][j] = sum;
- }
- }
- return result;
- }
- public static void main(String[] args) {
- try {
- double[][] a = new double[][]{
- {1, 2, 3, 4},
- {5, 6, 7, 8},
- {1, 1, 2, 2},
- {3, 3, 4, 4},
- };
- System.out.println("矩阵a:");
- ArrayUtils.print(a);
- double[][] b = new double[][]{
- {3, 2, 1, 4},
- {8, 6, 5, 8},
- {1, 2, 3, 4},
- {8, 7, 6, 5},
- };
- System.out.println("矩阵b:");
- ArrayUtils.print(b);
- //乘法
- double[][] c = MatrixUtils.mul(a, b);
- System.out.println("矩阵a*b=c:");
- ArrayUtils.print(c);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
标签:运算,int,double,矩阵,System,length,print,高数 来源: https://www.cnblogs.com/ko88/p/11859188.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。