问题描述
我有一个愚蠢的问题.大约10年前,我参加了向量数学课程,并且可能发誓我记得一个操作,该操作使我可以将向量的值相乘,如下所示:
I've got a silly question. I took a vector math class about 10 years ago and could've sworn I remembered an operation that allowed me to multiply the values of a vector together like so:
Vector3 v1 = new Vector3(1, 0, 2)
Vector3 v2 = new Vector3(5, 5, 5)
//Vector3 v3 = SomeVectorOperation(v1, v2) = (1 * 5, 0 * 5, 2 * 5)
现在,在查看我的所有笔记以及可以在网上找到的所有内容时,这似乎根本不是一项常见的操作.当然,我可以编写一个实现该功能的函数:
Now in reviewing all my notes, and everything I can find online, it doesn't look like this is a common operation at all. Of course I can write a function that does it:
Vector3 VectorMult(Vector3 v1, Vector3 v2) {
return new Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
}
到目前为止,我至少发现了几个这样的操作会有所帮助的实例,所以我不确定为什么它不存在某种形式.所以,我想我有两个问题:
So far I've found at least a few instances where an operation like this would be helpful, so I'm not sure why it wouldn't exist already in some form. So, I guess I have two questions:
- 有没有比自己创建自定义函数更简单的方法来获得所需的结果?
- 为什么没有这样的标准向量运算开始?
非常感谢您的光临!
推荐答案
当我们的电气工程师想要听起来更聪明时,我们将其称为哈达玛产品,否则,它只是元素明智的产品".
When we electrical engineers want to sound smart, we call it the Hadamard product, but otherwise it’s just the "element-wise product".
您正在使用哪个库? GLSL?本征? GSL?我们可以寻找如何在其中进行元素级乘法. (通常可以使用SIMD对其进行加速,因此库提供的优化实现将比您的手动功能要快.)
What library are you using? GLSL? Eigen? GSL? We can look for how to do element-wise multiplication in it. (It can often be accelerated using SIMD, so an optimized implementation provided by a library will be faster than your hand-rolled function.)
Unity将此称为 Vector3.Scale
:将两个向量相乘组件方面."
Unity calls this Vector3.Scale
: "Multiplies two vectors component-wise."
这篇关于将向量值相乘的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!