是否可以简化这种基于分支的向量数学运算?
我试图在 C++ 中实现类似以下内容:
class MyVector; // 3 component vector class
MyVector const kA = /* ... */;
MyVector const kB = /* ... */;
MyVector const kC = /* ... */;
MyVector const kD = /* ... */;
// I'd like to shorten the remaining lines, ideally making it readable but less code/operations.
MyVector result = kA;
MyVector const kCMinusD = kC - kD;
if(kCMinusD.X <= 0)
{
result.X = kB.X;
}
if(kCMinusD.Y <= 0)
{
result.Y = kB.Y;
}
if(kCMinusD.Z <= 0)
{
result.Z = kB.Z;
}
将代码解释为英语,我有四个“已知”向量。其中两个向量具有我在结果中可能想要也可能不想要的值,并且我是否想要它们取决于基于其他两个向量的分量的分支。
我觉得我应该能够通过一些矩阵数学和掩码来简化这段代码,但我无法理解它。
现在我将使用分支,但我很好奇是否有更好的方法仍然可以理解,并且代码更简洁。
编辑:
参考马克的评论,我将解释我在这里想做的事情。
这段代码是我正在研究的一些弹簧物理学的摘录。组成部分如下:
kC 为当前弹簧长度,kD 为最小弹簧长度。
kA 和kB 是两组弹簧张力,其每个分量对于每个分量可以是唯一的(即,沿X、Y或Z的不同弹簧张力)。 kA 是弹簧未完全压缩时的张力,kB 是弹簧完全压缩时的张力。
我想建立一个结果“向量”,它只是 kC 和 kD 的合并,取决于弹簧是否被压缩。
I'm trying to achieve something like the following in C++:
class MyVector; // 3 component vector class
MyVector const kA = /* ... */;
MyVector const kB = /* ... */;
MyVector const kC = /* ... */;
MyVector const kD = /* ... */;
// I'd like to shorten the remaining lines, ideally making it readable but less code/operations.
MyVector result = kA;
MyVector const kCMinusD = kC - kD;
if(kCMinusD.X <= 0)
{
result.X = kB.X;
}
if(kCMinusD.Y <= 0)
{
result.Y = kB.Y;
}
if(kCMinusD.Z <= 0)
{
result.Z = kB.Z;
}
Paraphrasing the code into English, I have four 'known' vectors. Two of the vectors have values that I may or may not want in my result, and whether I want them or not is contingent on a branch based on the components of two other vectors.
I feel like I should be able to simplify this code with some matrix math and masking, but I can't wrap my head around it.
For now I'm going with the branch, but I'm curious to know if there's a better way that still would be understandable, and less code-verbose.
Edit:
In reference to Mark's comment, I'll explain what I'm trying to do here.
This code is an excerpt from some spring physics I'm working on. The components are as follows:
kC is the springs length currently, and kD is minimum spring length.
kA and kB are two sets of spring tensions, each component of which may be unique per component (i.e., a different spring tension along the X, Y, or Z). kA is the springs tension if it's not fully compressed, and kB is the springs tension if it IS fully compressed.
I'd like to build up a resultant 'vector' that simply is the amalgamation of kC and kD, dependant on whether the spring is compressed or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您所在的平台,编译器可能能够优化语句,例如
使用 fsel (浮点选择)指令或条件移动。就我个人而言,我认为这种方式代码看起来也更好、更简洁,但这是主观的。
如果代码确实对性能至关重要,并且您不介意将矢量类更改为 4 个浮点数而不是 3 个浮点数,则可以使用 SIMD(例如 Intel 平台上的 SSE、PowerPC 上的 VMX)进行比较并选择答案。如果您继续这样做,它会像这样:(伪代码)
这需要一段时间来习惯,并且最初可能不太可读,但您最终会习惯以 SIMD 模式思考。
当然,通常的警告是适用的 - 在分析之前不要进行优化等。
Depending on the platform you're on, the compiler might be able to optimize statements like
using fsel (floating point select) instructions or conditional moves. Personally, I think the code looks nicer and more concise this way too, but that's subjective.
If the code is really performance critical, and you don't mind changing your vector class to be 4 floats instead of 3, you could use SIMD (e.g SSE on Intel platforms, VMX on PowerPC) to do the comparison and select the answers. If you went ahead with this, it would like this: (in pseudo code)
This takes a while getting used to, and it might be less readable initially, but you eventually get used to thinking in SIMD mode.
The usual caveats apply, of course - don't optimize before you profile, etc.
如果你的向量元素是整数,你可以这样做:(
注意这以不同的方式处理 == 0 的情况,不确定你是否关心)
如果你的向量元素是双精度而不是整数,你可以做类似的事情,因为符号位在同样的地方,您只需转换为整数,执行掩码,然后再转换回来。
If your vector elements are ints, you can do:
(note this handles the == 0 case differently, not sure if you care)
If your vector elements are doubles instead of ints, you can do something similar as the sign bit is in the same place, you just have to convert to integers, do the mask, and convert back.
如果您在源代码中寻求干净的表达式而不是运行时优化,您可能会考虑从“工具箱”的角度解决这个问题。假设您在 MyVector 上定义了
sign
、gt
(大于)和le
(小于或等于)。然后分为两行:使用运算符重载:
为了获得灵感,这里是 MatLab函数参考。显然,有许多具有此类功能的 C++ 矢量库可供选择。
如果分析显示有必要,您随时可以进一步优化。但通常最大的性能问题是您能否很好地了解全局并重用中间计算。
If you're seeking a clean expression in source more than a runtime optimization, you might consider solving this problem from the "toolbox" point of view. So let's say that on MyVector you defined
sign
,gt
(greater-than), andle
(less-than-or-equal-to). Then in two lines:With operator overloading:
For inspiration here's the MatLab function reference. And obviously there are many C++ vector libraries to choose from with such functions.
You can always go in and optimize further if profiling shows it necessary. But often the biggest performance issues are how well you can see the big picture and reuse intermediate computations.
由于您只进行减法,因此重写如下:
Since you are only doing subtraction you are rewrite as below: