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!

  • $\begingroup$ Note that this is just a particular inner product of the two vectors, albeit with respect to an indefinite form. $\endgroup$ – Travis Jun 26 '15 at 17:41
  • $\begingroup$ 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. $\endgroup$ – Henning Makholm Jun 26 '15 at 17:47
  • $\begingroup$ 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. $\endgroup$ – Elliot Gorokhovsky Jun 26 '15 at 17:54
  • $\begingroup$ If you're in C++, then I would agree, since the libraries themselves are implemented in C++, so it comes down mostly to algorithms. $\endgroup$ – 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.$

  • $\begingroup$ Why do I need the identity matrix? $\endgroup$ – Elliot Gorokhovsky Jun 26 '15 at 17:40
  • $\begingroup$ It's not the identity matrix. notice that the 1's are on the antidiagonal, not the diagonal. $\endgroup$ – Alex S Jun 26 '15 at 17:41
  • $\begingroup$ How about dot($x$,dot($A$,$y$)) $\endgroup$ – Alex S Jun 26 '15 at 17:44
  • 2
    $\begingroup$ The transpose is a mathematical formalism. Computer languages usually don't care. $\endgroup$ – Alex S Jun 26 '15 at 17:45
  • $\begingroup$ 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]) $\endgroup$ – 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])

Of course, this is more suited for StackOverflow.

  • $\begingroup$ 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 $\endgroup$ – Elliot Gorokhovsky Jun 26 '15 at 23:45
  • $\begingroup$ @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. $\endgroup$ – Vedran Šego Jun 27 '15 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.