矢量矩阵乘法顺序可能会影响性能? [英] Vector-Matrix multiplication order can affect performance?

查看:396
本文介绍了矢量矩阵乘法顺序可能会影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是一个行主VS列重大问题。这是计算问题的顺序有关性能的基础上,矩阵乘法的结合律: A(BC)=(AB)C

This is not a row-major vs column-major question. This is an order of calculation question as pertaining to performance, based on the associative property of matrix multiplication: A(BC)=(AB)C

如果我有2个矩阵, A B ,和一个vector v ,我想他们都乘起来按照一定的顺序,例如 ABV ,我可以做(AB)V A(BV)

If I have 2 matrices, A and B, and a vector v and I want to multiply them all together in a certain order, for example ABv, I can do (AB)v or A(Bv).

我突然想起,编程,我得到更好的性能,从少得多的计算,如果我使用第二种方法,总是用一个向量乘以矩阵。

It occurs to me, programmatically, that I get better performance from far fewer calculations if I use the second method and always multiply a matrix with a vector.

例如,如果我们处理的4X4矩阵:

For example, if we are dealing with 4x4 matrices:

AB 结果16个人计算,一个新的矩阵,每个结果是从一个点积

AB results in 16 individual calculations, a new matrix, each result is from a dot product

矩阵*向量结果4计算,每个从积

Matrix*vector results in 4 calculations, each from a dot product

因此​​:

(AB)V 16 + 4点积运算= 20

(AB)v is 16+4 dot product calculations=20

A(BV)为两矩阵向量的产品,或4 + 4点积运算= 8

A(Bv) is two matrix-vector products, or 4+4 dot product calculations = 8

我在想是否正确?这表明,在执行很多很多的矢量矩阵EX pressions像这样将极大地提高性能,如果我每次都开始与载体?

Am I thinking correctly? This suggests that performing many many vector-matrix expressions like this will dramatically improve performance if I start with the vector each time?

因此​​,将是有意义的结构进行基于矢量矩阵库*矩阵左到右的计算顺序(即使你选择notate从右到左用列主格式),因为与乘以一个向量矩阵产品是很常见的图形。

Thus it would make sense to structure a matrix library that performs based on vector*matrix left-to-right calculation order (even if you choose to notate right-to-left with column-major formatting) since multiplying a vector with matrix products is very common in graphics.

推荐答案

在矩阵的单次操作的有限范围内和1个向量参与,你和tmyklebu有它的权利。但是,通常你需要知道你把它应用在实践之前的一个更大的范围内。这个问题围绕多久 A 和B相对于频率的 v 的修改 的变化。如果 A B 是相对静止的(他们不经常变化)与 v 的,你可能会更好precomputing比较 AB 并把它应用到任何值的 v 的发生有。

Within the limited context of a single operation of the matrices and a single vector involved, you and tmyklebu have it right. However, there is usually a larger context you need to be aware of before you apply it in practice. That issue revolves around how often A and B change relative to how often v changes. If A and B are relatively static (they don't change very often) compared with v, you may be better off precomputing AB and applying it to whatever value v happens to have.

此外,在实践中,有一定的几何由多个载体可更有效地变换并计算一起由第一计算的 AB 并然后应用该变换到所有在几何的载体。

Furthermore, in practice, there is some geometry comprised of multiple vectors which can be more efficiently transformed and computed together by first computing AB and then applying that transformation to all of the vectors in the geometry.

这篇关于矢量矩阵乘法顺序可能会影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆