Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Numpy What's it called / how do you represent that operation where you multiply each element of two vectors?

For example, suppose I have:

x = array([1, 2, 3])
y = array([4, 5, 6])

Standard "array multiplication" in python does z = x * y = array([4, 10, 18]). In matlab, to get the same effect, you do *. IIRC.

What is this operation called, and which symbol is used to represent it?

like image 638
cjaynga Avatar asked Apr 19 '11 08:04

cjaynga


People also ask

How do you multiply each element in a NumPy array in Python?

You can use np. multiply to multiply two same-sized arrays together. This computes something called the Hadamard product. In the Hadamard product, the two inputs have the same shape, and the output contains the element-wise product of each of the input values.

How does NumPy multiply work?

What does Numpy Multiply Function do? The numpy multiply function calculates the product between the two numpy arrays. It calculates the product between the two arrays, say x1 and x2, element-wise.

What is the operation in NumPy Python?

Using Arithmetic Operators with Numpy You can perform arithmetic operations on these arrays. For example, if you add the arrays, the arithmetic operator will work element-wise. The output will be an array of the same dimension. You can run an arithmetic operation on the array with a scalar value.


3 Answers

It is the Hadamard product represented with an open circle: http://en.wikipedia.org/wiki/Matrix_multiplication#Hadamard_product

like image 179
Daan Avatar answered Nov 15 '22 13:11

Daan


It seems to me you are after the expression S = sum_i( x_i * y_i)? That is called the inner product. From numpy documentation:

from numpy import *
x = array([1,2,3])
y = array([2,2,2])
inner(x,y)          <-- Should give 1*2 + 2*2 + 3*2 = 12

The operation you have illustrated, and what you get with .* in matlab, is called a Schur/Hadamard product, often a small open circle is used as symbol. Since this is what you get with the normal '*' operator in numpy I doubt there is a separate Schur function.

like image 38
user422005 Avatar answered Nov 15 '22 12:11

user422005


Actually there are three different ways to multiply all the elements of two vectors.

The first one, the inner or dot product, returns a scalar.

The second one, the cross product, returns a vector.

The third one, the tensor product, returns a second-order tensor.

I'm sure you mean the first one, because it's familiar to most people, but I thought it'd be good to post this for the sake of completeness.

like image 34
duffymo Avatar answered Nov 15 '22 12:11

duffymo