用JavaScript乘法矩阵 [英] Multiplying matrices in JavaScript
本文介绍了用JavaScript乘法矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试在JavaScript中将两个矩阵相乘,并编写了以下函数:
Hi,
I am currently trying to multiply two matrices in JavaScript and have written the following function:
MathHelper.multiplyMatrix = function(a, b) {
if (a.length != b.length)
return undefined;
var result = [];
var rowLength = Math.sqrt(a.length);
for (i = 0; i < a.length; i++) {
var index = parseInt(i / rowLength);
var val = 0;
for (var x = 0; x < rowLength; x++) {
val += a[index * rowLength + x] * b[x * rowLength + (i % rowLength)];
}
result[i] = val;
}
return result;
};
我刚接触矩阵,想知道是否有人知道这样做的更有效方法?
问候,
Dom
I am new to matrices and was wonder if anyone knows of a more efficient way of doing this?
Regards,
Dom
推荐答案
我没有检查代码,但是为什么不使用更好地适合矩阵性质并大大简化索引的二维数组呢?以防万一:
http://www.kavoir.com/2009/02/javascript-multi-Dimension- array.html [^ ].
—SA
I did not check up the code, but why not using 2-dimensional arrays which better fit the nature of a matrix and greatly simplify indexing? Just in case:
http://www.kavoir.com/2009/02/javascript-multi-dimensional-array.html[^].
—SA
我认为您会喜欢 Sylvester [< ^ ]
它是JavaScript的向量,矩阵和几何库.
最好的问候
Espen Harlinn
I think you''re going to like Sylvester[^]
It''s a vector, matrix and geometry library for JavaScript.
Best regards
Espen Harlinn
这篇关于用JavaScript乘法矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文