代码拉取完成,页面将自动刷新
/**
* Multiply two matrices together and get their product
*
* <p>Time Complexity: O(n^3)
*
* @author Micah Stairs
*/
package com.williamfiset.algorithms.linearalgebra;
class MatrixMultiplication {
// Returns the result of the product of the matrices 'a' and 'b'
// or null if the matrices are the wrong dimensions
static double[][] multiply(double[][] a, double[][] b) {
int aRows = a.length, aCols = a[0].length;
int bRows = b.length, bCols = b[0].length;
if (aCols != bRows) return null;
double[][] c = new double[aRows][bCols];
for (int i = 0; i < aRows; i++)
for (int j = 0; j < bCols; j++) for (int k = 0; k < aCols; k++) c[i][j] += a[i][k] * b[k][j];
return c;
}
public static void main(String[] args) {
double[][] a = {
{1, 2, 3, 4},
{4, 3, 2, 1},
{1, 2, 2, 1}
};
double[][] b = {
{1, 0},
{2, 1},
{0, 3},
{0, 0}
};
double[][] c = multiply(a, b);
for (double[] row : c) System.out.println(java.util.Arrays.toString(row));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。