本文实例讲述了JS实现简单的二维矩阵乘积运算方法。分享给大家供大家参考,具体如下:
Console控制台截图如下:
(上图为输出结果直接上代码了(A矩阵可以乘以B矩阵的前提是A矩阵的列数等于B矩阵的行数)
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
</head>
<body>
</body>
<script type="text/javascript">
function log(msg) {
console.log(msg);
}
/**
* 可视化的打印出矩阵的数据
*/
function printMatrixData(data) {
console.log(data);
if(!data) {
return;
}
var numberSize = 5;
for(var i=0, len=data.length; i<len; i++) {
var row = data[i];
var rowLog = "(";
for(var j=0, jLen=row.length; j<jLen; j++) {
rowLog += row[j];
// 补齐空格
rowLog += indent(numberSize - (row[j]+"").length);
}
rowLog+=")";
console.log(rowLog);
}
}
/**
* 拼接指定长度的空格
*/
function indent(length) {
var empty = "";
for(var i=0; i<length; i++) {
empty += " ";
}
return empty;
}
/**
* 矩阵原型
*/
function Matrix(data) {
// 这里必须传一个二维数组,最好严格检验一下
if(typeof data !== "object" || typeof data.length === "undefined" || !data) {
throw new Error("data's type is error");
}
this.data = data;
this.cols = data.length;
}
var M = {
findByLocation: function(data, xIndex, yIndex) {
if(data && data[xIndex]) {
return data[xIndex][yIndex];
}
},
// 矩阵乘积
multiply: function(m, n) {
if(!m instanceof Matrix && !n instanceof Matrix) {
throw new Error("data's type is error");
}
var mData = m.data;
var nData = n.data;
if(mData.length == 0 || nData.length == 0) {
return 0;
}
if(mData[0].length != nData.length) {
throw new Error("the two martrix data is not allowed to dot");
}
var result = [];
for(var i=0, len=mData.length; i<len; i++) {
var mRow = mData[i];
result[i] = [];
for(var j=0, jLen=mRow.length; j<jLen; j++) {
var resultRowCol = 0;
// 如果n矩阵没有足够的列数相乘,转入m矩阵下一行
if(typeof this.findByLocation(nData, 0, j) === "undefined") {
break;
}
for(var k=0, kLen=jLen; k<kLen; k++) {
resultRowCol += mRow[k]*this.findByLocation(nData, k, j);
}
result[i][j] = resultRowCol;
}
}
return result;
}
};
var m = new Matrix([[2, -1], [-2, 1], [-1, 2]]);
var n = new Matrix([[4, -3], [3, 5]]);
var result = M.multiply(m, n);
printMatrixData(result);
var m2 = new Matrix([[2, 3, 1], [5, 2, 4], [-3, 2, 0]]);
var n2 = new Matrix([[11], [5], [8]]);
var result2 = M.multiply(m2, n2);
printMatrixData(result2);
</script>
</html>
更多关于JavaScript运算相关内容感兴趣的读者可查看本站专题:《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。