Ai
1 Star 0 Fork 3

aiobc/Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MatrixMultiplication.java 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
Rafael Chaves 提交于 2020-02-03 02:07 +08:00 . Adopt standard Java project layout (#120)
/**
* 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));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiobc/Algorithms.git
git@gitee.com:aiobc/Algorithms.git
aiobc
Algorithms
Algorithms
master

搜索帮助