Q:

Java Vector inner products

I have an arraylist of m-dimensional vectors (stored as simple arrays) v1, v2 ... vn.
I have to repeatedly calculate inner products between two vectors that I select from among these vectors.
One way to do this is a simple for loop across all it's components.
double sum=0;
for(int i=0; i<m; i++)
    sum+=v1[i]*v2[i];

This is pretty much the only linear algebra operation I intend on performing on my data.

Would it be more efficient to import a linalg library like JAMA or la4j, store everything as matrices, and calculate the inner products? (these aren't even large matrix multiplications, just inner products between 1D vectors)
How does la4j(etc) implement a dot product? Would it not also iterate through each index and multiply each pair of components?

A:

la4j is Open Source, have a look at the code. Using the AbstractVector's inner product is less efficient than your own code, since it instantiates additional operation objects, here the OoPlaceInnerProduct.
However: In most cases I would still prefer using an existing, well-tested vector math package than implementing my own one. (Knuth: “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil”.)
Note that multithreading doesn't help either. Even the highly efficient LAPACK package uses the same code as yours.

