# Vector times reverse of vector

I'm programming, and I need to do a dot product of one vector and the reverse of another vector. For example, if my two vectors are $$<x_1,x_2,x_3>,<y_1,y_2,y_3>$$ I want to calculate $x_1y_3 + x_2y_2 + x_3y_1$. How do I express this using linear algebra operations? It's always faster to tell the computer to do those than to implement them yourself. I'm really shaky on this stuff!

• Note that this is just a particular inner product of the two vectors, albeit with respect to an indefinite form. – Travis Jun 26 '15 at 17:41
• If by "it's always faster to tell the computer to do those" you mean that it's universally more efficient to shoehorn the computation into an existing linear-algebra library than to program the computation you want explicitly, then that can be extremely false. – Henning Makholm Jun 26 '15 at 17:47
• I'm using python, so it is universally true, since if you can get numpy to do your work for you it'll be orders of magnitude faster. – Elliot Gorokhovsky Jun 26 '15 at 17:54
• If you're in C++, then I would agree, since the libraries themselves are implemented in C++, so it comes down mostly to algorithms. – Elliot Gorokhovsky Jun 26 '15 at 17:55

Call your first vector $x$ and the other vector $y$. Let $$A=\left(\begin{array}{ccc} 0 & 0 & 1\\ 0 & 1 & 0\\ 1 & 0 & 0\\ \end{array}\right).$$ Then the value you want is $x^T\cdot A\cdot y.$

• Why do I need the identity matrix? – Elliot Gorokhovsky Jun 26 '15 at 17:40
• It's not the identity matrix. notice that the 1's are on the antidiagonal, not the diagonal. – Alex S Jun 26 '15 at 17:41
• How about dot($x$,dot($A$,$y$)) – Alex S Jun 26 '15 at 17:44
• The transpose is a mathematical formalism. Computer languages usually don't care. – Alex S Jun 26 '15 at 17:45
• Smart! In terms of programming, it's probably slower (e.g in numpy) to do: dot(x, dot(A,y)) than to do dot(x, y[::-1]) – PlasmaBinturong Jun 5 at 15:41

You mentioned in the comments that you are using Python with NumPy. Then there is a way to exploit that:

>>> import numpy as np
>>> x = np.array([1,2,3])
>>> y = np.array([4,5,6])
>>> y[::-1]
array([6, 5, 4])
>>> np.dot(x,y[::-1])
28


Of course, this is more suited for StackOverflow.

• I have to accept the other answer, because it answers the question, but this is more useful! I might end up not using it simply because I want to JIT-compile with pypy, and pypy is not compatible with numpy – Elliot Gorokhovsky Jun 26 '15 at 23:45
• @RenéG I'd suggest using exceptions to make use of NumPy when it's available and still have it work when it's not available. – Vedran Šego Jun 27 '15 at 0:08