9

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!

Ahmed Fasih
  • 6,458
  • 7
  • 54
  • 95
SemperCallide
  • 1,950
  • 6
  • 26
  • 42

1 Answers1

13

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

Ahmed Fasih
  • 6,458
  • 7
  • 54
  • 95
  • 1
    Thank you! I'm glad to know it has a name. I'm using Unity, which has built-in vector functions of all sorts, but so far I haven't noticed one that does this specific operation – SemperCallide Aug 13 '17 at 22:43
  • 1
    Looks like they call it [`Vector3.Scale`](https://docs.unity3d.com/ScriptReference/Vector3.Scale.html): “Multiplies two vectors component-wise.” – Ahmed Fasih Aug 14 '17 at 09:43
  • 1
    Wow! I can't believe I missed that. In retrospect, that's all a scale operation really is, I suppose. Thank you so much. – SemperCallide Aug 14 '17 at 20:19
  • 1
    I can't believe they thought having a static method called `Scale` of all things was better than using a `*` operator... Can't wait for Extension operators – AustinWBryan Aug 31 '18 at 02:57
  • I agree with avoiding `*` for this. Someone who's got dot-product on their mind could easily read `v1 * v2` and think it's a dot-product. – orion elenzil Aug 10 '21 at 17:06