# How to perform element-wise multiplication with mathutils.Vector?

In Blender's I can add/subtract mathutils.Vector without any problems.

vec3 = vec1 - vec2  # OK return a vector


But! It's paint to multiply/divide vector. For example if i multiply vectors i'll get a float.

vec3 = vec1 * vec2  # ERROR return a float


Is there a method to multiply/divide vectors correctly? like vec1.mult(vec2) which exists in other game engines.

• vec1.cross(vec2) returns the cross product vec1 x vec2 and vec1.dot(vec2) returns the dot product vec1 · vec2.
– user7952
Mar 18 '15 at 21:22
• Note, this is known as "component-wise" or "element-wise" multiplication, see: math.stackexchange.com/questions/32516/… Mar 27 '15 at 19:47
• @mifth, I'll look into adding element-wise-multiple, there doesn't seem to be a convention for this for object-oriented-Vector API's though, asked here: programmers.stackexchange.com/questions/277583/… Mar 27 '15 at 20:42
• @ideasman42 While I agree that v1.hadamard_product(v2) is overly verbose, how about simply v1.hadamard(v2) or v1.compwise(v2)?
– user7952
Mar 27 '15 at 20:53
• @SixthOfFour, best move suggestions to the page linked. Mar 28 '15 at 7:44

## 2 Answers

After a little bit of research, I concluded that the closest you can get is to use mathutils.Vector.cross() to do a cross multiplication of two vectors, or mathutils.Vector.dot() for dot multiplication; this info was gathered from The API documentation.

However, this doesn't seem to be the behavior you want - it seems you want to multiply the individual components of the vector, for which you can use something similar to this:

def mult(vec, vec2):
temp = []

for x, i in enumerate(vec):
temp.append(i * vec2[x])

return mathutils.Vector(temp)

• Just curious: does anybody know why this is not mentioned in this source? ( docs.blender.org/api/current/mathutils.html ) To make it even more puzzling, it is mentioned in the v2.44 documentation... Am I looking at the wrong package or something? Apr 9 '20 at 6:17

I am amazed mathutils.Vector class does not support vector scaling by another vector!

This can be for example useful when you get scale vector from matrix decomposition and you want to scale other vectors accordingly.

This is with a generator expression for better performance:

vec3 = Vector(x * y for x, y in zip(vec1, vec2))

• generator-expression is a little nicer then list-comprehension here, edited. Mar 27 '15 at 19:39
• @ideasman42 good point! Mar 27 '15 at 20:04