Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to multiply vector values together?

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:

  1. Is there an easier way to get the result I'm looking for than making my own custom function?
  2. Is there a reason why there is no standard vector operation like this to begin with?

Thank you very much for your time!

like image 370
SemperCallide Avatar asked Aug 12 '17 22:08

SemperCallide


1 Answers

When we electrical engineers want to sound smart, we call it the Hadamard product, but otherwise it’s just the “element-wise product”.

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.)

Edit: Unity calls this Vector3.Scale: “Multiplies two vectors component-wise.”

like image 159
Ahmed Fasih Avatar answered Jan 03 '23 05:01

Ahmed Fasih